-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
826 lines (769 loc) · 22.1 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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
require('dotenv').config()
const mysql = require('mysql');
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const socket = require('socket.io');
const exphbs = require("express-handlebars");
const handlebars = require("handlebars");
const indexRouter = require('./indexRouter.js');
const nodemailer = require('nodemailer');
//init config vars
const PORT = process.env.PORT || 4002;
//connect database
var connection = mysql.createConnection({
host : process.env.JAWSDB_URL,
user : process.env.SQL_USER,
password : process.env.SQLPASSWORD,
database : process.env.DATABASE
});
//engine setup
var app = express();
const hbs = exphbs.create({
extname: "hbs",
defaultLayout: "main",
helpers: {
neq: function (a, b) {
return (a != b);
},
eq: function (a, b) {
return (a == b);
},
not: function (a) {
return (!a);
},
and: function (a, b) {
return (a && b);
},
empty: function(s) {
if (typeof s == typeof new Set()) {
return (s.size == 0);
} else {
return true;
}
}
}
});
//db session setup
app.use(session({
secret: process.env.HASHKEY,
resave: true,
saveUninitialized: true
}));
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use("/", indexRouter);
//union of sql message row data packet arrays
function messageUnion(a, b) {
let obj = {};
//use unique identifier id as keys
a.forEach((item) => {
obj[item.id] = item;
});
//if 2+ items have same id, they will override object key
b.forEach((item) => {
obj[item.id] = item;
});
//return obj values
return Object.values(obj);
}
//remove item from array
function removeItem(array, item) {
const index = array.indexOf(item);
if (index > -1) {
array.splice(index, 1);
}
return array;
}
//authenticate login
app.post('*/auth', function(request, response) {
var email = request.body.email;
var password = request.body.password;
if (email && password) {
connection.query('SELECT * FROM accounts WHERE email = ? AND password = ?', [email, password], function(error, results, fields) {
if (results.length > 0 && results[0].active == 1) {
request.session.userid = results[0].id;
request.session.loggedin = true;
request.session.email = email;
request.session.name = results[0].name;
request.session.password = results[0].password;
//expires in 1 year
request.session.cookie.maxAge = 365 * 24 * 60 * 60 * 1000;
response.redirect('/chat');
} else {
response.redirect('/login/true');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
//register new user
app.post('*/register-post', function(request, response) {
//get info from form body
var email = request.body.email;
var password = request.body.password;
var name = request.body.name;
var notifications = request.body.notifications;
//turn notifications to boolean
if (notifications == 'on') {
notifications = true;
} else {
notifications = false;
}
//create (psudo) random token
var token = Math.random().toString().replace(/\./g, '');
//check input is valid
if (email.length > 255) {
response.send('max email length is 255 characters');
response.end();
} else if (password.length > 255) {
response.send('max pass length is 255 characters');
response.end();
} else if (name.length > 63) {
response.send('max name length is 63 characters');
response.end();
} else if (!email.includes('@')) {
response.send('must be real email');
response.end();
} else if (email.includes(' ') || name.includes(' ')) {
response.send('no spaces allowed');
response.end();
} else if (email.includes('/') || name.includes('/')) {
response.send('no / allowed');
response.end();
} else if (name.includes('Both')) {
response.send('You cant have the word "Both" in your name. Its a really weird rule because Im really bad at coding');
response.end();
}
//check if email or name is already taken
else if (email && password && name) {
connection.query(
'SELECT * FROM accounts WHERE email = ? OR name = ?',
[email, name],
(err, res, fields)=> {
if (res.length == 0) {
connection.query(
"INSERT INTO accounts (name, email, password, token, notifications, grouptype, active) VALUES (?, ?, ?, ?, ?, ?, ?);",
[
name,
email,
password,
token,
notifications,
false,
false
],
(err, res, fields)=> {
if (name != 'Noah' && name != 'Pranav') {
connection.query("INSERT INTO accounts (name, email, password, token, notifications, grouptype, active) VALUES (?, ?, ?, ?, ?, ?, ?);",
[
'Both-' + name,
email,
password,
token,
notifications,
true,
false
],
(err, res, fields)=> {
if (!err) {
response.redirect('/verify/' + name);
//send verification email
mail(
email,
"NoahandPranav Account Verification",
"This is your verification code-\n" + token
);
} else {console.log(err);}
});
} else {
response.redirect('/verify/' + name);
//send verification email
mail(
email,
"NoahandPranav Account Verification",
"This is your verification code-\n" + token
);
}
});
} else {
response.send('That email or username is already taken');
response.end();
}
}
);
}
});
//logout
app.post('*/logout', function(request, response) {
//delete cookie
request.session.destroy();
//redirect
response.redirect('/');
});
//verify post method
app.post('*/verify-post', function(request, response) {
let name = request.body.name;
let token = request.body.token;
if (!name.includes('Both')) {
connection.query(
//find matching acount
`SELECT * FROM accounts WHERE name = ? AND token = ?;`,
[name, token],
(err, res, feilds) => {
//if one account matches
if (res.length == 1) {
//get vars for cookie
let data = res[0];
let email = data.email;
let password = data.password;
//activate account
connection.query('UPDATE accounts SET active = ? WHERE name = ? AND token = ?;',
[true, name, token],
(err, res, feilds) => {
//expires in 1 year
request.session.cookie.maxAge = 365 * 24 * 60 * 60 * 1000;
//set cookie
request.session.loggedin = true;
request.session.email = email;
request.session.name = name;
request.session.password = password;
//redirect to chat
response.redirect('/chat');
});
} else {
response.redirect('back');
}
}
);
} else {
response.send('An account with "Both" in its name cannot be verified');
response.end();
}
});
//verify route
//chat with Noah
app.get("/verify/:name?", (req, result) => {
result.set('Cache-Control', 'no-store');
let name = req.params.name;
result.render("verify", {
name,
});
});
//functional chat page
app.get("/chat/:person", (req, result) => {
result.set('Cache-Control', 'no-store');
//get cookie vars
let email = req.session.email;
let name = req.session.name;
let password = req.session.password;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
//get person
let person = req.params.person;
if (person == "noah") {
person = "Noah";
} else if (person == "pranav") {
person = "Pranav";
} else if (person == "Both") {
} else {
person = null;
}
if (loggedin) {
//get all accounts
connection.query('SELECT * FROM accounts;', (err, res, feilds) => {
//reformat contact list
var contactList = res;
for (let i = 0; i < contactList.length; i++) {
let elm = contactList[i];
contactList[i] = Object.fromEntries([['name', elm.name], ['id', elm.id]]);
}
//get users non group chat messages from db
connection.query(
`SELECT messages.id, senders.name AS sender, recievers.name AS reciever, messages.message
FROM messages
INNER JOIN accounts AS senders
ON senders.id = messages.sender_id AND messages.type = 0
LEFT JOIN accounts AS recievers
ON recievers.id = messages.reciever_id
HAVING (reciever = ? OR sender = ?) AND NOT reciever = ?
ORDER BY id;`,
[name, name, 'Both-' + name],
(err, res, feilds) => {
//render page
let messages = res;
let target = name;
let permissions = false;
if (
(process.env.NOAH_PASSWORD == password && name == "Noah") ||
(process.env.PRANAV_PASSWORD == password && name == "Pranav")
) {
//if permissinos, get all group chats
permissions = true;
connection.query(
`SELECT messages.id, senders.name AS sender, recievers.name AS reciever, messages.message, recievers.grouptype
FROM messages
INNER JOIN accounts AS senders
ON senders.id = messages.sender_id AND messages.type = 0
LEFT JOIN accounts AS recievers
ON recievers.id = messages.reciever_id
HAVING recievers.grouptype = 1
ORDER BY id;`,
(err, res, feilds) => {
//get union of messages and res
messages = messageUnion(messages, res);
//render page
result.render("front-end-chat", {
name,
email,
loggedin,
contactList,
messages,
target,
person,
permissions,
notes,
});
});
} else {
//if no permissions
//only three contacts
contactList = [
{name: "Noah", id: -1},
{name: "Pranav", id: -1},
{name: "Both", id: -1},
];
//get "both" messages
connection.query(
`SELECT messages.id, senders.name AS sender, recievers.name AS reciever, messages.message
FROM messages
INNER JOIN accounts AS senders
ON senders.id = messages.sender_id AND messages.type = 0
LEFT JOIN accounts AS recievers
ON recievers.id = messages.reciever_id
HAVING reciever = ?
ORDER BY id;`,
['Both-' + name],
(err, res, feilds) => {
//change reciever name from "Both-x" to "Both";
res.forEach((item) => {
item.reciever = 'Both';
});
//get union of messages and res
messages = messageUnion(messages, res);
//render page
result.render("front-end-chat", {
name,
email,
loggedin,
contactList,
messages,
target,
person,
permissions,
notes,
});
});
}
delete contactList;
delete messages;
}
);
});
} else {
result.render("login", {
name,
email,
loggedin,
notes,
});
}
});
//index
app.get("/", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
res.render("index", {
name,
email,
loggedin,
notes,
});
});
//delete later
app.get("/test", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
res.render("temp", {
name,
email,
loggedin,
notes,
});
});
//get user socket.id
app.get("/get-socketid/:username", (req, res) => {
let name = req.params.username;
res.json({
id: nameToId[name],
noah: noahSocket,
pranav: pranavSocket
});
});
//chat
app.get("/chat", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
res.render("chat", {
name,
email,
loggedin,
notes,
});
});
//login
app.get("/login/:invalid?", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let invalid = req.params.invalid;
let notes = noteObj[name];
res.render("login", {
name,
email,
loggedin,
invalid,
notes,
});
});
//forgot
app.get("/forgot", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
res.render("forgot", {
name,
email,
loggedin,
notes,
});
});
//register
app.get("/register", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
res.render("register", {
name,
email,
loggedin,
notes,
});
});
app.get("/profile", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let notes = noteObj[name];
if (loggedin) {
res.render("profile", {
name,
email,
loggedin,
notes,
});
} else {
res.render("login", {
name,
email,
loggedin,
notes,
});
}
});
//catch all, make sure this is defined last
app.get("*", (req, res) => {
res.set('Cache-Control', 'no-store');
let email = req.session.email;
let name = req.session.name;
let loggedin = req.session.loggedin;
let search = req.params;
let notes = noteObj[name];
res.render("catch-all", {
name,
email,
loggedin,
search,
notes,
});
});
//run server
var server = app.listen(PORT, () => {console.log('listening on port', PORT, '...');});
//notifications
const noteObj = {}; //keys: names, vals: set of notifications
//temporary code to initialize with notificaiotns
noteObj["Noah"] = new Set();
function addNotification(sender, reciever) {
if (noteObj[reciever]) {
noteObj[reciever].add(sender);
} else {
noteObj[reciever] = new Set();
noteObj[reciever].add(sender);
}
}
function removeNotification(sender, reciever) {
if (noteObj[reciever]) {
noteObj[reciever].delete(sender);
} else {
return
}
}
//socket.io setup
//vars
io = socket(server);
const users = {};
const nameToId = {};
var noahSocket = null;
var pranavSocket = null;
//user class
class User {
constructor(name, email, id) {
this.name = name;
this.email = email;
this.id = id;
}
}
//add new user
function addUser(name, email, id) {
let newUser = new User(name, email, id);
users[id] = newUser;
nameToId[name] = id;
}
//insert msg in db
function insertMsg(message, send_name, rcv_name, type) {
//query for sender info
connection.query(
'SELECT * FROM accounts WHERE name = ?;',
[send_name],
(err, res, feilds) => {
if (!err) {
if (res.length == 1) {
let send_sql_id = res[0].id;
//query for rcv info
connection.query(
'SELECT * FROM accounts WHERE name = ?;',
[rcv_name],
(err, res, feilds) => {
if (!err) {
if (res.length == 1) {
let rcv_sql_id = res[0].id;
connection.query(
'INSERT INTO messages (sender_id, reciever_id, type, message) VALUES (?, ?, ?, ?);',
[send_sql_id, rcv_sql_id, type, message],
(err, res, feilds) => {return}
);
}
//handle possible errors
else {
throw '2 accts have same creds: ' + res.toString();
}
} else {
console.log(err);
}
}
);
} else {
throw '2 accts have same creds: ' + res.toString();
}
}
}
);
}
io.on('connection', socket => {
//special new user for noah
socket.on('new-Noah', (name, email) => {
noahSocket = socket.id;
addUser(name, email, socket.id);
socket.broadcast.emit('user-connected', name);
});
//special new user for pranav
socket.on('new-Pranav', (name, email) => {
pranavSocket = socket.id;
addUser(name, email, socket.id);
socket.broadcast.emit('user-connected', name);
});
//register new user
socket.on('new-user', (name, email) => {
addUser(name, email, socket.id);
socket.broadcast.emit('user-connected', name);
});
//send to noah (depricated)
socket.on('send-noah', data => {
let name = users[socket.id].name;
insertMsg(data.message, name, "Noah", 0);
socket.to(noahSocket).emit(
"chat-message",
socket.id,
{message: data['message'], name: name}
);
});
//send message
socket.on('private-chat', (id, data) => {
let name = data.senderName;
insertMsg(data.message, name, data.recieverName, 0);
socket.to(id).emit(
"chat-message",
socket.id,
{message: data['message'], name: name});
});
//send message without saving
socket.on('private-chat-without-save', (id, data) => {
let name = data.senderName;
socket.to(id).emit(
"chat-message",
socket.id,
{message: data['message'], name: name});
});
//remove notificaiton
socket.on('remove-notification', (sender, reciever) => {
removeNotification(sender, reciever);
});
//add notificaiton
socket.on('add-notification', (id, data) => {
//init vars
let recieverName = data.recieverName;
let senderName = data.senderName;
let message = data.message;
//add local notification
addNotification(senderName, recieverName);
//fix special case for group chat
if (recieverName.includes('Both')) {
//get recipients
let grouChatSubject = recieverName.replace('Both-', '');
let possibleRecipients = ['Noah', 'Pranav', grouChatSubject];
let recipients = removeItem(possibleRecipients, senderName);
//add local notification
addNotification(senderName, recipients[0]);
//get email, notifications of first reciever
connection.query(
'SELECT email, notifications FROM accounts WHERE name = ?',
[recipients[0]],
(err, res, feilds) => {
let email = res[0].email;
//if notifications enabled, send email
if (res[0].notifications) {
mail(
email,
'New N+P Message From ' + senderName,
senderName + ':\n\n"' + message + '"\n\nNoahandPranav.herokuapp.com/chat/' + senderName
);
}
//add local notification
addNotification(senderName, recipients[1]);
//get email, notifications of second reciever
connection.query(
'SELECT email, notifications FROM accounts WHERE name = ?',
[recipients[1]],
(err, res, feilds) => {
let email = res[0].email;
//if notifications enabled, send email
if (res[0].notifications) {
mail(
email,
'New N+P Message From ' + senderName,
senderName + ':\n\n"' + message + '"\n\nNoahandPranav.herokuapp.com/chat/' + senderName
);
}
}
);
}
);
}
//normal case
else {
//add local notification
addNotification(senderName, recieverName);
//get email, notifications of reciever
connection.query(
'SELECT email, notifications FROM accounts WHERE name = ?',
[recieverName],
(err, res, feilds) => {
let email = res[0].email;
//if notifications enabled, send email
if (res[0].notifications) {
mail(
email,
'New N+P Message From ' + senderName,
senderName + ':\n\n"' + message + '"\n\nNoahandPranav.herokuapp.com/chat/' + senderName
);
}
}
);
}
});
//save message without re-emiting
socket.on('save-message', (id, data, type) => {
let senderName = data.senderName;
let recieverName = data.recieverName;
//insert into sql db
insertMsg(data.message, senderName, recieverName, type);
});
//user disconnect
socket.on('disconnect', () => {
if (socket.id == noahSocket) {
noahSocket = null;
} else if (socket.id == pranavSocket){
pranavSocket = null;
}
if (users[socket.id]) {
socket.broadcast.emit('user-disconnected', users[socket.id].name, socket.id);
delete nameToId[users[socket.id].name];
delete users[socket.id];
}
});
});
//node mail code
// Step 1
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.NOAH_EMAIL,
pass: process.env.EMAIL_PASSWORD
}
});
function mail(reciever, subject, text) {
// Step 2
let mailOptions = {
from: '[email protected]',
to: reciever,
subject: subject,
text: text
};
// Step 3
transporter.sendMail(mailOptions, (err, data) => {
if (err) {
return console.log(err);
}
});
}