inserting members map when creating conversations

This commit is contained in:
talksik
2022-05-29 08:01:18 -05:00
parent f704676f62
commit 68f9c85ee8
3 changed files with 58 additions and 19 deletions
+7 -7
View File
@@ -173,9 +173,9 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
// if there is already a convo with exactly me and him, then select it
const findExistingConversation = Object.values(conversationMap).find((convo) => {
if (
convo.membersList?.length === 2 &&
convo.membersList.includes(user.uid) &&
convo.membersList.includes(otherUserId)
convo.memberIdsList?.length === 2 &&
convo.memberIdsList.includes(user.uid) &&
convo.memberIdsList.includes(otherUserId)
) {
return true;
}
@@ -593,7 +593,7 @@ function ConversationRow({ conversation }: { conversation: Conversation }) {
useEffect(() => {
(async () => {
const userPromises = conversation.membersList
const userPromises = conversation.memberIdsList
.filter((memberId) => memberId !== user.uid)
.map(async (memberId) => await getUser(memberId));
@@ -603,7 +603,7 @@ function ConversationRow({ conversation }: { conversation: Conversation }) {
})();
return;
}, [conversation.membersList, getUser, user, setConversationUsers]);
}, [conversation.memberIdsList, getUser, user, setConversationUsers]);
return (
<ListItem key={`${conversation.id}-priorityConvoList`}>
@@ -684,7 +684,7 @@ function ConversationDetails() {
useEffect(() => {
(async () => {
const userPromises = selectedConversation.membersList
const userPromises = selectedConversation.memberIdsList
.filter((memberId) => memberId !== user.uid)
.map(async (memberId) => await getUser(memberId));
@@ -694,7 +694,7 @@ function ConversationDetails() {
})();
return;
}, [selectedConversation.membersList, getUser, user, setConversationUsers]);
}, [selectedConversation.memberIdsList, getUser, user, setConversationUsers]);
return (
<>
+22 -3
View File
@@ -22,7 +22,7 @@ import {
import { User as FirebaseUser } from 'firebase/auth';
import { firestoreDb } from './connect';
import { User } from '@nirvana/core/src/models/user.model';
import Conversation from '@nirvana/core/src/models/conversation.model';
import Conversation, { ConversationMember, MemberMap, MemberRole, MemberState } from '@nirvana/core/src/models/conversation.model';
import useAuth from '../providers/AuthProvider';
interface Document {
@@ -141,7 +141,15 @@ export const createOneOnOneConversation = async (
otherUserId: string,
myUserId: string,
): Promise<string | undefined> => {
const newConversation = new Conversation(myUserId, [myUserId, otherUserId], null);
const myMember = new ConversationMember(myUserId, MemberRole.admin, MemberState.inbox)
const otherMember = new ConversationMember(otherUserId, MemberRole.regular, MemberState.inbox)
const newMemberMap: MemberMap = {
[myUserId]: {...myMember},
[otherUserId]: {...otherMember}
}
const newConversation = new Conversation(myUserId, [myUserId, otherUserId], newMemberMap);
try {
const newDoc = await addDoc(db.conversations, newConversation);
@@ -157,7 +165,18 @@ export const createGroupConversation = async (
otherUserIds: string[],
myUserId: string,
): Promise<void> => {
const newConversation = new Conversation(myUserId, [myUserId, ...otherUserIds]);
const myMember = new ConversationMember(myUserId, MemberRole.admin, MemberState.inbox)
const newMemberMap: MemberMap = {
[myUserId]: {...myMember}
}
otherUserIds.forEach((otherMemberId) => {
const otherMember = new ConversationMember(otherMemberId, MemberRole.regular, MemberState.inbox)
newMemberMap[otherMemberId] = {...otherMember}
})
const newConversation = new Conversation(myUserId, [myUserId, ...otherUserIds], newMemberMap);
try {
await addDoc(db.conversations, newConversation);