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
dotansimha authored and DAB0mB committed Dec 24, 2016
1 parent cd2c66a commit b280ab6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/server/methods.ts
Expand Up @@ -10,6 +10,28 @@ const nonEmptyString = Match.Where((str) => {

export function initMethods() {
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 b280ab6

Please sign in to comment.