Skip to content

Commit 17932b6

Browse files
dotansimhadarkbasic
authored andcommitted
Step 8.10: Implement addChat method
1 parent 248080c commit 17932b6

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎api/server/methods.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ const nonEmptyString = Match.Where((str) => {
99
});
1010

1111
Meteor.methods({
12+
addChat(receiverId: string): void {
13+
if (!this.userId) {
14+
throw new Meteor.Error('unauthorized',
15+
'User must be logged-in to create a new chat');
16+
}
17+
18+
check(receiverId, nonEmptyString);
19+
20+
if (receiverId === this.userId) {
21+
throw new Meteor.Error('illegal-receiver',
22+
'Receiver must be different than the current logged in user');
23+
}
24+
25+
const chatExists = !!Chats.collection.find({
26+
memberIds: { $all: [this.userId, receiverId] }
27+
}).count();
28+
29+
if (chatExists) {
30+
throw new Meteor.Error('chat-exists',
31+
'Chat already exists');
32+
}
33+
34+
const chat = {
35+
memberIds: [this.userId, receiverId]
36+
};
37+
38+
Chats.insert(chat);
39+
},
1240
updateProfile(profile: Profile): void {
1341
if (!this.userId) throw new Meteor.Error('unauthorized',
1442
'User must be logged-in to create a new chat');

0 commit comments

Comments
 (0)