-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
525 lines (482 loc) · 14.5 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
// Requiring the dependencies
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const {
v4: uuidv4
} = require('uuid');
const mongoose = require('mongoose');
const socket = require('socket.io');
const sgMail = require('@sendgrid/mail')
require('dotenv').config();
//Setting API Key for sendGrid Mail API
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
// Declaring variables
const PORT = process.env.PORT || 3000;
const app = express();
var numClients = {};
// Configuration of application
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('public'));
// mongodb://localhost:27017
//Connecting to mongodb Atlas DB server
var mongodb_connection = "mongodb+srv://" + process.env.mongo_Atlas_username + ":" + process.env.mongo_Atlas_pass + "@clusterteamsclone.iebjk.mongodb.net/userDB";
mongoose.connect(mongodb_connection, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Defining Schemas
const chatSchema = {
message: String,
senderName: String,
senderID: String
};
const teamSchema = {
teamName: String,
teamID: String,
members: [String],
chats: [chatSchema]
}
const userSchema = {
name: String,
email: String,
password: String,
organization: String,
mobileNumber: String,
teams: [teamSchema],
};
// Adding collections in Database
const User = new mongoose.model("User", userSchema);
const Teams = new mongoose.model("teamList", teamSchema);
const Chats = new mongoose.model("chatList", chatSchema);
// **************************************************************** EXPRESS ROUTING (GET) ****************************************************************
app.get('/', function(req, res) {
// Rendering home page
res.render('home', {
loginStatus: ''
});
})
app.get('/register', function(req, res) {
// Rendering registration page
res.render('register', {
status: ''
});
})
app.get('/dashboard/:userID', function(req, res) {
// Checking if User exists - to prevent hard routes from loading
User.findOne({
_id: req.params.userID
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
// Rendering Dashboard
res.render('dashboard', {
user: foundUser
});
} else {
// Redirecting to login page
res.redirect('/');
}
}
});
})
app.get('/:userID/createTeam', function(req, res) {
// Rendering create team page
res.render('createTeam', {
userId: req.params.userID
});
})
app.get('/:userID/joinTeam', function(req, res) {
// Rendering join team page
res.render('joinTeam', {
userId: req.params.userID,
joinStatus: ''
});
})
app.get('/:userID/:teamID', function(req, res) {
// Checking if user exists - to prevent hard routes
User.findOne({
_id: req.params.userID
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
var callStatus;
// Checking the current call status for that team
if (numClients[req.params.teamID]) {
if (numClients[req.params.teamID].length == 0) {
callStatus = 'START CALL';
} else {
callStatus = 'JOIN CALL';
}
} else {
callStatus = 'START CALL';
}
if (foundUser) {
// Checking if team exists- to prevent hard routes
Teams.findOne({
teamID: req.params.teamID
}, function(err, foundTeam) {
if (err) {
console.log(err);
} else {
// Rendering Team Dashboard
res.render('teamTemplate', {
user: foundUser,
team: foundTeam,
callStatus: callStatus
});
}
})
} else {
// Redirecting to login page
res.redirect('/');
}
}
});
})
app.get('/:userID/:teamID/call', function(req, res) {
// Checking if user exists - to prevent hard routes
User.findOne({
_id: req.params.userID
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
// Checking if team exists - to prevent hard routes
Teams.findOne({
teamID: req.params.teamID
}, function(err, foundTeam) {
if (err) {
console.log(err);
} else {
// Rendering call room
res.render('room', {
user: foundUser,
team: foundTeam,
roomID: req.params.teamID,
audio: req.query.audio,
video: req.query.video
});
}
})
} else {
// Redirecting to login page
res.redirect('/');
}
}
})
})
// **************************************************************** EXPRESS ROUTING (POST) ****************************************************************
app.post('/', function(req, res) {
var email = req.body.emailID;
var password = req.body.password;
// Checking if user exists - to prevent hard routes
User.findOne({
email: email
}, function(err, foundUser) {
if (err) {
res.render('home', {
loginStatus: "Unknown error. Please try again later or contact the admin."
})
console.log(err);
} else {
if (foundUser) {
// Checking if the password entered matches with the password stored in our database
if (foundUser.password == password) {
// Renering dashboard if password is correct
res.redirect('/dashboard' + `/${foundUser._id}`);
} else {
// Notifying user if password does not match
res.render('home', {
loginStatus: "Incorrect email ID or password. Please try again."
})
}
} else {
// Notifying user if they need to register
res.render('home', {
loginStatus: "User not found."
})
}
}
});
});
app.post('/register', function(req, res) {
var email = req.body.emailID;
// Checking if the user already exixts
User.findOne({
email: email
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
// If user exists, notify them to login
res.render('register', {
status: 2
});
} else {
// If user does not exist, create a new user
const newUser = new User({
// Setting user details from the registeration form
name: req.body.fullName,
email: req.body.emailID,
password: req.body.password,
organization: req.body.organization,
mobileNumber: req.body.mobileNumber
});
// Saving the new user
newUser.save(function(err) {
if (err) {
console.log(err);
} else {
// Notifying the user that they have successfully registered
res.render('register', {
status: 1
});
}
});
}
}
});
});
// Creating a new team
app.post('/:userID/createTeam', function(req, res) {
var teamName = req.body.teamName;
var teamID = uuidv4();
// Setting team details as entered by the user
const newTeam = new Teams({
teamName: teamName,
teamID: teamID,
});
// Saving the new team
newTeam.save(function(err) {
if (err) {
console.log(err);
}
});
/Finding the user in our database/
User.findOne({
_id: req.params.userID
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
// Updating the teamlist to add the user as a memeber of the team for mailing list
Teams.updateOne({
teamID: newTeam.teamID
}, {
$push: {
"members": foundUser.email
}
}, function(err) {
if (err) {
console.log(err);
}
});
} else {
console.log("User not found");
}
}
})
// Updating the user database to add the team in the teams list of the user
User.updateOne({
_id: req.params.userID
}, {
$push: {
"teams": newTeam
}
}, function(err) {
if (err) {
console.log(err);
} else {
// Redirectign to the dashboard
res.redirect('/dashboard' + `/${req.params.userID}`);
}
});
});
app.post('/:userID/joinTeam', function(req, res) {
var teamID = req.body.teamID;
// Checking if the team exists
Teams.findOne({
teamID: teamID
}, function(err, foundTeam) {
if (err) {
console.log(err);
} else {
if (foundTeam) {
// Checking if the user exists - to prevent hard routing
User.findOne({
_id: req.params.userID
}, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
// CHecking if the user is already a member of that team
var alreadyMember = false;
foundUser.teams.forEach(function(team) {
console.log(team.teamID);
if (team.teamID == teamID) {
alreadyMember = true;
}
});
if (alreadyMember) {
// If user is already a memeber of the team, Redirect to dashboard
res.redirect('/dashboard' + `/${req.params.userID}`);
} else {
// If user is not a memeber of the team, update the teamlist and user DB and then redirect to dashboard
Teams.updateOne({
teamID: foundTeam.teamID
}, {
$push: {
"members": foundUser.email
}
}, function(err) {
if (err) {
console.log(err);
}
})
User.updateOne({
_id: req.params.userID
}, {
$push: {
"teams": foundTeam
}
}, function(err) {
if (err) {
console.log(err);
} else {
res.redirect('/dashboard' + `/${req.params.userID}`);
}
});
}
}
}
});
} else {
// If team does not exist, Notify the user that the team ID entered is invalid
res.render('joinTeam', {
userId: req.params.userID,
joinStatus: 'Team does not exit. Please enter a valid Team ID.'
})
}
}
});
});
// When user attempts to start/join a call
app.post('/:userID/:teamID', function(req, res) {
var callID = req.params.teamID;
// Check if the user want to join the call with video and mic on/off
var audio = Boolean(req.body.audioControl);
var video = Boolean(req.body.videoControl);
// Redirect to the call room
res.redirect(`/${req.params.userID}` + `/${callID}` + '/call?audio=' + audio + '&video=' + video);
});
// Server listening on PORT
var server = app.listen(PORT, function() {
console.log("Server is running on port " + PORT);
})
// *********************************************************** SOCKET CONNECTIONS FOR CHAT AND CALL ***********************************************************
// Initializing the socket
var io = socket(server);
// Socket connection
io.on('connection', function(socket) {
// On socket join-call
socket.on('join-call', (roomID, userId) => {
socket.join(roomID)
// Update number of Clients for that room (Number of users on call)
if (numClients[roomID] == undefined) {
numClients[roomID] = [userId];
} else {
numClients[roomID].push(userId);
}
console.log("user connected: " + socket.id);
// Emit to room that user is connected
socket.to(roomID).emit('user-connected', userId);
// When socket disconnects
socket.on('disconnect', () => {
// Update number of Clients for that room (Number of users on call)
numClients[roomID].splice(numClients[roomID].indexOf(userId));
console.log("user disconnected: " + socket.id);
// emit to room that user disconnected
socket.to(roomID).emit('user-disconnected', userId);
})
})
// On socket join-room (Chat only)
socket.on('join-room', (roomID) => {
socket.join(roomID)
console.log("user connected: " + socket.id);
socket.on('disconnect', () => {
console.log("user disconnected: " + socket.id);
})
})
// When users send a chat
socket.on('sendingMessage', function(data, roomID) {
// Create new chat object
const chat = new Chats({
message: data.message,
senderName: data.username,
senderID: data.userID
});
// Add the chat in User database for all the users that are a memeber of the team
User.updateMany({
"teams.teamID": roomID
}, {
$push: {
"teams.$.chats": chat
}
}, function(err) {
if (err) {
console.log(err);
}
});
// Emit the message in the room for all memebers of the team
io.sockets.in(roomID).emit("broadcastMessage", data);
});
// When users Schedule a Meeting
socket.on('sendMeetingInvite', function(data, teamID) {
// Find the team in our database to get the mailing list
Teams.findOne({
teamID: teamID
}, function(err, foundTeam) {
if (err) {
console.log(err);
} else {
// Construct the mail
const msg = {
// Send to all the email IDs in the mailing list of that team
to: foundTeam.members,
// Send from our verified sender email address for this application
from: '[email protected]', // To change this address, make sure the sender is verified with your sendgrid account
subject: 'TeamCall: Meeting Schedule',
html: '<h1>' + data.scheduledBy + ' Schdeuled a Meeting!</h1><br><p><strong> Title of meeting: </strong>' + data.meetingTitle + '<br><strong> Date of Meeting: </strong>' + data.meetingDate + '<br><strong> Time of Meeting: </strong>' + data.meetingTime,
}
// Sending the constructed mail with the help of sengGrid API
sgMail
.send(msg)
.then(() => {
// Emit to user that e-mail has been successfully sent.
socket.emit('emailStatus', {
status: "Sent"
});
console.log('Email sent to team members');
})
.catch((error) => {
// Emit to user that e-mail has not been sent due to an error.
socket.emit('emailStatus', {
status: "Not Sent"
});
console.error(error);
})
}
})
});
})