-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
736 lines (731 loc) · 28 KB
/
app.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const port = process.env.PORT || 8989
const http = require('http').createServer(app)
const io = require('socket.io')(http)
const path = require('path')
const session = require('express-session')
const mongoose = require('mongoose')
const mongodbstore = require('connect-mongodb-session')(session)
const morgan = require('morgan')
const helmet = require('helmet')
const cors = require('cors')
const xs = require('xss')
const csrf = require('csurf')
const chokidar = require('chokidar')
const cookieParser = require('cookie-parser')
const rfs = require('rotating-file-stream')
const sharedsession = require('express-socket.io-session')
const requestIp = require('request-ip')
const fs = require('fs-extra')
const moment = require('moment-timezone')
const {v4: uuidv4} = require('uuid')
const nl2br = require('nl2br')
const sys = require('systeminformation')
const hr = require('@tsmx/human-readable')
const route = require('./routes/index')
const admin = require('./routes/admin')
const uploader = require('./routes/uploader')
const {updateAdminSocketID, user_socket_id, election_handler, user_data, users_election_handler, course, mycourse, year, myyear, newNotification, sy} = require('./routes/functions')
//models
const election = require('./models/election')
const users = require('./models/user')
const conversations = require('./models/conversations')
mongoose.connect(process.env.db_url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const store = new mongodbstore({
uri: process.env.db_url,
collection: 'sessions'
})
store.on('error', (err) => {
console.log(err)
})
const dir = path.join(__dirname, './public')
const log_stream = rfs.createStream('logs.log', {
interval: '1d',
path: path.join(__dirname, 'log')
})
const appsession = session({
name: "wmsu-session-id",
secret: process.env.session_secret,
expires: 1000 * 60 * 60 * 24,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 1,
httpOnly: true,
sameSite: 'strict'
},
store: store,
resave: true,
saveUninitialized: true,
connectionOptions: {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 10000
}
})
app.use(
helmet({
contentSecurityPolicy: false,
})
)
app.use(morgan(':status :remote-addr :method :url :response-time ms <br>', { stream: log_stream }))
app.use(express.static(dir))
app.use(requestIp.mw())
app.use(express.urlencoded({ extended: false }))
app.use(express.json()) // json
app.use(uploader)
app.use(cors())
app.set('view engine', 'ejs')
if(app.get('env') === 'production'){
app.set('trust proxy', 1)
}
app.use(cookieParser())
app.use(csrf({cookie: true}))
app.use(appsession)
app.use(route) //all user req
app.use(admin) //all admin req
//http 404 req
app.use(function(req, res) {
return req.method === "GET" ? res.status(404).render('error/404') : res.status(404).send()
})
io.use(sharedsession(appsession, {
autoSave: true,
resave: true,
saveUninitialized: true
}))
//socket.io admin & user namespace
const admin_socket = io.of("/admin")
const users_socket = io.of("/users")
admin_socket.use(sharedsession(appsession, {
autoSave: true,
resave: true,
saveUninitialized: true
}))
users_socket.use(sharedsession(appsession, {
autoSave: true,
resave: true,
saveUninitialized: true
}))
//admin websocket events
admin_socket.on('connection', async (socket) => {
let {myid, currentElection, user_type, islogin} = socket.handshake.session
//update admin socket id every connection
if(islogin !== "okay" && user_type !== "Admin"){
socket.disconnect()
} else {
updateAdminSocketID(myid, socket.id)
admin_socket_id = socket.id
console.log("Admin Connected with soket Id of ", socket.id)
}
//election events
//if admin requests updated data of election
socket.on('election-data', async (data, res) => {
let new_election_data = {
voters: {
accepted: 0,
pending: 0,
voted: 0,
total: 0
},
candidates: {
accepted: 0,
pending: 0,
deleted: 0,
total: 0
},
partylists: 0,
positions: 0
}
try {
await election.find({_id: {$eq: xs(data.id)}}).then( (elec) => {
if(elec.length !== 0){
const election = elec.length === 0 ? [] : elec[0]
//accepeted voters
for(let i = 0; i < election.voters.length; i++){
if(election.voters[i].status === 'Accepted'){
new_election_data.voters.accepted += 1
}
if(election.voters[i].status === 'Pending'){
new_election_data.voters.pending += 1
}
if(election.voters[i].isvoted){
new_election_data.voters.voted += 1
}
}
//accepeted candidates
for(let i = 0; i < election.candidates.length; i++){
if(election.candidates[i].status === 'Accepted'){
new_election_data.candidates.accepted += 1
}
if(election.candidates[i].status === 'Pending'){
new_election_data.candidates.pending += 1
}
if(election.candidates[i].status === 'Deleted'){
new_election_data.candidates.deleted += 1
}
}
new_election_data.candidates.total = election.candidates.length
new_election_data.voters.total = election.voters.length
new_election_data.partylists = election.partylist.length
new_election_data.positions = election.positions.length
res({
status: true,
data: new_election_data
})
} else {
res({
status: false,
data: new_election_data
})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e){
res({
status: false,
msg: 'error'
})
}
})
socket.on('voter-accepted', async (data) => {
//get voter socket id
await users.find({_id: {$eq: xs(data.voterID)} }, {socket_id: 1}).then( (v) => {
const socket_id = v.length === 0 ? '' : v[0].socket_id
if(socket_id !== ''){
users_socket.to(socket_id).emit('voter-accepted', {electionID: currentElection})
}
})
})
socket.on('candidacy-form-accepted', async (data) => {
try {
//get candidate information
await election.find({
_id: {$eq: xs(currentElection)},
candidates: {$elemMatch: {id: {$eq: xs(data.candidacyID)}}}
}, {candidates: {$elemMatch: {id: {$eq: xs(data.candidacyID)}}}}).then( async (ca) => {
const student_id = ca.length === 0 ? '' : ca[0].candidates[0].student_id
const socket_id = await user_socket_id(student_id)
if(socket_id !== false){
users_socket.to(socket_id).emit('candidacy-accepted', {candidacyID: data.candidacyID})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e) {
console.log(e)
}
})
socket.on('elections', async (res) => {
res({
elections: await election.countDocuments()
})
})
//send event to all user in this election that dome information of election is change
socket.on('election-change', async (data) => {
//get all voters in this election
try {
await election.find({
_id: {$eq: xs(currentElection)}
}, {voters: 1}).then( async (elec) => {
if(elec.length > 0){
for(let i = 0; i < elec[0].voters.length; i++){
//get socket id of voter
await users.find({
student_id: {$eq: xs(elec[0].voters[i].student_id)}
}, {socket_id: 1}).then( (sid) => {
if(sid.length > 0){
if(sid[0].socket_id !== "Offline" || sid[0].socket_id !== "Waiting For Student"){
users_socket.to(sid[0].socket_id).emit('election-changed', {electionID: xs(data.electionID)})
}
}
}).catch( (e) => {
throw new Error(e)
})
}
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e) {
console.log(e)
}
})
socket.on('disconnect', () => {
console.log("Admin Disonnected with soket Id of ", socket.id)
updateAdminSocketID(myid, "Offline")
})
//election candidate names
socket.on('candidate-names', async (data, res) => {
let ca_data = {
names: [],
votes: []
}
try {
//get election
await election.find({
_id: {$eq: xs(data.id)}
}, {candidates: 1}).then( async (elec) => {
if(elec.length > 0){
//get all candidates that match in the sent position
const candidates = elec[0].candidates
for(let c = 0; c < candidates.length; c++){
if(candidates[c].position === xs(data.position) && candidates[c].status === "Accepted"){
ca_data.names.push(candidates[c].fullname.split(" ")[0])
ca_data.votes.push(candidates[c].votes.length)
}
}
res({
status: true,
data: ca_data
})
} else {
res({
status: false,
data: ca_data
})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e){
res({
status: false,
data: ca_data
})
}
})
//election courses
socket.on('get-courses', async (res) => {
let e_courses = []
await election.find({
_id: {$eq: xs(currentElection)}
}, {courses: 1}).then( async (elec) => {
if(elec.length > 0){
for(let c = 0; c < elec[0].courses.length; c++){
e_courses.push(await mycourse(elec[0].courses[c]))
}
res({
status: true,
data: e_courses
})
}
}).catch( (e) => {
res({
status: false,
data: e.message
})
})
})
//election year
socket.on('get-year', async (res) => {
let e_year = []
await election.find({
_id: {$eq: xs(currentElection)}
}, {year: 1}).then( async (elec) => {
if(elec.length > 0){
for(let y = 0; y < elec[0].year.length; y++){
e_year.push(await myyear(elec[0].year[y]))
}
res({
status: true,
data: e_year
})
}
}).catch( (e) => {
res({
status: false,
data: e.message
})
})
})
//total election voters & voters active
socket.on('election-voters-total', async (data, res) => {
const {id} = data
try {
await election.find({
_id: {$eq: xs(id)}
}, {voters: 1}).then( async (elec) => {
if(elec.length > 0){
const voters = elec[0].voters
let online_voters = 0
for(let v = 0; v < voters.length; v++){
await users.find({
student_id: {$eq: xs(voters[v].student_id)}
}, {socket_id: 1}).then( (status) => {
if(status.length > 0){
status[0].socket_id !== "Offline" ? online_voters += 1 : online_voters = online_voters
}
}).catch( (e) => {
throw new Error(e)
})
}
res({
status: true,
data: {
voters: voters.length,
active_voters: online_voters
}
})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e){
res({
status: false,
data: e.message
})
}
})
//send notification
socket.on('send-notification', async (data, res) => {
const status = await user_socket_id(data.student_id)
if(status !== "Offline"){
users_socket.to(status).emit('new_notification')
}
})
//get server information
socket.on('server-info', async (res) => {
const system = {
os: await sys.osInfo(),
cpu: await sys.cpu(),
memory: await sys.mem(),
disk: await sys.diskLayout()
}
res({
os: `${system.os.distro} ${system.os.release} ${system.os.codename} ${system.os.arch}`,
cpu: `${system.cpu.manufacturer} ${system.cpu.brand} ${system.cpu.speedMax}GHz ${system.cpu.cores} Cores`,
memory: `Used ${hr.fromBytes(system.memory.used, { fullPrecision: true })} / Total ${hr.fromBytes(system.memory.total, { fullPrecision: true })}`,
storage: system.disk.length > 0 ? `${hr.fromBytes(system.disk[0].size, {fullPrecision: true})}` : 'NaN'
})
})
})
//user websocket events
users_socket.on('connection', async (socket) => {
let {myid, electionID, islogin, user_type, device, chat} = socket.handshake.session
const {student_id} = await user_data(myid)
//update socket id every user connted to server
if(islogin && user_type === "Voter"){
await users.updateOne({
_id: {$eq: xs(myid)},
"devices.id": {$eq: xs(device)}
}, {$set: {
socket_id: socket.id,
"devices.$.status": 'Online',
sy: await sy(),
last_seen: ''
}}).then( async () => {
const kachat = await user_data(chat)
socket.to(kachat.socket_id).emit('kachat-reconnect')
console.log("New User Connected with soket Id of ", socket.id,)
admin_socket.emit('connected', {id: xs(myid)})
})
} else {
socket.disconnect()
}
socket.on('disconnect', async () => {
const kachat = await user_data(chat)
const socket_id = socket.id
await users.updateOne({
_id: {$eq: xs(myid)},
"devices.id": {$eq: xs(device)}
}, {$set: {
socket_id: 'Offline',
"devices.$.status": 'Offline',
sy: await sy(),
last_seen: moment().tz("Asia/Manila").format()
}}).then( async () => {
socket.to(kachat.socket_id).emit('kachat-disconnect')
console.log("New User Diconnected with soket Id of ", socket_id)
admin_socket.emit('user-disconnected', {id: xs(myid)})
})
})
//election events
socket.on('success-join-election', async (data) => {
//notify all admins that their is new voter attempt to join the election
admin_socket.emit('new-user-join-election', {id: myid, election: data.electionID})
})
//if user file for candidacy
socket.on('file-candidacy', (data) => {
admin_socket.emit('new-voter-file-for-candidacy', {id: myid, election: electionID})
})
//get election data
socket.on('election-status', async (data, res) => {
let electionStatus = {
id: data.electionID,
election: {
status: '',
title: ''
},
voters: {
total: 0,
status: ''
}
}
try {
await election.find({
_id: {$eq: xs(data.electionID)},
voters: {$elemMatch: {student_id: {$eq: xs(student_id)}}}
}, {voters: 1, status: 1, election_title: 1}).then( (elec) => {
const e_data = elec.length === 0 ? [] : elec[0].voters
electionStatus.election.title = elec.length === 0 ? '' : elec[0].election_title
//get voter status
if(e_data.length !== 0){
for(let i = 0; i < e_data.length; i++){
if(e_data[i].student_id === student_id){
electionStatus.voters.status = e_data[i].status
break
}
}
electionStatus.voters.total = elec.length === 0 ? 0 : elec[0].voters.length
electionStatus.election.status = elec.length === 0 ? '' : elec[0].status
res({
status: true,
data: electionStatus
})
} else {
res({
status: false,
data: {}
})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e) {
console.log(e)
res({
status: false,
data: {},
msg: "Something went wrong"
})
}
})
//candiates total reactions & views
socket.on('candidates-reactions&views', async (res) => {
try {
//get all candidates reaction & views
await election.find({
_id: {$eq: xs(electionID)}
}, {"candidates.id": 1, "candidates.reactions": 1, "candidates.views": 1}).then( (elec) => {
res({
status: true,
candidates: elec[0].candidates
})
}).catch( (e) => {
throw new Error(e)
})
} catch (e) {
res({
status: false,
msg: "Internal Error"
})
}
})
//email status
socket.on('email-status', async (res) => {
const {email} = await user_data(myid)
if(email.status === "Verified") {
res({status: true})
}
if(email.status === "Not Verified") {
res({status: false})
}
})
//device status
socket.on('device-status', async (data, res) => {
const {devices} = await user_data(myid)
let device_status = false
for(let i = 0; i < devices.length; i++){
if(devices[i].id === data.id && devices[i].verified){
device_status = true
break
}
}
res({
status: device_status
})
})
//election candidate names
socket.on('candidate-names', async (data, res) => {
let ca_data = {
names: [],
votes: []
}
try {
//get election
await election.find({
_id: {$eq: xs(data.id)}
}, {candidates: 1}).then( async (elec) => {
if(elec.length > 0){
//get all candidates that match in the sent position
const candidates = elec[0].candidates
for(let c = 0; c < candidates.length; c++){
if(candidates[c].position === xs(data.position) && candidates[c].status === "Accepted"){
ca_data.names.push(candidates[c].fullname)
ca_data.votes.push(candidates[c].votes.length)
}
}
res({
status: true,
data: ca_data
})
} else {
res({
status: false,
data: ca_data
})
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e){
res({
status: false,
data: ca_data
})
}
})
//send message
socket.on('send-message', async (data, res) => {
const {message, kachatid} = data
const {firstname, middlename, lastname, student_id} = await user_data(myid)
const new_msg = {
id: uuidv4(),
firstname: firstname,
fullname: `${firstname} ${middlename} ${lastname}`,
userid: xs(myid).toString(),
message: nl2br(xs(message)),
created: moment().tz("Asia/Manila").format()
}
try {
await conversations.find({
$and: [
{"userIDs.id": {$eq: xs(chat ? chat : kachatid)}},
{"userIDs.id": {$eq: xs(myid).toString()}}
]
}).then( async (convo) => {
if(convo.length > 0){
await conversations.updateOne({
$and: [
{"userIDs.id": {$eq: xs(chat ? chat : kachatid)}},
{"userIDs.id": {$eq: xs(myid).toString()}}
]
}, {$push: {messages: new_msg}}).then( async () => {
const {socket_id} = await user_data(chat ? chat : kachatid)
socket.to(socket_id).emit('new-message', new_msg)
return res({
status: true,
message: new_msg
})
}).catch( (e) => {
throw new Error(e)
})
} else {
throw new Error('Convo not found')
}
}).catch( (e) => {
throw new Error(e)
})
} catch (e) {
console.log(e)
return res({
status: false
})
}
}),
//typing status
socket.on('typing', async (data) => {
const {socket_id} = await user_data(chat ? chat : data.kachatid)
socket.to(socket_id).emit('typing')
})
socket.on('not-typing', async (data) => {
const {socket_id} = await user_data(chat ? chat : data.kachatid)
socket.to(socket_id).emit('not-typing')
})
})
//detect if flog file is change
chokidar.watch('./log/').on('all', async (event, path) => {
await fs.readFile('./log/logs.log', 'utf8', (err, data) => {
admin_socket.emit('logs', {logs: data})
})
})
start()
//check election every 10 seconds
setInterval(async () => {
await users_election_handler()
const election_status = await election_handler()
if (election_status !== undefined) {
//if there is new election started
if (election_status.status && election_status.type === "Started") {
//send event to admin & user that there is new election has been started
admin_socket.emit('new-election-started', { electionID: election_status.electionID })
//get all voter in this election and notify
await election.find({_id: {$eq: xs(election_status.electionID)}}, {voters: 1, election_title: 1}).then( async (elec) => {
if(elec.length > 0){
const voters = elec[0].voters
if(voters.length > 0){
for(let i = 0; i < voters.length; i++){
await users.find({student_id: {$eq: xs(voters[i].student_id)}}, {socket_id: 1, _id: 1}).then( async (s) => {
await newNotification(s[0]._id, 'election', {
id: uuidv4(),
type: 'info',
content: `${elec[0].election_title} has been started`,
created: moment().tz("Asia/Manila").format()
})
if(s[0].socket_id !== "Offline"){
users_socket.to(s[0].socket_id).emit('new_notification')
users_socket.to(s[0].socket_id).emit('new-election-started', { electionID: election_status.electionID })
}
})
}
}
}
})
}
//if there is new election ended
if (election_status.status && election_status.type === "Ended") {
//send event to admin & user that there is new election has been started
admin_socket.emit('new-election-ended', { electionID: election_status.electionID })
//get all voter in this election and notify
await election.find({_id: {$eq: xs(election_status.electionID)}}, {voters: 1, election_title: 1}).then( async (elec) => {
if(elec.length > 0){
const voters = elec[0].voters
if(voters.length > 0){
for(let i = 0; i < voters.length; i++){
await users.find({student_id: {$eq: xs(voters[i].student_id)}}, {socket_id: 1, _id: 1}).then( async (s) => {
await newNotification(s[0]._id, 'election', {
id: uuidv4(),
type: 'info',
content: `${elec[0].election_title} has been ended`,
created: moment().tz("Asia/Manila").format()
})
if(s[0].socket_id !== "Offline"){
users_socket.to(s[0].socket_id).emit('new_notification')
users_socket.to(s[0].socket_id).emit('new-election-ended', { electionID: election_status.electionID })
}
})
}
}
}
})
}
}
}, 2000)
async function start() {
await election_handler()
await users_election_handler()
if(process.env.NODE_ENV === 'production'){
await fs.remove(`${process.cwd()}/uploads/`)
await fs.remove(`${process.cwd()}/log/`)
}
http.listen(port, () => {
console.log(`Server Started on port ${port}`)
})
}