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 DAB0mB committed Feb 13, 2017
1 parent 10816b5 commit 1bdf657
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 1bdf657

Please sign in to comment.