forked from fmoga/servustalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHandlers.js
236 lines (213 loc) · 6.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
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');
});
}
function getHistory(req, res) {
isUserAllowed(req, res, function() {
year = parseInt(req.params.year);
month = parseInt(req.params.month);
day = parseInt(req.params.day);
if (isNaN(day) || isNaN(month) || isNaN(year)) {
res.render('404');
return;
}
if (!Date.validateDay(day, year, month)) {
res.render('404');
return;
}
lower_date = new Date(year, month, day, 0, 0, 0, 0);
upper_date = lower_date.clone();
upper_date.addDays(1);
data = {};
persistency.getMessages(lower_date, upper_date, function(err, messages) {
if (err) {
console.warn('Error getting messages: ' + err, err.stack);
res.render('404');
} else {
persistency.getUsers(function(err, users) {
if (err) {
console.warn('Error getting users: ' + err, err.stack);
res.render('404');
} else {
data.messages = messages;
data.users = users;
res.contentType('json');
res.send(data);
}
});
}
});
});
}
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 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);
});
});
}
}
exports.index = index
exports.login = login
exports.access = access
exports.history = history
exports.getHistory = getHistory
exports.beta = beta
exports.whitelist = whitelist
exports.acceptUser = acceptUser
exports.banUser = banUser
exports.setRealtimeEngine = setRealtimeEngine
exports.pay = pay
exports.vote = vote
exports.getMessages = getMessages