-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
429 lines (347 loc) · 12 KB
/
app.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
"use strict";
var crypto = require("crypto");
var express = require('express');
var passport = require("passport");
var OAuth2Strategy= require('passport-oauth2').Strategy;
var mongoose = require('mongoose');
var User = require("./models/users.js");
var RegistrationToken = require("./models/registration_tokens.js");
var app = express();
var todoist = require("./todoist.js");
var _ = require("lodash");
var Q = require("q");
var URL = require("url");
var https = require("https");
var baseUrl = process.env.BASE_URL;
// Connect to database
mongoose.connect(process.env.MONGO_URI);
mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
});
mongoose.set('debug', true);
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('todoist', new OAuth2Strategy({
authorizationURL: 'https://todoist.com/oauth/authorize',
tokenURL: 'https://todoist.com/oauth/access_token',
clientID: process.env.CLIENT_ID_TODOIST,
clientSecret: process.env.CLIENT_SECRET_TODOIST,
callbackURL: baseUrl + 'auth/todoist/callback',
scope: 'task:add,data:read_write',
state: 'slacklemore',
passReqToCallback: true},
function(request, accessToken, refreshToken, profile, done) {
var slack_id = request.session.slack_id;
User.findOneAndUpdate({slack_id: slack_id}, {todoist_oauth_token: accessToken}, { upsert: true, 'new': true}, function(err, user) {
if (err) { console.error('Finding / Updating User Todoist error: ' + err); }
return done(err, user);
});
}
));
passport.use('slack', new OAuth2Strategy({
authorizationURL: 'https://slack.com/oauth/authorize',
tokenURL: 'https://slack.com/api/oauth.access',
clientID: process.env.CLIENT_ID_SLACK,
clientSecret: process.env.CLIENT_SECRET_SLACK,
callbackURL: 'https://nko2015-hicapacity-54610.onmodulus.net/auth/slack/callback',
scope: 'commands',
state: 'slacklemore'},
function(accessToken, refreshToken, profile, done) {
console.log("HERE");
console.log(profile);
User.findOneAndUpdate({slack_id: profile.id}, {slack_oauth_token: accessToken}, { upsert: true, 'new': true}, function(err, user) {
if (err) { console.error('Finding / Updating User Slack error: ' + err); }
return done(err, user);
});
}
));
app.configure(function() {
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
function respond(res, text) {
var isJSON = (typeof text !== 'string');
var content_type = isJSON ? 'application/json' : 'text/html';
text = isJSON ? JSON.stringify(text) : text;
console.log("Responding 200 OK with: " + text);
res.writeHead(200, "OK", {'Content-Type': content_type});
res.end(text);
}
function postJSON(url, json){
var body = JSON.stringify(json);
var options = URL.parse(url);
options.method = "POST";
options.headers = {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
};
console.log(`postJSON(url=${url}, json=${body}, options=${JSON.stringify(options)})`);
var req = https.request(options, function(res, err){
console.log("postJSON: status="+res.statusCode+", url="+url);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(body);
req.end(function(err, res) {
console.log("postJSON end url="+url);
});
}
function handle_register(req, res, user) {
console.log(">>>> In register handler");
var slack_id = req.body.user_id;
var registration_hash = crypto.randomBytes(20).toString('hex');
var now = new Date();
var days_valid = 1;
var valid_until = now.setTime(now.getTime() + days_valid * 86400000);
RegistrationToken.findOneAndUpdate({slack_id: slack_id}, {registration_hash: registration_hash, valid_until: valid_until}, { upsert: true, 'new': true}, function(err, token) {
if (err) { console.error('Finding / Updating Registration Token error: ' + err); }
var link = baseUrl + 'todoist/' + token.registration_hash;
var text = ["Welcome to Slash Todoist! It looks like we've not seen you before.",
`<${link}|Please click here to link to your todoist.com account>`].join("\n");
respond(res, text);
});
}
function handle_unregister(req, res, user) {
console.log(">>>> In unregister handler");
if (user) {
User.remove({ _id: user.id }, function(err) {
if (err) { console.error('Removing User error: ' + err); }
else {
respond(res, "Boomtime. You've been unregistered!");
}
});
} else {
respond(res, 'To unregister, you need to register first!');
}
}
function handle_add(req,res,user) {
var response_url = req.body.response_url;
var item = req.body.text;
if(item.substr(0,4).toLowerCase() == "add "){
item = item.substr(4);
}
var link = "https://todoist.com/app";
var text = `Adding "${req.body.text}" in your <${link}|todoist Inbox>`;
//text += "\n" + JSON.stringify(req.body,2,2);
//text += "\n response_url="+response_url;
respond(res, text);
var token = user.todoist_oauth_token;
var item_text = req.body.text;
Q.spawn(function* () {
var result = yield todoist.itemAdd(token, item_text);
//postJSON(req.body.response_url, { text: "The result was\n"+JSON.stringify(result) });
});
}
function filterItemsDueInLessThanNDays(items, ndays) {
var now_timestamp = Date.now();
var items_due = [];
for(var i = 0, n = items.length; i < n; ++i){
var item = items[i];
var due_date_str = item.due_date_utc;
if(due_date_str){
var due_timestamp = new Date(due_date_str);
var due_in = due_timestamp - now_timestamp;
var threshold = ndays * 24 * 60 * 60 * 1000;
if(due_in < threshold){
items_due.push(item);
}
}
}
return items_due;
}
function buildItemsDueResponseJSON(items){
var json = {
text:"",
attachments: []
};
var text = [];
for(var i = 0, n = items.length; i < n; ++i){
var item = items[i];
var content = `${item.date_string}: ${item.content}`;
var link = `https://todoist.com/showTask?id=${item.id}`;
var txt = (item.date_string ? `${item.date_string} - ` : "") + `<${link}|${item.content}>`;
text.push(txt);
json.attachments.push(
{
title: content,
title_link: link,
text: txt,
fallback: content
}
);
}
json.text = text.join("\n");
// disable rich attachments for now
// comment next-line to re-enable
delete json.attachments;
if(!json.text) {
json.text = `No items found. Good job!`;
}
console.log("Items JSON="+JSON.stringify(json,2,2));
return json;
};
function respondDueItems(token, response_url, ndays){
Q.spawn(function* () {
var json = yield todoist.getAll(token);
var items = filterItemsDueInLessThanNDays(json.Items, ndays);
var response_json = buildItemsDueResponseJSON(items);
postJSON(response_url, response_json);
});
}
function respondListProjectItems(token, response_url, project){
Q.spawn(function* () {
var json = yield todoist.getAll(token);
var i;
var project_id;
for(i = 0; i < json.Projects.length && !project_id; ++i){
if(json.Projects[i].name.toLowerCase() === project.toLowerCase()){
project_id = json.Projects[i].id;
}
}
var items = [];
for(i = 0; i < json.Items.length; ++i){
var item = json.Items[i];
if(item.project_id === project_id){
items.push(item);
}
}
var response_json = buildItemsDueResponseJSON(items);
postJSON(response_url, response_json);
});
}
function handle_today(req, res, user) {
respond(res, "Your Todoist items due today:");
respondDueItems(user.todoist_oauth_token, req.body.response_url, 1);
}
function handle_week(req, res, user) {
respond(res, "Your Todoist items due in the next week:");
respondDueItems(user.todoist_oauth_token, req.body.response_url, 7);
}
function handle_list(req, res, user) {
var project = req.body.text.substr("list ".length) || "Inbox";
respond(res, `Your Todoist items in project ${project}:`);
respondListProjectItems(user.todoist_oauth_token, req.body.response_url, project);
}
function handle_labels(req, res, user) {
todoist.getAll(user.todoist_oauth_token)
.then(function(data){
var text = JSON.stringify(data);//.Labels.join(", ");
respond(res, text);
})
.done();
}
function buildProjectsResponseText(projects){
var text = ['Your Todoist projects:'];
for(var i = 0; i < projects.length; ++i) {
var project = projects[i];
var link = `https://todoist.com/app?#project%2F${project.id}`;
text.push(`<${link}|${project.name}>`);
}
return text.join("\n");
}
function handle_projects(req, res, user) {
todoist.getAll(user.todoist_oauth_token)
.then(function(data) {
var responseText = buildProjectsResponseText(data.Projects);
respond(res, responseText);
})
.done();
}
app.post('/slash', function(req, res) {
User.findOne({ 'slack_id': req.body.user_id }, function (err, user) {
if (err) { console.error('Finding User error: ' + err); }
var command = req.body.text.split(" ")[0];
// If user doesn't exist, hijack command to register
if(!user || (user && user.todoist_oauth_token == null)) {
command = "register";
}
command = command.toLowerCase();
// How's this for ugly?
var handlers = {
register: handle_register,
unregister: handle_unregister,
add: handle_add,
today: handle_today,
week: handle_week,
[7]: handle_week,
list: handle_list,
projects: handle_projects,
labels: handle_labels,
};
console.log(command);
var handler = handlers[command] || handlers.add;
handler(req, res, user);
});
});
app.get('/todoist/:hash', function(req, res) {
var registration_hash = req.params.hash;
RegistrationToken.findOne({registration_hash: registration_hash}, function(err, token) {
if (err) { console.error('Finding Registration Token error: ' + err); }
if (token && token.valid_until > new Date()) {
req.session.slack_id = token.slack_id;
res.redirect('/auth/todoist');
} else {
respond(res, 'Unable to find your registration token, please try again');
}
});
});
// OAuth stuffs
app.get('/auth/todoist', passport.authenticate('todoist'));
app.get('/auth/todoist/callback',
passport.authenticate(
'todoist', { failureRedirect: '/sadface' }),
function(req, res) {
res.redirect('/happyface/todoist');
});
app.get('/auth/slack', passport.authenticate('slack'));
app.get('/auth/slack/callback',
passport.authenticate(
'slack', { failureRedirect: '/sadface' }),
function(req, res) {
res.redirect('/happyface/slack');
});
// Sadface :(
app.get('/sadface', function(req, res) {
respond(res, 'Something bad happened. Robot overlords have been notified. :(');
});
// Happyfaces :)
app.get('/happyface/todoist', function(req, res) {
res.sendfile(__dirname + '/public/happyface_todoist.html');
});
app.get('/happyface/slack', function(req, res) {
res.sendfile(__dirname + '/public/happyface_slack.html');
});
// Help, Yo
app.get('/help', function(req, res) {
res.sendfile(__dirname + '/public/help.html');
});
function* logall() {
var token = process.env.DEBUG_TODOIST_TOKEN;
var json = yield todoist.getAll(token);
console.log(JSON.stringify(json, 2,2));
}
var debug_token = process.env.DEBUG_TODOIST_TOKEN;
if(debug_token) {
todoist.itemAdd(debug_token, "@DEBUG app started "+(new Date().toISOString()));
//Q.spawn(logall);
if(process.env.DEBUG_SLACK_RESPONSE_URL) {
//respondDueItems(debug_token, process.env.DEBUG_SLACK_RESPONSE_URL, 7);
respondListProjectItems(debug_token, process.env.DEBUG_SLACK_RESPONSE_URL, "Inbox");
}
}
var port = process.env.PORT || 8080;
app.listen(port);
console.log('Express server started on port %s', port);