-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfatc.sjs
367 lines (323 loc) · 10.7 KB
/
fatc.sjs
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
/*
This is more of a scaffold than a proper twitter client.
It lacks any sophistication and basically just allows you to sign-in
and tweet, it shows and auto-updates your timeline, and it loads older
tweets when you scroll down the page.
It's easy to extend, so go on and hack it :-)
Oh, and IT RUNS COMPLETELY CLIENT-SIDE. There is no server code involved.
It talks to twitter directly. You don't even need a proper domain to
run it from; you can serve it from 'localhost'. E.g. if you have a Mac,
you can serve it via Personal Web Sharing - see
http://www.macinstruct.com/node/112 .
* HOW TO GET A TWITTER APPLICATION ID:
Log into twitter and go to http://dev.twitter.com/apps/ .
Register a new application, setting the 'Callback URL' to the site where
your app will live (this can be 'http://localhost'!).
Set the 'Default Access type' to 'Read & Write'.
Then change the application id below to the @Anywhere API key of your app.
* ABOUT STRATIFIED JAVASCRIPT / ONI APOLLO:
Oni Apollo, the library that we use here, allows you to make calls to
the Twitter API directly from the client in a NON-CALLBACK style.
See http://onilabs.com/api#twitter.
It allows you to call any function in the standard RESTful Twitter API
(see http://dev.twitter.com/doc) in this way:
var result = T.call("statuses/home_timeline", params);
StratifiedJS is the extended JS used by Oni Apollo to make the
non-callback style possible (and more!). See http://stratifiedjs.org/sjsdocs.
*/
var API_KEY = "OQTPVZIstxfxFVB2kD5oA";
//----------------------------------------------------------------------
// Initialization
// Load the @Anywhere API and init with your application id:
var T = require("twitter").initAnywhere({id:API_KEY});
// We'll use jquery; load it from Google's CDN and install stratified
// bindings ($click, etc):
require("jquery-binding").install();
// We'll also use various methods from the common module (supplant, ...)
var common = require("common");
var tweeting_button = $("#tweeting_button");
var status_el = $("#status");
var counter = $("#tweeting_status");
//----------------------------------------------------------------------
// main program loop
// Show the twitter connect button:
T("#login").connectButton();
// Run our application logic in an endless loop:
while (true) {
try {
main();
}
catch(e) {
alert("Error:"+e);
}
}
//----------------------------------------------------------------------
// main application logic:
function main() {
// First wait until we're connected:
if (!T.isConnected())
T.waitforEvent("authComplete");
$("#login").hide();
$("#welcome").hide();
$("#timeline").empty();
try {
// Let's set the last tweet and the background based on the
// user's prefs:
var profile = T.call("users/show", {user_id: T.currentUser.id});
$("#current_user").text(profile.screen_name);
$("#current_user").prepend("<img src='"+profile.profile_image_url + "'/>");
setLatestTweet(profile.status);
$("body").css({
background: common.supplant("#{color} url({image}) {repeat}", {
color: profile.profile_background_color,
image: profile.profile_background_image_url,
repeat: profile.profile_background_tile ? "repeat" : "no-repeat"
})
});
// Now we'll do several things in parallel, by combining them with
// the stratified construct waitfor/and:
waitfor {
waitfor_signout();
// Now exit main(). The other clauses in the waitfor/and will
// automatically be aborted cleanly (i.e. eventlisteners will be
// removed, etc).
return;
}
and {
// Endlessly handle tweets by the user:
tweet_loop();
}
and {
// Endlessly update the timeline:
update_timeline_loop();
}
and {
// Endlessly load older tweets when the user scrolls down:
show_more_loop();
}
and {
ui_mute_loop();
}
}
finally {
// Clean up:
$("#current_user").empty();
$("#timeline").empty();
$("#welcome").show();
$("body").css({background:""});
}
}
//----------------------------------------------------------------------
// Helper functions
function setLatestTweet(tweet) {
$("#latest")[tweet?"show":"hide"]();
if (tweet)
$("#latest span").text(tweet.text);
}
// 'characters left' counter
function update_counter() {
counter.text(140 - status_el.val().length);
}
function ui_mute_loop() {
if (!window["localStorage"]) return;
var filterArray = [];
tweetFilter = function(tweet) {
for (var i = filterArray.length; i >= 0; --i) {
var keyword = filterArray[i];
if (!keyword) continue;
if (keyword.indexOf("@") == 0 && tweet.user.screen_name == keyword.substring(1)) return true;
else if (tweet.text.indexOf(keyword) != -1) return true;
}
return false;
};
var button = $("<a href='#'>Mute list</a>").prependTo("#session_buttons");
try {
while(true) {
filterArray = localStorage.mute ? localStorage.mute.split(" ") : [];
button.$click();
var rv = prompt("Enter a space-separated list of #keywords or @users you want to mute.", localStorage.mute);
if (rv != null) {
localStorage.mute = rv;
$(window).trigger("settingschanged");
}
}
} finally {
button.remove();
}
}
// Append/prepend a tweet to the timeline
function showTweet(tweet, append) {
if (window["tweetFilter"] && tweetFilter(tweet)) return;
var date = new Date(tweet.created_at.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
"$1 $2 $4 $3 UTC"));
var elapsed = Math.round(((new Date()).getTime() - date.getTime()) / 60000);
if (elapsed < 60) {
var date_string = elapsed + " minutes";
}
else if (elapsed < 60*24) {
var date_string = Math.round(elapsed / 60) + " hours";
}
else {
var date_string = date.getDate() + "/" + date.getMonth();
}
if (tweet.entities && tweet.entities.urls) {
for (var i = tweet.entities.urls.length - 1, entity; entity = tweet.entities.urls[i]; --i) {
tweet.text = common.supplant("{a}<a target='_blank' href='{b}'>{b}</a>{c}", {
a: tweet.text.substring(0, entity.indices[0]),
b: entity.url,
c: tweet.text.substring(entity.indices[1])
});
}
}
// Construct the html for the tweet. Note how we can use
// multiline strings in StratifiedJS. We also use the
// 'supplant' function from the 'common' module for getting
// values into our string:
$("#timeline")[append ? "append" : "prepend"](common.supplant("\
<div class='timeline_item user_{screenname}'>
<div class='tweet_wrapper' tweetid='{tweetid}'>
<span class='tweet_thumb'>
<img src='{image}' width='48' height='48'/>
</span>
<span class='tweet_body'>
<span class='screenname'>{screenname}</span>
<span class='content'>{text}</span>
<span class='meta'>{meta}</span>
</span>
</div>
</div>
", {
tweetid: tweet.id,
text: tweet.text,
image: tweet.user.profile_image_url,
screenname: tweet.user.screen_name,
meta: date_string
}));
}
// Helper to fetch new tweets:
function fetch_tweets(params) {
try {
return T.call("statuses/home_timeline", params);
}
catch(e) {
// Are we still connected? If not, quit the app:
if (!T.isConnected())
throw "Disconnected";
// else try again in 10s:
// XXX should really have a back-off algo here
// XXX should examine exception object.
hold(10*1000);
return fetch_tweets(params);
}
}
// Function that periodically fetches new tweets from twitter and
// displays them:
function update_timeline_loop() {
// Run an endless loop:
while (true) {
// Fetch tweets from twitter:
var timeline = fetch_tweets({
include_entities: true,
count: 30,
since_id: $(".tweet_wrapper:first").attr("tweetid")
});
if (timeline && timeline.length) {
// Prepend tweets to timeline:
for (var i = timeline.length-1, tweet; tweet = timeline[i]; --i)
showTweet(tweet, false);
}
waitfor {
// Twitter is rate-limited to ~150calls/h. Wait for 60 seconds
// until we fetch more tweets.
hold(60*1000);
}
or {
$(window).waitFor("settingschanged");
$("#timeline").empty();
}
}
}
// Helper that waits until the user scrolls to the bottom of the page:
function waitforScrollToBottom() {
do {
$(window).$scroll();
}
while ($(document).height()- $(window).height() - $(document).scrollTop() > 300)
}
// Runs a loop that waits for the user to scroll to the bottom, and
// then loads more tweets:
function show_more_loop() {
while (true) {
waitforScrollToBottom();
var timeline = null;
waitfor {
// show a cancel button:
$("#timeline").append("\
<div class='timeline_item loading_more'>
Loading more tweets... click here to cancel
</div>");
// wait for it to be clicked; if that happens before the request
// completes, the request will be cancelled, by virtue of the
// waitfor/or.
$(".timeline_item:last").$click();
}
or {
timeline = fetch_tweets({
count: 30,
max_id: $(".tweet_wrapper:last").attr("tweetid")-1
});
if (!timeline.length) return; // we've loaded all tweets there are
}
finally {
// remove the cancel button:
$(".timeline_item:last").remove();
}
if (timeline && timeline.length) {
// Append tweets to timeline:
for (var i = 0, tweet; tweet = timeline[i]; ++i)
showTweet(tweet, true);
}
}
}
// Runs an endless loop that checks for the 'tweet' button to be
// clicked and sends out a tweet if it is:
function tweet_loop() {
try {
$(".tweet_box").show();
while (true) {
tweeting_button.$click();
tweeting_button.attr("disabled", "disabled");
$("#tweeting_status").text("Tweeting...");
try {
var tweet = T.call("statuses/update", {status: status_el.val() });
status_el.val("");
showTweet(tweet);
setLatestTweet(tweet);
}
catch (e) {
alert("Error posting: " + e.response.error);
}
update_counter();
tweeting_button.removeAttr("disabled");
}
}
finally {
$(".tweet_box").hide();
}
}
// shows a signout button and blocks until it is clicked
function waitfor_signout() {
try {
// Show a signout button and wait for it to be clicked:
var signout = $("<a href='#'>Sign out of twitter</a>").appendTo("#logout");
var e = signout.$click();
e.returnValue = false;
e.preventDefault();
// Ok, signout button was clicked; sign out, hide button & clear timeline:
twttr.anywhere.signOut();
}
finally {
signout.remove();
$("#login").show();
}
}