-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsubscriptions.js
82 lines (69 loc) · 2.03 KB
/
subscriptions.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
const request = require('request-promise')
const auth = require('../helpers/auth.js')
const webhook_view = require('./webhook.js')
module.exports = function (req, response) {
var saved_bearer_token
var json_response
// get list of subs
auth.get_twitter_bearer_token()
.then(function(bearer_token) {
saved_bearer_token = bearer_token
return auth.get_webhook_id(bearer_token)
})
.then(webhook_id => {
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/webhooks/' + webhook_id + '/subscriptions/all/list.json',
auth: {
'bearer': saved_bearer_token
}
}
return request.get(request_options)
})
// hydrate user objects from IDs
.then(function (body) {
var json_body = json_response = JSON.parse(body)
// if no subs, render as is and skip user hydration
if (!json_body.subscriptions.length) {
response.render('subscriptions', json_body)
return Promise.resolve()
}
// construct comma delimited list of user IDs for user hydration
var user_id
json_body.subscriptions.forEach(function(sub) {
if (user_id) {
user_id = user_id + ',' + sub.user_id
} else {
user_id = sub.user_id
}
});
var request_options = {
url: 'https://api.twitter.com/1.1/users/lookup.json?user_id=' + user_id,
auth: {
'bearer': saved_bearer_token
}
}
return request.get(request_options)
})
// replace the subscriptions list with list of user objects
// and render list
.then(function (body) {
// only render if we didn't skip user hydration
if (body) {
json_response.subscriptions = JSON.parse(body)
response.render('subscriptions', json_response)
}
})
.catch(function (body) {
console.log(body)
var json_response = {
title: 'Error',
message: 'Subscriptions could not be retrieved.',
button: {
title: 'Ok',
url: '/'
}
}
resp.status(500);
resp.render('status', json_response)
})
}