/
server.js
276 lines (254 loc) · 9.42 KB
/
server.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
var oauth = require('oauth');
var request = require('request');
// main manager function
/**
* Manager Function
*
* Managers all watchers and url calls to the twitter service.
*
* Should eventually be used to monitor usage limits.
* Handles all oAuth requests.
*/
module.exports = function(options) {
var self = this;
if(options.redis) {
var datastore = require('./redis.js')(options.redis.host, options.redis.port);
}
self.cacheDuration = 60; // 60 secs default cache.
self.consumer = new oauth.OAuth(
"https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
options.consumerKey,
options.consumerSecret,
"1.0A",
options.domain + options.loginCallback,
"HMAC-SHA1"
);
/**
* Fetches the url provided passing along all the oauth params.
* @param {string} url URL to fetch
* @param {[type]} oauthToken Oauth token provided by twitter.
* @param {[type]} oauthTokenSecret Oauth secret provided by twitter.
* @param {Function} callback Function to call when all data has been completed. Takes error, data
*/
self.fetch = function(url, oauthToken, oauthTokenSecret, callback) {
var self = this;
var get = function(url, oauthToken, oauthTokenSecret, callback) {
self.consumer.get(url, oauthToken, oauthTokenSecret, function (error, data, response) {
if(response && response.headers) {
// this info should get sent back with each request.
var limit = response.headers['x-rate-limit-remaining'];
if(limit === "0") { //rate limit has not been reached.
callback({limitReached: true, auth: data}, null);
return;
}
}
if (error) {
callback(error, null);
return;
}
try { // sometime data is coming back as invalid json. https://dev.twitter.com/discussions/9554
data = JSON.parse(data);
if(data.error) {
callback(data, null);
return;
}
if(options.redis) {
var key = 'twitterStore:'+url;
datastore.set(key, JSON.stringify(data));
datastore.expire(key, self.cacheDuration);
}
if(callback) {
callback(null, data, limit);
}
} catch(error) {
// leaving this in so we can keep an eye of invalid json being returned.
console.log('ERROR', error);
}
});
};
if(options.redis) {
datastore.get('twitterStore:'+url, function(redisError, data) {
if(redisError || !data){
get(url, oauthToken, oauthTokenSecret, callback);
} else { // we have cached data, lets send that.
callback(null, JSON.parse(data));
}
});
}else {
get(url, oauthToken, oauthTokenSecret, callback);
}
};
/**
* Returns mentions for the logged in user.
*
* docs: https://dev.twitter.com/docs/api/1.1/get/statuses/mentions_timeline
*
* @param {String} oauthToken oauth token provided by twitter.
* @param {String} oauthTokenSecret oauth secret provided by twitter.
* @param {String} sinceId only get tweets after this retweet.
* @param {Function} callback Called with the mentions. (error, data)
*/
self.retweets = function(oauthToken, oauthTokenSecret, sinceId, callback) {
var processData = function(error, data, limit) {
callback(error, {
limit: limit,
tweets: data
});
};
var q = (sinceId && sinceId!='false') ? '?since_id='+sinceId : '';
self.fetch('https://api.twitter.com/1.1/statuses/mentions_timeline.json'+q, oauthToken, oauthTokenSecret, callback);
};
/**
* Returns info about the user specified by the handle.
*
* docs: https://dev.twitter.com/docs/api/1.1/get/users/lookup
*
* @param {String} oauthToken oauth token provided by twitter.
* @param {String} oauthTokenSecret oauth secret provided by twitter.
* @param {Function} callback Called with the mentions. (error, data)
*/
self.user = function(handle, oauthToken, oauthTokenSecret, callback) {
var processData = function(error, data, limit) {
callback(error, {
limit: limit,
user: data
});
};
self.fetch('https://api.twitter.com/1.1/users/lookup.json?screen_name='+handle, oauthToken, oauthTokenSecret, processData);
};
/**
* Returns info about the user specified by the handle.
*
* docs: https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
*
* @param {String} oauthToken oauth token provided by twitter.
* @param {String} oauthTokenSecret oauth secret provided by twitter.
* @param {Function} callback Called with the mentions. (error, data)
*/
self.verify = function(oauthToken, oauthTokenSecret, callback) {
var processData = function(error, data, limit) {
callback(error, {
limit: limit,
user: data
});
};
self.fetch('https://api.twitter.com/1.1/account/verify_credentials.json', oauthToken, oauthTokenSecret, processData);
};
/**
* Search tweets
* @param {String} term Search term
* @param {[type]} oauthToken oauth token provided by twitter.
* @param {[type]} oauthTokenSecret [oauth token provided by secret.
* @param {Function} callback Called once complete (error, data)
*/
self.search = function(term, oauthToken, oauthTokenSecret, callback) {
self.processData = function(error, data, limit) {
if(error) {
callback(error, null);
return error;
}
var processTweet = function(tweet) {
return {
text: tweet.text,
image: tweet.user.profile_image_url,
timestamp: tweet.created_at,
username: tweet.user.screen_name,
id: tweet.id,
url: 'https://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str
};
};
var tweets = data.statuses;
if(tweets){ // got tweets
var c = tweets.length;
var processedTweets = [];
while(c--) {
processedTweets.push(processTweet(tweets[c]));
}
callback(null, {
limit: limit,
tweets: processedTweets.reverse()
}, limit);
}else {
callback(null, null);
}
};
self.fetch('https://api.twitter.com/1.1/search/tweets.json?q='+encodeURI(term)+'', oauthToken, oauthTokenSecret, processData);
};
/*
Search with no auth.
Probably shouldn't be in here but what the heck.
*/
self.freeSearch = function(search, callback) {
request('http://search.twitter.com/search.json?q='+search, callback);
};
// oauth routes.
//
// Come on - you know this one.
self.logout = function(req, res, next) {
req.session.oauthAccessToken = null;
req.session.oauthAccessTokenSecret = null;
req.session.destroy();
res.json({'logout': 'ok'});
};
// Used to connect using oauth.
self.oauthConnect = function(req, res, next) {
var referer = req.header('Referer');
if(referer){
req.session.originalUrl = referer; // stored so we can return them to here later.
}
self.consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if (error) {
res.send("Error getting OAuth request token : ", 500);
console.log('oAuth error: '+ error);
} else {
req.session.oauthRequestToken = oauthToken; // we will need these values in the oauthCallback so store them on the session.
req.session.oauthRequestTokenSecret = oauthTokenSecret;
var connectCallback = function(req, res, next) { // keep track of the site id in the sesion for the callback.
console.log('concb');
req.session.siteId = req.params.siteId;
req.session.apiKey = req.params.apiKey;
req.session.siteToken = req.params.siteToken;
}
if(options.connectCallback) {
options.connectCallback(req, res, next);
}else {
connectCallback(req, res, next);
}
console.log('DO REDIRECT');
res.redirect("https://twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);
}
});
};
self.oauthCallback = function(req, res, next) {
console.log('wotcha');
self.consumer.getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if (error) {
res.send("Access Denied." , 500);
console.log('oAuth Error: step2: ' + JSON.stringify(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]");
} else {
req.session.oauthAccessToken = oauthAccessToken; // ensure we are clearing the session variables.
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
if(options.oauthCallbackCallback) {
console.log(1);
options.oauthCallbackCallback(req, res, next, results.screen_name, oauthAccessToken, oauthAccessTokenSecret);
}else {
console.log(2);
res.redirect(options.completeCallback);
}
}
});
};
/**
* First attempt at writing middleware - use with great caution. Actually, prob best not to use it.
*/
self.middleware = function(req, res, next) {
if(!req.session.oauthAccessToken && !req.session.oauthRequestTokenSecret) { // not idea
console.log('fail', options.login);
res.redirect(options.login);
}else {
next();
}
};
return self;
};