-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsettings.js
321 lines (308 loc) · 9.96 KB
/
settings.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
var Sequelize = require("sequelize");
var Promise = require("bluebird");
var moment = require("moment-timezone");
var validator = require('validator');
var model = require("../model.js");
var p = require("../permissions.js");
var config = require("../config.json");
var options = require("./options.js");
var home = require("./home.js");
exports.get = function(req, res) {
if (!p.is_logged_in(req)) {
res.redirect("/login");
return;
}
if (!p.is_ta(req) && !p.is_owner(req)) {
res.sendStatus(403);
return;
}
// If we need to display a toast on this request, it'll be in a cookie.
// Store the message and clear the cookie so the user only sees it once.
var toast = null;
if (req.cookies.toast) {
toast = req.cookies.toast;
res.clearCookie("toast");
}
var current_semester = null;
options.current_semester().then(function(semester) {
current_semester = semester;
return Promise.props({
webhook_url: options.slack_webhook(),
ask_question_guide_link: options.ask_question_guide_link(),
cooldown_time: options.cooldown_time(),
notif_time_threshold: options.notif_time_threshold(),
notif_time_interval: options.notif_time_interval(),
topics: function() {
return model.Topic.findAll({
where: {
semester: current_semester
},
order: [['due_date', 'ASC']]
})
}(),
tas: function() {
return model.TA.findAll({
where: {
semester: current_semester
},
order: [['email', 'ASC']]
});
}()
});
}).then(function(results) {
results.tas.forEach(function(ta) {
ta.is_self = (req.session.TA && ta.id == req.session.TA.id);
});
return res.render("settings", {
title: config.title,
session: req.session,
toast: toast,
video_chat_url: (req.session.TA ? req.session.TA.video_chat_url : undefined),
current_semester: current_semester,
slack_webhook: results.webhook_url,
ask_question_guide_link: results.ask_question_guide_link,
cooldown_time: results.cooldown_time,
notif_time_threshold: results.notif_time_threshold,
notif_time_interval: results.notif_time_interval,
topics: results.topics,
tas: results.tas,
owner_email: config.owner_email,
is_admin: p.is_admin(req),
is_ta: p.is_ta(req)
});
});
};
function respond(req, res, message, data) {
if (req.query.json) {
res.json({message: message, data: data});
} else {
if (message) {
res.cookie("toast", message);
}
res.redirect("/settings");
}
}
function post_add_topic(req, res) {
var name = req.body.topic_name;
var out_date = moment.tz(new Date(req.body.out_date), config.timezone).toDate();
var due_date = moment.tz(new Date(req.body.due_date), config.timezone).toDate();
if (!out_date.getTime() || !due_date.getTime() || !name) {
respond(req, res, "Error: One of the fields was invalid.");
return;
}
options.current_semester().then(function(semester) {
return model.Topic.create({
name: name,
out_date: out_date,
due_date: due_date,
semester: semester
})
}).then(function() {
home.clear_topics_cache();
respond(req, res, "Topic added");
});
}
function post_delete_topic(req, res) {
var id = req.body.id;
model.Topic.findByPk(id).then(function(instance) {
if (!instance) {
throw new Error("There is no topic with that ID");
}
return instance.destroy();
}).then(function() {
home.clear_topics_cache();
home.clear_entries_cache();
respond(req, res, "Topic deleted");
}).catch(function(error) {
respond(req, res, "Error: " + error);
});
}
function post_update_topic(req, res) {
var id = req.body.id;
var name;
var out_date;
var due_date;
model.Topic.findByPk(id).then(function(instance) {
if (!instance) {
throw new Error("There is no topic with that ID");
}
name = req.body.topic_name;
out_date = moment.tz(new Date(req.body.out_date), config.timezone).toDate();
due_date = moment.tz(new Date(req.body.due_date), config.timezone).toDate();
if (!out_date.getTime() || !due_date.getTime() || !name) {
throw new Error("One of the fields was invalid.");
}
return instance.update({
name: name,
out_date: out_date,
due_date: due_date
});
}).then(function() {
home.clear_topics_cache();
home.clear_entries_cache();
respond(req, res, "Topic updated");
}).catch(function(error) {
respond(req, res, "Error: " + error);
});
}
function post_add_ta(req, res) {
if (!req.body.name || !req.body.email) {
respond(req, res, "Error: All fields are required.");
return;
}
if (!validator.isEmail(req.body.email)) {
respond(req, res, "Error: The provided email address is not valid.");
return;
}
var added;
options.current_semester().then(function(semester) {
return model.TA.findOrCreate({
where: {
email: req.body.email,
semester: semester,
},
defaults: {
full_name: req.body.name,
time_helped: 0,
num_helped: 0,
time_today: 0,
num_today: 0,
admin: req.body.admin == "true" || req.body.email == config.owner_email
}
});
}).then(function(result) {
var instance = result[0];
added = result[1];
if (instance.email == req.session.email && !req.session.TA) {
return req.session.update({
ta_id: instance.id
});
} else {
return Promise.resolve(req.session);
}
}).then(function() {
if (added) {
respond(req, res, "TA added");
} else {
respond(req, res, "Error: TA already exists.")
}
});
}
function post_delete_ta(req, res) {
var id = req.body.id;
if (req.session.TA && id == req.session.TA.id) {
respond(req, res, "Error: Cannot delete yourself");
return;
}
model.TA.findByPk(id).then(function(instance) {
if (!instance) {
throw new Error("There is no topic with that ID");
}
if (instance.helping_id) {
throw new Error("Cannot remove a TA while they are helping a student");
}
return instance.destroy();
}).then(function() {
respond(req, res, "TA deleted");
}).catch(function(error) {
respond(req, res, "Error: " + error);
});
}
function post_update_ta(req, res) {
var id = req.body.id;
var name;
var email;
var instance;
var updated_fields;
model.TA.findByPk(id).then(function(inst) {
if (!inst) {
throw new Error("There is no TA with that ID");
}
instance = inst;
name = req.body.name;
email = req.body.email;
admin = req.body.admin == "true";
if (!name) {
throw new Error("TA name was missing or invalid.");
}
updated_fields = {
full_name: name,
admin: admin
}
if (req.session.TA && id == req.session.TA.id) {
if (!admin) {
throw new Error("You cannot demote yourself to a regular TA.")
}
} else {
if (!email || !validator.isEmail(email)) {
throw new Error("TA email was missing or invalid.");
}
updated_fields['email'] = email;
updated_fields['admin'] = admin || email == config.owner_email;
}
return options.current_semester();
}).then(function(semester) {
return model.TA.findOne({
where: {
semester: semester,
email: email
}
});
}).then(function(result) {
if (result && result.id != instance.id) {
throw new Error("A TA with that email address already exists.");
}
return instance.update(updated_fields);
}).then(function() {
respond(req, res, "TA updated");
}).catch(function(error) {
respond(req, res, error.toString());
});
}
function post_update_url(req, res) {
req.session.TA.update({
video_chat_url: req.body.video_chat_url
}).then(function(result) {
home.clear_entries_cache();
respond(req, res, "Video Chat URL updated");
});
}
exports.post = function(req, res) {
if (!p.is_logged_in(req)) {
res.redirect("/login");
return;
}
var action = req.body.action;
var handled = false;
if (p.is_ta(req)) {
if (action == "UPDATEURL") {
post_update_url(req, res);
handled = true;
}
}
if (p.is_admin(req)) {
if (action == "ADDTOPIC") {
post_add_topic(req, res);
handled = true;
} else if (action == "DELETETOPIC") {
post_delete_topic(req, res);
handled = true;
} else if (action == "UPDATETOPIC") {
post_update_topic(req, res);
handled = true;
} else if (action == "ADDTA") {
post_add_ta(req, res);
handled = true;
} else if (action == "UPDATETA") {
post_update_ta(req, res);
handled = true;
} else if (action == "DELETETA") {
post_delete_ta(req, res);
handled = true;
}
}
if (!handled) {
res.sendStatus(403);
return;
}
};