diff --git a/packages/core/src/models/conversation.model.ts b/packages/core/src/models/conversation.model.ts index ef20be2..2e76e30 100644 --- a/packages/core/src/models/conversation.model.ts +++ b/packages/core/src/models/conversation.model.ts @@ -5,7 +5,9 @@ export default class Conversation { constructor( public createdByUserId: string, - public membersList: string[], + public memberIdsList: string[], + + public members: MemberMap, public name: string | null = null, @@ -17,11 +19,29 @@ export default class Conversation { ) {} } -// export class Member { -// constructor( -// public id: string, -// public userId: string, -// public role: 'admin' | 'regular', -// public joinedDate = Timestamp.now(), -// ) {} -// } +export type MemberMap = { + [memberId: string]: ConversationMember +} + +export class ConversationMember { + constructor( + public userId: string, // NOTE: serves as the user ID + + public role: MemberRole, + + public memberState: MemberState, + public isActive: boolean = true, + public lastActiveDate: Timestamp | null = null, + public joinedDate = Timestamp.now(), + ) {} +} + +export enum MemberRole { + admin = 'admin', + regular = 'regular', +} +export enum MemberState { + priority = 'priority', + inbox = 'inbox', + // deleted = "deleted" +} \ No newline at end of file diff --git a/packages/desktop/src/components/Terminal.tsx b/packages/desktop/src/components/Terminal.tsx index be54619..a86ee3c 100644 --- a/packages/desktop/src/components/Terminal.tsx +++ b/packages/desktop/src/components/Terminal.tsx @@ -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 ( @@ -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 ( <> diff --git a/packages/desktop/src/firebase/firestore.ts b/packages/desktop/src/firebase/firestore.ts index e56da6d..271f669 100644 --- a/packages/desktop/src/firebase/firestore.ts +++ b/packages/desktop/src/firebase/firestore.ts @@ -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 => { - 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 => { - 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);