-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
589 lines (503 loc) · 21.9 KB
/
server.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();
var http = require('http').Server(app);
require('./routes/routes.js')(app);
var db = require('./config/db');
require('datejs');
var io = require('socket.io')(http);
var HashMap = require('hashmap');
var User = require('./models/User.js');
var ChatController = require('./controller/ChatController.js');
var NotificationController = require('./controller/PushNotificationController.js');
var ConversationMessages = require('./models/ConversationMessages.js');
var Conversation = require('./models/Conversation.js');ConversationUser
var ConversationUser = require('./models/ConversationUser.js');
var logger = require('./config/lib/logger.js');
var mongoose = require('mongoose');
require('./routes/accounts');
mongoose.Promise = global.Promise;
//mongoose.createConnection(db.url);
mongoose.connect(db.url);
// Heroku assigns a port if port = process.env.PORT
var port = process.env.PORT || 3100;
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(bodyParser.json());
//app.use(express.static(__dirname + '/public'));
http.listen(port, function(){
console.log('listening on *:'+port);
});
var rooms = [];
var userMobileList = [];
var userHashMaps = new HashMap();
// Connection Event of SocketIO
io.sockets.on('connection', function(socket) {
console.log ("Socket Connected with id :"+socket.id);
// to add user mobile no. and socketID in hashmap
socket.on('onConnect', function (phoneNo) {
try{
//removing spaces bw phone no if any
phoneNo = phoneNo.replace(/ +/g, "");
logger.info('onConnect Event Called for Phone Num:' + phoneNo);
userHashMaps.set(phoneNo,socket.id);
socket.phoneNo=phoneNo;
logger.info("Users on socket " + userHashMaps.count());
logger.info(' Exit onConnect Event');
} catch (err){
logger.info('An Exception Has occured in onConnect event ' + err);
}
}); //end of onConnect Event
//To verify If user is registerd on app or not
socket.on('verifyUser', function (phoneNo, callback) {
try{
//removing spaces bw phone no if any
phoneNo = phoneNo.replace(/ +/g, "");
logger.info('verifyUser Event Called for Phone Num:' + phoneNo);
var query = { phone : phoneNo };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
callback(false);
}
if (user){
logger.info('User Found For Phone No: ' + phoneNo );
userHashMaps.set(phoneNo,socket.id);
socket.phoneNo=phoneNo;
callback(true);
}
else {
logger.info('User not Found For Phone No: ' + phoneNo );
callback(false);
}
});
logger.info(' Exit verifyUser Event');
} catch (err){
logger.info('An Exception Has occured in verifyUser event' + err);
}
}); //end of verifyUser Event
//Creating room event called when individual chat is initiated
socket.on('createRoom', function ( userMobileNumberFrom, userMobileNumberTo,callback) {
try{
//removing spaces bw phone no if any
userMobileNumberFrom = userMobileNumberFrom.replace(/ +/g, "");
userMobileNumberTo = userMobileNumberTo.replace(/ +/g, "");
logger.info('createRoom Event Called for userMobileNumberFrom : '+userMobileNumberFrom + ' & userMobileNumberTo ' + userMobileNumberTo);
var newconversation;
var conversationId;
//if (userMobileNumberFrom && userMobileNumberTo)
if ((userMobileNumberFrom===userMobileNumberTo)||(!(userMobileNumberFrom && userMobileNumberTo))){
logger.info ('Sender and Recipient are same or Either one is empty' );
socket.emit('roomId',null);
}
else{
// check if conversation between These Two users ever occured before, send conversationId / roomId in response
ChatController.chkPreviousIndividualConversation(userMobileNumberFrom,userMobileNumberTo,function(data){
logger.info ("chkPreviousIndividualConversation response :"+data);
if (data){
// Previous Conversation Found
logger.info ('Previous Conversation Id Received :' + data );
conversationId=data;
socket.room = conversationId;
socket.join(conversationId);
//logger.info ('Sending room Id To client : ' + conversationId );
logger.info ('Sending room Id ' + conversationId + ' TO Client : '+ userMobileNumberFrom );
socket.emit('roomId',conversationId);
//send an invitation
var socketid= userHashMaps.get (userMobileNumberTo);
logger.info('sending a notification to socket: '+ socketid);
var conversationObj ={
fromPhoneNo:userMobileNumberFrom,
conversationId:conversationId,
isGroupConversation:false
}
if(userMobileNumberTo){
if (io.sockets.connected[socketid]) {
logger.info( socketid + ' is in connected Sockets List ');
io.sockets.connected[socketid].emit('conversationRequest', conversationObj);
}
logger.info('Sending Onesignal Notifcation to '+ userMobileNumberTo );
var query = { phone :userMobileNumberTo };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
}
if (user){
logger.info('User Found For Phone No: ' + userMobileNumberTo );
logger.info('Sending Notification to player id ' + user.palyer_id );
NotificationController.sendNotifcationToPlayerId(user.palyer_id,conversationObj,"conversationRequest");
}
else {
logger.info('User not Found For Phone No: ' + userMobileNumberTo );
}
});
}
}
else{
//Creating New Conversation
newconversation= new Conversation();
newconversation.save(function (err, conversation) {
if (err) logger.error('Error Occured while Saving new conversation :'+ err);
if (conversation){
logger.info ('conversation created for id :'+conversation._id );
conversationId=conversation._id;
logger.info ('Creating Conversation Users against conversation id : '+conversation._id );
var newConversationUser= new ConversationUser({
_conversationId: conversationId,
_userMobile: userMobileNumberFrom
});
newConversationUser.save(function (err, conversationUser) {
if (err) logger.error('Error Occured while Saving new newConversationUser 1 :'+ err);
});
newConversationUser= new ConversationUser({
_conversationId: conversationId,
_userMobile: userMobileNumberTo
});
newConversationUser.save(function (err, conversationUser) {
if (err) logger.error('Error Occured while Saving new newConversationUser 2:'+ err);
});
}
socket.room = conversationId;
socket.join(conversationId);
logger.info ('Sending TO Client : '+ userMobileNumberFrom );
logger.info (' Emiting room Id :' + conversationId );
socket.emit('roomId',conversationId);
});
}
logger.info(' Exit createRoom Event');
});
}
} catch (err){
logger.info('An Exception Has occured in createRoom event' + err);
}
}); //end of createRoom Event
//Swithing Room
socket.on('joinRoom', function (conversationId) {
try{
logger.info('joinRoom Event Called for room id :' + conversationId);
//Joining New Room
rooms.push(conversationId);
socket.room = conversationId;
socket.join(conversationId);
socket.emit('roomId',conversationId);
logger.info('JoinRoom Event Exit');
}catch(err){
logger.info('An Exception Has occured in joinRoom event ' + err);
}
}); //end of joinRoom Event
// leave Room
socket.on('leaveRoom', function (conversationId) {
try{
logger.info('leaveRoom Event Called for room id :' + conversationId);
//Leaving the socket's current room
socket.room=null;
socket.leave(socket.room);
socket.emit('roomId',null);
logger.info('LeaveRoom Event Exit');
}catch(err) {
logger.info('An Exception Has occured in leaveRoom event' + err);
}
}); //end of leaveRoom Event
socket.on('groupRequest', function (conversationId) {
try {
logger.info('groupRequest Event Called for conversation id :' + conversationId);
var socketid;
var conversation;
var createdDate;
var myDate;
ChatController.findConversation (conversationId , function(con){
if (con){
logger.info ('Conversation Found for Id : '+ conversationId);
conversation=con;
ChatController.findConversationMembers(conversationId, function(members){
if (members){
logger.info ('findConversationMembers Response, Members List Size : ' + members.length);
myDate = new Date(members[0].createdAt);
createdDate = myDate.getTime();
var conversationObj ={
//fromPhoneNo:userMobileNumberFrom,
conversationId:conversationId,
isGroupConversation:con.isGroupConversation,
adminMobile:con.adminMobile,
photoUrl:con.conversationImageUrl,
conversationName:con.conversationName,
createdAt:createdDate,
}
//Notifying All Group Members
for (var i=0; i < members.length ; i++){
var phoneNo=members[i]._userMobile;
if (phoneNo!==(conversationObj.adminMobile)){
logger.info('Getting Socket Id against Phone No :' +phoneNo)
socketid= userHashMaps.get (phoneNo);
//Emiting on socket
logger.info('Emiting groupConversationRequest to socket: '+ socketid);
if (io.sockets.connected[socketid]) {
logger.info( socketid + ' is in connected Sockets List ');
io.sockets.connected[socketid].emit('groupConversationRequest', conversationObj);
}
//Sending Push Notiifcation To Group Members
logger.info('Sending Onesignal Notifcation of groupConversationRequest to '+ phoneNo );
//var phoneNo=members[i]._userMobile;
var query = { phone : phoneNo };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
}
if (user){
logger.info('User Found For Phone No: ' + phoneNo );
logger.info('Sending Notification to player id ' + user.palyer_id );
NotificationController.sendNotifcationToPlayerId(user.palyer_id,conversationObj,"groupConversationRequest");
}
else {
logger.info('User not Found For Phone No: ' + phoneNo );
}
});
}
}
}
});
}
if (con==null){
logger.info ('Conversation For conversationId : '+ conversationId + 'is null');
}
});
if (conversation!==null && conversation !== undefined ){
logger.info ('Conversation Not Found');
}
} catch (err){
logger.info('An Exception Has occured in groupRequest event ' + err);
}
}); //end of groupRequest Event
socket.on('groupRequestToNewMembers', function (conversationId) {
try {
logger.info('groupRequest Event Called for conversation id :' + conversationId);
var socketid;
var conversation;
var createdDate;
var myDate;
ChatController.findConversation (conversationId , function(con){
if (con){
logger.info ('Conversation Found for Id : '+ conversationId);
conversation=con;
ChatController.findConversationMembers(conversationId, function(members){
if (members){
logger.info ('findConversationMembers Response, Members List Size : ' + members.length);
myDate = new Date(members[0].createdAt);
createdDate = myDate.getTime();
var conversationObj ={
//fromPhoneNo:userMobileNumberFrom,
conversationId:conversationId,
isGroupConversation:con.isGroupConversation,
adminMobile:con.adminMobile,
photoUrl:con.conversationImageUrl,
conversationName:con.conversationName,
createdAt:createdDate,
}
logger.info (' Conversation createdAt :' + conversationObj.createdAt);
//Notifying All Group Members
for (var i=0; i < members.length ; i++){
var phoneNo=members[i]._userMobile;
if (phoneNo!==(conversationObj.adminMobile)){
logger.info('Getting Socket Id against Phone No :' +phoneNo)
socketid= userHashMaps.get (phoneNo);
//Emiting on socket
logger.info('Emiting groupConversationRequest to socket: '+ socketid);
if (io.sockets.connected[socketid]) {
logger.info( socketid + ' is in connected Sockets List ');
io.sockets.connected[socketid].emit('groupConversationRequest', conversationObj);
}
//Sending Push Notiifcation To Group Members
logger.info('Sending Onesignal Notifcation of groupConversationRequest to '+ phoneNo );
//var phoneNo=members[i]._userMobile;
var query = { phone : phoneNo };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
}
if (user){
logger.info('User Found For Phone No: ' + phoneNo );
logger.info('Sending Notification to player id ' + user.palyer_id );
NotificationController.sendNotifcationToPlayerId(user.palyer_id,conversationObj,"groupConversationRequest");
}
else {
logger.info('User not Found For Phone No: ' + phoneNo );
}
});
}
}
}
});
}
if (con==null){
logger.info ('Conversation with conversationId : '+ conversationId + 'is either deleted or not created yet');
}
});
if (conversation!==null && conversation !== undefined ){
logger.info ('Conversation Not Found');
}
} catch (err){
logger.info('An Exception Has occured in groupRequest event ' + err);
}
}); //end of groupRequestToNewMembers Event
socket.on('sendMessage', function (data) {
try {
logger.info('Data Object : '+data);
data=JSON.parse(data);
var myDate;
var createdDate;
logger.info("listening sendMessage event on server : \n "+data.messageText +"**"+ data.messageType +"**"+ data._conversationId + "**"+data._messageFromMobile+"**"+data._messageToMobile);
//check if room disconnected join again
var conversationId=data._conversationId;
if (!(socket.room)){
socket.room = conversationId;
socket.join(conversationId);
}
var conversationMessage = new ConversationMessages();
conversationMessage.messageType = data.messageType;
conversationMessage.messageText = data.messageText;
conversationMessage._conversationId = data._conversationId;
conversationMessage._messageToMobile = data._messageToMobile;
conversationMessage._messageFromMobile = data._messageFromMobile;
conversationMessage.save(function (err, conMes) {
if (conMes){
myDate = new Date(conMes.createdAt);
createdDate = myDate.getTime();
logger.info('Conversation msg create at :' +createdDate );
var msg ={
messageType:data.messageType,
messageText:data.messageText,
_conversationId:data._conversationId,
_messageToMobile:data._messageToMobile,
_messageFromMobile:data._messageFromMobile,
createdAt:createdDate,
conversationName:null,
conversationImageUrl:null,
//updatedAt:conversationMessage.updatedAt
}
var socketid;
var sendNotifcationFlag;
var recipientSocket;
if (data._messageToMobile){
// individual Chat
logger.info('Individual Chat - SendMessage');
socketid= userHashMaps.get (data._messageToMobile);
logger.info('sending a notification to socket: '+ socketid);
sendNotifcationFlag=true;
if (socketid){
recipientSocket=io.sockets.connected[socketid];
logger.info('Check room of Sender socket : ' + socket + 'where phone No is :'+socket.phoneNo + 'and room : ' +socket.room);
logger.info('Check room of Recipent socket :' + socketid + 'where phone No is :'+recipientSocket.phoneNo + 'and room : ' +recipientSocket.room);
if (recipientSocket){
logger.info('Receipient Socket Found' );
if (recipientSocket.room===socket.room) {
logger.info('Rooms Matched' );
//logger.info('Conversation msg create at before emiting:' +msg.createdAt );
recipientSocket.emit('receiveMessage', msg);
sendNotifcationFlag=false;
}
else {
logger.info('Rooms Not Matched recipientSocket.room : ' +recipientSocket.room + 'SenderSocket.room : ' +socket.room );
}
}
else {
logger.info('Receipient Socket Not Found , recipientSocket : ' + recipientSocket);
}
}
if (sendNotifcationFlag===true){
logger.info('Sending Onesignal Notifcation to '+ data._messageToMobile );
var query = { phone :data._messageToMobile };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
}
if (user){
logger.info('User Found For Phone No: ' + data._messageToMobile );
logger.info('Sending Notification to player id ' + user.palyer_id );
logger.info('Msg Object : ' + msg);
//logger.info('Individual Conversation msg before Push Notification:' );
NotificationController.sendNotifcationToPlayerId(user.palyer_id,msg,"receiveMessage");
//msg=null;
}
else {
logger.info('User not Found For Phone No: ' + data._messageToMobile );
}
});
}
}else{
//group Chat
logger.info('Group Chat - SendMessage');
logger.info ("Emiting to Group room : "+socket.room);
//logger.info('Group Conversation msg createAt before Emiting :' +msg.createdAt );
socket.to(socket.room).emit('receiveMessage', msg);
var phoneNo;
//Sending Message As push Notification to all members
if (conversationId){
ChatController.findConversation (conversationId , function(con){
if (con){
msg.conversationName=con.conversationName;
msg.conversationImageUrl=con.conversationImageUrl;
ChatController.findConversationMembers(conversationId, function(members){
logger.info ('findConversationMembers Response, Members List Size : ' + members.length);
//Notifying All Group Members
for (var i=0; i < members.length ; i++){
phoneNo=members[i]._userMobile;
if (phoneNo!==(data._messageFromMobile)){
//Sending Push Notiifcation To Group Members
logger.info('Sending Onesignal Notifcation of receiveMessage to '+ phoneNo );
var query = { phone : phoneNo };
User.findOne(query).exec(function(err, user){
if (err){
logger.error('Some Error occured while finding user' + err );
}
if (user){
logger.info('User Found For Phone No: ' + user.phone );
logger.info('Group Conversation msg createAt before Push Notiifcation :' +msg.createdAt );
logger.info('Sending Notification of Group : '+msg.conversationName+ 'Phone No: ' + user.phone +' & to player id : ' + user.palyer_id );
NotificationController.sendNotifcationToPlayerId(user.palyer_id,msg,"receiveMessage");
//msg=null;
//socketid= userHashMaps.get ( user.phone);
//logger.info ('socketid : '+ socketid);
//recipientSocket=io.sockets.connected[socketid];
//logger.info ('recipientSocket : '+ recipientSocket);
//if (recipientSocket){
//if (recipientSocket.room===conversationId) {
//do not send notification
//NotificationController.sendNotifcationToPlayerId(user.palyer_id,msg,"receiveMessage");
//}
//else {
//NotificationController.sendNotifcationToPlayerId(user.palyer_id,msg,"receiveMessage");
//}
//}else{
//}
}
else {
logger.info('User not Found For Phone No: ' + user.phone );
}
});
}
}
}); //end of findConversationMembers call
}
else {
logger.info('Error Finding Group conversation for convesation id :'+ conversationId);
}
});
}//// end if conversationID
}// end of else data._messageToMobile
}
if (err){
logger.error('Eror Saving Conversation message' +err);
}
});
}catch(err){
logger.info('An Exception Has occured in sendMessage event' + err);
}//end of catch
}); //end of sendMessage Event
socket.on('disconnect', function () {
logger.info('Disconnect Event \n ' + socket.phoneNo + ' is disconnected' );
userHashMaps.remove(socket.phoneNo);
logger.info("Connected Users Count : " + userHashMaps.count());
});//end of disconnect Event
}); //end of Connection Event