forked from fuzzie360/nus-stalker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stalker.js
487 lines (404 loc) · 13.5 KB
/
stalker.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
var config = require('./config');
var util = require('util');
var http = require('http');
var _ = require('underscore');
var etagify = require('etagify');
var express = require('express');
var cachify = require('connect-cachify');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var ejs = require('ejs');
var passport = require('passport');
var OpenIDStrategy = require('passport-openid').Strategy;
var mysql = require('mysql');
var Sequelize = require('sequelize');
var MySQLSessionStore = require('connect-mysql-session')(express);
var sequelize = new Sequelize(config.db.database, config.db.user, config.db.pass, config.db.opt);
var Models = sequelize.import(__dirname + '/models.js');
var Student = Models.Student;
var Module = Models.Module;
var ModuleDepartment = Models.ModuleDepartment;
var Career = Models.Career;
var Faculty = Models.Faculty;
var Course = Models.Course;
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
passport.use(new OpenIDStrategy({
providerURL: 'https://openid.nus.edu.sg/',
returnURL: 'http://'+config.server.host+'/auth/openid/return',
realm: 'http://'+config.server.host+'/',
stateless: true,
profile: true
},
function(identifier, profile, done) {
profile.id = identifier.split('/').pop();
done(null, profile);
}
));
var assets = require('./assets');
// set up express
var app = express();
var server = http.createServer(app);
app.use(cachify.setup(assets, {
root: __dirname + "/public",
production: false
}));
app.engine('html', ejs.renderFile);
app.use(express.static(__dirname + '/public'));
app.use(etagify());
app.use(cookieParser());
app.use(bodyParser());
app.use(express.session({
store: new MySQLSessionStore(config.db.database,
config.db.user,
config.db.pass,
config.session.opt),
secret: config.session.secret
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
// serve frontend
server.listen(config.server.port, config.server.ip);
process.stdout.write('INFO:\tServer listening at port ' + config.server.port + '\n');
app.get('/', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
var randoms = [
'ALL YOUR DATABASE ARE BELONG TO US',
'Nothing is beyond our reach',
'Protecting NUS from terrorists since 2013',
'Hide your kids, hide your wife',
'Wow. So stalk. Much creep.',
'You come here often, huh',
'Yep, this will definately make her love you',
'Now with 50% more stalk, per stalk',
'The Google of NUS',
'Has anyone really been far even as decided to use even go want to do look more like?',
'What does the fox say?',
'What\'s the meaning of Stongehenge?',
'Drive a Civic. It\'s a car you can trust'
];
res.render('main.ejs', {
messages: req.flash('error'),
random: _.sample(randoms)
});
});
function prependPlusToQuery(q) {
return _.map(q.match(/\w+|"[^"]+"/g), function(x) {
if (x.length > 0 && x[0] == '-') return;
return '+'+x;
}).join(' ');
}
app.get('/search', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
if (!req.query.q) {
req.flash('error', 'You must give a search term to query');
res.redirect('/');
return;
}
req.query.q = req.query.q.replace(/^\s+|\s+$/g, '');
if (req.query.q.length < 4) {
req.flash('error', 'Query is too short (must be at least 4 characters long)');
res.redirect('/');
return;
}
if (req.query.q.toUpperCase().indexOf('DROP TABLE') != -1) {
res.redirect('/bobby.txt');
return;
}
Student.search(req.query.q).success(function(students) {
Module.search(req.query.q).success(function(modules) {
if (students.length === 1 && modules.length === 0) {
res.redirect('/student/' + students[0].matric );
return;
} else if (modules.length === 1 && students.length === 0) {
res.redirect('/module/' + modules[0].code );
return;
}
res.render('search.ejs', {
search: req.query.q,
students: students,
modules: modules,
truncated: students.length == 100 || modules.length == 100
});
});
});
});
app.get('/student/suggest', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
Student.suggest(req.query.q).success(function(names) {
var data = [];
for (var i=0; i<names.length; i++) {
var orig = names[i].displayName;
var matric = names[i].matric;
var name = names[i].displayName || '';
var first = names[i].firstName || '';
var last = names[i].lastName || '';
name = name.replace(/[^\w\s]/gi,'');
first = first.replace(/[^\w\s]/gi,'');
last = last.replace(/[^\w\s]/gi,'');
var tokens = _.without(_.union(name.split(' '), first.split(' '), last.split(' ')), '');
data.push({
value: orig,
tokens: tokens,
id: matric
});
}
res.json(data);
});
});
app.get('/student/:matric', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
Student.find({
where: { matric: req.params.matric },
include: [Career, Faculty, Course, {
model: Module,
include: [ModuleDepartment]
}],
joinTableAttributes: ['year', 'semester']
}).success(function(student) {
if (!student) {
res.send(404);
return;
}
if (student.courses.length > 0) {
var coursesGrouped = _.groupBy(student.courses, function(c) {
return c.studentCourse.year * 100 + c.studentCourse.semester;
});
var maxKey = _.max(_.keys(coursesGrouped));
student.courses = coursesGrouped[maxKey];
}
student.similarStudents().success(function(similarModules) {
res.render('student.ejs', {
student: student,
similarModules: similarModules
});
});
});
});
app.get('/student/:matric/graph.json', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
Student.find({
where: { matric: req.params.matric },
include: [Module]
}).success(function(student) {
if (!student) {
res.send(404);
return;
}
var groupCount = 1;
var studentIndex = {};
var graph = {
nodes: [
{
name: student.displayName,
group: 0
}
],
links: []
};
studentIndex[student.matric] = 0;
student.similarStudents(20).success(function(students) {
function iter(students) {
if (_.isEmpty(students)) {
res.json(graph);
return;
}
var student = _.first(students);
if (studentIndex[student.matric] === undefined) {
graph.nodes.push({
name: student.displayName,
group: groupCount++
});
studentIndex[student.matric] = graph.nodes.length-1;
}
graph.links.push({
source: 0,
target: studentIndex[student.matric],
value: 1+1/student.common
});
Student.similarStudents(student.id, 20).success(function(students2) {
for (var i=0; i<students2.length; i++) {
var student2 = students2[i];
if (studentIndex[student.matric] === 0) {
continue;
}
if (studentIndex[student2.matric] === undefined) {
graph.nodes.push({
name: student2.displayName,
group: graph.nodes[studentIndex[student.matric]].group
});
studentIndex[student2.matric] = graph.nodes.length-1;
}
graph.links.push({
source: studentIndex[student.matric],
target: studentIndex[student2.matric],
value: 1+1/student2.common
});
}
iter(_.rest(students));
});
}
iter(students);
});
/*
var moduleIndex = {};
var studentIndex = {};
var graph = {
nodes: [
{
name: student.displayName,
group: 0,
}
],
links: []
};
studentIndex[student.matric] = 0;
for (var i=0; i<student.modules.length; i++) {
var module = student.modules[i];
graph.nodes.push({
name: module.code,
group: module.ModuleDepartmentId
});
moduleIndex[module.code] = graph.nodes.length-1;
graph.links.push({
source: 0,
target: moduleIndex[module.code],
value: 2
});
}
student.similarStudents(25).success(function(students) {
function iter(students) {
if (_.isEmpty(students)) {
res.json(graph);
return;
}
var student = _.first(students);
Student.find({
where: { matric: student.matric },
include: [Module]
}).success(function(student) {
graph.nodes.push({
name: student.displayName,
group: 0
});
studentIndex[student.matric] = graph.nodes.length-1;
for (var i=0; i<student.modules.length; i++) {
var module = student.modules[i];
if (moduleIndex[module.code] !== undefined) {
graph.links.push({
source: studentIndex[student.matric],
target: moduleIndex[module.code]
});
continue;
}
graph.nodes.push({
name: module.code,
group: module.ModuleDepartmentId
});
moduleIndex[module.code] = graph.nodes.length-1;
graph.links.push({
source: studentIndex[student.matric],
target: moduleIndex[module.code],
value: 1
});
}
iter(_.rest(students));
});
}
iter(students);
});*/
});
});
app.get('/module/suggest', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
Module.suggest(req.query.q).success(function(modules) {
var data = [];
for (var i=0; i<modules.length; i++) {
var name = modules[i].name || '';
var code = modules[i].code;
var tokens = _.union(name.split(' '), code);
data.push({
value: code + ' ' + name,
tokens: tokens,
id: code
});
}
res.json(data);
});
});
app.get('/module/:code', function(req, res) {
if (config.auth && !req.isAuthenticated()) {
res.redirect('/login');
return;
}
res.etagify();
Module.find({
where: { code: req.params.code },
include: [ModuleDepartment, {
model: Student,
include: [Faculty]
}]
}).success(function(module) {
if (!module) {
res.send(404);
return;
}
module.alsoTook().success(function(alsoTook) {
res.render('module.ejs', {
module: module,
alsoTook: alsoTook
});
});
});
});
app.get('/login', function(req, res) {
if (!config.auth || req.isAuthenticated()) {
res.redirect('/');
return;
}
res.etagify();
res.render('login.ejs');
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.post('/auth/openid', passport.authenticate('openid'));
app.get('/auth/openid/return', passport.authenticate('openid', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.get('/forbidden', function (req, res) {
res.render('forbidden.ejs');
});