forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
222 lines (181 loc) · 4.54 KB
/
twitter.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
(function(hello) {
var base = 'https://api.twitter.com/';
hello.init({
twitter: {
// Ensure that you define an oauth_proxy
oauth: {
version: '1.0a',
auth: base + 'oauth/authenticate',
request: base + 'oauth/request_token',
token: base + 'oauth/access_token'
},
login: function(p) {
// Reauthenticate
// https://dev.twitter.com/oauth/reference/get/oauth/authenticate
var prefix = '?force_login=true';
this.oauth.auth = this.oauth.auth.replace(prefix, '') + (p.options.force ? prefix : '');
},
base: base + '1.1/',
get: {
me: 'account/verify_credentials.json',
'me/friends': 'friends/list.json?count=@{limit|200}',
'me/following': 'friends/list.json?count=@{limit|200}',
'me/followers': 'followers/list.json?count=@{limit|200}',
// Https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
'me/share': 'statuses/user_timeline.json?count=@{limit|200}',
// Https://dev.twitter.com/rest/reference/get/favorites/list
'me/like': 'favorites/list.json?count=@{limit|200}'
},
post: {
'me/share': function(p, callback) {
var data = p.data;
p.data = null;
var status = [];
// Change message to status
if (data.message) {
status.push(data.message);
delete data.message;
}
// If link is given
if (data.link) {
status.push(data.link);
delete data.link;
}
if (data.picture) {
status.push(data.picture);
delete data.picture;
}
// Compound all the components
if (status.length) {
data.status = status.join(' ');
}
// Tweet media
if (data.file) {
data['media[]'] = data.file;
delete data.file;
p.data = data;
callback('statuses/update_with_media.json');
}
// Retweet?
else if ('id' in data) {
callback('statuses/retweet/' + data.id + '.json');
}
// Tweet
else {
// Assign the post body to the query parameters
hello.utils.extend(p.query, data);
callback('statuses/update.json?include_entities=1');
}
},
// See: https://dev.twitter.com/rest/reference/post/favorites/create
'me/like': function(p, callback) {
var id = p.data.id;
p.data = null;
callback('favorites/create.json?id=' + id);
}
},
del: {
// See: https://dev.twitter.com/rest/reference/post/favorites/destroy
'me/like': function() {
p.method = 'post';
var id = p.data.id;
p.data = null;
callback('favorites/destroy.json?id=' + id);
}
},
wrap: {
me: function(res) {
formatError(res);
formatUser(res);
return res;
},
'me/friends': formatFriends,
'me/followers': formatFriends,
'me/following': formatFriends,
'me/share': function(res) {
formatError(res);
paging(res);
if (!res.error && 'length' in res) {
return {data: res};
}
return res;
},
'default': function(res) {
res = arrayToDataResponse(res);
paging(res);
return res;
}
},
xhr: function(p) {
// Rely on the proxy for non-GET requests.
return (p.method !== 'get');
}
}
});
function formatUser(o) {
if (o.id) {
if (o.name) {
var m = o.name.split(' ');
o.first_name = m.shift();
o.last_name = m.join(' ');
}
// See: https://dev.twitter.com/overview/general/user-profile-images-and-banners
o.thumbnail = o.profile_image_url_https || o.profile_image_url;
}
return o;
}
function formatFriends(o) {
formatError(o);
paging(o);
if (o.users) {
o.data = o.users.map(formatUser);
delete o.users;
}
return o;
}
function formatError(o) {
if (o.errors) {
var e = o.errors[0];
o.error = {
code: 'request_failed',
message: e.message
};
}
}
// Take a cursor and add it to the path
function paging(res) {
// Does the response include a 'next_cursor_string'
if ('next_cursor_str' in res) {
// See: https://dev.twitter.com/docs/misc/cursoring
res.paging = {
next: '?cursor=' + res.next_cursor_str
};
}
}
function arrayToDataResponse(res) {
return Array.isArray(res) ? {data: res} : res;
}
/**
// The documentation says to define user in the request
// Although its not actually required.
var user_id;
function withUserId(callback){
if(user_id){
callback(user_id);
}
else{
hello.api('twitter:/me', function(o){
user_id = o.id;
callback(o.id);
});
}
}
function sign(url){
return function(p, callback){
withUserId(function(user_id){
callback(url+'?user_id='+user_id);
});
};
}
*/
})(hello);