-
Notifications
You must be signed in to change notification settings - Fork 3
/
syndicate.js
184 lines (163 loc) · 4.77 KB
/
syndicate.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
var sockethubClient;
var retryTimeout;
function init_sockethub(cfg){
if(!cfg)
return;
clearInterval(retryTimeout);
var sc;
console.log('registering at sockethub',cfg)
function register(){
if(sc){
sc.disconnect();
}
sc = SockethubClient.connect(cfg);
sc.on('registered', function(resp){
console.log('socketHub registration done!')
clearInterval(retryTimeout);
dove_it = document.getElementById('dove_it').dataset;
remove_class(sockethub_widget,'offline');
remove_class(sockethub_widget, 'expanded');
sockethubClient = sc;
})
sc.on('disconnected', function(resp){
console.log(resp);
add_class(sockethub_widget, 'offline');
add_class(dove_widget, 'offline');
})
sc.on('registration-failed', function(resp){
console.error('socketHub registration failed', resp)
})
}
retryTimeout = setInterval(register, 32127);
register();
options.syndicate = 'true'
push_state(options);
return sc;
}
var update_timeout
function process_twitter_message(message){
if(message.verb == 'post'){
remoteStorage.profile.load().then(function(profile){
var data = {
screenname : profile.screenname,
fullname : message.actor.name,
text : message.object.text,
date : ( new Date(message.object.date) ).getTime(),
avatar : message.actor.image,
twitter_id : message.object.id.toString()
}
console.log('reciving post via fetch : ', data);
remoteStorage.microblog.store(data);
clearTimeout(update_timeout) // update runs once after 10 seconds reciving no post
update_timeout = setTimeout(function(){
remoteStorage.microblog.update(data.screenname)
}, 10000)
})
}
}
function process_response(message) {
//console.log('SH received message', message);
if(message.platform == 'twitter')
process_twitter_message(message);
}
function sockethub_eventlisteners(){
sockethubClient.on( 'message', process_response);
sockethubClient.on('unexpected-response', function(resp){
console.log('UNEXPECTED-RESPONSE ', resp, arguments);
})
remoteStorage.profile.load().then(function(profile){
if(!profile)
sockethubClient.on('message', grab_profile)
})
}
function set_twitter_credentials(cfg){
return sockethubClient.set('twitter',
{ credentials : { me : cfg } }
).then(function (resp) {
if(resp.status != true)
throw(resp);
console.log('successfully set credentials for twitter account', resp);
remove_class(dove_widget, 'offline');
forEach(document.getElementsByClassName('dove'), function(el){
el.classList.remove('disabled');
})
}).then(undefined, function (err) {
console.log('error sending credentials for twitter :( ', err);
} );
}
function syndicate_to_twitter(post){
console.log('syndication in progress',post)
return sockethubClient.sendObject(
{
platform: 'twitter',
verb: 'post',
actor: { address: 'me' },
object: {
text: post.text
},
target : [{ address : 'a@b.c'}]
}
).then(function (response) {
console.log('post sucessful, heres the response: ', response);
setTimeout(fetch_tweets,1000);
}, function (err) {
console.log('oh no! ', err);
});
}
function fetch_tweets(feed)
{
if(!(feed instanceof String) )
feed = 'user'
var obj = {
platform : 'twitter',
verb : 'fetch',
actor : { address : 'me' },
target : [ { address : feed }],
poll : true
}
console.log(obj);
return sockethubClient.sendObject(obj).then(function(response) {
console.log('Here\'s the response of fetch : ',response)
}, function(e){
console.log('oh noooo!', e)
})
}
function grab_profile (message){
if(message.platform == 'twitter' && message.verb == 'post'){
remoteStorage.profile.load().then(function(profile){
if(!profile){
profile = {
screenname : message.actor.address,
name : message.actor.name,
profile_image_url : message.actor.image
}
console.log("grabed a profile from your tweets", profile)
remoteStorage.profile.save(profile);
}
})
}
}
function rs_init_syndication(){
return remoteStorage['credentials-sockethub'].get('profile').then(
function(cfg){
var sc = init_sockethub(cfg);
if(!sc)
return;
sc.on('registered', function(){
sockethub_eventlisteners();
remoteStorage['credentials-twitter'].get('profile').then(
function(twitter_cfg){
set_twitter_credentials(twitter_cfg)
} )
} )
} )
}
function init_syndication(form){
sc = init_sockethub(gui_sh_cfg(form));
if(!sc)
return;
sc.on('registered', function(){
sockethub_eventlisteners();
set_twitter_credentials(gui_twitter_cfg())
})
}