/
methods.ts
135 lines (111 loc) · 4.02 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Chats } from './collections/chats';
import { Messages } from './collections/messages';
import { MessageType, Profile } from './models';
import { check, Match } from 'meteor/check';
import { Users } from "./collections/users";
import { fcmService } from "./services/fcm";
import { facebookService, FbProfile } from "./services/facebook";
const nonEmptyString = Match.Where((str) => {
check(str, String);
return str.length > 0;
});
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);
},
removeChat(chatId: string): void {
if (!this.userId) {
throw new Meteor.Error('unauthorized',
'User must be logged-in to remove chat');
}
check(chatId, nonEmptyString);
const chatExists = !!Chats.collection.find(chatId).count();
if (!chatExists) {
throw new Meteor.Error('chat-not-exists',
'Chat doesn\'t exist');
}
Chats.remove(chatId);
},
updateProfile(profile: Profile): void {
if (!this.userId) throw new Meteor.Error('unauthorized',
'User must be logged-in to create a new chat');
check(profile, {
name: nonEmptyString,
pictureId: Match.Maybe(nonEmptyString)
});
Meteor.users.update(this.userId, {
$set: {profile}
});
},
addMessage(type: MessageType, chatId: string, content: string) {
if (!this.userId) throw new Meteor.Error('unauthorized',
'User must be logged-in to create a new chat');
check(type, Match.OneOf(String, [ MessageType.TEXT, MessageType.LOCATION ]));
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');
}
const userId = this.userId;
const senderName = Users.collection.findOne({_id: userId}).profile.name;
const memberIds = Chats.collection.findOne({_id: chatId}).memberIds;
const tokens: string[] = Users.collection.find(
{
_id: {$in: memberIds, $nin: [userId]},
fcmToken: {$exists: true}
}
).map((el) => el.fcmToken);
for (let token of tokens) {
console.log("Sending FCM notification");
fcmService.sendNotification({"title": `New message from ${senderName}`, "text": content}, token);
}
return {
messageId: Messages.collection.insert({
chatId: chatId,
senderId: this.userId,
content: content,
createdAt: new Date(),
type: type
})
};
},
countMessages(): number {
return Messages.collection.find().count();
},
saveFcmToken(token: string): void {
if (!this.userId) throw new Meteor.Error('unauthorized', 'User must be logged-in to call this method');
check(token, nonEmptyString);
Users.collection.update({_id: this.userId}, {$set: {"fcmToken": token}});
},
async getFbProfile(): Promise<FbProfile> {
if (!this.userId) throw new Meteor.Error('unauthorized', 'User must be logged-in to call this method');
if (!Users.collection.findOne({'_id': this.userId}).services.facebook) {
throw new Meteor.Error('unauthorized', 'User must be logged-in with Facebook to call this method');
}
//TODO: handle error: token may be expired
const accessToken = await facebookService.getAccessToken(this.userId);
//TODO: handle error: user may have denied permissions
return await facebookService.getProfile(accessToken);
}
});