-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfriends.ts
287 lines (257 loc) · 8.94 KB
/
friends.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { Request, Response } from 'express';
import { sanitizeUserData, UNAVAILABLE_USER, User } from '../core/db/schemas/User.model';
import { Friendship, FriendshipData, getFriendship, getUserFriendRequests, getUserFriends, usersHaveFriendshipRecord } from '../core/db/schemas/Friendship.model';
import { validateAuthenticationToken } from '../core/auth/auth';
import { liveServer } from '..';
export const handleGetFriends = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// Get the user's friends
const rawFriends = await getUserFriends(user);
const clientFriendList: any[] = [];
for (const friendship of rawFriends) {
const result = {
initiator: UNAVAILABLE_USER,
recipient: UNAVAILABLE_USER,
accepted: friendship.accepted,
sentAt: friendship.sentAt,
acceptedAt: friendship.acceptedAt,
};
result.initiator = sanitizeUserData(await User.findById(friendship.initiator) || UNAVAILABLE_USER);
result.recipient = sanitizeUserData(await User.findById(friendship.recipient) || UNAVAILABLE_USER);
clientFriendList.push(result);
}
res.status(200).send({
message: `Successfully retrieved friends`,
friends: clientFriendList,
});
}
export const handleGetFriendRequests = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// Get the user's friend requests
const rawFriendRequests = await getUserFriendRequests(user);
const clientFriendRequestList: any[] = [];
for (const friendship of rawFriendRequests) {
const result = {
_id: friendship._id,
initiator: UNAVAILABLE_USER,
recipient: UNAVAILABLE_USER,
accepted: friendship.accepted,
sentAt: friendship.sentAt,
acceptedAt: friendship.acceptedAt,
};
result.initiator = sanitizeUserData(await User.findById(friendship.initiator) || UNAVAILABLE_USER);
result.recipient = sanitizeUserData(await User.findById(friendship.recipient) || UNAVAILABLE_USER);
clientFriendRequestList.push(result);
}
res.status(200).send({
message: `Successfully retrieved friend requests`,
friendRequests: clientFriendRequestList,
});
}
export const handleCreateFriendRequest = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// If the request body doesn't contain the other user's username
if (!req.body.recipient || !(typeof req.body.recipient === 'string')) {
return res.status(400).send({
message: `Failed to create friend request: recipient username not specified.`,
});
}
const recipient = await User.findOne({ username: req.body.recipient });
// If the other user doesn't exist
if (!recipient) {
return res.status(400).send({
message: `Recipient user doesn't exist.`,
});
}
// If the users already have a pending or canceled friendship request
if (await usersHaveFriendshipRecord(user, recipient)) {
return res.status(400).send({
message: `Already pending friend request or existing friendship between specified users.`,
});
}
// Create a new friend request
const friendship = await Friendship.create({
initiator: user._id,
recipient: recipient._id,
});
// Return the friend request ID to the user
return res.status(200).send({
message: `Successfully created friend request.`,
requestId: friendship._id,
});
}
export const handleAcceptFriendRequest = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// If the request body doesn't contain the request ID
if (!req.body.request || !(typeof req.body.request === 'string')) {
return res.status(400).send({
message: `Failed to accept friend request: request ID not specified.`,
});
}
const friendship = await Friendship.findById(req.body.request);
if (!friendship) {
return res.status(400).send({
message: `Invalid friend request ID.`,
});
}
if (user._id.toString() !== friendship.recipient.toString() || friendship.accepted) {
return res.status(401).send({
message: `User not eligible to accept request.`,
});
}
// Accept the request
friendship.accepted = true;
friendship.acceptedAt = new Date();
await friendship.save();
return res.status(200).send({
message: `Successfully updated friendship request`,
friendship,
});
}
export const handleDeclineFriendRequest = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// If the request body doesn't contain the request ID
if (!req.body.request || !(typeof req.body.request === 'string')) {
return res.status(400).send({
message: `Failed to decline friend request: request ID not specified.`,
});
}
const friendship = await Friendship.findById(req.body.request);
if (!friendship) {
return res.status(400).send({
message: `Invalid friend request ID.`,
});
}
if (user._id.toString() !== friendship.recipient.toString() || friendship.accepted) {
return res.status(401).send({
message: `User not eligible to decline request.`,
});
}
// Delete the request
await friendship.deleteOne();
return res.status(200).send({
message: `Successfully declined friendship request`,
});
}
export const handleRemoveFriend = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action`,
});
}
// If the request body doesn't contain the other user's ID
if (!req.body.friend || !(typeof req.body.friend === 'string')) {
return res.status(400).send({
message: `Failed to remove friend: friend ID not specified.`,
});
}
const friend = await User.findById(req.body.friend);
// If the other user doesn't exist
if (!friend) {
return res.status(400).send({
message: `Friend's account doesn't exist.`,
});
}
const friendship = await getFriendship(user, friend);
if (!friendship || !friendship.accepted) {
return res.status(400).send({
message: `Users aren't friends.`,
});
}
// Delete the friendship
await friendship.deleteOne();
return res.status(200).send({
message: `Successfully removed friend.`,
});
}
export const handleGetFriendGames = async (req: Request, res: Response) => {
const token = req.header('Authtoken');
if (!token) {
return res.status(401).send({
message: `Must be logged in to perform this action.`
});
}
const { user, success } = await validateAuthenticationToken(token);
if (!success || !user) {
return res.status(401).send({
message: `Must be logged in to perform this action.`,
});
}
// Get the list of friendships involving this user
const friends = await getUserFriends(user);
// Convert it to a list of user IDs
const friendIds = [];
for (const f of friends) {
if (f.recipient.toString() === user._id.toString()) friendIds.push(f.initiator.toString());
else friendIds.push(f.recipient.toString());
}
// Get the list of games friends are currently in
const friendGames = liveServer.gameManager.getGamesWithPlayers(friendIds);
// Return the list to the client
return res.status(200).send({
message: `Successfully retreived friend games.`,
friendGames,
});
}