This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
/
avianlib.js
705 lines (653 loc) · 24 KB
/
avianlib.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
AvianLib: Javascript OAuth for Twitter
Primarily designed for Twitter, this is an OAuth lib written entirely in Javascript (for use in JS environments not bound by cross-domain restrictions. Appcelerator, WebOS, Safari extensions, Chrome extensions, et cetera). Avian itself is specific to Twitter, but the OAuth object (found in avianlib_oauth.js) should be easily used for different OAuth applications.
Dual Licensed Modified BSD (3-clause) and GPL v2
*/
function Avian(params) {
params = params || {};
this.OAuth = params.OAuth;
this.log_to_console = params.log_to_console || false;
if(typeof params.responses == 'object' && params.responses != null) {
this.responses = params.responses;
} else {
this.responses = {};
}
if(typeof params.metadata == 'object' && params.metadata != null) {
this.metadata = params.metadata;
} else {
this.metadata = { tweets: {}, direct_messages: {} };
}
this.user_id = (params.user_id)?params.user_id:0;
//be careful that you provide these params as int and not string
this.unreadTweets = (params.unreadTweets)?params.unreadTweets:0;
this.unreadMentions = (params.unreadMentions)?params.unreadMentions:0;
this.unreadDMs = (params.unreadDMs)?params.unreadDMs:0;
this.maxTweetsStored = 30; //configurable. how many do you want to keep around?
this.defaultNumTweetsRequested = 30; //default number to obtain for methods like getHomeTimeline
//for PATHS we track with a boolean whether auth is required, but we don't do anything with it
this.PATHS = {
'request_token': {
url: 'https://api.twitter.com/oauth/request_token',
method: 'GET',
concat: false,
auth: true
},
'access_token': {
url: 'https://api.twitter.com/oauth/access_token',
method: 'GET',
concat: false,
auth: true
},
'home_timeline': {
url: 'https://api.twitter.com/1/statuses/home_timeline.json',
method: 'GET',
concat: false,
auth: true
},
'mentions': {
url: 'https://api.twitter.com/1/statuses/mentions.json',
method: 'GET',
concat: false,
auth: true
},
'update': {
url: 'https://api.twitter.com/1/statuses/update.json',
method: 'POST',
concat: false,
auth: true
},
'retweet': {
url: 'https://api.twitter.com/1/statuses/retweet/',
method: 'POST',
concat: true,
auth: true
},
'show': {
url: 'https://api.twitter.com/1/statuses/show/',
method: 'GET',
concat: true,
auth: true
},
'destroy': {
url: 'https://api.twitter.com/1/statuses/destroy/',
method: 'POST',
concat: true,
auth: true
},
'direct_messages': {
url: 'https://api.twitter.com/1/direct_messages.json',
method: 'GET',
concat: false,
auth: true
},
'dm_sent': {
url: 'https://api.twitter.com/1/direct_messages/sent.json',
method: 'GET',
concat: false,
auth: true
},
'dm_new': {
url: 'https://api.twitter.com/1/direct_messages/new.json',
method: 'POST',
concat: false,
auth: true
},
'dm_destroy': {
url: 'https://api.twitter.com/1/direct_messages/destroy/',
method: 'POST',
concat: true,
auth: true
},
'friendships_create': {
url: 'https://api.twitter.com/1/friendships/create/',
method: 'POST',
concat: true,
auth: true
},
'friendships_destroy': {
url: 'https://api.twitter.com/1/friendships/destroy/',
method: 'POST',
concat: true,
auth: true
},
//trends doesn't require auth...we send it anyway for now
'trends': {
url: 'https://search.twitter.com/trends.json',
method: 'GET',
concat: false,
auth: false
},
'favorites': {
url: 'https://api.twitter.com/1/favorites.json',
method: 'GET',
concat: false,
auth: true
},
'favorites_create': {
url: 'https://api.twitter.com/1/favorites/create/',
method: 'POST',
concat: true,
auth: true
},
'favorites_destroy': {
url: 'https://api.twitter.com/1/favorites/destroy/',
method: 'POST',
concat: true,
auth: true
},
'nearby_places': {
url: 'https://api.twitter.com/1/geo/nearby_places.json',
method: 'GET',
concat: false,
auth: false
}
};
}
Avian.prototype = {
getRequestToken: function(userSuccessCallback,userErrorCallback) {
//if present, we don't want them since their signature will mess up a new auth request
delete this.OAuth.oauth_token;
delete this.OAuth.oauth_token_secret;
var info = this.getPath('request_token');
this.OAuth.getToken(info.url,info.method,{},userSuccessCallback,userErrorCallback);
},
getAccessToken: function(userSuccessCallback,userErrorCallback) {
var info = this.getPath('access_token');
this.OAuth.getToken(info.url,info.method,{},userSuccessCallback,userErrorCallback);
},
queryTwitter : function(apiMethod,params,userSuccessCallback,userErrorCallback) {
this.logging('queryTwitter params:');
this.logging(params);
var self = this;
var successCallback = function(data, textStatus, xhr) {
//self.logging('success callback response: '+data);
var parsedObj;
try {
parsedObj = JSON.parse(data);
} catch(e) {
parsedObj = eval(data); //boo eval.
}
self.logging(parsedObj);
if(typeof self.responses[apiMethod] == 'object' && self.responses[apiMethod] instanceof Array === true) {
self.responses[apiMethod] = parsedObj.concat(self.responses[apiMethod]);
} else {
self.responses[apiMethod] = parsedObj;
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
};
var errorCallback = function(xhr, textStatus, errorThrown) {
//should probably handle the common twitter errors here
//http://apiwiki.twitter.com/HTTP-Response-Codes-and-Errors
self.logging('queryTwitter error callback response: '+errorThrown);
if(typeof userErrorCallback == 'function') { userErrorCallback(errorThrown); }
};
var info = this.getPath(apiMethod);
var url = info.url;
if(info.concat) {
//this is an ugly hack. tries to guess whether we want to fetch via user_id or status_id (usually the latter)
//TODO, better method (this is method #2, but maybe 3rd time's the charm)
var id = (typeof params.status_id != 'undefined')?params.status_id:params.user_id;
//build proper URL
url = info.url+id+'.json';
if(typeof params.status_id != 'undefined') {
delete params.status_id;
} else if (typeof params.user_id != 'undefined') {
delete params.status_id;
}
}
this.logging(url);
this.OAuth.request(url,info.method,params,successCallback,errorCallback);
},
getPath : function(apiMethod) {
return this.PATHS[apiMethod];
},
addTweetMetadata: function(obj) {
this.logging('adding tweet metadata');
for(var i=0;i < obj.length;i++) {
var tweet = obj[i];
if(typeof this.metadata.tweets[tweet.id_str] == 'undefined') {
this.metadata.tweets[tweet.id_str] = {unread:true};
}
if(this.user_id == tweet.user.id) {
//user_id matches, this tweet is from the auth'd user. mark as read.
this.metadata.tweets[tweet.id_str] = {unread:false};
}
}
},
addDirectMessageMetadata: function(obj,sentReceived) {
this.logging('adding DM metadata');
for(var i=0;i < obj.length;i++) {
var dm = obj[i];
if(typeof this.metadata.direct_messages[dm.id_str] == 'undefined') {
if(sentReceived == 'received') {
this.metadata.direct_messages[dm.id_str] = {unread:true};
} else {
//the user sent this so we don't want it to be unread...
this.metadata.direct_messages[dm.id_str] = {unread:false};
}
}
}
},
//gets the home_timeline.
getHomeTimeline : function(params,userSuccessCallback,userErrorCallback) {
//by default, get only the latest tweets
if(typeof this.responses.home_timeline != 'undefined' && typeof this.responses.home_timeline[0] != 'undefined' && typeof params.since_id == 'undefined') {
params.since_id = this.responses.home_timeline[0].id_str;
}
if(typeof params.count == 'undefined') {
params.count = this.defaultNumTweetsRequested;
}
if(typeof params.include_entities == 'undefined') {
params.include_entities = 1;
}
var self = this;
function homeTimelineSuccessCallback(parsedObj) {
parsedObj = parsedObj.filter(function(tweet) { return tweet !== null; }); //nuke any nulls that come back. this line was added in response to the null bug twitter had 8/24/10
//loop through the tweets, adding metadata for each one
self.addTweetMetadata(parsedObj);
//iterate over the metadata to see if all these new tweets have been read previously (perhaps as a mention)
var newUnreadTweets = 0;
for(var i=0;i < parsedObj.length;i++) {
if(self.metadata.tweets[parsedObj[i].id_str].unread == true) {
newUnreadTweets++;
}
}
self.unreadTweets += newUnreadTweets;
var numToRemove = self.responses['home_timeline'].length - self.maxTweetsStored;
if (numToRemove > 0) {
var index = self.responses['home_timeline'].length-numToRemove-1; //0 indexed so we need to subtract 1 more
var removedTweets = self.responses['home_timeline'].splice(index,numToRemove);
/*for(var j=0;j< removedTweets.length;j++) {
var id = removedTweets[j].id_str;
delete self.metadata.tweets[id_str]; //delete our metadata object for this tweet
}*/
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter('home_timeline',params,homeTimelineSuccessCallback,userErrorCallback);
},
getMentions : function(params,userSuccessCallback,userErrorCallback) {
//by default, get only the most recent mentions
if(typeof this.responses.mentions != 'undefined' &&typeof this.responses.mentions[0] != 'undefined' && typeof params.since_id == 'undefined') {
params.since_id = this.responses.mentions[0].id_str;
}
if(typeof params.count == 'undefined') {
params.count = this.defaultNumTweetsRequested;
}
if(typeof params.include_entities == 'undefined') {
params.include_entities = 1;
}
var self = this;
function mentionsSuccessCallback(parsedObj) {
parsedObj = parsedObj.filter(function(tweet) { return tweet !== null; }); //nuke any nulls that come back. this line was added in response to the null bug twitter had 8/24/10
self.addTweetMetadata(parsedObj);
//iterate over the metadata to see if all these new tweets have been read previously (perhaps in home timeline)
var newUnreadMentions = 0;
for(var i=0;i < parsedObj.length;i++) {
if(self.metadata.tweets[parsedObj[i].id_str].unread == true) {
newUnreadMentions++;
}
}
self.unreadMentions += newUnreadMentions;
//see if the response array exceeds our allowed limit.
//if so, truncate and strip the metadata out
var numToRemove = self.responses['mentions'].length - self.maxTweetsStored;
if (numToRemove > 0) {
var index = self.responses['mentions'].length-numToRemove-1; //0 indexed so we need to subtract 1 more
var removedTweets = self.responses['mentions'].splice(index,numToRemove);
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter('mentions',params,mentionsSuccessCallback,userErrorCallback);
},
getTweet: function(status_id,userSuccessCallback,userErrorCallback) {
var found = false;
if(typeof this.responses.home_timeline != 'undefined') {
for(var i=0;i < this.responses.home_timeline.length;i++) {
if(this.responses.home_timeline[i].id_str == status_id) {
//okay we've already got the tweet. set the response object
//and call their success callback
this.responses.show = this.responses.home_timeline[i];
if(typeof userSuccessCallback == 'function') { userSuccessCallback(this.responses.home_timeline[i]); }
found = true;
break;
}
}
}
if(typeof this.responses.mentions != 'undefined' && found != true) {
for(var j=0;j < this.responses.mentions.length;j++) {
if(this.responses.mentions[j].id_str == status_id) {
//okay we've already got the tweet. set the response object
//and call their success callback
this.responses.show = this.responses.mentions[j];
if(typeof userSuccessCallback == 'function') { userSuccessCallback(this.responses.mentions[j]); }
found = true;
break;
}
}
}
if(!found) {
this.queryTwitter('show',{status_id:status_id},userSuccessCallback,userErrorCallback);
}
},
destroyTweet: function(status_id,userSuccessCallback,userErrorCallback) {
var self = this;
function destroySuccessCallback(data) {
//rip it out of our timeline object
if(typeof self.responses.home_timeline != 'undefined') {
for(var i=0;i < self.responses.home_timeline.length;i++) {
if(self.responses.home_timeline[i].id_str == status_id) {
self.arrayRemove(self.responses.home_timeline,i);
break;
}
}
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(data); }
}
function destroyErrorCallback(errorThrown) {
self.logging('destroy failed on xhr');
if(typeof userErrorCallback == 'function') { userErrorCallback(errorThrown); }
}
this.queryTwitter('destroy',{status_id:status_id},destroySuccessCallback,destroyErrorCallback);
},
getThread: function(status_id,userSuccessCallback,userErrorCallback) {
this.responses.thread = [];
var self = this;
function successCallback(data) {
if(data.in_reply_to_status_id_str == null) {
self.responses.thread.push(data);
if(typeof userSuccessCallback == 'function') { userSuccessCallback(self.responses.thread); }
} else {
self.responses.thread.push(data);
self.getTweet(data.in_reply_to_status_id_str,successCallback,errorCallback);
}
}
function errorCallback(errorThrown) {
self.logging('getThread failed on xhr');
if(typeof userErrorCallback == 'function') { userErrorCallback(errorThrown,self.responses.thread); }
}
this.getTweet(status_id,successCallback,errorCallback);
},
replyToTweet: function(params,userSuccessCallback,userErrorCallback) {
this.queryTwitter('update',params,userSuccessCallback,userErrorCallback);
},
retweet: function(status_id,userSuccessCallback,userErrorCallback) {
this.queryTwitter('retweet',{status_id:status_id},userSuccessCallback,userErrorCallback);
},
getDirectMessages: function(sentReceived,params,userSuccessCallback,userErrorCallback) {
var apiMethod;
if(sentReceived == 'received') {
apiMethod = 'direct_messages';
} else {
apiMethod = 'dm_sent';
}
//by default, get only the most recent
if(typeof this.responses[apiMethod] != 'undefined' && typeof params.since_id == 'undefined') {
params.since_id = this.responses[apiMethod][0].id_str;
}
if(typeof params.count == 'undefined') {
params.count = this.defaultNumTweetsRequested;
}
var self = this;
function directMessagesSuccessCallback(parsedObj) {
self.addDirectMessageMetadata(parsedObj,sentReceived);
//iterate over the metadata to see if all these new DMs have been read previously
var newUnreadDMs = 0;
for(var i=0;i < parsedObj.length;i++) {
if(self.metadata.direct_messages[parsedObj[i].id_str].unread == true) {
newUnreadDMs++;
}
}
self.unreadDMs += newUnreadDMs;
//see if the response array exceeds our allowed limit.
var numToRemove = self.responses[apiMethod].length - self.maxTweetsStored;
if (numToRemove > 0) {
var index = self.responses[apiMethod].length-numToRemove-1; //0 indexed so we need to subtract 1 more
var removedDirectMessages = self.responses[apiMethod].splice(index,numToRemove);
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter(apiMethod,params,directMessagesSuccessCallback,userErrorCallback);
},
getReceivedDirectMessages: function(params,userSuccessCallback,userErrorCallback) {
this.getDirectMessages('received',params,userSuccessCallback,userErrorCallback);
},
getSentDirectMessages: function(params,userSuccessCallback,userErrorCallback) {
this.getDirectMessages('sent',params,userSuccessCallback,userErrorCallback);
},
sendDirectMessage: function(status,user_id,userSuccessCallback,userErrorCallback) {
this.queryTwitter('dm_new',{text:status,user:user_id},userSuccessCallback,userErrorCallback);
},
destroyDirectMessage: function(status_id,userSuccessCallback,userErrorCallback) {
var self = this;
function destroySuccessCallback(data) {
//rip it out of our direct message objects
var found = false;
if(typeof self.responses.direct_messages != 'undefined') {
for(var i=0;i < self.responses.direct_messages.length;i++) {
if(self.responses.direct_messages[i].id_str == status_id) {
found = true;
self.arrayRemove(self.responses.direct_messages,i);
break;
}
}
}
if(typeof self.responses.dm_sent != 'undefined' && found != true) {
for(var j=0;j < self.responses.dm_sent.length;j++) {
if(self.responses.dm_sent[j].id_str == status_id) {
found = true;
self.arrayRemove(self.responses.dm_sent,j);
break;
}
}
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(data); }
}
this.queryTwitter('dm_destroy',{status_id:status_id},destroySuccessCallback,userErrorCallback);
},
//convenience method to mimic getTweet. no equivalent API call, so it has to be present in the response arrays
getDM: function(status_id,userSuccessCallback,userErrorCallback) {
var found = false;
if(typeof this.responses.direct_messages != 'undefined') {
for(var i=0;i < this.responses.direct_messages.length;i++) {
if(this.responses.direct_messages[i].id_str == status_id) {
if(typeof userSuccessCallback == 'function') { userSuccessCallback(this.responses.direct_messages[i]); }
found = true;
break;
}
}
}
if(typeof this.responses.dm_sent != 'undefined' && found != true) {
for(var j=0;j < this.responses.dm_sent.length;j++) {
if(this.responses.dm_sent[j].id_str == status_id) {
if(typeof userSuccessCallback == 'function') { userSuccessCallback(this.responses.dm_sent[j]); }
break;
}
}
}
},
markAllDMsAsRead : function() {
for(var i in this.metadata.direct_messages) {
this.metadata.direct_messages[i].unread = false;
}
this.unreadDMs = 0;
},
markDMAsRead: function(status_id) {
if(this.metadata.direct_messages[status_id].unread) {
this.metadata.direct_messages[status_id].unread = false;
if(this.unreadDMs >= 1) {
this.unreadDMs -= 1;
}
}
},
getDMMetadata: function(status_id) {
return this.metadata.direct_messages[status_id];
},
createFriendship: function(user_id,userSuccessCallback,userErrorCallback) {
this.queryTwitter('friendships_create',{user_id:user_id},userSuccessCallback,userErrorCallback);
},
destroyFriendship: function(user_id,userSuccessCallback,userErrorCallback) {
this.queryTwitter('friendships_destroy',{user_id:user_id},userSuccessCallback,userErrorCallback);
},
createFavorite: function(status_id,userSuccessCallback,userErrorCallback) {
var self = this;
function createFavoriteSuccessCallback(parsedObj) {
/*for(var i=0;i < self.responses.home_timeline.length;i++) {
if(self.responses.home_timeline[i].id_str == status_id) {
self.responses.home_timeline[i].favorited = true;
break;
}
}
for(var j=0;j < self.responses.mentions.length;i++) {
if(self.responses.mentions[i].id_str == status_id) {
self.responses.mentions[i].favorited = true;
break;
}
}*/
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter('favorites_create',{status_id:status_id},createFavoriteSuccessCallback,userErrorCallback);
},
destroyFavorite: function(status_id,userSuccessCallback,userErrorCallback) {
var self = this;
function destroyFavoriteSuccessCallback(parsedObj) {
/*for(var i=0;i < self.responses.home_timeline.length;i++) {
if(self.responses.home_timeline[i].id_str == status_id) {
self.responses.home_timeline[i].favorited = false;
break;
}
}
for(var j=0;j < self.responses.mentions.length;i++) {
if(self.responses.mentions[j].id_str == status_id) {
self.responses.mentions[j].favorited = false;
break;
}
}*/
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter('favorites_destroy',{status_id:status_id},destroyFavoriteSuccessCallback,userErrorCallback);
},
//TODO: track unread here? refactor to use a more generic object? these get methods are all very similar.
getFavorites : function(params,userSuccessCallback,userErrorCallback) {
//by default, get only the most recent favorites
if(typeof this.responses.favorites != 'undefined' &&typeof this.responses.favorites[0] != 'undefined' && typeof params.since_id == 'undefined') {
params.since_id = this.responses.favorites[0].id_str;
}
if(typeof params.count == 'undefined') {
params.count = this.defaultNumTweetsRequested;
}
var self = this;
function favoritesSuccessCallback(parsedObj) {
self.addTweetMetadata(parsedObj);
//see if the response array exceeds our allowed limit.
//if so, truncate and strip the metadata out
var numToRemove = self.responses['favorites'].length - self.maxTweetsStored;
if (numToRemove > 0) {
var index = self.responses['favorites'].length-numToRemove-1; //0 indexed so we need to subtract 1 more
var removedTweets = self.responses['favorites'].splice(index,numToRemove);
}
if(typeof userSuccessCallback == 'function') { userSuccessCallback(parsedObj); }
}
this.queryTwitter('favorites',params,favoritesSuccessCallback,userErrorCallback);
},
getNearbyPlaces: function(params,userSuccessCallback,userErrorCallback) {
if(params.max_results == 'undefined') {
params.max_results = 1;
}
this.queryTwitter('nearby_places',params,userSuccessCallback,userErrorCallback);
},
getTrends: function(userSuccessCallback,userErrorCallback) {
this.queryTwitter('trends',{},userSuccessCallback,userErrorCallback);
},
markAllTweetsAsRead : function() {
for(var i in this.metadata.tweets) {
this.metadata.tweets[i].unread = false;
}
this.unreadTweets = 0;
this.unreadMentions = 0;
},
markTweetAsRead: function(status_id) {
if(this.metadata.tweets[status_id].unread) {
this.metadata.tweets[status_id].unread = false;
if(this.tweetInHomeTimeline(status_id)) {
if(this.unreadTweets >= 1) {
this.unreadTweets -= 1;
}
}
if(this.tweetInMentions(status_id)) {
if(this.unreadMentions >= 1) {
this.unreadMentions -= 1;
}
}
}
},
tweetInHomeTimeline: function(status_id) {
for(var i=0; i < this.responses.home_timeline.length; i++) {
if (this.responses.home_timeline[i].id_str == status_id) {
return true;
}
}
return false;
},
tweetInMentions: function(status_id) {
for(var i=0; i < this.responses.mentions.length; i++) {
if (this.responses.mentions[i].id_str == status_id) {
return true;
}
}
return false;
},
getTweetMetadata: function(status_id) {
return this.metadata.tweets[status_id];
},
//returns number of tweets present in the home_timeline, mentions, dm sent, and dm received arrays
numTweetsInTimelines : function() {
var response = {home_timeline:0,mentions:0,direct_messages:0,dm_sent:0};
try {
response.home_timeline = (this.responses.home_timeline.length > 0)?this.responses.home_timeline.length:0;
} catch(e) {
response.home_timeline = 0;
}
try {
response.mentions = (this.responses.mentions.length > 0)?this.responses.mentions.length:0;
} catch(e) {
response.mentions = 0;
}
try {
response.direct_messages = (this.responses.direct_messages.length > 0)?this.responses.direct_messages.length:0;
} catch(e) {
response.direct_messages = 0;
}
try {
response.dm_sent = (this.responses.dm_sent.length > 0)?this.responses.dm_sent.length:0;
} catch(e) {
response.dm_sent = 0;
}
return response;
},
//returns true if oauth is complete, false in any other scenario (ie, request token present but not access token)
checkAuthStatus: function() {
try {
if (this.OAuth.oauth_token.search(/^([0-9]+)-/) != -1) {
return true;
} else {
return false;
}
} catch(e) {
return false;
}
},
logging: function(data) {
if(this.log_to_console) {
try {
console.log(data);
} catch(e) {}
}
},
// Array Remove - By John Resig (MIT Licensed)
arrayRemove: function(array, from, to) {
var rest = array.slice((to || from) + 1 || array.length);
array.length = from < 0 ? array.length + from : from;
return array.push.apply(array, rest);
}
};