more logic for getting conversation if it's not there but should be
This commit is contained in:
@@ -19,8 +19,8 @@ export default function getConversationRoutes() {
|
|||||||
|
|
||||||
router.use(express.json());
|
router.use(express.json());
|
||||||
|
|
||||||
// get a conversation
|
// get a conversation by id
|
||||||
router.get('/:conversationId', authCheck);
|
router.get('/:conversationId', authCheck, getConversationById);
|
||||||
|
|
||||||
// get all conversations that I am in
|
// get all conversations that I am in
|
||||||
// todo: add in pagination for performance, while allowing getting long polling updates?
|
// todo: add in pagination for performance, while allowing getting long polling updates?
|
||||||
@@ -38,6 +38,24 @@ export default function getConversationRoutes() {
|
|||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getConversationById = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const { conversationId } = req.params;
|
||||||
|
|
||||||
|
if (!conversationId) {
|
||||||
|
return next(Error('Must provide a conversation Id'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const conversationResult = await ConversationService.getConversationById(conversationId);
|
||||||
|
|
||||||
|
const responseObj = new NirvanaResponse(
|
||||||
|
conversationResult,
|
||||||
|
undefined,
|
||||||
|
conversationResult ? 'here is the conversation' : 'No conversation found',
|
||||||
|
);
|
||||||
|
|
||||||
|
return res.json(responseObj);
|
||||||
|
};
|
||||||
|
|
||||||
const getAllConversations = async (req: Request, res: Response, next: NextFunction) => {
|
const getAllConversations = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const userInfo = res.locals.userInfo as JwtClaims;
|
const userInfo = res.locals.userInfo as JwtClaims;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,19 @@ import { ObjectId } from 'mongodb';
|
|||||||
import { collections } from './database.service';
|
import { collections } from './database.service';
|
||||||
|
|
||||||
export default class ConversationService {
|
export default class ConversationService {
|
||||||
|
static async getConversationById(conversationId: string) {
|
||||||
|
const query = { _id: new ObjectId(conversationId) };
|
||||||
|
|
||||||
|
const res = await collections.conversations?.findOne(query);
|
||||||
|
|
||||||
|
// exists
|
||||||
|
if (res?._id) {
|
||||||
|
return res as Conversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
static async createConversation(newConversation: Conversation) {
|
static async createConversation(newConversation: Conversation) {
|
||||||
return await collections.conversations?.insertOne(newConversation);
|
return await collections.conversations?.insertOne(newConversation);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,3 +89,9 @@ export async function getConversations(): Promise<NirvanaResponse<Conversation[]
|
|||||||
export async function checkIfOnOnOneExists(otherUserId: string): Promise<NirvanaResponse<string>> {
|
export async function checkIfOnOnOneExists(otherUserId: string): Promise<NirvanaResponse<string>> {
|
||||||
return await NirvanaApi.fetch(`/conversations/check/${otherUserId}`, 'GET', true);
|
return await NirvanaApi.fetch(`/conversations/check/${otherUserId}`, 'GET', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getConversationById(
|
||||||
|
conversationId: string,
|
||||||
|
): Promise<NirvanaResponse<Conversation>> {
|
||||||
|
return await NirvanaApi.fetch(`/conversations/${conversationId}`, 'GET', true);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
|
import { ConversationMap, MasterConversation } from '../util/types';
|
||||||
import React, { useCallback, useEffect, useState } from 'react';
|
import React, { useCallback, useEffect, useState } from 'react';
|
||||||
import { checkIfOnOnOneExists, createConversation, getConversations } from '../api/NirvanaApi';
|
import {
|
||||||
|
checkIfOnOnOneExists,
|
||||||
|
createConversation,
|
||||||
|
getConversationById,
|
||||||
|
getConversations,
|
||||||
|
} from '../api/NirvanaApi';
|
||||||
|
|
||||||
import Conversation from '@nirvana/core/models/conversation.model';
|
import Conversation from '@nirvana/core/models/conversation.model';
|
||||||
import { ConversationMap } from '../util/types';
|
|
||||||
import CreateConversationRequest from '@nirvana/core/requests/CreateConversationRequest.request';
|
import CreateConversationRequest from '@nirvana/core/requests/CreateConversationRequest.request';
|
||||||
import { Typography } from '@mui/material';
|
import { Typography } from '@mui/material';
|
||||||
import User from '@nirvana/core/models/user.model';
|
import User from '@nirvana/core/models/user.model';
|
||||||
@@ -13,7 +18,7 @@ import { useImmer } from 'use-immer';
|
|||||||
interface IConversationContext {
|
interface IConversationContext {
|
||||||
conversations: ConversationMap;
|
conversations: ConversationMap;
|
||||||
|
|
||||||
selectedConversation?: Conversation;
|
selectedConversation?: MasterConversation;
|
||||||
// handleSetConversation?: (conversationId: string) => void;
|
// handleSetConversation?: (conversationId: string) => void;
|
||||||
|
|
||||||
handleStartConversation?: (otherUsers: User[]) => void;
|
handleStartConversation?: (otherUsers: User[]) => void;
|
||||||
@@ -30,7 +35,7 @@ export function ConversationProvider({ children }: { children: React.ReactNode }
|
|||||||
// the map that we manage throughout the app
|
// the map that we manage throughout the app
|
||||||
const [conversationMap, setConversationMap] = useImmer<ConversationMap>({});
|
const [conversationMap, setConversationMap] = useImmer<ConversationMap>({});
|
||||||
|
|
||||||
const [selectedConversation, setSelectedConversation] = useState<Conversation>(undefined);
|
const [selectedConversation, setSelectedConversation] = useState<MasterConversation>(undefined);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
doFetch();
|
doFetch();
|
||||||
@@ -56,7 +61,7 @@ export function ConversationProvider({ children }: { children: React.ReactNode }
|
|||||||
* temporaryOverrideSort: tell me if you want to temporarily prioritize this conversation above all others
|
* temporaryOverrideSort: tell me if you want to temporarily prioritize this conversation above all others
|
||||||
*/
|
*/
|
||||||
const selectConversation = useCallback(
|
const selectConversation = useCallback(
|
||||||
(conversationId: string, temporaryOverrideSort = false) => {
|
async (conversationId: string, temporaryOverrideSort = false) => {
|
||||||
/**
|
/**
|
||||||
* see if there is such a conversation in our map
|
* see if there is such a conversation in our map
|
||||||
* if not, then go and fetch it from backend
|
* if not, then go and fetch it from backend
|
||||||
@@ -69,9 +74,29 @@ export function ConversationProvider({ children }: { children: React.ReactNode }
|
|||||||
toast.success('we have that conversation!!!');
|
toast.success('we have that conversation!!!');
|
||||||
|
|
||||||
setSelectedConversation(conversationMap[conversationId]);
|
setSelectedConversation(conversationMap[conversationId]);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetch and add to conversation map
|
||||||
|
const retrieveConversationResult = await getConversationById(conversationId);
|
||||||
|
if (!retrieveConversationResult.data) {
|
||||||
|
toast.error('unable to select conversation');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setConversationMap((draft) => {
|
||||||
|
draft[conversationId] = {
|
||||||
|
...retrieveConversationResult.data,
|
||||||
|
tunedInUsers: [],
|
||||||
|
connectedUserIds: [],
|
||||||
|
temporaryOverrideSort,
|
||||||
|
};
|
||||||
|
|
||||||
|
setSelectedConversation(draft[conversationId]);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[conversationMap, setSelectedConversation],
|
[conversationMap, setSelectedConversation, setConversationMap],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleStartConversation = useCallback(
|
const handleStartConversation = useCallback(
|
||||||
|
|||||||
@@ -2,15 +2,17 @@ import { ContentBlock } from '@nirvana/core/models/content.model';
|
|||||||
import Conversation from '@nirvana/core/models/conversation.model';
|
import Conversation from '@nirvana/core/models/conversation.model';
|
||||||
import User from '@nirvana/core/models/user.model';
|
import User from '@nirvana/core/models/user.model';
|
||||||
|
|
||||||
export type ConversationMap = {
|
export type MasterConversation = Conversation & {
|
||||||
[conversationId: string]: Conversation & {
|
tunedInUsers: string[];
|
||||||
tunedInUsers: string[];
|
connectedUserIds: string[];
|
||||||
connectedUserIds: string[];
|
|
||||||
|
|
||||||
// allows client side to have this pushed up in the list and can uncheck it once user is done with this
|
// allows client side to have this pushed up in the list and can uncheck it once user is done with this
|
||||||
// if action is take, normal ordering should take place with database upserts
|
// if action is take, normal ordering should take place with database upserts
|
||||||
temporaryOverrideSort?: boolean;
|
temporaryOverrideSort?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type ConversationMap = {
|
||||||
|
[conversationId: string]: MasterConversation;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type UserMap = {
|
export type UserMap = {
|
||||||
|
|||||||
Reference in New Issue
Block a user