Skip to content

Commit

Permalink
Step 6.3: Define 'addChat' Method
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and dotansimha committed Nov 27, 2016
1 parent 9c26b3f commit ca08e4f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/imports/methods/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ Meteor.methods({
$set: {profile}
});
},
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);
},
addMessage(chatId: string, content: string): 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 ca08e4f

Please sign in to comment.