-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_websockets.js
394 lines (340 loc) · 10.5 KB
/
server_websockets.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
`use strict`;
const init = require('./server_init');
const io = require('socket.io')(init.httpServer);
const log = require('./server_logger');
const db = require('./dataImport');
const handleFriendRequest = sock => {
sock.on('msgSearchUserRequest', (usr, reqUsr) => {
console.log("SEARCH INITIATED FOR -> ", usr)
db.pullUserbyUN(usr).then((obj) => {
db.pushFriendRequest(usr, reqUsr).then(() => {
sock.emit('msgSearchUserRequestConfirm', "Request Sent!")
}).catch(() => {
sock.emit('msgSearchUserRequestReject')
})
}).catch((err) => {
console.log(err)
sock.emit('msgSearchUserQueryRequestReject')
})
})
}
const handleFriendRequestList = sock => {
sock.on('msgFriendRequestQuery', usr => {
console.log("User requests a friend request")
db.pullFriendRequests(usr).then((list) => {
console.log(list)
proms = []
usrs = []
list.forEach(req => {
var a = pullUser(req).then((userData) => {
usrs = usrs.concat(userData) // add this to the list
}).catch((err) => {
console.log(err)
})
proms = proms.concat(a) // add to the list
})
Promise.all(proms).then(() => {
sock.emit(msgFriendRequestResponse, usrs) // send back the list
})
})
})
}
const handleUserInitialization = sock => {
sock.on('msgUserInitialization', msg => {
// retrieve user info from database
db.pullUser(msg.userID).then(
result => {
sock.emit('msgUserInitializationConfirmed', result);
log.logUserConnect(result.first_name + ' ' + result.last_name, result.id);
},
err => {
sock.emit('msgUserInitializationRejected', msg);
log.logWebSocketsError('Failed to initialize user with ID ' + msg.userID);
}
);
});
};
const handleQuestionPostRequest = sock => {
log.logWebSocketsEntry('Handling question post request');
sock.on('msgQuestionPostRequest', msg => {
ast = db.pullActiveQuestion(msg.userID).then((data) => {
if (data === "null") {
db.pushQuestion(
msg.userID,
msg.questionStatement,
msg.questionType,
msg.containsImage,
msg.containsVoice,
msg.imageLink,
msg.voiceLink,
msg.date,
msg.categoryID,
msg.answerTimeLimit,
false
).then(
result => {
sock.emit('msgQuestionPostRequestConfirmed', result);
log.logWebSocketsEntry('Question posting successful');
// db.pushNotification(msg.userID, 2, msg.questionStatement, result._id).then(
// r2 => {
// log.logWeSocketsEntry('Notification posted for question id ' + result._id);
// },
// e2 => {
// log.logWebSocketsError('Failed to post notification for question id ' + result._id);
// }
// );
},
err => {
sock.emit('msgQuestionPostRequestRejected', msg);
log.logWebSocketsError('Question posting failed');
}
);
} else {
sock.emit('msgQuestionPostRequestBounds'); //
log.logWebSocketsError('Question Posting negative, already existing question')
}
}).catch((err) => {
sock.emit('msgQuestionPostRequestRejected', msg);
log.logWebSocketsError('Question posting failed');
})
});
};
const handleQuestionVoteRequest = sock => {
sock.on('msgQuestionVoteRequest', msg => {
db.pushVote(
msg.userID,
msg.questionID,
msg.value
).then(
result => {
sock.emit('msgQuestionVoteRequestConfirmed', result);
log.logUserQuestionResponse(msg.userID, msg.questionID);
const notifContent = 'Vote posted for your question.';
db.pullQuestion(msg.questionID).then(
data => {
db.pushNotification(data.user_id, 2, notifContent, msg.questionID).then(
r2 => {
log.logWebSocketsEntry('Notification for vote of question id ' + msg.questionID + ' pushed.');
},
e2 => {
log.logWebSocketsError('Notification push failed for vote of question id ' + msg.questionID);
}
);
},
dataerr => {
log.logWebSocketsError('Pull Question Failed');
}
)
},
err => {
sock.emit('msgQuestionVoteRequestRejected', msg);
log.logWebSocketsError('Question vote request failed for question with ID ' + msg.questionID);
}
);
});
};
const handleAnswerPostRequest = sock => {
sock.on('msgAnswerPostRequest', msg => {
db.pushAnswer(
msg.questionID,
msg.option1,
msg.option2,
msg.option3,
msg.option1Link,
msg.option2Link,
msg.option3Link
).then(
result => {
sock.emit('msgAnswerPostRequestConfirmed', result);
log.logWebSocketsEntry('Answer to question with ID ' + msg.questionID + ' added');
},
err => {
sock.emit('msgAnswerPostRequestRejected', msg);
log.logWebSocketsError('Unable to post answer to question with ID ' + msg.questionID);
}
);
});
};
const handleQuestionRetrieveRequest = sock => {
sock.on('msgQuestionRetrieveRequest', msg => {
db.pullQuestion(msg.questionID).then(
result => {
sock.emit('msgQuestionRetrieveRequestConfirmed', result);
log.logWebSocketsEntry('Question retrieval for ID ' + msg.questionID + ' added');
},
err => {
sock.emit('msgQuestionRetrieveRequestRejected', msg);
log.logWebSocketsError('Unable to retrieve question with ID ' + msg.questionID);
}
);
});
};
const handleAnswerRetrieveRequest = sock => {
sock.on('msgAnswerRetrieveRequest', msg => {
db.pullAnswerOptions(msg.questionID).then(
result => {
sock.emit('msgAnswerRetrieveRequestConfirmed', result);
log.logWebSocketsEntry('Answer retrieval for question with ID ' + msg.questionID + ' added');
},
err => {
sock.emit('msgAnswerRetrieveRequestRejected', msg);
log.logWebSocketsError('Unable to retrieve answer for question with ID ' + msg.questionID);
}
);
});
};
const handleUserFeedRetrieveRequest = sock => {
sock.on('msgUserFeedRetrieveRequest', msg => {
db.pullRecentQuestions(10).then(
result => {
sock.emit('msgUserFeedRetrieveRequestConfirmed', result);
log.logWebSocketsEntry('User feed retrieval successful');
},
err => {
sock.emit('msgUserFeedRetrieveRequestRejected', msg);
log.logWebSocketsError('Unable to retrieve user feed.');
}
);
});
};
const handleUserLoginRequest = sock => {
sock.on('msgUserLoginRequest', msg => {
// console.log('Username ' + msg.userName + ' password ' + msg.password);
db.pullUserAuthenticate(msg.userName, msg.password).then(
result => {
db.pullActiveQuestion(result._id).then(qResult => {
sock.emit('msgUserLoginRequestConfirmed', result, qResult);
log.logWebSocketsEntry('User active question retrieval successful ' + msg.userName + qResult);
},
err => {
sock.emit('msgUserLoginRejected');
log.logWebSocketsError('Active Question Lookup Failed ' + msg.userName + " " + result._id);
})
},
err => {
sock.emit('msgUserLoginRejected');
log.logWebSocketsError('User login failed for username ' + msg.password);
}
);
});
};
const handleUserSignupRequest = sock => {
sock.on('msgUserSignupRequest', msg => {
db.pushUser(
msg.userName,
msg.email,
msg.profileImageExists,
msg.profileImageLink,
msg.firstName,
msg.lastName,
msg.country,
msg.gender,
msg.password_hash
).then(
result => {
sock.emit('msgUserSignupRequestConfirmed', result);
log.logWebSocketsEntry('User signup successful for username ' + msg.userName);
},
err => {
sock.emit('msgUserSignupRequestRejected', msg);
log.logWebSocketsError('User signup failed for username ' + msg.userName);
}
);
});
};
const handleQuestionVotesRetrieveRequest = sock => {
sock.on('msgQuestionVotesRetrieveRequest', msg => {
db.pullVotes(msg.questionID).then(
result => {
sock.emit('msgQuestionVotesRetrieveRequestConfirmed', result);
log.logWebSocketsEntry('Votes retrieval successful for question with ID' + msg.questionID);
},
err => {
sock.emit('msgQuestionVotesRetrieveRequestRejected', msg);
log.logWebSocketsError('Votes retrieval failed for question with ID ' + msg.questionID);
}
);
});
};
const handleNotificationPushRequest = sock => {
sock.on('msgNotificationPushRequest', msg => {
db.pushNotification(
msg.notificationUserID,
msg.notificationType,
msg.notificationContent,
msg.notificationQuest
).then(
result => {
sock.emit('msgNotificationPushRequestConfirmed', result);
log.logWeSocketsEntry(`Notification push by user ${msg.notificationUserID} confirmed.`);
},
err => {
sock.emit('msgNotificationPushRequestRejected', msg);
log.logWebSocketsError(`Notification push by user ${msg.notificationUserID} rejected`);
}
);
});
};
const handleNotificationPullRequest = sock => {
sock.on('msgNotificationPullRequest', msg => {
db.pullNotifications(msg.notificationUserID).then(
result => {
sock.emit('msgNotificationPullRequestConfirmed', result);
log.logWebSocketsEntry(`Notification pulls by user ${msg.notificationUserID} confirmed.`);
},
err => {
sock.emit('msgNotificationPullRequestRejected', msg);
log.logWebSocketsError(`Notification pulls by user ${msg.notificationUserID} rejected.`);
}
);
});
};
const handleUpdateSettings = sock => {
sock.on('msgUpdateSettings', msg => {
db.changeUsername(msg.userID, msg.userName).then(
result => {
db.changePassword(msg.userID, msg.passWord).then(
r2 => {
db.changeProfilePic(msg.userID, msg.profilePicture).then(
r3 => {
sock.emit('msgUpdateSettingsConfirmed');
log.logWebSocketsEntry('Settings updated for user ' + msg.userID);
},
e3 => {
sock.emit('msgUpdateSettingsRejected');
log.logWebSocketsError('Failed to update settings for user ' + msg.userID);
}
)
},
e2 => {
sock.emit('msgUpdateSettingsRejected');
log.logWebSocketsError('Failed to update settings for user ' + msg.userID);
})
},
error => {
sock.emit('msgUpdateSettingsRejected');
log.logWebSocketsError('Failed to update settings for user ' + msg.userID);
})
});
};
const initWebSocketConnection = () => {
io.on('connection', sock => {
log.logWebSocketsEntry('Client connected');
handleFriendRequest(sock);
handleFriendRequestList(sock);
handleUserInitialization(sock);
handleQuestionPostRequest(sock);
handleQuestionVoteRequest(sock);
handleAnswerPostRequest(sock);
handleQuestionRetrieveRequest(sock);
handleAnswerRetrieveRequest(sock);
handleUserFeedRetrieveRequest(sock);
handleUserLoginRequest(sock);
handleUserSignupRequest(sock);
handleQuestionVotesRetrieveRequest(sock);
handleNotificationPushRequest(sock);
handleNotificationPullRequest(sock);
handleUpdateSettings(sock);
});
};
module.exports = { initWebSocketConnection };