-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
750 lines (620 loc) · 22.5 KB
/
app.js
File metadata and controls
750 lines (620 loc) · 22.5 KB
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
// This requires the necessary libraries for the webapp.
// (1) express - this provides the express web framework
// (2) handlebars - this provides the handlebars templating framework
var express = require('express');
var handlebars = require('express-handlebars');
// The body parser is used to parse the body of an HTTP request.
var bodyParser = require('body-parser');
// Require session library.
var session = require('express-session');
// Require flash library.
var flash = require('connect-flash');
// The cookie parser is used to parse cookies in an HTTP header.
var cookieParser = require('cookie-parser');
var fs = require('fs');
var multer = require('multer');
var upload = multer({ dest: 'public/uploads' });
//////////////////////////////////////////////////////////////////////
///// Express App Setup //////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// The express library is a simple function. When you invoke this
// function it returns an express web application that you build from.
var app = express();
var router = express.Router();
// This will set an "application variable". An application variable is
// a variable that can be retrieved from your app later on. It is
// simply a key/value mapping. In this case, we are mapping the key
// 'port' to a port number. The port number will either be what you
// set for PORT as an environment variable (google this if you do not
// know what an evironment variable is) or port 3000.
app.set('port', process.env.PORT || 3000);
// This does the setup for handlebars. It first creates a new
// handlebars object giving it the default layout. This indicates
// that the default layout is called main.js in the views/layouts
// directory. We then set the app's view engine to 'handlebars' - this
// lets your express app know what the view engine is. We then set an
// app variable 'view engine' to 'handlebars'. This is mostly boiler
// plate so you need not worry about the details.
var view = handlebars.create({ defaultLayout: 'main' });
app.engine('handlebars', view.engine);
app.set('view engine', 'handlebars');
// This does the setup for static file serving. It uses express'
// static middleware to look for files in /public if no other route
// matches. We use the __dirname special variable which indicates the
// directory this server is running in and append it to '/public'.
app.use(express.static(__dirname + '/public'));
// The `testmw` function represents out testing middleware. We use
// this in our views to conditionally include the Mocha and Chai
// testing framework as well as our own tests. Because this is a
// middleware function it expects to receive the request object
// (`req`), response object (`res`), and `next` function as arguments.
// The `next` function is used to continue processing the request
// with subsequent routes.
function testmw(req, res, next) {
// This checks the 'env' application variable to determine if we are
// in "production" mode. An application is in "production" mode if
// it is actually deployed. This can be set by the NODE_ENV
// environment variable. It also checks to see if the request has
// given a `test` querystring parameter, such as
// http://localhost:3000/about?test=1. If the route has that set
// then showTests will be set to a "truthy" value. We can then
// use that in our handlebars views to conditionally include tests.
res.locals.showTests = app.get('env') !== 'production' &&
req.query.test;
// Passes the request to the next route handler.
next();
}
// This adds our testing middleware to the express app.
app.use(testmw);
// Body Parser:
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cookie Parser:
app.use(cookieParser());
// Session Support:
app.use(session({
secret: 'octocat',
// Both of the options below are deprecated, but should be false
// until removed from the library - sometimes, the reality of
// libraries can be rather annoying!
saveUninitialized: false, // does not save uninitialized session.
resave: false // does not save session if not modified.
}));
// Use Flash
app.use(flash());
//////////////////////////////////////////////////////////////////////
///// User Defined Routes ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
var team = require('./lib/team');
var database = require('./lib/database');
var authentication = require('./routes/authentication');
var course = require('./routes/class');
var authenticateLogin = authentication.authenticateLogin;
var authenticateAdmin = authentication.authenticateAdmin;
// Setup sockets.io
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', (socket) => {
function getSocketCollisionId(conv_id, from_class) {
return 2 * conv_id + (from_class ? 1 : 0);
}
socket.on('subscribe', (conv_id, from_class) => {
socket.join(getSocketCollisionId(conv_id, from_class));
});
socket.on('send_private_message', (data) => {
// Display the message to clients in this conversation.
var msg_data = {from_user: data.sender_info.user_id,
fname: data.sender_info.fname,
lname: data.sender_info.lname,
message: data.msg};
io.sockets.in(getSocketCollisionId(data.conv_id, false)).emit('display_private_message', msg_data, data.conv_id);
// Save the message in the database.
database.createNewMessage(data.sender_info.user_id, data.conv_id, data.msg, (err, success) => {
if (err) {
console.log('Error saving message: ' + err);
}
});
});
socket.on('send_class_message', (data) => {
// Display the message to clients in this conversation.
var msg_data = {from_user: data.sender_info.user_id,
fname: data.sender_info.fname,
lname: data.sender_info.lname,
message: data.msg};
io.sockets.in(getSocketCollisionId(data.course_id, true)).emit('display_class_message', msg_data, data.course_id);
// Save the message in the database.
database.createNewClassMessage(data.sender_info.user_id, data.course_id, data.msg, (err, success) => {
if (err) {
console.log('Error saving message: ' + err);
}
});
});
socket.on('disconnect', () => {
// no-op
});
});
// Routes involving user login and registration.
app.use('/auth', require('./routes/authentication').router);
//Routes involving classes
app.use('/course', require('./routes/class').router);
app.post('/reportcontent',function(req, res){
var userId = req.session.user.id;
console.log(req.body);
var formData = req.body;
//If the message hasn't yet been assigned an id we have to look it up in the db using the message and author
//It is faster to not wait for an id to be assigned than to feed the message to the database then display it.
if(formData.reported_message !== '0') {
database.reportContent(userId, formData.explanation, formData.reported_user, formData.reported_message, function (err) {
if (err) {
console.log(err);
res.end();
} else {
res.redirect("/class?cid=" + req.body.course_id);
res.end();
}
});
}
else
{
database.getClassMessageId(formData.reported_user, formData.message_content, function(err, messageIds){
if(err){
console.log(err);
}else{
database.reportContent(userId, formData.explanation, formData.reported_user, messageIds[0].id, function(){
if (err) {
console.log(err);
res.end();
} else {
res.redirect("/class?cid=" + req.body.course_id);
res.end();
}
});
}
});
}
});
app.post('/addevent', function (req, res) {
var calendarDate = req.body.date;
var title = req.body.name;
var description = req.body.description;
var course_id = req.body.course_id;
database.addCalendarEvent(course_id, calendarDate, title, description, (err, results)=> {
if(err){
console.log(err);
}else{
res.redirect("/class?cid=" + course_id);
res.end();
}
});
});
/*
This allows users to uploads a profile picture to their profile
Relies on a library called multer which takes in form data files
*/
app.post('/upload', upload.single('photo'), function (req, res, next) {
var userId = req.session.user.id;
console.log(req.file);
if(typeof req.file === 'undefined'){
//maybe add logic here to do something
}else {
var imageName = req.file.originalname;
if (!imageName) {
console.log("There was an error");
res.redirect("/");
res.end();
} else {
var newPath = req.file.path;
fs.readFile(newPath, function (err, data) {
//Saves the path of the picture in the database so it can be found when a profile is loaded
database.saveProfilePictureUrl('/uploads/' + req.file.filename, userId, function () {
console.log('saved path successfully.');
res.redirect('back');
});
/// write file to uploads folder
fs.writeFile(newPath, data, function (err) {
});
});
}
}
});
/*
Saves biography of the user's profile
*/
app.post('/savebio',(req,res) => {
var userId = req.session.user.id;
var body = req.body;
var bioText = body['val'];
database.saveBioData(userId, bioText, function(err){
console.log(err);
});
});
/*
Saves activities of the user's profile
*/
app.post('/saveact',(req,res) => {
var userId = req.session.user.id;
var body = req.body;
var activities = body['val'];
database.saveActivitesData(userId, activities, function(err){
console.log(err);
});
});
app.post('/savegrad',(req,res) => {
var userId = req.session.user.id;
var body = req.body;
var gradYear = body['val'];
database.saveGraduationYearData(userId, gradYear, function(err){
console.log(err);
});
});
app.post('/savemajor',(req,res) => {
var userId = req.session.user.id;
var body = req.body;
var major = body['val'];
database.saveMajorData(userId, major, function(err){
console.log(err);
});
});
app.post('/deletereportedcontent', (req,res) => {
var body = req.body;
var messageId = body['messageId'];
database.deleteReportedContent(messageId, function(err, result){
res.send({success:true});
});
});
app.post('/allowreportedcontent', (req,res) => {
var body = req.body;
var messageId = body['messageId'];
database.allowReportedContent(messageId, function(err, result){
res.send({success:true});
});
});
/*
This allows users to uploads a profile picture to their profile
Relies on a library called multer which takes in form data files
*/
app.post('/uploadResource', upload.single('classResource'), function (req, res, next) {
var userId = req.session.user.id;
console.log(req.file);
console.log("courseID = " + req.body.course_id);
if(typeof req.file === 'undefined'){
//maybe add logic here to do something
}else {
var fileName = req.body.filetitle;
if (!fileName) {
console.log("There was an error -> No filename");
res.redirect("/");
res.end();
} else {
var newPath = req.file.path;
fs.readFile(newPath, function (err, data) {
database.saveResource('/uploads/' + req.file.filename, fileName, req.body.course_id, userId, function(err) {
if(err){
console.log(err);
}else{
/// write file to uploads folder
fs.writeFile(newPath, data, function (err) {
res.redirect("/class?cid=" + req.body.course_id);
res.end();
console.log('saved path successfully.');
});
}
// res.redirect('back');
});
});
}
}
});
String.prototype.replaceAll = function(str1, str2, ignore) {
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
};
// Home/Splash screen.
app.get('/', (req, res) => {
// Check whether the user's logged in and online
// If so, render the home view
var message = req.flash('home') || '';
if(authentication.isOnline(req.session.user)) {
var userId = req.session.user.id;
var data = {message: message};
database.getUsersCalendar(userId, (err, result) => {
if (err) {
data.message = err;
res.render('home', data);
} else {
data.calendar = result;
database.coursesForUser(userId, (err, result) => {
if (err) {
data.message = err;
res.render('home', data);
} else {
data.courses = result;
for (var i = 0; i < data.calendar.length; i++) {
var cal = data.calendar[i].calendar_date;
var date = new Date(cal);
var dateString = date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear() + " ";
data.calendar[i].calendar_date = dateString;
}
data.calendar = JSON.stringify(data.calendar).replaceAll('\'', '\\\'')
database.getAllUserResources(userId, (err, result) => {
if (err) {
data.message = err;
res.render('home', data);
} else {
data.resources = result;
res.render('home', data);
}
});
}
});
}
});
// Otherwise, user is not logged in
// Render the landing view
} else {
res.render('landing', {
loggedOut: true
});
}
});
app.get('/settings', authenticateLogin, (req, res) => {
res.render('settings');
});
app.get('/profile', authenticateLogin, (req, res) => {
var message = req.flash('profile') || '';
var data = {message: message, edits: true};
var userId = req.session.user.id;
var goToUser = req.query.query;
if(typeof goToUser !== 'undefined'){
userId = goToUser;
data.edits = false;
}
// Start by getting user info.
database.getProfileInfo(userId, (err, info) => {
if (err) {
data.message = err;
} else {
data.user = info;
}
// Now get user courses.
database.coursesForUser(userId, (err, courses) => {
if(typeof goToUser !== 'undefined'){
// Now get your own courses.
database.coursesForUser(req.session.user.id, (err2, ownCourses) => {
if (err || err2) {
data.message = err + ', ' + err2;
} else {
data.courses = courses;
data.own_courses = ownCourses;
}
// Render with data.
res.render('profile', data);
});
} else {
if (err) {
data.message = err;
} else {
data.courses = courses;
data.own_courses = courses;
}
// Render with data.
res.render('profile', data);
}
});
});
});
app.get('/admin', authenticateAdmin, (req, res) => {
var message = req.flash('admin') || '';
var data = {message: message};
database.getReportedContent(function content(err, reportedContent){
if(err){
data.message = err;
}else{
data.reported = reportedContent;
}
res.render('admin', data);
});
});
app.get('/class', authenticateLogin, (req, res) => {
//res.render('class');
var userId = req.session.user.id;
var initial_cid = req.query.cid || -1;
// Get the user's enrolled courses.
database.coursesForUser(userId, (err, courses) => {
var data = {init_cid: initial_cid};
if (err) {
data.error = err;
} else {
data.init_course = courses[initial_cid - 1];
data.courses = courses;
// Fill data with sender info.
data.sender = {id: userId, fname: req.session.user.fname, lname: req.session.user.lname};
}
res.render('class', data);
});
});
app.get('/class/messages/fetch', authenticateLogin, (req, res) => {
var course_id = req.query.course_id;
database.getClassMessages(course_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.messages = results;
}
res.send(data);
});
});
app.get('/class/events/fetch', authenticateLogin, (req, res) => {
var course_id = req.query.course_id;
database.getCalendarsForCourse(course_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.events = results;
}
res.send(data);
});
});
app.get('/class/resources/fetch', authenticateLogin, (req, res) => {
var course_id = req.query.course_id;
database.getClassResources(course_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.resources = results;
}
res.send(data);
});
});
app.get('/class/members/fetch', authenticateLogin, (req, res) => {
var user_id = req.session.user.id;
var course_id = req.query.course_id;
database.getUsersForCourse(user_id, course_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.members = results;
}
res.send(data);
});
});
app.get('/messages', authenticateLogin, (req, res) => {
var message = req.flash('messages') || '';
var data = {message: message, sender: req.session.user};
var userId = req.session.user.id;
// First get all conversations that involve us.
database.getConversation(userId, (err, results) => {
if (err) {
data.message = err;
console.log(err);
} else {
data.conversations = results;
}
// Get the user's enrolled courses.
database.coursesForUser(userId, (err, courses) => {
if (err) {
data.error = err;
console.log(err);
} else {
data.courses = courses;
}
res.render('messages', data);
});
});
});
app.get('/messages/fetch', authenticateLogin, (req, res) => {
var conv_id = req.query.conv_id;
database.getConversationMessages(conv_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.messages = results;
}
res.send(data);
});
});
app.get('/friends/fetch', authenticateLogin, (req, res) => {
var user_id = req.session.user.id;
database.getPossibleFriends(user_id, (err, results) => {
var data = {};
if (err) {
data.error = err;
} else {
data.friends = results;
}
res.send(data);
});
});
app.get('/messages/new_conversation', authenticateLogin, (req, res) => {
var friend_id = req.query.friend_id;
var user_id = req.session.user.id;
var data = {};
// First make sure this conversation doesn't already exist.
database.getConversationForBothUsers(user_id, friend_id, (err, results) => {
if (err) {
data.error = err;
res.send(data);
} else if (results.length > 0) {
data.error = "You're already talking to this friend!";
res.send(data);
}
// Now create a new conversation and return it to the view.
else {
database.createPrivateConversation(user_id, friend_id, (err, results) => {
if (err) {
data.error = err;
} else if (results.length === 0) {
data.error = "Something went wrong. Please try again later";
} else {
data.conversation = results[0];
}
res.send(data);
});
}
});
});
app.get('/team', (req, res) => {
var result;
if (req.query.user) {
// Retrieve the selected user from the query.
result = team.one(req.query.user);
} else {
// No query string, show the whole team.
result = team.all();
}
if (!result.success) {
notFound404(req, res);
} else {
res.render('team', {
members: result.data,
multiple: result.multiple,
pageTestScript: '/qa/tests-team.js'
});
}
});
app.get('/about', (req, res) => {
res.render('about')
});
//////////////////////////////////////////////////////////////////////
///// Error Middleware ///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// A middleware function that will be invoked if no other route path
// has been matched. HTTP 404 indicates that the resource was not
// found. We set the HTTP status code in the response object to 404.
// We then render our views/404.handlebars view back to the client.
function notFound404(req, res) {
res.status(404);
res.render('404');
}
// A middleware function that will be invoked if there is an internal
// server error (HTTP 500). An internal server error indicates that
// a serious problem occurred in the server. When there is a serious
// problem in the server an additional `err` parameter is given. In
// our implementation here we print the stack trace of the error, set
// the response status code to 500, and render our
// views/500.handlebars view back to the client.
function internalServerError500(err, req, res, next) {
console.error(err.stack);
res.status(500);
res.render('500');
}
// This adds the two middleware functions as the last two middleware
// functions. Because they are at the end they will only be invoked if
// no other route defined above does not match.
app.use(notFound404);
app.use(internalServerError500);
//////////////////////////////////////////////////////////////////////
//// Application Startup /////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Starts the express application up on the port specified by the
// application variable 'port' (which was set above). The second
// parameter is a function that gets invoked after the application is
// up and running.
server.listen(app.get('port'), () => {
console.log('Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate');
});