-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversation.ts
123 lines (112 loc) · 4.28 KB
/
Conversation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import firebase from "firebase/app"
import { getMessages, uploadFile, MESSAGE_COL } from "../firebase"
import { FirestoreModel } from "./FirestoreModel"
import { Message, MessageInFirestore } from "./Message"
import { User, UserInFirestore } from "./User"
// The actual conversation document model in firestore
export interface ConversationInFirestore {
// The random id assigned to this conversation
id: string
// Reference objects to the conversation participants in firestore
users: firebase.firestore.DocumentReference<UserInFirestore>[]
// A map of users that are currently typing a message in this conversation
usersTyping: { [uid: string]: boolean }
// The number of messages sent
messageCount: number
}
interface ConstructorProps {
id: string
users: User[]
usersTyping: { [uid: string]: boolean }
messageCount: number
ref: firebase.firestore.DocumentReference<ConversationInFirestore>
}
// A class that represents a conversation document in firestore and contains useful operations on the document
// and its message subcollection
export class Conversation implements FirestoreModel<ConversationInFirestore> {
readonly id: string
users: User[]
usersTyping: { [uid: string]: boolean }
messageCount: number
ref: firebase.firestore.DocumentReference<ConversationInFirestore>
constructor({ id, users, usersTyping: userIsTyping, messageCount, ref }: ConstructorProps) {
this.id = id
this.users = users
this.usersTyping = userIsTyping
this.messageCount = messageCount
this.ref = ref
}
// Given a message, creates a new message document in its nested collection and, if provided, uploads the passed file to
// the project's storage bucket
async addMessage(sender: User, message: string, file?: File): Promise<Message> {
const messageRef = this.ref
.collection(MESSAGE_COL)
.doc() as firebase.firestore.DocumentReference<MessageInFirestore>
const sendDate = new Date()
const body: MessageInFirestore = {
id: messageRef.id,
message: message,
sender: sender.ref,
sendDate: sendDate as any,
}
if (file) {
body.media = {
imageSource: await uploadFile(file),
}
}
const promises = [
messageRef.set(body),
this.ref.update({
messageCount: firebase.firestore.FieldValue.increment(1),
}),
this.updateActivity(message),
]
if (this.messageCount === 0) {
promises.push(this.createMissingConversationPointers(sender))
}
await Promise.all(promises)
return new Message({
parent: this,
id: messageRef.id,
message: message,
sender: sender,
sendDate: sendDate,
ref: messageRef,
})
}
// This is called when the first message is sent in a new conversation. For each recipient, a new conversation ref
// is created, which will show in their message sidebar
async createMissingConversationPointers(sender: User): Promise<void> {
const promises = []
for (const user of this.users) {
if (user.id !== sender.id) {
// create new
promises.push(user.createConversationPointer(sender, this))
}
}
await Promise.all(promises)
}
async getMessages(): Promise<Message[]> {
return await getMessages(this.id)
}
// Updates the latest activity date for all conversation pointers with an activity message that'll show as a preview to the conversation in the sidebar
async updateActivity(activity: string): Promise<void> {
await this.users[0].updateConversationActivity(this.users[1], activity)
}
async setUserTyping(user: User, isTyping: boolean) {
await this.ref.update({
[`usersTyping.${user.id}`]: isTyping,
})
}
async save(): Promise<void> {
await this.ref.set({
id: this.id,
usersTyping: this.usersTyping,
users: this.users.map((user) => user.ref),
messageCount: this.messageCount,
})
}
async delete(): Promise<void> {
await this.ref.delete()
}
}