Skip to content

Commit

Permalink
Step 8.10: Implement addChat method
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha authored and darkbasic committed Oct 16, 2017
1 parent 248080c commit 17932b6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api/server/methods.ts
Expand Up @@ -9,6 +9,34 @@ const nonEmptyString = Match.Where((str) => {
});

Meteor.methods({
addChat(receiverId: string): void {
if (!this.userId) {
throw new Meteor.Error('unauthorized',
'User must be logged-in to create a new chat');
}

check(receiverId, nonEmptyString);

if (receiverId === this.userId) {
throw new Meteor.Error('illegal-receiver',
'Receiver must be different than the current logged in user');
}

const chatExists = !!Chats.collection.find({
memberIds: { $all: [this.userId, receiverId] }
}).count();

if (chatExists) {
throw new Meteor.Error('chat-exists',
'Chat already exists');
}

const chat = {
memberIds: [this.userId, receiverId]
};

Chats.insert(chat);
},
updateProfile(profile: Profile): void {
if (!this.userId) throw new Meteor.Error('unauthorized',
'User must be logged-in to create a new chat');
Expand Down

0 comments on commit 17932b6

Please sign in to comment.