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
+29 -9
View File
@@ -5,7 +5,9 @@ export default class Conversation {
constructor( constructor(
public createdByUserId: string, public createdByUserId: string,
public membersList: string[], public memberIdsList: string[],
public members: MemberMap,
public name: string | null = null, public name: string | null = null,
@@ -17,11 +19,29 @@ export default class Conversation {
) {} ) {}
} }
// export class Member { export type MemberMap = {
// constructor( [memberId: string]: ConversationMember
// public id: string, }
// public userId: string,
// public role: 'admin' | 'regular', export class ConversationMember {
// public joinedDate = Timestamp.now(), 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"
}
+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 // if there is already a convo with exactly me and him, then select it
const findExistingConversation = Object.values(conversationMap).find((convo) => { const findExistingConversation = Object.values(conversationMap).find((convo) => {
if ( if (
convo.membersList?.length === 2 && convo.memberIdsList?.length === 2 &&
convo.membersList.includes(user.uid) && convo.memberIdsList.includes(user.uid) &&
convo.membersList.includes(otherUserId) convo.memberIdsList.includes(otherUserId)
) { ) {
return true; return true;
} }
@@ -593,7 +593,7 @@ function ConversationRow({ conversation }: { conversation: Conversation }) {
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const userPromises = conversation.membersList const userPromises = conversation.memberIdsList
.filter((memberId) => memberId !== user.uid) .filter((memberId) => memberId !== user.uid)
.map(async (memberId) => await getUser(memberId)); .map(async (memberId) => await getUser(memberId));
@@ -603,7 +603,7 @@ function ConversationRow({ conversation }: { conversation: Conversation }) {
})(); })();
return; return;
}, [conversation.membersList, getUser, user, setConversationUsers]); }, [conversation.memberIdsList, getUser, user, setConversationUsers]);
return ( return (
<ListItem key={`${conversation.id}-priorityConvoList`}> <ListItem key={`${conversation.id}-priorityConvoList`}>
@@ -684,7 +684,7 @@ function ConversationDetails() {
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const userPromises = selectedConversation.membersList const userPromises = selectedConversation.memberIdsList
.filter((memberId) => memberId !== user.uid) .filter((memberId) => memberId !== user.uid)
.map(async (memberId) => await getUser(memberId)); .map(async (memberId) => await getUser(memberId));
@@ -694,7 +694,7 @@ function ConversationDetails() {
})(); })();
return; return;
}, [selectedConversation.membersList, getUser, user, setConversationUsers]); }, [selectedConversation.memberIdsList, getUser, user, setConversationUsers]);
return ( return (
<> <>
+22 -3
View File
@@ -22,7 +22,7 @@ import {
import { User as FirebaseUser } from 'firebase/auth'; import { User as FirebaseUser } from 'firebase/auth';
import { firestoreDb } from './connect'; import { firestoreDb } from './connect';
import { User } from '@nirvana/core/src/models/user.model'; 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'; import useAuth from '../providers/AuthProvider';
interface Document { interface Document {
@@ -141,7 +141,15 @@ export const createOneOnOneConversation = async (
otherUserId: string, otherUserId: string,
myUserId: string, myUserId: string,
): Promise<string | undefined> => { ): 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 { try {
const newDoc = await addDoc(db.conversations, newConversation); const newDoc = await addDoc(db.conversations, newConversation);
@@ -157,7 +165,18 @@ export const createGroupConversation = async (
otherUserIds: string[], otherUserIds: string[],
myUserId: string, myUserId: string,
): Promise<void> => { ): 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 { try {
await addDoc(db.conversations, newConversation); await addDoc(db.conversations, newConversation);