-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremote.js
313 lines (272 loc) · 8.4 KB
/
remote.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
var rs_scope = {
'profile':'rw',
'credentials-twitter': 'rw',
'credentials-sockethub': 'rw',
//'credentials-facebook' : 'rw',
'microblog':'rw'
}
var sockethub_widget;
var dove_widget;
function init_remotestorage(){
//remoteStorage = new RemoteStorage;
remoteStorage.displayWidget();
for(var i in rs_scope) {
remoteStorage.access.claim(i, rs_scope[i] );
}
remoteStorage.caching.reset();
remoteStorage.caching.enable('/public/profile/');
//remoteStorage.caching.enable('/public/microblog/microposts/');
// only the users path should be synced
remoteStorage.caching.enable('/credentials-twitter/')
remoteStorage.caching.enable('/credentials-sockethub/')
//remoteStorage.caching.enable('/credentials-facebook/')
document.getElementById('login').style.display = 'none';
remoteStorage.on('ready', rs_on_ready );
remoteStorage.on('disconnect', rs_on_disconnect);
sockethub_widget = document.getElementById('sockethub-widget')
dove_widget = document.getElementById('dove-widget')
options.me = 'true';
push_state(options);
remoteStorage.onChange('/public/microblog/microposts/', function(resp){
console.log("RS processing onchang : ", resp);
var item = undefined;
if( !resp.newValue && !resp.oldValue ){
console.log("weired things happen")
return;
}
if (typeof(resp.newValue) == 'undefined'
&& ( item = post_by_id(resp.oldValue.post_id) ) ) {
console.log("DELETE POST");
gui_delete_post(item);
} else if(!post_by_id(resp.newValue.post_id)
&& typeof(resp.oldValue) == 'undefined' ) {
console.log("NEW POST");
new_post(resp.newValue);
} else if(resp.oldValue && resp.newValue
&& resp.oldValue != resp.newValue
&& ( item = post_by_id(resp.oldValue.post_id) )){
console.log("UPDATE POST");
item.fill_post(undefined,resp.newValue)
} else {
console.log("uncaught onchange event : SHOULD NEVER HAPPEN please file a bug report")
}
})
remoteStorage.onChange('/public/profile/me', function(resp) {
console.log("profile.onchange : ", resp);
//console.log("UPDATEING PROFILE : ", resp);
if(resp.newValue != resp.oldValue){
var me = resp.newValue;
options.base_url = remoteStorage.remote.href + '/public';
set_profile(me);
}
})
remoteStorage.onChange('/credentials-twitter/profile',function(resp){
var cfg = resp.newValue
if(cfg) {
console.log('setting twitter in gui')
var item = f(dove_widget,'expandable');
['consumer_key','consumer_secret',
'access_token', 'access_token_secret'].forEach(
function(key){
if(item[key])
item[key].value = cfg[key];
})
if(sockethubClient){
set_twitter_credentials(cfg);
}
}
})
remoteStorage.onChange('/credentials-sockethub/profile', function(resp) {
var cfg = resp.newValue;
if(cfg) {
var item = f(sockethub_widget,'expandable');
['host','port'].forEach(
function(key){
if(item[key])
item[key].value = cfg[key];
})
item.secret.value = cfg.register.secret;
item.ssl.checked = cfg.ssl
}
} )
}
function rs_on_disconnect() {
clear_all_posts()
forEach(document.getElementsByClassName('remote'), function(el){
el.style.display = 'none'
});
forEach(document.getElementsByClassName('edit_profile'), function(el){
el.style.display = 'none'
});
}
function rs_on_ready(){
console.log('!!! on ready !!!')
clear_all_posts()
options.base_url = remoteStorage.remote.href + '/public';
remoteStorage.credentialsTwitter.get('profile').then(gui_set_twitter);
remoteStorage.credentialsSockethub.get('profile').then(gui_set_sh);
remoteStorage.profile.load().then(set_profile).then(
function(profile){
if(!profile)
return;
remoteStorage.caching.enable('/public/microblog/microposts/'+profile.screenname+'/');
remoteStorage.microblog.list(profile.screenname).then(function(l){
console.log("list() said :",l)
return;
l.forEach(function(post){
if(!post_by_id(post.post_id)){
//console.log("post does not exists", post);
new_post(post);
} else;
//console.log("post does exist",post);
});
}, function(e){
console.log(e)
});
}
)
forEach(document.getElementsByClassName('remote'), function(el){
el.style.display = 'block'
})
}
function store_post(data){
return remoteStorage.microblog.post(data).then(function(){
console.log('storing this one', data);
}, function(e){
console.error("something went wrong ... ",e,data)
});
}
function create_post(){
var data = new Object;
data.text = document.forms.micropost.text.value
function post_it(data){
console.log(data);
if(sockethubClient && dove_it['yes'] )
syndicate_to_twitter(data);
store_post(data);
}
remoteStorage.profile.load().then(
function(me){
if(!me)
throw("profile not found")
data.screenname = me.screenname
data.fullname = me.name;
data.avatar = me.profile_image_url
post_it(data);
}).then( undefined, function(e){
console.log("unable to fill in your profile info : ", e)
post_it(data);
} )
}
// delete and restore posts
function gui_delete_post(post){
var div = post.div;
add_class(div,'deleted');
f(div, 'delete').onclick = function(){
restore_post(post);
}
}
function delete_post(data){
console.log("removing ", data);
remoteStorage.microblog.remove(data);
}
function restore_post(post){
var data = post.data;
var div = post.div;
remoteStorage.microblog.post(data).then(
function() {
remove_class(div, 'deleted');
f(div,'delete').onclick = function(){
delete_post(data);
}
}
)
}
// profile edit buttons
function done_editing_profile_callback(event){
event.preventDefault();
done_profile(this);
}
function edit_profile_callback(event){
event.preventDefault();
edit_profile(this);
}
function edit_profile(profile_data){
['screenname','name','description','location', 'homepage',
'profile_image_url' ].forEach( function(key){
var i = profile_div[key];
i.value = profile_data[key];
i.style.display = 'inline';
})
var t = f(profile_div, 'edit');
t.innerHTML = 'done';
t.onclick = done_editing_profile_callback.bind(profile_data);
}
function done_profile(profile_data){
['screenname','name','description','location', 'homepage',
'profile_image_url' ].forEach(
function(key){
var i = profile_div[key];
profile_data[key] = i.value ;
i.style.display = 'none';
}
)
remoteStorage.profile.save(profile_data);
var t = f(profile_div, 'edit');
t.innerHTML = 'edit';
t.onclick = edit_profile_callback.bind(profile_data);
}
//credentials
function gui_sh_cfg(form){
if(typeof(form) === 'undefined')
form = f(sockethub_widget,'expandable');
return { host : form.host.value,
port : form.port.value,
ssl : form.ssl.checked,
register : {
secret : form.secret.value
}
}
}
function gui_set_sh(cfg){
if(!cfg)
return;
var form = f(sockethub_widget, 'expandable')
form.host.value = cfg.host;
form.port.value = cfg.port;
form.ssl.checked = cfg.ssl;
form.secret.value = cfg.register.secret;
return cfg;
}
function gui_twitter_cfg(form){
if(typeof(form) === 'undefined')
form = f(dove_widget,'expandable');
return {
consumer_key : form.consumer_key.value,
consumer_secret : form.consumer_secret.value,
access_token : form.access_token.value,
access_token_secret : form.access_token_secret.value
}
}
function gui_set_twitter(cfg){
if(!cfg)
return;
var form = f(dove_widget, 'expandable')
form.consumer_key.value = cfg.consumer_key;
form.consumer_secret.value = cfg.consumer_secret;
form.access_token.value = cfg.access_token;
form.access_token_secret.value = cfg.access_token_secret;
return cfg;
}
function store_sh_credentials(form){
var data = gui_sh_cfg(form);
remoteStorage['credentials-sockethub'].store('profile', data)
}
function store_twitter_credentials( form){
var data = gui_twitter_cfg(form);
remoteStorage['credentials-twitter'].store('profile', data);
if(sockethubClient){
console.log("set_twitter_credential",data)
set_twitter_credentials(data);
}
}