-
Notifications
You must be signed in to change notification settings - Fork 185
/
methods.ts
33 lines (28 loc) · 854 Bytes
/
methods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Chats } from './collections/chats';
import { Messages } from './collections/messages';
import { MessageType } from './models';
import { check, Match } from 'meteor/check';
const nonEmptyString = Match.Where((str) => {
check(str, String);
return str.length > 0;
});
Meteor.methods({
addMessage(type: MessageType, chatId: string, content: string) {
check(type, Match.OneOf(String, [ MessageType.TEXT ]));
check(chatId, nonEmptyString);
check(content, nonEmptyString);
const chatExists = !!Chats.collection.find(chatId).count();
if (!chatExists) {
throw new Meteor.Error('chat-not-exists',
'Chat doesn\'t exist');
}
return {
messageId: Messages.collection.insert({
chatId: chatId,
content: content,
createdAt: new Date(),
type: type
})
};
}
});