-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
15134 lines (14441 loc) · 476 KB
/
app.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var app_version=1;
var app_protocol='V';//V for Voice :)
var events_protocol='VE';//Voice Events
var storage_prefix='viz_voice_';
var debug=false;
var pwa=false;
/* + start broadcast channel feature */
//broadcast channel for communication between tabs
//main pid is tab with focus
//if no tabs with focus then last active tab is main
//if no tabs with focus and no last active tab then largest pid is main
//only main pid execute all timers (others tabs stop timers)
//if main pid closed then next largest pid is main
var bc=new BroadcastChannel('viz_voice');
var pid=0;
var max_pid=0;
var set_pid_timer=false;
var set_main_pid_timer=false;
var pid_active=true;
function check_pid_active(e){
pid_active=(e.type==='focus');
console.log('check_pid_active <<< ',pid_active);
if(pid_active){
if(!main_pid){//not was main_pid (need start timers)
start_timers();
}
main_pid=true;
if(0!=pid){//if pid==0 then we are not ready to send messages (will be main after set new pid)
console.log('MAIN PID !!!',pid);
bc.postMessage({type:'main',pid:pid});
}
}
else{
if(!main_pid){//extremely rare case when we are not main_pid and not last active
stop_timers();
main_pid=false;
bc.postMessage({type:'who_main',pid:pid});
}
}
};
window.onfocus=check_pid_active;
window.onblur=check_pid_active;
var main_pid=false;
var need_find_main_pid=false;
function set_main_pid(){
if(pid>max_pid){
if(!main_pid){//not was main_pid (need start timers)
start_timers();
}
main_pid=true;
console.log('MAIN PID !!!',pid);
bc.postMessage({type:'main',pid:pid});
}
else{
stop_timers();
main_pid=false;
}
}
function set_pid(){
pid=1+max_pid;
main_pid=true;
console.log('New PID >>> ',pid);
bc.postMessage({type:'pong',pid:pid});
bc.postMessage({type:'main',pid:pid});
}
window.onbeforeunload=function(e){
if(main_pid){//only for main_pid, need to find new main_pid
pid=0;
bc.postMessage({type:'who_main',pid:pid});
}
};
bc.onmessage=function(e){
console.log('BroadcastChannel >>> ',e.data);
if('who_main'==e.data.type){
need_find_main_pid=true;
max_pid=0;
if(0!=e.data.pid){
if(max_pid<e.data.pid){
max_pid=e.data.pid;
}
}
bc.postMessage({type:'pong',pid:pid});
clearTimeout(set_main_pid_timer);
set_main_pid_timer=setTimeout(function(){
set_main_pid();
},100);
}
if('ping'==e.data.type){
bc.postMessage({type:'pong',pid:pid});
}
if('main'==e.data.type){
main_pid=false;
stop_timers();
console.log('main_pid <<< ',main_pid);
console.log('new main_pid <<< ',e.data.pid);
}
if('pong'==e.data.type){
if(0!=e.data.pid){
if(max_pid<e.data.pid){
max_pid=e.data.pid;
}
}
if(0==pid){
clearTimeout(set_pid_timer);
set_pid_timer=setTimeout(function(){
set_pid();
},50);
}
if(need_find_main_pid){
clearTimeout(set_main_pid_timer);
set_main_pid_timer=setTimeout(function(){
set_main_pid();
},50);
}
}
if('notifications_count'==e.data.type){
let counter_notifications=$('.counter-notifications');
let count=e.data.count;
if(0==count){
counter_notifications.removeClass('show');
}
else{
counter_notifications.html(count);
counter_notifications.addClass('show');
}
}
if('feed_count'==e.data.type){
let view=$('.view[data-level="0"]');
let new_objects=view.find('.objects .new-objects');
let counter_feed=$('.counter-feed');
let items=e.data.count;
new_objects.data('items',items);
if(0==items){
new_objects.removeClass('show');
counter_feed.removeClass('show');
}
else{
new_objects.html(ltmp(ltmp_arr.feed_new_objects,{items:items}));
new_objects.addClass('show');
counter_feed.html(items);
counter_feed.addClass('show');
}
}
};
//set pid after 100ms from last pong if no pong received
set_pid_timer=setTimeout(function(){
set_pid();
},100);
bc.postMessage({type:'ping',pid:pid});
/* - end broadcast channel feature */
/* + start check img for complete status and naturalHeight */
//many images can be deleted from web
//we need check status of image and change src if image not loaded
function check_images(view){
view=view||$(document);
view.find('img').each(function(){
let img=$(this);
if(img.hasClass('img-checked')){
return;
}
img.addClass('img-checked');
if(img[0].complete){
if(img[0].naturalHeight==0){
img.attr('src',ltmp_global.unavailable_image);
$(img).css('align-self','center');
}
}
else{
img.on('load',function(){
img.addClass('img-checked');
if(img[0].naturalHeight==0){
img.attr('src',ltmp_global.unavailable_image);
$(img).css('align-self','center');
}
});
img.on('error',function(){
img.addClass('img-checked');
img.attr('src',ltmp_global.unavailable_image);
$(img).css('align-self','center');
});
}
});
}
/* - end check img for complete status and naturalHeight */
if(window.matchMedia('(display-mode: standalone)').matches){
pwa=true;
}
if(window.matchMedia('(display-mode: fullscreen)').matches){
pwa=true;
}
var current_theme='';
var whitelabel_account='';//main whitelabel account to redirect
var whitelabel_accounts=[whitelabel_account];//always subscribed to whitelabels accounts
var whitelabel_deep=10;//count of max activity loaded for feed on startup
var whitelabel_redirect=false;//redirect to whitelabel profile on feed loadup
var whitelabel_app_title='The Free Speech Project';
var whitelabel_copy_link=false;//rewrite viz:// in copy link action for share content outside
var whitelabel_logo=false;
var whitelabel_init=false;
var sync_cloud_url='https://readdle.me/cloud/';
var sync_cloud_domain=false;
if(false!==sync_cloud_url){
sync_cloud_domain=sync_cloud_url.split('://')[1].split('/')[0];
}
var sync_cloud_activity=0;
var sync_cloud_update=0;
if(null!=localStorage.getItem(storage_prefix+'sync_cloud_activity')){
sync_cloud_activity=parseInt(localStorage.getItem(storage_prefix+'sync_cloud_activity'));
if(isNaN(sync_cloud_activity)){
localStorage.removeItem(storage_prefix+'sync_cloud_activity');
}
}
if(null!=localStorage.getItem(storage_prefix+'sync_cloud_update')){
sync_cloud_update=parseInt(localStorage.getItem(storage_prefix+'sync_cloud_update'));
if(isNaN(sync_cloud_update)){
localStorage.removeItem(storage_prefix+'sync_cloud_update');
}
}
var preview_url='https://readdle.me/preview/';
var account_pattern=/@[a-z0-9\-\.]*/g;
var block_pattern=/\/([0-9]+)/g;
var install_event;
window.addEventListener('beforeinstallprompt',(e)=>{
e.preventDefault();
install_event=e;
});
if('file://'!=document.location.origin){
if('serviceWorker' in navigator){
navigator.serviceWorker.register('/sw.js',{scope:'/',registrationStrategy:'registerImmediately'}).then(()=>{
console.log('Service Worker Registered');
});
}
}
if(typeof String.prototype.replaceAll == "undefined") {
String.prototype.replaceAll = function(match, replace){
return this.replace(new RegExp(match, 'g'), () => replace);
}
}
function auth_signature_data(domain,action,account,authority,nonce){
return domain+':'+action+':'+account+':'+authority+':'+(new Date().getTime() / 1000 | 0)+':'+nonce;
}
function auth_signature_check(hex){
let byte=hex.substring(0,2);
if('1f'==byte){
return true;
}
if('20'==byte){
return true;
}
return false;
}
let passwordless_auth_sessions=[];
function passwordless_auth(account,regular_key,ignore_session){
ignore_session=typeof ignore_session==='undefined'?false:ignore_session;
let unixtime=new Date().getTime() / 1000 | 0;//unixtime
let session=false;
if(!ignore_session){
for(let i in passwordless_auth_sessions){
if(passwordless_auth_sessions[i]['account']==account){
if(passwordless_auth_sessions[i]['expire']>unixtime){
session=passwordless_auth_sessions[i]['session'];
}
else{//clear expired sessions
passwordless_auth_sessions.splice(i,1);
}
}
}
}
if(false===session){
var nonce=0;
var data='';
var signature='';
while(!auth_signature_check(signature)){
data=auth_signature_data(sync_cloud_domain,'auth',account,'regular',nonce);
signature=viz.auth.signature.sign(data,regular_key).toHex();
nonce++;
}
clearTimeout(passwordless_auth_session_update_timer);
passwordless_auth_session_update_timer=setTimeout(function(){
passwordless_auth_session_update();
},100);
return {data,signature};
}
else{
return {session};
}
}
var passwordless_auth_session_update_interval=10*60*1000-10;//10min-10sec
var passwordless_auth_session_update_timer=0;
function passwordless_auth_session_update(callback){
if(typeof callback==='undefined'){
callback=function(){};
}
if(''==current_user){
return;
}
if(typeof users[current_user] === 'undefined'){
return;
}
if(typeof users[current_user].regular_key === 'undefined'){
return;
}
if(false===sync_cloud_domain){
return;
}
let xhr = new XMLHttpRequest();
xhr.timeout=5000;
xhr.overrideMimeType('text/plain');
xhr.open('POST',sync_cloud_url);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout = function() {
console.log('passwordless_auth_session_update timeout',sync_cloud_url);
};
xhr.onreadystatechange = function() {
if(4==xhr.readyState && 200==xhr.status){
try{
let json=JSON.parse(xhr.response);
//console.log('passwordless_auth_session_update response',json);
if(typeof json.session !== 'undefined'){
if(typeof json.expire !== 'undefined'){
for(let i in passwordless_auth_sessions){//clear old sessions
if(passwordless_auth_sessions[i]['account']==current_user){
passwordless_auth_sessions.splice(i,1);
}
}
passwordless_auth_sessions.push({account:current_user,session:json.session,expire:json.expire})
}
}
clearTimeout(passwordless_auth_session_update_timer);
passwordless_auth_session_update_timer=setTimeout(function(){
passwordless_auth_session_update();
},passwordless_auth_session_update_interval);
callback(json.session);
}
catch(e){
console.log('passwordless_auth_session_update response json error',xhr.response,e);
callback(false);
}
}
if(4==xhr.readyState && 200!=xhr.status){
callback(false);
}
};
let auth_data=passwordless_auth(current_user,users[current_user].regular_key,true);
auth_data.action='session';
xhr.send(JSON.stringify(auth_data));
clearTimeout(passwordless_auth_session_update_timer);
passwordless_auth_session_update_timer=setTimeout(function(){
passwordless_auth_session_update();
},passwordless_auth_session_update_interval);
}
var check_sync_cloud_activity_interval=5*60*1000;
var check_sync_cloud_activity_timer=0;
function check_sync_cloud_activity(callback){
if(typeof callback==='undefined'){
callback=function(){};
}
if(!settings.sync_cloud){
return;
}
if(''==current_user){
return;
}
if(typeof users[current_user] === 'undefined'){
return;
}
if(typeof users[current_user].regular_key === 'undefined'){
return;
}
if(false===sync_cloud_domain){
return;
}
let xhr = new XMLHttpRequest();
xhr.timeout=5000;
xhr.overrideMimeType('text/plain');
xhr.open('POST',sync_cloud_url);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout = function() {
console.log('check_sync_cloud_activity timeout',sync_cloud_url);
};
xhr.onreadystatechange = function() {
if(4==xhr.readyState && 200==xhr.status){
try{
let json=JSON.parse(xhr.response);
console.log('check_sync_cloud_activity response',json);
if(json.activity>=sync_cloud_activity){
if(json.update>sync_cloud_update){
load_sync_cloud_get_updates();
callback(true);
}
}
clearTimeout(check_sync_cloud_activity_timer);
check_sync_cloud_activity_timer=setTimeout(function(){
check_sync_cloud_activity();
},check_sync_cloud_activity_interval);
}
catch(e){
console.log('check_sync_cloud_activity response json error',xhr.response,e);
callback(false);
}
}
if(4==xhr.readyState && 200!=xhr.status){
callback(false);
}
};
let auth_data=passwordless_auth(current_user,users[current_user].regular_key);
auth_data.action='activity';
xhr.send(JSON.stringify(auth_data));
clearTimeout(check_sync_cloud_activity_timer);
check_sync_cloud_activity_timer=setTimeout(function(){
check_sync_cloud_activity();
},check_sync_cloud_activity_interval);
}
var sync_cloud_updates_queue=[];
var sync_cloud_update_worker_busy=false;
var sync_cloud_update_worker_timer=0;
var sync_cloud_update_worker_interval=1*1000;
function sync_cloud_update_worker(){
if(!sync_cloud_update_worker_busy){
sync_cloud_update_worker_busy=true;
let types_id={
1:'backup',
2:'subscribe',
3:'reset',
4:'ignore',
5:'pin_hashtag',
6:'reset_hashtag',
7:'ignore_hashtag',
};
let update=sync_cloud_updates_queue.shift();
if(typeof update !== 'undefined'){
let type=types_id[update.type];
if('backup'==type){
import_cloud(update.value,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('subscribe'==type){
subscribe_update(update.value,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('reset'==type){
reset_update(update.value,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('ignore'==type){
ignore_update(update.value,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('pin_hashtag'==type){
hashtag_update(update.value,1,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
render_right_addon();
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('reset_hashtag'==type){
hashtag_update(update.value,0,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
render_right_addon();
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else
if('ignore_hashtag'==type){
hashtag_update(update.value,2,function(result){
if(false===result){
console.log('sync_cloud_update_worker error with update',update);
}
else{
render_right_addon();
console.log('sync_cloud_update_worker OK with update',update);
}
sync_cloud_update_worker_busy=false;
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
});
}
else{
console.log('sync_cloud_update_worker MISS with update',update);
sync_cloud_update_worker_busy=false;
}
}
else{
sync_cloud_update_worker_busy=false;
}
}
}
var load_sync_cloud_update_busy=false;
function load_sync_cloud_get_updates(){
if(!load_sync_cloud_update_busy){
load_sync_cloud_update_busy=true;
let xhr = new XMLHttpRequest();
xhr.timeout=5000;
xhr.overrideMimeType('text/plain');
xhr.open('POST',sync_cloud_url);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout = function() {
console.log('load_sync_cloud_update timeout',sync_cloud_url);
};
xhr.onreadystatechange = function() {
if(4==xhr.readyState && 200==xhr.status){
try{
let json=JSON.parse(xhr.response);
console.log('load_sync_cloud_update response',json);
if(typeof json.result !=='undefined'){
for(let i in json.result){
let update=json.result[i];
if(update.time>=sync_cloud_activity){
if(update.id>sync_cloud_update){
sync_cloud_updates_queue.push({type:update.type,value:update.value});
sync_cloud_activity=update.time;
sync_cloud_update=update.id;
localStorage.setItem(storage_prefix+'sync_cloud_activity',sync_cloud_activity);
localStorage.setItem(storage_prefix+'sync_cloud_update',sync_cloud_update);
}
}
}
clearTimeout(sync_cloud_update_worker_timer);
sync_cloud_update_worker_timer=setTimeout(function(){
sync_cloud_update_worker();
},sync_cloud_update_worker_interval);
}
}
catch(e){
console.log('load_sync_cloud_update response json error',xhr.response,e);
}
load_sync_cloud_update_busy=false;
}
};
let auth_data=passwordless_auth(current_user,users[current_user].regular_key);
auth_data.action='get_updates';
auth_data.activity=sync_cloud_activity;
auth_data.update=sync_cloud_update;
xhr.send(JSON.stringify(auth_data));
}
}
function sync_cloud_put_update(type_str,value,callback){
if(typeof callback==='undefined'){
callback=function(){};
}
let types_arr={
'backup':1,
'subscribe':2,
'reset':3,
'ignore':4,
'pin_hashtag':5,
'reset_hashtag':6,
'ignore_hashtag':7,
};
if(typeof types_arr[type_str] !== 'undefined'){
let xhr = new XMLHttpRequest();
xhr.timeout=5000;
xhr.overrideMimeType('text/plain');
xhr.open('POST',sync_cloud_url);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout = function() {
console.log('sync_cloud_put_update timeout',sync_cloud_url);
};
xhr.onreadystatechange = function() {
if(4==xhr.readyState && 200==xhr.status){
try{
let json=JSON.parse(xhr.response);
console.log('sync_cloud_put_update response',json);
if(typeof json.result !=='undefined'){
sync_cloud_activity=json.result.activity;
sync_cloud_update=json.result.update;
localStorage.setItem(storage_prefix+'sync_cloud_activity',sync_cloud_activity);
localStorage.setItem(storage_prefix+'sync_cloud_update',sync_cloud_update);
}
if(false!==json.result){
callback(true);
}
else{
callback(false);
}
}
catch(e){
console.log('sync_cloud_put_update response json error',xhr.response,e);
callback(false);
}
}
if(4==xhr.readyState && 200!=xhr.status){
console.log('sync_cloud_put_update response status error',xhr.status);
callback(false);
}
};
let auth_data=passwordless_auth(current_user,users[current_user].regular_key);
auth_data.action='put_update';
auth_data.type=types_arr[type_str];
auth_data.value=value;
xhr.send(JSON.stringify(auth_data));
}
else{
callback(false);
}
}
function compare_account_name(a,b) {
if (a.account < b.account)
return -1;
if (a.account > b.account)
return 1;
return 0;
}
var mute_notifications=false;
if(null!=localStorage.getItem(storage_prefix+'mute_notifications')){
mute_notifications=parseInt(localStorage.getItem(storage_prefix+'mute_notifications'));
if(isNaN(mute_notifications)){
localStorage.removeItem(storage_prefix+'mute_notifications');
}
else{
setTimeout(function(){
localStorage.removeItem(storage_prefix+'mute_notifications');
mute_notifications=false;
},mute_notifications*1000);
}
}
var is_safari=navigator.vendor && navigator.vendor.indexOf('Apple') > -1 &&
navigator.userAgent &&
navigator.userAgent.indexOf('CriOS') == -1 &&
navigator.userAgent.indexOf('FxiOS') == -1;
var is_firefox=navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
var is_chrome_ios=navigator.userAgent.indexOf('CriOS') > -1;
var is_ucbrowser=navigator.userAgent.indexOf('UCBrowser') > -1;
var is_samsungbrowser=navigator.userAgent.indexOf('SamsungBrowser') > -1;
var is_macintosh=navigator.userAgent.indexOf('Macintosh') > -1;
var trx_need_commit=false;
var api_gates=[
'https://api.viz.world/',
'https://node.viz.cx/',
'https://viz.lexai.host/',
];
var select_best_api_gate=true;
var default_api_gate=api_gates[0];
var best_gate=-1;
var best_gate_latency=-1;
if(null!=localStorage.getItem(storage_prefix+'api_gate')){
default_api_gate=localStorage.getItem(storage_prefix+'api_gate');
select_best_api_gate=false;
}
var api_gate=default_api_gate;
console.log('using default node',default_api_gate);
viz.config.set('websocket',default_api_gate);
var dgp={};
if(select_best_api_gate){
select_best_gate();
}
else{
update_dgp(true);
}
function update_api_gate(value=false,latency){
console.log('new dgp:',dgp);
if(false==value){
api_gate=api_gates[best_gate];
}
else{
api_gate=value;
}
console.log('using new node',api_gate,'latency: ',(typeof latency !== 'undefined')?latency:best_gate_latency);
viz.config.set('websocket',api_gate);
localStorage.setItem(storage_prefix+'api_gate',api_gate);
update_dgp(true);
}
var check_api_timer=0;
function check_api_gate(node,callback){
if(''!=node){
let protocol='none';
let node_protocol=node.substring(0,node.indexOf(':'));
if('http'==node_protocol||'https'==node_protocol){
protocol='http';
}
if('ws'==node_protocol||'wss'==node_protocol){
protocol='websocket';
}
if('websocket'==protocol){
callback(ltmp_arr.node_request,true);
let socket=new WebSocket(node);
clearTimeout(check_api_timer);
check_api_timer=setTimeout(function(){
callback(ltmp_arr.node_not_respond);
socket.close();
},3000);
let latency_start=new Date().getTime();
let latency=-1;
socket.onmessage=function(event){
latency=new Date().getTime() - latency_start;
//console.log(event);
try{
json=JSON.parse(event.data);
if((typeof json.result!='undefined')&&(typeof json.result.head_block_number!='undefined')){
update_api_gate(node,latency);
callback(false,ltmp_arr.node_success);
}
else{
callback(ltmp_arr.node_wrong_response);
console.log(json);
}
}
catch(err){
callback(ltmp_arr.node_wrong_response);
console.log(err);
}
socket.close();
}
socket.onerror=function(){
callback(ltmp_arr.node_not_respond);
};
socket.onopen=function(){
socket.send('{"id":1,"method":"call","jsonrpc":"2.0","params":["database_api","get_dynamic_global_properties",[]]}');
};
}
if('http'==protocol){
callback(ltmp_arr.node_request,true);
let xhr=new XMLHttpRequest();
xhr.timeout=3000;
let latency_start=new Date().getTime();
let latency=-1;
xhr.overrideMimeType('text/plain');
xhr.open('POST',node);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout=function(){
callback(ltmp_arr.node_not_respond);
};
xhr.onerror=function(){
callback(ltmp_arr.node_not_respond);
};
xhr.onreadystatechange=function(){
if(4==xhr.readyState && 200==xhr.status){
latency=new Date().getTime() - latency_start;
try{
json=JSON.parse(xhr.responseText);
if((typeof json.result!='undefined')&&(typeof json.result.head_block_number!='undefined')){
update_api_gate(node,latency);
callback(false,ltmp_arr.node_success);
}
else{
callback(ltmp_arr.node_wrong_response);
console.log(json);
console.log(xhr);
}
}
catch(err){
callback(ltmp_arr.node_wrong_response);
console.log(err);
}
}
}
xhr.send('{"id":1,"method":"call","jsonrpc":"2.0","params":["database_api","get_dynamic_global_properties",[]]}');
}
if('none'==protocol){
callback(ltmp_arr.node_protocol_error);
}
}
else{
callback(ltmp_arr.node_empty_error);
}
}
function select_best_gate(){
for(i in api_gates){
let current_gate=i;
let current_gate_url=api_gates[i];
let latency_start=new Date().getTime();
let latency=-1;
let protocol='websocket';
let gate_protocol=current_gate_url.substring(0,current_gate_url.indexOf(':'));
if('http'==gate_protocol||'https'==gate_protocol){
protocol='http';
}
if('websocket'==protocol){
let socket = new WebSocket(current_gate_url);
socket.onmessage=function(event){
latency=new Date().getTime() - latency_start;
if(best_gate!=current_gate){
if((best_gate_latency>latency)||(best_gate==-1)){
try{
let json=JSON.parse(event.data);
dgp=json.result;
best_gate=current_gate;
best_gate_latency=latency;
update_api_gate();
}
catch(e){
console.log('select_best_gate node error',current_gate_url,e);
}
}
}
socket.close();
}
socket.onopen=function(){
socket.send('{"id":1,"method":"call","jsonrpc":"2.0","params":["database_api","get_dynamic_global_properties",[]]}');
};
}
if('http'==protocol){
let xhr = new XMLHttpRequest();
xhr.timeout=3000;
xhr.overrideMimeType('text/plain');
xhr.open('POST',current_gate_url);
xhr.setRequestHeader('accept','application/json, text/plain, */*');
xhr.setRequestHeader('content-type','application/json');
xhr.ontimeout = function() {
console.log('select_best_gate node timeout',current_gate_url);
};
xhr.onreadystatechange = function() {
if(4==xhr.readyState && 200==xhr.status){
latency=new Date().getTime() - latency_start;
console.log('check node',current_gate_url,'latency: ',latency);
if(best_gate!=current_gate){
if((best_gate_latency>latency)||(best_gate==-1)){
try{
let json=JSON.parse(xhr.response);
dgp=json.result;
best_gate=current_gate;
best_gate_latency=latency;
update_api_gate();
}
catch(e){
console.log('select_best_gate node error',current_gate_url,e);
}
}
}
}
};
xhr.send('{"id":1,"method":"call","jsonrpc":"2.0","params":["database_api","get_dynamic_global_properties",[]]}');
}
}
}
function gate_connection_status(){
let view=$('.view[data-level="'+level+'"]');
if(-1==path.indexOf('viz://')){//look in services views
let path_parts=path.split('/');
view=$('.view[data-path="'+path_parts[0]+'"]');
}
if(true===dgp_error){
if(0==view.find('.gate-connection-error').length){
view.find('.header').after(ltmp(ltmp_arr.gate_connection_error));
}
}
else{
$('.gate-connection-error').remove();
}
}
var dgp_timer=0;
var dgp_error=false;
function update_dgp(auto=false){
clearTimeout(dgp_timer);
viz.api.getDynamicGlobalProperties(function(err,response){
if(err){
dgp_error=true;
}
if(response){
dgp_error=false;
dgp=response;
console.log('new dgp:',dgp);
}
gate_connection_status();
});
setTimeout(function(){if(0==Object.keys(dgp).length){select_best_gate();}},5000);//5sec after request
if(auto){
clearTimeout(dgp_timer);
dgp_timer=setTimeout(function(){update_dgp(true);},120000);//2min
}
}
var users={};
var current_user='';
if(null!=localStorage.getItem(storage_prefix+'users')){
users=JSON.parse(localStorage.getItem(storage_prefix+'users'));
}
if(null!=localStorage.getItem(storage_prefix+'current_user')){
current_user=localStorage.getItem(storage_prefix+'current_user');
}
var default_settings={
feed_size:10000,
activity_size:0,
activity_period:5,
activity_deep:50,
user_profile_ttl:60,
user_cache_ttl:10,
object_cache_ttl:10,
preview_cache_ttl:7200,
feed_subscribe_text:true,
feed_subscribe_replies:false,
feed_subscribe_shares:true,
feed_subscribe_mentions:true,