-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
354 lines (311 loc) · 11.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
// including necessary packages
const express = require('express');
const path = require('path');
const mongoose = require('mongoose')
const bcrypt = require("bcrypt");
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session');
const methodOverride = require('method-override')
const app = express();
const server = require('http').Server(app);
// including variables set in .env file
require('dotenv').config();
//connecting to MongoDB cloud via URI string defined in the .env file
mongoose.connect(process.env.dbURI, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true})
.then((res) => {console.log("Database connected!");})
.catch((err) => {console.log("error connecting to db" + err)})
//including methods to dynamically access the data of users ina particular meeting room
const {
addUser,
removeUser,
getUser,
getUsersInRoom
} = require('./utils/users')
// including database models
const User = require('./models/user')
const Chat = require('./models/chat')
const Chatroom = require('./models/chatroom')
//including passport configurations set for user authentication
const passport_config = require('./config_auth/passport-config')
passport_config(passport);
app.use(require('cors')())
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
app.use(flash())
app.use(session( {
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false
}))
app.use(methodOverride('_method'))
app.use(passport.initialize());
app.use(passport.session());
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
debug: true
});
app.use('/peerjs', peerServer);
// accessing server API of socket.io
const io = require('socket.io')(server)
// setting view engine as express
app.set('view engine', 'ejs');
const { v4: uuidv4} = require('uuid');
// Login route and login post methods
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login');
})
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureFlash: true,
failureRedirect: '/login'
}))
// User logout method
app.delete('/logout', (req, res) => {
req.logOut()
res.redirect('/login')
})
// User registeration route and registeration post method
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('register');
})
app.post('/register',async (req, res) => {
try {
//encrypt user password using bcrypt before storing it in a database
const encrypted_pwd = await bcrypt.hash(req.body.password, 10)
const user = new User({
username: req.body.username,
email: req.body.email,
password: encrypted_pwd,
})
// Check if user with same email id already a\exists in the database.
const result = await User.findOne({email: req.body.email}).select("email").lean();
if(result) {
console.log("user exists!")
res.redirect('/register')
}
else {
console.log("user does not exist!")
user.save()
.then((result) => {
res.redirect('/login');
})
.catch((err) => {
console.log(err);
})
}
}
catch {
res.redirect('/register')
}
})
app.get('/', checkAuthenticated, async (req, res) => {
const name = req.user.username;
// access the list of teams the user is a part of from the database and send it to the index.ejs page to be rendered
const result = await User.findOne({email: req.user.email}).select("chatroom_id").lean();
console.log(result)
var my_rooms = []
if(result)
my_rooms = result.chatroom_id;
else
my_rooms = [""]
console.log(my_rooms)
res.render('index', {name: name, rooms: my_rooms});
})
// Common team chatroom route
app.get('/chatroom/', (req, res) => {
console.log("Entering room route!");
res.redirect(`/chatroom/${uuidv4()}`);
});
app.get('/chatroom/:room', checkAuthenticated, async (req, res) => {
// get the prebious chat in the chatroom and send it to the ejs file to be displayed
const previous_chat = await Chat.aggregate([{
$match: {
chatroom_id: req.params.room
}
},
]);
res.render('chatroom2', {room_id: req.params.room, name: req.user.username, email: req.user.email, chat_history: previous_chat});
})
//video call route
app.get('/video_call/', (req, res) => {
console.log("Entering main route!");
res.redirect(`/video_call/${uuidv4()}`);
});
app.get('/video_call/:room', checkAuthenticated, (req, res) => {
res.render('room', {room_id: req.params.room, name: req.user.username});
})
// meeting end route
app.get('/meeting_end', (req, res) => {
res.render('end');
})
// listen for connection to socket
io.on('connection', socket => {
// listen for join-room and send the updated room data to all other peers in the room in case a new peer joins in
socket.on('join-room', async (room_id, user_id, username) => {
socket.join(room_id);
const { error, user } = addUser({ id: user_id, name: username, room: room_id });
socket.broadcast.to(room_id).emit('user-connected', user_id, username);
const users_in_room = getUsersInRoom(room_id) ;
//send data of all the peers in the room to the meeting participants in order to dynamically display the list of participants present in the meeting
io.to(room_id).emit('roomData', room = room_id, users = getUsersInRoom(room_id));
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = dd + "/" + mm + "/" + yyyy;
// create new chat entry to be stored in database
const new_chat = new Chat(
{ date_chat_id: "Meeting" + today + room_id,
chatroom_id: room_id,
chat_in_meeting: true,
content: ""
}
)
// check if the meeting room is associated with a team,
// Store chat ONLY if the meeting room is a part of a team
Chat.findOne({ date_chat_id: "Meeting" + today + room_id}, function(err, chat){
if(err) {
console.log(err);
}
var message;
if(chat) {
message = "chat exists";
console.log(message)
} else {
message= "chat doesn't exist";
console.log(message)
new_chat.save()
.then((result) => {
console.log("chat saved ")
})
.catch((err) => {
console.log(err);
})
}
})
// Listen for message from one of the peers in the room
// Store that message in the database(If the room is a team meeting room)
// broadcast that message to all the other memebers present in the room
socket.on('message', async (message, curr_time) => {
const html_li = `<li class = "blockquote blockquote-primary message"><span class = "message_info"><b> ${username} </b>   ${curr_time}</span><br>${message} </li>`
const result = await Chat.findOne({date_chat_id: "Meeting" + today + room_id}).select("_id").lean();
if (result){
Chat.updateOne({date_chat_id: "Meeting" + today + room_id}, { $addToSet: {content: html_li }}, function(err, result) {
if (err){
console.log(err);
}
else{
console.log("chat updated!");
}
});
}
io.to(room_id).emit('new_message', message, username)
})
// In case a peer is disconnected, update and resend the roomdata to all the peers present in the room
socket.on('disconnect', () => {
console.log("Broadcasting close event!")
socket.broadcast.to(room_id).emit('user-disconnected', user_id, username);
const user = removeUser(user_id);
io.to(room_id).emit('roomData', room = room_id, users = getUsersInRoom(room_id));
})
})
// Check if a new user has created or joined a chatroom
// Update the chatroom database accordingly
socket.on('join-chatroom', async (room_id, username, email) => {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = dd + "/" + mm + "/" + yyyy;
const result = await Chatroom.findOne({chatroom_id: room_id}).select("chatroom_id").lean();
if(result) {
Chatroom.updateOne({chatroom_id: room_id}, { $addToSet: {user_emails: email }}, function(err, result) {
if (err){
console.log(err);
}
else{
console.log("Chatroom users updated");
}
});
}
else {
const new_room = new Chatroom(
{
chatroom_id: room_id,
user_emails: [email]
}
)
new_room.save();
}
const new_chat = new Chat(
{ date_chat_id: today + room_id,
chatroom_id: room_id,
chat_in_meeting: false,
content: ""
}
)
Chat.findOne({ date_chat_id: today + room_id}, function(err, chat){
if(err) {
console.log(err);
}
var message;
if(chat) {
message = "chat exists";
console.log(message)
} else {
message= "chat doesn't exist";
console.log(message)
new_chat.save()
.then((result) => {
console.log("chat saved ")
})
.catch((err) => {
console.log(err);
})
}
})
// update user details to add chatroom in the list of rooms asociated with the user in the dayabase
User.updateOne({email: email}, { $addToSet: {chatroom_id: room_id }}, function(err, result) {
if (err){
console.log(err);
}
else{
console.log("User chatrooms updateOne: " + email + room_id);
}
});
socket.join(room_id);
// Listen for a message sent from within a chatroom
// Broadcast that message to other members in the chatroom
socket.on('chat_only_message', (message, curr_time) => {
const html_li = `<li class = "text-white mb-3 message"><span class = "message_info"><b> ${username} </b>   ${curr_time}</span><br>${message} </li>`
Chat.updateOne({date_chat_id: today + room_id}, { $addToSet: {content: html_li }}, function(err, result) {
if (err)
{
console.log(err);
}
else{
console.log("chat updated!");
}
});
io.to(room_id).emit('new_chat_only_message', message, username)
})
})
})
// Check if the user is authentiated
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/login')
}
// Check if the user is not authenticated
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
// set port for server to listen to
server.listen(process.env.PORT||3030);