-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (133 loc) · 4.16 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
// init project
const express = require('express');
const ScheduleGenerator = require('./lib/run')
const request = require('request')
const url = require('url')
const _ = require('lodash')
const sslRedirect = require('heroku-ssl-redirect')
var app = express();
app.use(sslRedirect());
app.use(express.static('public'));
const ALLOWED_SCHOOLS = ['uottawa'];
let gAuths = {} // store auth tokens for access in router and in websocket handler
// routes
app.get("/", function (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
app.get("/getToken/:school", (req, res) => {
var authURL = `${ScheduleGenerator.getAuthURL()}&state=${req.params.school}`;
res.redirect(authURL);
});
app.get("/run", (req, res) => {
var code = req.query.code;
var school = req.query.state;
if (!code || code === 'access_denied' || !school || !ALLOWED_SCHOOLS.includes(school))
res.redirect('/');
else {
// validate code
ScheduleGenerator.verifyAuth(code, (gAuth) => {
if (gAuth) {
gAuths[code] = gAuth
res.sendFile(`${__dirname}/views/${school}.html`);
} else {
res.redirect('/');
}
})
}
})
// listen for requests
var listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port);
});
listener.timeout = 300000; // timeout of 5mins
/* Websockets handling */
var io = require('socket.io')(listener);
io.on('connection', (socket) => {
console.log('a user connected')
var jar, chosenSemesters, courses = {}, insertedEventIds = [],
chosenColours, code = url.parse(socket.request.headers.referer, true).query.code;
// clean closure memory
socket.on('disconnect', () => {
console.log('a user disconnected');
delete gAuths[code];
ScheduleGenerator.logoutUser(jar, () => {
jar = null, chosenSemesters = null, courses = null, chosenColours = null, code = null, insertedEventIds = null;
});
})
socket.on('verify credentials', (data, clientCallback) => {
jar = request.jar();
ScheduleGenerator.verifyCredentials(data, jar, clientCallback, () => {
// this callback will only be called if credentials are valid
// this fetches the semesters to choose from
ScheduleGenerator.grabSemesters(jar, (semesters) => {
socket.emit('grab semesters', semesters);
})
})
});
socket.on('choose semesters', (data, clientCallback) => {
var ok = _.isArray(data) ? true : false;
if (ok)
chosenSemesters = data;
clientCallback(ok);
// TODO: grab schedules & parse
// @param courses is modified directly by `grabSchedules`
ScheduleGenerator.grabSchedules(jar, courses, chosenSemesters, (err) => {
var coursesInfo = getCoursesInfo(courses);
socket.emit('grab schedules', {
err,
coursesInfo
});
});
});
socket.on('choose colours', (data, clientCallback) => {
var ok = _.isPlainObject(data) ? true : false;
if (ok)
chosenColours = data;
clientCallback(ok);
});
socket.on('generate schedule', () => {
const gAuth = gAuths[code]
if (gAuth) {
ScheduleGenerator.insertCourses(gAuth, courses, chosenColours, (ok, course, insertedEventId) => {
socket.emit('inserted course', {
ok,
course
});
if (insertedEventId) {
insertedEventIds.push(insertedEventId);
}
}, (ok) => { // this callback is called when all courses are inserted (or there was an error)
var status = ok === null ? true : false;
socket.emit('inserted all courses', status);
console.log(insertedEventIds)
})
} else {
socket.emit('inserted all courses', false)
}
});
socket.on('delete schedule', (_, clientCallback) => {
const gAuth = gAuths[code];
if (!gAuth) {
return clientCallback(new Error('Invalid gAuth'));
}
ScheduleGenerator.deleteCourses(gAuth, insertedEventIds, clientCallback);
});
});
function getCoursesInfo(semesters) {
var coursesInfo = {};
_.forEach(semesters, (courses, semester) => {
coursesInfo[semester] = {};
_.forEach(courses, (data, course) => {
coursesInfo[semester][course] = data.info;
})
})
return coursesInfo;
}
setInterval(() => { // clean gAuths every hour
let now = Date.now()
Object.keys(gAuths).forEach(key => {
if (gAuths[key].credentials.expiry_date <= now) {
delete gAuths[key]
}
})
}, 1000 * 3600)