-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
510 lines (462 loc) · 14.8 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
//import dotenv module to access secret variables
require('dotenv').config()
//import express module, used to create the server
const express = require('express');
//import body parser, to handle JSON in request bodys
var bodyParser = require('body-parser');
const http = require('http');
const CryptoJS = require('crypto-js');
const jwt = require('jsonwebtoken');
//create an express app
const app = express();
const User = require('./objects/user.js');
const Database = require('./objects/database.js');
const Post = require('./objects/post.js');
const Random = require('./objects/random.js');
const Chat = require('./objects/chat.js');
const authenticateToken = require('./middleware/authenticate.js');
var cookieParser = require('cookie-parser');
const user = require('./models/user.js');
const posts = require('./models/posts.js');
app.use(cookieParser());
//make express use body-parser's json
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var fileupload = require('express-fileupload');
app.use(fileupload());
var Profilephotos = require('./objects/profilephotos.js');
const post = require('./objects/post.js');
//set json spaces to 2, used when serving JSON data
app.set('json spaces', 2);
//make public a static directory, to serve static files
app.use(express.static('public'));
//set view engine to ejs, which allows to embed JS into HTML
app.set('view engine', 'ejs');
var server = app.listen('3000', () => console.log("Listening to Port 3000"));
var io = require('socket.io')(server);
var validator = require('validator');
//GET request handler for homepage
//OOPs impelemented
app.get('/', authenticateToken, async function (req, res) {
if (req.user != null) {
console.log("Page boolean:", req.user.isPage);
if (req.user.isPage == false) {
let obj = await new User().getDashboard(req);
res.render((__dirname + '/public/views/dashboard/dashboard.ejs'), {
user: obj.user,
following: obj.following,
profilephoto: obj.profilephoto,
post: obj.post,
});
} else {
let obj = await new Page().getDashboard(req);
res.render(__dirname + '/public/views/pages/page.ejs', {
username: req.user.username,
user: req.user,
pageimage: obj.pageimage,
following: obj.following,
profilephoto: obj.profilephoto
})
}
} else {
res.render(__dirname + '/public/views/login/login.ejs');
}
})
//GET request handler for Signup Page
//OOPs not needed
app.get('/signup/:id', async (req, res) => {
let id= req.params.id;
let link;
try {
link= await new User().showdb('register',{'id':id});
console.log(link)
}
catch(err) {
console.log("An error occured while checking your link. Please try registering again.")
}
if(link!=null){
res.status(200).render(__dirname + '/public/views/signup/signup.ejs',{'id': id});
}
else{
res.send('Email not verified!');
}
})
app.get('/newsignup',authenticateToken,async(req,res)=>{
if (req.user!=null) {
return res.redirect('/');
}
else {
res.render(__dirname+'/public/views/signup/newsignup.ejs');
}
});
app.post('/newsignup', authenticateToken, async(req, res) => {
if (req.user!=null) {
return res.redirect('/');
}
else{
let email = req.body.email;
if (!validator.isEmail(email)) {
res.send("Invalid Email Address.");
}
else {
new User().sendRegisterMail(email,res);
}
}
})
//POST request handler for Signup, adds an User to the DB by creating an Object
//object oriented
app.post('/signup/:id', async (req, res) => {
let id= req.params.id;
let link= await new User().showdb('register',{'id':id});
if(link!=null){
req.body.email = link.email;
console.log(link);
console.log(req.body);
const user = await new User(req.body);
let status = await user.createUser(req);
console.log(status);
if (!status) {
res.send('You failed to register! Please make sure you entered all the data.')
}
else {
await link.remove();
res.send(`Success!
<script>
setTimeout(function () {
// after 2 seconds
window.location = "/login";
}, 1500)
</script>`);
}
}
else{
res.send("Your email is not verified!");
}
})
app.get('/login', async (req, res) => {
res.status(200).render(__dirname + '/public/views/login/login.ejs');
})
app.post('/login', async (req, res) => {
//authenticate user
const {
username,
password
} = req.body;
let userObj = await new User(req.body);
let user = await userObj.loginCheck(username, password);
await userObj.generateToken(user, res);
})
//OOPs implemented
app.get('/logout', authenticateToken, async (req, res) => {
if (req.user != null) {
await new User().logout(req.user, req, res); //polymorphism
res.redirect('/');
} else {
res.redirect('/');
}
})
//OOPs implemented
app.get('/logoutall', authenticateToken, async (req, res) => {
if (req.user != null) {
await new User().logout(req.user, req, res, true); //polymorphism
res.redirect('/');
} else {
res.redirect('/');
}
})
app.get('/showdb', async (req, res) => {
let db = await new User().showdb('User', {});
res.status(200)
res.type('json');
res.send("User Database:\n\n" + JSON.stringify(db, null, "\t"))
})
//OOPs Implemented
app.get('/users', authenticateToken, async (req, res) => {
if (req.user != null) {
let obj = await new User().getFollowingList(req);
res.render(__dirname + '/public/views/users/users.ejs', {
users: obj.users,
user: obj.user,
profilepic: obj.profilepic,
following: obj.following
});
} else {
res.redirect('/');
}
})
//OOPs Implemented
app.post('/follow/:id', authenticateToken, async (req, res) => {
if (req.user != null) {
await new User().followUser(req, res);
} else {
res.redirect('/login')
}
})
//Implemented OOPs
app.get('/post', authenticateToken, async (req, res) => {
if (req.user != null) {
let userdb = new User().showdb('user', {});
userdb = JSON.stringify(userdb);
let timeline = await new User().getTimeline(req.user);
res.render(__dirname + '/public/views/posts/post.ejs', {
post: timeline['post'],
by: timeline['by'],
profilepic: timeline['profilepic'],
user: req.user,
userdb
});
} else {
res.redirect('/');
}
})
//Implement OOPs
app.post('/post', authenticateToken, async (req, res) => {
req.body.id = req.user._id;
let newpost = new Post()
if (req.body.tag=="Announcement") {
if (req.isAdmin==false||req.isSuperAdmin==false){
return;
}
}
let post = await newpost.createPost(req.body, req);
if (req.body.tag==="Complaint") {
let chat = await new Chat().createChat(post._id);
}
if (req.body.tag!="Announcement") {
res.redirect('/post');
}
else {
res.redirect('/announcements');
}
})
//Implemented OOPs
app.post('/likepost/:id', authenticateToken, async (req, res) => {
if (req.user != null) {
new Post().likePost(req, res);
} else {
res.redirect('/login')
}
})
app.post('/deletepost/:id', authenticateToken, async (req, res) => {
let post = await new Post().showdb('Post',{'_id': req.params.id});
if (req.user && req.user._id==post.id) {
await post.remove();
res.send("removed");
}
else {
res.redirect('/')
}
})
app.post('/removelike/:id', authenticateToken, async (req, res) => {
if (req.user) {
let postid = req.params.id;
let post = await posts.findOne({
'_id': postid
});
if (post != null) {
let index = post.likes.indexOf(req.user._id);
if (index > -1) {
post.likes.splice(index, 1);
}
await post.save();
} else {
res.send('Invalid Post ID');
}
} else {
res.redirect('/');
}
})
//OOPs Implemented
app.get('/comments/:id', authenticateToken, async (req, res) => {
if (req.user != null) {
let obj = await new Post().getComment(req);
let author = await new User().showdb('user', {
'_id': obj.post.id
})
res.render(__dirname + '/public/views/comments/comments.ejs', {
post: obj.post,
user: obj.user,
requser: req.user,
author
});
} else {
res.redirect('/');
}
})
//OOPs Implemented
app.post('/postcomment/:id', authenticateToken, async (req, res) => {
if (req.user != null) {
await new Post().postComment(req);
res.redirect(`/comments/${req.params.id}`);
} else {
res.redirect(__dirname + '/public/views/login/login.ejs')
}
})
1
app.get('/profile/:id', authenticateToken, async (req, res) => {
if (req.user != null) {
let user = await new User().showdb('user', {
'_id': req.params.id
});
let post = await new Post().showdb('post', {
'id': req.params.id
}, true);
let profilepic = await new Profilephotos().showdb('profilephotos', {
'id': req.params.id
});
if (!user.isPage) {
res.render(__dirname + '/public/views/profile/profile.ejs', {
user,
post,
profilepic,
requser: req.user
});
} else {
username = req.user.username;
req.user = user;
let obj = await new Page().getDashboard(req);
res.render(__dirname + '/public/views/pages/page.ejs', {
user: req.user,
username,
pageimage: obj.pageimage,
following: obj.following,
profilephoto: obj.profilephoto
})
}
} else {
res.redirect('/')
}
})
app.get('/complaints', authenticateToken, async (req, res) => {
if (req.user!=null) {
if (req.user.isAdmin===true||req.user.isSuperAdmin===true) {
data = await new User().getComplaints();
res.render(__dirname + '/public/views/complaints/complaints.ejs', {
post: data['post'],
by: data['by'],
profilepic: data['profilepic'],
user: req.user
});
}
else {
res.send("Access Denied.");
}
}
else {
res.redirect('/');
}
})
app.post('/resolve/:id', authenticateToken, async (req, res) => {
if (req.user!=null) {
let cid = req.params.id;
complaint = await new Post().showdb('Post', {'_id': cid});
await complaint.remove();
let chatroom = await new Chat().getMessages(cid);
await chatroom.remove();
}
else {
res.redirect('/');
}
})
app.post('/upgrade/:id', authenticateToken, async(req, res) => {
if (req.user!=null && req.user.isAdmin===true) {
let cid = req.params.id;
complaint = await new Post().showdb('Post', {'_id': cid});
complaint.tag = "Help";
await complaint.save();
}
else {
res.redirect('/');
}
})
app.get('/chat/:id', authenticateToken, async (req, res) => {
if (req.user!=null) {
let roomid = req.params.id;
let chat = await new Chat().getMessages(roomid);
if (chat!=null) {
io.once('connection', async socket => {
socket.join(roomid)
console.log(`${req.user.name} connected to Chatroom ID ${req.params.id}. Socket ID is: ${socket.id}`);
io.to(roomid).emit('user connect', `${req.user.name} connected to the Chatroom.`);
socket.on('disconnect', () => {
console.log(`${req.user.name} disconnected from Chatroom ID ${req.params.id}.`)
io.to(roomid).emit('user disconnect', `${req.user.name} disconnected from the Chatroom.`);
});
socket.on('chat message', async msg => {
let timestamp = new Date().toLocaleString("en-GB", {timeZone: "Asia/Kolkata",month: "long",day:"2-digit",year:"numeric","hour":"2-digit","minute":"2-digit","second":"2-digit",hour12: true})
console.log(`${req.user.name} sent a message to Chatroom ID ${req.params.id}: ${msg}`);
io.to(roomid).emit('chat message', {msg, user: req.user.name, timestamp});
chat.messages.push({'message': msg, 'timestamp': timestamp, 'by': req.user.name});
await chat.save();
});
});
res.render(__dirname + '/public/views/chat/chat.ejs',{user:req.user,chat});
}
else{
res.send("This chat does not exist.")
}
}
})
app.get('/usercomplaints',authenticateToken, async (req,res)=>{
if (req.user!=null) {
let data = await new User().getComplaints(req.user);
res.render(__dirname + '/public/views/complaints/usercomplaints.ejs', {
post: data['post'],
by: data['by'],
profilepic: data['profilepic'],
user: req.user
});
}
else {
res.redirect('/');
}
})
app.get('/help', authenticateToken, async (req, res) => {
if (req.user!=null) {
if (req.user.isSuperAdmin===true) {
data = await new User().getComplaints({},true);
res.render(__dirname + '/public/views/complaints/complaints.ejs', {
post: data['post'],
by: data['by'],
profilepic: data['profilepic'],
user: req.user
});
}
else {
res.send("Access Denied.");
}
}
else {
res.redirect('/');
}
});
app.get('/announcements', authenticateToken, async (req, res) => {
if (req.user!=null) {
let data = await new User().getAnnouncements();
res.render(__dirname + '/public/views/announcements/announcements.ejs', {
post: data['post'],
by: data['by'],
profilepic: data['profilepic'],
user: req.user
});
}
else {
res.redirect('/');
}
})
//render 404 page, if the requested URL is wrong or doesn't exist on the server
app.use(function (req, res, next) {
res.status(404);
res.render(__dirname + '/public/views/404/404.ejs', {
url: req.url
});
});
app.get('/keepalive', (req, res) => {
res.send('Ping Recieved ' + Date.now())
console.log('Ping Recieved ' + Date.now());
})
setInterval(function () {
http.get(process.env.hosturl + '/keepalive');
}, 30000)