Permalink
Browse files

Step 8.10: Implement addChat method

  • Loading branch information...
dotansimha authored and darkbasic committed Jan 24, 2017
1 parent 2d8e8e5 commit 13d38061c7ce5674741a760d5f8f4c56780dda4c
Showing with 28 additions and 0 deletions.
  1. +28 −0 api/server/methods.ts
View
@@ -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');

0 comments on commit 13d3806

Please sign in to comment.