-
Notifications
You must be signed in to change notification settings - Fork 0
/
denby.js
89 lines (78 loc) · 2.03 KB
/
denby.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
;(function() {
var // State
config = {},
account = {},
friends = [],
// Cache
users = {},
// Socket.io client
socket = new io.Socket(null, {
secure: (document.location.protocol == 'https:'),
port: document.location.port
}),
// JSON-RPC client
jsonrpc = new JSONRPC({
socket: socket
});
/*
* SERVER MESSAGE STREAM
*/
socket.on('message', function(message) {
// Our favourite messages are objects. Filter out the rest.
if ( typeof message !== 'object' ) {
console.log('UFO: ' + JSON.stringify(message,null,2));
// JSON-RPC from server
} else if ( 'jsonrpc' in message && message.jsonrpc === '2.0' ) {
jsonrpc.receive(message);
// Denby protocol messages, including configuration
} else if ( 'DENBY' in message ) {
if ( 'CONFIG' in message.DENBY ) {
$.publish('/config', [message.DENBY.CONFIG]);
} else {
console.log('DENBY: ' + JSON.stringify(message.DENBY,null,2));
}
// Authentication failure handling
} else if ( 'AUTHFAIL' in message ) {
$.publish('/authfail', message.AUTHFAIL);
socket.disconnect();
// Everything else, including the user stream
// FIXME: Should web be doing caching and since_id tracking HERE?
} else {
if ( 'friends' in message ) {
friends = message.friends;
console.log('FRIENDS: ' + friends.length);
} else {
$.publish('/twitter', [message]);
}
}
});
/*
* CLIENT PUBSUB EVENTS
*/
$.subscribe('/twitter/account', function(update) {
if ( update && 'screen_name' in update ) {
if ( update.status ) delete update.status;
account = update;
}
});
var Denby = {
account: function() {
return account;
},
isFriend: function(id) {
return (friends.indexOf(id) >= 0);
},
connect: function(message) {
socket.on('connect', function connectmessage() {
if ( message ) socket.send(message);
socket.removeEvent('connect', connectmessage);
});
socket.connect();
},
send: function() {
jsonrpc.send.apply(jsonrpc, arguments);
},
version: '0.1.0'
};
return (window.Denby = Denby);
})();