-
Notifications
You must be signed in to change notification settings - Fork 2
/
requestHandlers.js
304 lines (278 loc) · 8.02 KB
/
requestHandlers.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
var persistency = require('./persistency'),
date_utils = require('date-utils'),
everyauth = require('everyauth'),
config = require('./config'),
util = require('util');
var realtime;
function setRealtimeEngine(engine) {
realtime = engine;
}
function isUserAllowed(req, res, success) {
if (!req.loggedIn) {
res.redirect('/login');
} else {
persistency.isUserWhitelisted(req.session.auth.google.user.id, function(whitelisted) {
if (whitelisted) success();
else res.redirect('/access');
});
}
}
function login(req, res) {
res.render('login');
}
function index(req, res) {
isUserAllowed(req, res, function() {
res.render('index', {
calendar_web_link: config.app.calendar
});
});
}
function history(req, res) {
isUserAllowed(req, res, function() {
res.render('history', {endpoint: '/getMessages/'});
});
}
function memegeist(req, res) {
isUserAllowed(req, res, function() {
res.render('memegeist', {endpoint: '/getMemes/'});
});
}
function topMessages(req, res) {
isUserAllowed(req, res, function() {
res.render('history', {endpoint: '/getTopMessages/'});
});
}
function getMessages(req, res) {
timestamp = parseInt(req.params.timestamp);
if (isNaN(timestamp)) {
res.render('404');
} else {
persistency.getMessagesChunk(timestamp, 100, function(err, messages) {
persistency.mergeMessagesWithUsers(messages, null, function(messages) {
res.contentType('json');
res.send(messages);
});
});
}
}
function getTopMessages(req, res) {
var beginTimestamp = new Date()
beginTimestamp.setTime(parseInt(req.params.timestamp));
var endTimestamp = new Date()
if (isNaN(beginTimestamp)) {
res.render('404');
} else {
persistency.getMessages(beginTimestamp, endTimestamp, function(err, messages) {
persistency.mergeMessagesWithUsers(messages, null, function(messages) {
//Sorts descendingly by the number of upvotes
messages.sort(function(firstMessage, secondMessage) {
firstVoteCount = 0
if (firstMessage.uptokes !== undefined)
firstVoteCount = firstMessage.uptokes - firstMessage.downtokes
secondVoteCount = 0
if (secondMessage.uptokes !== undefined)
secondVoteCount = secondMessage.uptokes - secondMessage.downtokes
return secondVoteCount - firstVoteCount
});
res.contentType('json');
res.send(messages);
});
});
}
}
function getMemes(req, res) {
timestamp = parseInt(req.params.timestamp);
if (isNaN(timestamp)) {
res.render('404');
} else {
persistency.getMemesChunk(timestamp, 100, function(err, messages) {
persistency.mergeMessagesWithUsers(messages, null, function(messages) {
res.contentType('json');
res.send(messages);
});
});
}
}
function loadMemes(req, res) {
isUserAllowed(req, res, function() {
persistency.getMemes(function(error, memes) {
if (error) {
res.statusCode = 500;
res.end('Error loading memes');
return;
}
res.contentType('json');
res.send(memes);
});
});
}
function beta(req, res) {
isUserAllowed(req, res, function() {
res.render('beta', {
calendar_web_link: config.app.calendar
});
});
}
function whitelist(req, res) {
isUserAllowed(req, res, function() {
persistency.getUsers(function(err, users) {
if (err) handleError(err);
else {
pending = [];
accepted = [];
banned = [];
userMap = {};
// exclude system user
users = users.filter(function(user) {
return user.id !== 'ServusTalk';
});
for (i in users) {
if (users[i].acceptedBy && users[i].bannedBy) {
// TODO this will happen for first person to login after everyone has been blacklisted
console.error('User ' + users[i].email + ' cannot be both banned and accepted at the same time');
}
if (users[i].acceptedBy) accepted.push(users[i]);
else if (users[i].bannedBy) banned.push(users[i]);
else pending.push(users[i]);
userMap[users[i].id] = users[i];
}
res.render('whitelist', {
accepted: accepted,
pending: pending,
banned: banned,
userMap: userMap
});
}
});
});
}
function handleError(err) {
console.warn('Error getting messages: ' + err, err.stack);
res.render('404');
}
function acceptUser(req, res) {
isUserAllowed(req, res, function() {
userId = req.params.userid;
persistency.getUser(userId, function(err, user) {
if (err) handleError(err);
else {
if (!user.acceptedBy) {
delete user.bannedBy;
user.acceptedBy = req.session.auth.google.user.id;
persistency.updateUser(user);
if (realtime) {
realtime.pushSystemMessage('99CC32', user.name + ' has been whitelisted by ' + req.session.auth.google.user.name);
}
}
res.redirect('/whitelist');
}
});
});
}
function banUser(req, res) {
isUserAllowed(req, res, function() {
userId = req.params.userid;
persistency.getUser(userId, function(err, user) {
if (err) handleError(err);
else {
if (!user.bannedBy) {
delete user.acceptedBy;
user.bannedBy = req.session.auth.google.user.id;
persistency.updateUser(user);
if (realtime) {
realtime.disconnectUser(userId);
realtime.pushSystemMessage('FF6A6A', user.name + ' has been blacklisted by ' + req.session.auth.google.user.name);
}
}
res.redirect('/whitelist');
}
});
});
}
function access(req, res) {
if (!req.loggedIn) {
res.redirect('/login');
} else {
persistency.isUserWhitelisted(req.session.auth.google.user.id, function(whitelisted) {
if (whitelisted) {
res.redirect('/');
} else {
res.render('access');
}
});
}
}
function pay(req, res) {
isUserAllowed(req, res, function() {
res.render('pay');
});
}
function vote(req, res) {
user_id = req.session.auth.google.user.id;
if (req.body.vote === undefined || req.body.message_ts === undefined ||
!(req.body.vote <= 1 && req.body.vote >= -1)) {
res.statusCode = 400;
res.end('Invalid request');
return;
}
message_ts = parseInt(req.body.message_ts);
vote = parseInt(req.body.vote);
persistency.saveVote(message_ts, user_id, vote, function(message) {
realtime.broadcast('vote', message);
});
res.end('uptokes!');
}
function map(req, res) {
isUserAllowed(req, res, function() {
res.render('map');
});
}
function distinctCheckins(req, res) {
isUserAllowed(req, res, function() {
persistency.getDistinctCheckins(function(error, checkins) {
if (error) {
res.statusCode = 500;
res.end('Error retrieving checkins');
return;
}
IGNORE_LOCATIONS = {'null': true, '127.0.0.1': true};
locations = {};
for(i in checkins) {
location = checkins[i].text.location;
lat = checkins[i].text.lat;
lng = checkins[i].text.lng;
if (!IGNORE_LOCATIONS[location]) {
if (locations[location]) {
locations[location]['count']++;
} else {
locations[location] = {};
locations[location]['count'] = 1;
locations[location]['lat'] = lat;
locations[location]['lng'] = lng;
}
}
}
res.contentType('json');
res.send(locations);
});
});
}
exports.index = index
exports.login = login
exports.access = access
exports.history = history
exports.memegeist = memegeist
exports.topMessages = topMessages
exports.beta = beta
exports.whitelist = whitelist
exports.acceptUser = acceptUser
exports.banUser = banUser
exports.setRealtimeEngine = setRealtimeEngine
exports.pay = pay
exports.vote = vote
exports.getMessages = getMessages
exports.getTopMessages = getTopMessages
exports.getMemes = getMemes
exports.loadMemes = loadMemes
exports.map = map
exports.distinctCheckins = distinctCheckins