-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.html
4349 lines (4328 loc) · 276 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script>
if ( window.matchMedia( '(prefers-color-scheme: dark)' ).media === 'not all' || window.location.href.includes( "darkmode=true" ) ) {
document.documentElement.style.display = 'none';
document.head.insertAdjacentHTML('beforeend', '<link rel="stylesheet" href="./styles/light.css" onload="document.documentElement.style.display = \'\'">',);
}
</script>
<link rel="stylesheet" href="./styles/dark.css?v=68" media="(prefers-color-scheme: dark)" />
<link rel="stylesheet" href="./styles/light.css?v=68" media="(prefers-color-scheme: light)"/>
<!-- The main stylesheet -->
<link rel="stylesheet" href="./styles/style.css?v=68" />
<link rel="shortcut icon" type="image/png" href="favicon.png">
<link rel="manifest" href="manifest.json">
<title>Anigma</title>
<script src="https://bitcoincore.tech/apps/bitcoinjs-ui/lib/bitcoinjs-lib.js"></script>
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script src="https://bundle.run/browserify-cipher@1.0.1"></script>
<script src="https://supertestnet.github.io/bitcoin-chess/js/qrcode.js"></script>
<script src="https://bundle.run/buffer@6.0.3"></script>
<script src="https://bundle.run/bech32@2.0.0"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>
<script src="translation.js?v=68"></script>
</head>
<body>
<!-- based on this: https://github.com/YunusEmreNalbant/css-chat-interface -->
<script>
function isBech32( string ) {
try {
var words = bech32.bech32.decode( string ).words;
return true;
} catch( e ) {
return false;
}
}
function pubkeyToNpub( hex ) {
return bech32.bech32.encode( "npub", bech32.bech32.toWords( buffer.Buffer.from( hex, "hex" ) ) );
}
function pubkeyFromNpub( npub ) {
return buffer.Buffer.from( bech32.bech32.fromWords( bech32.bech32.decode( npub ).words ) ).toString( "hex" );
}
function privkeyToNsec( hex ) {
return bech32.bech32.encode( "nsec", bech32.bech32.toWords( buffer.Buffer.from( hex, "hex" ) ) );
}
function privkeyFromNsec( nsec ) {
return buffer.Buffer.from( bech32.bech32.fromWords( bech32.bech32.decode( nsec ).words ) ).toString( "hex" );
}
</script>
<script>
admin = false;
if ( window.location.href.includes( "admin=true" ) ) {
var admin = true;
}
</script>
<script>
navigator.serviceWorker && navigator.serviceWorker.register( './sw.js' ).then( function( registration ) {
//console.log( 'Excellent, registered with scope: ', registration.scope );
});
sessionStorage[ "time-offset" ] = String( "0" );
</script>
<script>
function getData( url, apikey ) {
return new Promise( function( resolve, reject ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if ( this.readyState == 4 && this.status == 200 ) {
resolve( xhttp.responseText );
};
}
xhttp.open( "GET", url, true );
if ( apikey ) {
xhttp.setRequestHeader( "X-Api-Key", apikey );
}
xhttp.send();
});
}
</script>
<script>
function postJson( url, apikey, json ) {
return new Promise( function( resolve, reject ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if ( this.readyState == 4 && ( this.status >= 200 && this.status < 300 ) ) {
resolve( xhttp.responseText );
};
}
xhttp.open( "POST", url, true );
xhttp.setRequestHeader( "Content-type", "application/json" );
xhttp.setRequestHeader( "X-Api-Key", apikey );
xhttp.send ( json );
});
}
</script>
<script>
async function getBalance() {
if ( !wallet_url || wallet_url == "" ) {
return `${t('loading')}`;
}
var domain = wallet_url.substring( 0, wallet_url.indexOf( "/wallet" ) );
var url = domain + "/api/v1/wallet";
var winfo = await getData( url, readkey )
winfo = JSON.parse( winfo );
var balance = String( Math.floor( winfo[ "balance" ] / 1000 ) );
if ( document.getElementsByClassName( "manage-wallet-outer" )[ 0 ] && document.getElementsByClassName( "manage-wallet-outer" )[ 0 ].getElementsByClassName( "message" )[ 0 ] ) {
document.getElementsByClassName( "manage-wallet-outer" )[ 0 ].getElementsByClassName( "message" )[ 0 ].innerText = balance + " sats";
}
if ( document.getElementsByClassName( "message-header" )[ 0 ] && document.getElementsByClassName( "message-header" )[ 0 ].getElementsByClassName( "message" )[ 0 ] && document.getElementsByClassName( "message-header" )[ 0 ] && document.getElementsByClassName( "message-header" )[ 0 ].getElementsByClassName( "message" )[ 0 ].innerText == `${t('loading')}` ) {
document.getElementsByClassName( "message-header" )[ 0 ].getElementsByClassName( "message" )[ 0 ].innerText = String( balance ) + " sats";
}
if ( localStorage[ "wallet_balance" ] ) {
var old_wallet_balance = Number( localStorage[ "wallet_balance" ] );
} else {
localStorage[ "wallet_balance" ] = 0;
var old_wallet_balance = 0;
}
if ( !sessionStorage[ "wallet_checked" ] ) {
sessionStorage[ "wallet_checked" ] = "checked";
localStorage[ "wallet_balance" ] = String( balance );
}
if ( balance - old_wallet_balance > 0 ) {
var balance_for_notification = balance - old_wallet_balance;
if ( balance_for_notification > 999 ) {
balance_for_notification = "999+";
}
document.getElementsByClassName( "manage-wallet-outer" )[ 0 ].getElementsByClassName( "notification" )[ 0 ].innerText = String( balance_for_notification );
document.getElementsByClassName( "manage-wallet-outer" )[ 0 ].setAttribute( "data-current-balance", String( balance ) );
document.getElementsByClassName( "manage-wallet-outer" )[ 0 ].getElementsByClassName( "notification" )[ 0 ].style.display = "inline";
}
}
</script>
<script>
async function setOffset() {
var time = await getData( "https://worldtimeapi.org/api/ip" );
var realnow = JSON.parse( time )[ "unixtime" ];
var claimednow = Math.floor( Date.now() / 1000 );
var offset = realnow - claimednow;
sessionStorage[ "time-offset" ] = offset;
}
</script>
<script>
function bold( text ) {
var bold = /\*\*(.*?)\*\*/gm;
var html = text.replace( bold, '<strong>$1</strong>' );
return html;
}
function italicize( text ) {
var italicize = /\*(.*?)\*/gm;
var html = text.replace( italicize, '<span style="font-style: italic;">$1</span>' );
return html;
}
function triplebackticks( text ) {
var triplebackticks = /\`\`\`([\s\S]*?)\`\`\`/g;
var html = text.replace( triplebackticks, '<pre style="white-space: pre-wrap; padding: 5px; background-color: #999; font-size: 12px; color: white;">$1</pre>' );
return html;
}
function backticks( text ) {
var backticks = /\`(.*?)\`/gm;
var html = text.replace( backticks, '<pre style="display: inline-block; white-space: pre-wrap; padding: 5px; background-color: #999; font-size: 12px; color: white;">$1</pre>' );
return html;
}
function youtube_parser( url ) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match( regExp );
return ( match && match[ 7 ].length == 11 ) ? match[ 7 ] : false;
}
function isStringUrl( string ) {
if ( string.indexOf( " " ) >= 0 || string.indexOf( "<" ) >= 0 || string.indexOf( ">" ) >= 0 || string.indexOf( ">" ) >= 0 || string.indexOf( "<" ) >= 0 || string.indexOf( "'" ) >= 0 || string.indexOf( '"' ) >= 0 ) {
return;
}
try {
var arr = string.match( /\S+\.{1}[a-zA-Z0-9\?\=\&\$\+\,\;\@\#\%\-\_]{2,}\/?[a-zA-Z0-9/\\?\=\&\$\+\,\;\@\#\%\-\_\.]*/g );
if ( typeof( arr ) == "object" && arr.length == 1 ) {
return true;
}
return;
} catch( e ) {
return;
}
}
function urlify( text, isMine ) {
var ytRegex = /(http:|https:)?\/\/(www\.)?(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/g
var imgRegex = /\b(https?:\/\/\S+(?:\.png|\.jpe?g|\.gif|\.webp|\.PNG|\.JPE?G|\.GIF|\.WEBP)\S*)\b/g;
var urlRegex = /\S+\.{1}[a-zA-Z0-9\?\=\&\$\+\,\;\@\#\%\-\_]{2,}\/?[a-zA-Z0-9/\\?\=\&\$\+\,\;\@\#\%\-\_\.]*/g;
var returnable = "";
if ( !isMine && !text.match( ytRegex ) && !text.match( imgRegex ) ) {
returnable = text.replace( urlRegex, ( url ) => {
if ( url.includes( ".." ) ) {
return url;
}
var url2 = url;
if ( !url.startsWith( "http://" ) && !url.startsWith( "https://" ) ) {
url2 = "http://" + url;
}
return `<a onclick="event.stopPropagation()" href="${url2}" target="_blank" rel="nofollow noopener" style="color: #4287f5;">${url}</a>`;
})
}
if ( isMine && !text.match( ytRegex ) && !text.match( imgRegex ) ) {
returnable = text.replace( urlRegex, ( url ) => {
if ( url.includes( ".." ) ) {
return url;
}
var url2 = url;
if ( !url.startsWith( "http://" ) && !url.startsWith( "https://" ) ) {
url2 = "http://" + url;
}
return `<a onclick="event.stopPropagation()" href="${url2}" target="_blank" rel="nofollow noopener" style="color: white;">${url}</a>`;
})
}
if ( text.match( imgRegex ) ) {
returnable = text.replace( imgRegex, ( img ) => {
if ( localStorage[ "img_cache" ] ) {
var imgs = JSON.parse( localStorage[ "img_cache" ] );
} else {
var imgs = [];
localStorage[ "img_cache" ] = JSON.stringify( imgs );
}
if ( isMine && imgs.includes( img ) ) {
var i; for ( i=0; i<imgs.length; i++ ) {
if ( imgs[ i ] == img ) {
imgs.splice( i, 1 );
}
}
imgs.unshift( img );
}
if ( isMine && imgs.length >= 50 && !imgs.includes( img ) ) {
imgs.unshift( img );
imgs.pop();
} else if ( isMine && !imgs.includes( img ) ) {
imgs.unshift( img );
}
localStorage[ "img_cache" ] = JSON.stringify( imgs );
return `<a href="${img}" target="_blank" rel="nofollow noopener" onclick="event.stopPropagation()"><img src="${img}" onclick="event.stopPropagation()" /></a>`;
})
}
if ( text.match( ytRegex ) ) {
returnable = text.replace( ytRegex, ( yt ) => {
if ( yt.endsWith( ")" ) ) {
yt = yt.substring( 0, yt.length - 1 );
}
var vid = `<div class="youtube-wrapper" onclick="event.stopPropagation()"><img class="ratio" src="./16x9-transparent.png" onclick="event.stopPropagation()" /><iframe class="youtube-video" src="${"https://www.youtube-nocookie.com/embed/" + youtube_parser( yt )}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen onclick="event.stopPropagation()"></iframe></div><span class="p-in-p" onclick='moveVid( this.previousElementSibling );event.stopPropagation()'>Picture-in-picture</span>`;
return vid;
})
}
return returnable;
}
function markdownify( text, isMine ) {
var returnable = urlify( text, isMine );
returnable = bold( returnable );
returnable = italicize( returnable );
returnable = triplebackticks( returnable );
returnable = backticks( returnable );
return returnable;
}
</script>
<script>
function addDmsToSidebar() {
var i; for ( i=0; i<document.getElementsByClassName( "dm-outer" ).length; i++ ) {
document.getElementsByClassName( "dm-outer" )[ i ].remove();
i = i - 1;
}
Object.keys( dms_info ).forEach( function( pubkey ) {
if ( sessionStorage[ pubkey + "-counter" ] ) {
var messagecounter = Number( sessionStorage[ pubkey + "-counter" ] );
} else {
var messagecounter = 0;
}
var name = DOMPurify.sanitize( dms_info[ pubkey ][ "name" ] );
var picture = DOMPurify.sanitize( dms_info[ pubkey ][ "picture" ] );
if ( namemap[ pubkey ] && namemap[ pubkey ] != "none" ) {
name = DOMPurify.sanitize( namemap[ pubkey ] );
dms_info[ pubkey ][ "name" ] = name;
}
if ( imagemap[ pubkey ] && imagemap[ pubkey ] != "none" ) {
picture = DOMPurify.sanitize( imagemap[ pubkey ] );
dms_info[ pubkey ][ "picture" ] = picture;
}
var num_of_new_messages = messagecounter;
if ( !document.getElementsByClassName( pubkey.substring( 0, 20 ) )[ 0 ] ) {
document.getElementsByClassName( "channels" )[ 0 ].firstElementChild.innerHTML += displayDMbox( pubkey, name, picture, messagecounter );
}
});
}
</script>
<script>
function shutKeyboard() {
if ( Number( sessionStorage[ "keyboard_is_open" ] ) && window.innerHeight > ( Number( sessionStorage[ "full-window-height" ] ) * 0.9 ) ) {
document.getElementsByClassName( "input" )[ 0 ].firstElementChild.blur();
sessionStorage[ "keyboard_is_open" ] = String( 0 );
if ( sessionStorage[ "keyboard_size" ] ) {
var keyboard_size = Number( sessionStorage[ "keyboard_size" ] );
if ( document.getElementsByClassName( "active" )[ 0 ].getAttribute( "data-id" ) ) {
var type = "content";
} else {
var type = "message-content";
}
var current_height = document.getElementsByClassName( type )[ 0 ].scrollTop;
var new_height = ( current_height - keyboard_size ) - 50;
var max_height = document.getElementsByClassName( type )[ 0 ].scrollHeight;
if ( new_height > max_height - 1500 ) {new_height = max_height;}
document.getElementsByClassName( type )[ 0 ].scrollTo({top: new_height, left: 0, behavior: "smooth"});
}
}
setTimeout( shutKeyboard, 1000 );
}
</script>
<script>
async function importWalletFromNostrOrMakeOne( alterpubkey ) {
var importWalletSubId = "00000003" + bitcoinjs.ECPair.makeRandom().privateKey.toString( "hex" ).substring( 8 )
var importWalletFilter = { "#p": [ alterpubkey ] }
var importWalletSub = [ "REQ", importWalletSubId, importWalletFilter ];
socket.send( JSON.stringify( importWalletSub ) );
setTimeout( function() {
var closer = [ "CLOSE", importWalletSubId ];
socket.send( JSON.stringify( closer ) );
}, 1500 );
}
</script>
<script>
if ( window.location.href.includes( "brilliancebitcoin.github.io/nostrgram" ) && !window.location.href.includes( "brilliancebitcoin.github.io/nostrgram-test" ) ) {
//window.location.href = "https://anigma.io/";
}
var { getSharedSecret, schnorr, utils } = nobleSecp256k1;
var crypto = window.crypto;
var getRand = size => crypto.getRandomValues( new Uint8Array( size ) );
var sha256 = bitcoinjs.crypto.sha256;
const urlParams = new URLSearchParams(window.location.search);
var channelParam = urlParams.get( 'channel' );
var profileParam = urlParams.get( 'profile' );
if ( profileParam && profileParam.startsWith( "npub" ) && isBech32( profileParam ) ) {
profileParam = pubkeyFromNpub( profileParam );
}
var relayParam = urlParams.get( 'relay' );
if ( localStorage[ "backup" ] ) {
var backup = JSON.parse( localStorage[ "backup" ] );
var privKey = backup[ "privkey" ];
var pubKey = backup[ "pubkey" ];
var subscribed_channels = JSON.parse( backup[ "channels" ] );
if ( channelParam && !subscribed_channels.includes(channelParam) ) {
subscribed_channels.push( channelParam );
}
// var relay = relayParam || JSON.parse( backup[ "relays" ] )[ 0 ];
// var relay = relayParam || "wss://relay.nostr.ch";
var relay = relayParam || "wss://relay.snort.social";
} else {
var keypair = bitcoinjs.ECPair.makeRandom();
var privKey = keypair.privateKey.toString( "hex" );
var pubKey = keypair.publicKey.toString( "hex" );
pubKey = pubKey.substring( 2 );
var subscribed_channels = [ channelParam || "25e5c82273a271cb1a840d0060391a0bf4965cafeb029d5ab55350b418953fbb" ];
// var relay = relayParam || "wss://relay.damus.io";
var relay = relayParam || "wss://relay.snort.social";
var stuff_to_backup = {}
stuff_to_backup[ "privkey" ] = privKey;
stuff_to_backup[ "pubkey" ] = pubKey;
stuff_to_backup[ "channels" ] = JSON.stringify( subscribed_channels );
stuff_to_backup[ "relays" ] = JSON.stringify( [ relay ] );
localStorage[ "backup" ] = JSON.stringify( stuff_to_backup );
}
if ( localStorage[ "namemap" ] ) {
var namemap = JSON.parse( localStorage[ "namemap" ] );
} else {
var namemap = {}
}
if ( localStorage[ "imagemap" ] ) {
var imagemap = JSON.parse( localStorage[ "imagemap" ] );
} else {
var imagemap = {}
}
if ( localStorage[ "dms_info" ] ) {
var dms_info = JSON.parse( localStorage[ "dms_info" ] );
} else {
var dms_info = {}
}
if ( localStorage[ "msg_cache" ] ) {
var msg_cache = JSON.parse( localStorage[ "msg_cache" ] );
} else {
var msg_cache = {}
subscribed_channels.forEach( function( id ) {
msg_cache[ id ] = [];
});
Object.keys( dms_info ).forEach( function( pubkey ) {
msg_cache[ pubkey ] = [];
});
localStorage[ "msg_cache" ] = JSON.stringify( msg_cache );
}
if ( localStorage[ "local_pubkey_blacklist" ] ) {
var local_pubkey_blacklist = JSON.parse( localStorage[ "local_pubkey_blacklist" ] );
} else {
var local_pubkey_blacklist = [];
}
var wallet_id = "";
var adminkey = "";
var readkey = "";
var wallet_url = "";
var lud06 = "";
var array_of_pubkeys = [];
var existing_messages = [];
var array_of_replies = [];
var socket = new WebSocket( relay );
var heartbeat = false;
var alterPrivKey = privKey.substring( 32 ) + privKey.substring( 0, 32 );
var alterPubKey = nobleSecp256k1.getPublicKey( alterPrivKey, true ).substring( 2 );
var i_should_create_a_new_wallet = true;
sessionStorage[ "my-name" ] = pubKey.substring( 0, 15 ) + "...";
sessionStorage[ "full-window-height" ] = window.innerHeight;
sessionStorage[ "keyboard_is_open" ] = String( 0 );
async function getWallet() {
if ( localStorage[ "wallet_info" ] ) {
var wallet_info = JSON.parse( localStorage[ "wallet_info" ] );
wallet_id = wallet_info[ "wallet_id" ];
adminkey = wallet_info[ "adminkey" ];
readkey = wallet_info[ "readkey" ];
wallet_url = wallet_info[ "wallet_url" ];
var domain = wallet_url.substring( 0, wallet_url.indexOf( "/wallet" ) );
var url = domain + "/lnurlp/api/v1/links";
var lnurls = await getData( url, readkey )
lnurls = JSON.parse( lnurls );
lnurls.sort( function( a, b ) {
return b.id - a.id;
});
if ( lnurls[ 0 ] && lnurls[ 0 ][ "lnurl" ] ) {
var prevlud06 = lud06;
lud06 = lnurls[ 0 ][ "lnurl" ];
}
if ( prevlud06 != lud06 ) {
submitUserInfo();
}
} else {
importWalletFromNostrOrMakeOne( alterPubKey );
}
}
async function handleMessage( message ) {
heartbeat = true;
var [ type, subId, event ] = JSON.parse( message.data );
var { kind, content } = event || {}
if ( content && content.length > 3000 ) return;
if ( kind && subId.startsWith( "00000000" ) ) {
if ( kind != 40 ) {
showToast( `${t('unknownError')}` );
}
var isError = checkForChannelSubscriptionErrors( content );
if ( isError ) {
showToast( `${t('unknownError')}` );
} else {
const idPurified = DOMPurify.sanitize( event.id, { USE_PROFILES: { html: false, xml: false, svgFilters: false, mathMl: false } });
const picPurified = DOMPurify.sanitize( JSON.parse( content )[ "picture" ], { USE_PROFILES: { html: false, xml: false, svgFilters: false, mathMl: false } } );
const namePurified = DOMPurify.sanitize( JSON.parse( content )[ "name" ], { USE_PROFILES: { html: false, xml: false, svgFilters: false, mathMl: false } } );
var picture =
document.getElementsByClassName( "removable-channels" )[ 0 ].innerHTML += `
<div class="removable-channel ${event.id}">
<span class="x-circle" onclick="removeChannel( '${idPurified}' )">×</span>
<span class="img-circle" style="background-image: url('${picPurified}')"></span>
<span class="removable-channel-name">${namePurified}</span>
<div style="clear: both"></div>
</div>
`;
}
}
if ( subId.startsWith( "00000001" ) ) {
if ( content && !checkForUserMetaErrors( content ) ) {
//path if I am updating my own info
if ( event.pubkey == pubKey ) {
var myMetaInfo = {}
if ( "name" in JSON.parse( content ) ) {
if ( document.getElementsByClassName( "display-name" )[ 0 ] ) {
document.getElementsByClassName( "display-name" )[ 0 ].value = JSON.parse( content )[ "name" ];
}
if ( document.getElementsByClassName( "user-profile-inner" )[ 0 ] && document.getElementsByClassName( "user-profile-inner" )[ 0 ].getElementsByClassName( "name" )[ 0 ] && JSON.parse( content )[ "name" ] && JSON.parse( content )[ "name" ] != "" ) {
document.getElementsByClassName( "user-profile-inner" )[ 0 ].getElementsByClassName( "name" )[ 0 ].innerHTML = JSON.parse( content )[ "name" ];
}
if ( document.getElementsByClassName( "profile-name" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
document.getElementsByClassName( "profile-name" )[ 0 ].innerText = JSON.parse( content )[ "name" ];
}
if ( document.getElementsByClassName( "profile-pubkey-title" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
document.getElementsByClassName( "profile-pubkey-title" )[ 0 ].innerText = JSON.parse( content )[ "name" ] + "'s public key";
}
sessionStorage[ "my-name" ] = JSON.parse( content )[ "name" ];
}
if ( "about" in JSON.parse( content ) ) {
if ( document.getElementsByClassName( "about-input" )[ 0 ] ) {
document.getElementsByClassName( "about-input" )[ 0 ].value = JSON.parse( content )[ "about" ];
}
}
if ( "picture" in JSON.parse( content ) ) {
if ( document.getElementsByClassName( "user-picture" )[ 0 ] ) {
document.getElementsByClassName( "user-picture" )[ 0 ].value = JSON.parse( content )[ "picture" ];
}
if ( document.getElementsByClassName( "big-picture" )[ 0 ] && JSON.parse( content )[ "picture" ] && JSON.parse( content )[ "picture" ] != "" ) {
document.getElementsByClassName( "big-picture" )[ 0 ].src = JSON.parse( content )[ "picture" ];
}
if ( document.getElementsByClassName( "user-in-sidebar" )[ 0 ] && document.getElementsByClassName( "user-in-sidebar" )[ 0 ].getElementsByTagName( "span" )[ 0 ] && JSON.parse( content )[ "picture" ] && JSON.parse( content )[ "picture" ] != "" ) {
document.getElementsByClassName( "user-in-sidebar" )[ 0 ].getElementsByTagName( "span" )[ 0 ].style = "background-image: url('" + JSON.parse( content )[ "picture" ] + "')";
}
}
Object.keys( JSON.parse( content ) ).forEach( function( key ) {
myMetaInfo[ key ] = JSON.parse( content )[ key ];
})
localStorage[ "my-meta-info" ] = JSON.stringify( myMetaInfo );
}
//path if I am updating someone else's info
if ( "name" in JSON.parse( content ) && !( !JSON.parse( content )[ "name" ] || JSON.parse( content )[ "name" ] == "" ) ) {
//stop if the metadata you are getting belongs to a different user
if ( document.querySelector( ".profile-pubkey" ) && event.pubkey != pubkeyFromNpub( document.querySelector( ".profile-pubkey" ).innerText ) ) return;
namemap[ event.pubkey ] = DOMPurify.sanitize( JSON.parse( content )[ "name" ] );
localStorage[ "namemap" ] = JSON.stringify( namemap );
var i; for ( i=0; i<document.getElementsByClassName( event.pubkey ).length; i++ ) {
document.getElementsByClassName( event.pubkey )[ i ].innerText = DOMPurify.sanitize( JSON.parse( content )[ "name" ] );
}
if ( document.getElementsByClassName( "profile-name" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
document.getElementsByClassName( "profile-name" )[ 0 ].innerText = DOMPurify.sanitize( JSON.parse( content )[ "name" ] );
}
if ( document.getElementsByClassName( "profile-pubkey-title" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
document.getElementsByClassName( "profile-pubkey-title" )[ 0 ].innerText = DOMPurify.sanitize( JSON.parse( content )[ "name" ] ) + "'s public key";
}
} else {
namemap[ event.pubkey ] = "none";
localStorage[ "namemap" ] = JSON.stringify( namemap );
}
if ( "about" in JSON.parse( content ) && !( !JSON.parse( content )[ "about" ] || JSON.parse( content )[ "about" ] == "" ) ) {
//stop if the metadata you are getting belongs to a different user
if ( document.querySelector( ".profile-pubkey" ) && event.pubkey != pubkeyFromNpub( document.querySelector( ".profile-pubkey" ).innerText ) ) return;
if ( document.getElementsByClassName( "profile-about" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
document.getElementsByClassName( "profile-about" )[ 0 ].innerHTML = markdownify( DOMPurify.sanitize( JSON.parse( content )[ "about" ] ) );
document.getElementsByClassName( "profile-about" )[ 0 ].style.display = "block";
}
}
if ( "lud06" in JSON.parse( content ) && !( !JSON.parse( content )[ "lud06" ] || JSON.parse( content )[ "lud06" ] == "" ) ) {
//stop if the metadata you are getting belongs to a different user
if ( document.querySelector( ".profile-pubkey" ) && event.pubkey != pubkeyFromNpub( document.querySelector( ".profile-pubkey" ).innerText ) ) return;
if ( document.getElementsByClassName( "profile-lud06" )[ 0 ] && !document.getElementsByClassName( "tipper" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && pubkeyFromNpub( document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText ) == event.pubkey ) {
var button = document.createElement( "button" );
button.className = "tipper";
button.innerText = "Tip this user";
button.onclick = function() {
var a = document.createElement( "a" );
a.href = "lightning:" + JSON.parse( content )[ "lud06" ].toLowerCase();
a.target = "_blank";
a.append( createQR( DOMPurify.sanitize( JSON.parse( content )[ "lud06" ] ) ) );
document.getElementById( "modal" ).innerHTML = "";
document.getElementById( "modal" ).append( a );
document.getElementById( "modal" ).innerHTML += '<div align="center" style="font-weight: bold;">click or scan</div>';
document.getElementById( "black-bg" ).style.display = "block";
document.getElementById( "modal" ).style.display = "block";
}
document.getElementsByClassName( "profile-lud06" )[ 0 ].append( button );
document.getElementsByClassName( "profile-lud06" )[ 0 ].style.display = "block";
}
}
if ( "picture" in JSON.parse( content ) && !( !JSON.parse( content )[ "picture" ] || JSON.parse( content )[ "picture" ] == "" ) ) {
//stop if the metadata you are getting belongs to a different user
if ( document.querySelector( ".profile-pubkey" ) && event.pubkey != pubkeyFromNpub( document.querySelector( ".profile-pubkey" ).innerText ) ) return;
picture = JSON.parse( content )[ "picture" ];
if ( !isStringUrl( picture ) ) {
picture = `https://www.gravatar.com/avatar/${event.pubkey}?s=128&d=retro`;
}
imagemap[ event.pubkey ] = DOMPurify.sanitize( picture );
localStorage[ "imagemap" ] = JSON.stringify( imagemap );
var i; for ( i=0; i<document.getElementsByClassName( event.pubkey ).length; i++ ) {
if ( document.getElementsByClassName( event.pubkey )[ i ] && document.getElementsByClassName( event.pubkey )[ i ].parentElement && document.getElementsByClassName( event.pubkey )[ i ].parentElement.parentElement && document.getElementsByClassName( event.pubkey )[ i ].parentElement.parentElement.getElementsByClassName( "picture" )[ 0 ] ) {
document.getElementsByClassName( event.pubkey )[ i ].parentElement.parentElement.getElementsByClassName( "picture" )[ 0 ].style.backgroundImage = "url(" + DOMPurify.sanitize( picture ) + ")";
}
}
if ( document.getElementsByClassName( "big-picture" )[ 0 ] && document.getElementsByClassName( "channel-wrapper" )[ 0 ].getElementsByClassName( "name" )[ 0 ].innerText != `${t('settings')}` && document.getElementsByClassName( "profile-pubkey" )[ 0 ] && document.getElementsByClassName( "profile-pubkey" )[ 0 ].innerText == event.pubkey ) {
document.getElementsByClassName( "big-picture" )[ 0 ].src = DOMPurify.sanitize( JSON.parse( content )[ "picture" ] );
}
} else {
imagemap[ event.pubkey ] = "none";
localStorage[ "imagemap" ] = JSON.stringify( imagemap );
}
} else if ( content && checkForUserMetaErrors( content ) ) {
namemap[ event.pubkey ] = "none";
localStorage[ "namemap" ] = JSON.stringify( namemap );
imagemap[ event.pubkey ] = "none";
localStorage[ "imagemap" ] = JSON.stringify( imagemap );
}
var closer = [ "CLOSE", subId ];
socket.send(JSON.stringify( closer ));
}
//this 03 code runs when the user's client checks if wallet data is stored on nostr
//this happens only when it can't find wallet data in local storage
//wallet data should be in a certain format and the function should check if the
//sender is himself and that the sig is valid, otherwise malicious people might
//send fake wallet data and thus replace the user's real wallet with the hacker's.
//If and only if there is no wallet data stored on nostr, it should create a wallet
if ( subId.startsWith( "00000003" ) ) {
if ( JSON.parse( message[ "data" ] )[ 0 ] == "EOSE" && ( !localStorage[ "wallet_info" ] || localStorage[ "wallet_info" ] == "" ) ) {
if ( !i_should_create_a_new_wallet ) return;
var mywallet = await getData( "https://legend.lnbits.com/wallet?nme=" + pubkeyToNpub( pubKey ).substring( 0, 15 ) );
var key1prep = mywallet.split( "Wallet ID:" );
wallet_id = key1prep[ 1 ].substring( 14, 46 );
var key2prep = mywallet.split( "Admin key:" );
adminkey = key2prep[ 1 ].substring( 14, 46 );
var key3prep = mywallet.split( "Invoice/read key:" );
readkey = key3prep[ 1 ].substring( 14, 46 );
var key4prep = mywallet.split( "<qrcode" );
wallet_url = key4prep[ 2 ].split( "'" )[ 1 ] + key4prep[ 2 ].split( "'" )[ 3 ];
wallet_obj = {}
wallet_obj[ "wallet_id" ] = wallet_id;
wallet_obj[ "adminkey" ] = adminkey;
wallet_obj[ "readkey" ] = readkey;
wallet_obj[ "wallet_url" ] = wallet_url;
localStorage[ "wallet_info" ] = JSON.stringify( wallet_obj );
sendEncryptedNote( JSON.stringify( wallet_obj ), alterPubKey );
var json = {}
json[ "description" ] = pubKey;
json[ "min" ] = 1;
json[ "max" ] = 9007199254740991;
json[ "comment_chars" ] = 240;
var domain = wallet_url.substring( 0, wallet_url.indexOf( "/wallet" ) );
var response = await postJson( domain + "/lnurlp/api/v1/links", adminkey, JSON.stringify( json ) );
var response = JSON.parse( response );
lud06 = response[ "lnurl" ];
submitUserInfo();
} else if ( JSON.parse( message[ "data" ] )[ 0 ] != "EOSE" ) {
i_should_create_a_new_wallet = false;
var thisevent = JSON.parse( message[ "data" ] )[ 2 ];
if ( thisevent[ "pubkey" ] != pubKey ) {
i_should_create_a_new_wallet = true;
return;
}
var sig_is_valid = await nobleSecp256k1.schnorr.verify( thisevent[ "sig" ], thisevent[ "id" ], thisevent[ "pubkey" ] );
if ( !sig_is_valid ) {
var alert = `${t('importWalletError')} (id: ${thisevent[ "id" ]}). ${t('relayPart')}`;
alert( alert );
return;
}
var wallet = JSON.parse( decrypt( privKey, alterPubKey, thisevent[ "content" ] ) );
var walletIsValid = checkForWalletImportErrors( wallet );
if ( !walletIsValid ) {
showToast( `${t('importWalletError')}`);
return;
}
wallet_id = wallet[ "wallet_id" ];
adminkey = wallet[ "adminkey" ];
readkey = wallet[ "readkey" ];
wallet_url = wallet[ "wallet_url" ];
localStorage[ "wallet_info" ] = JSON.stringify( wallet );
var domain = wallet_url.substring( 0, wallet_url.indexOf( "/wallet" ) );
var url = domain + "/lnurlp/api/v1/links";
var lnurls = await getData( url, readkey )
lnurls = JSON.parse( lnurls );
lnurls.sort( function( a, b ) {
return b.id - a.id;
});
if ( lnurls[ 0 ] && lnurls[ 0 ][ "lnurl" ] ) {
lud06 = lnurls[ 0 ][ "lnurl" ];
}
submitUserInfo();
}
}
if ( kind == 40 ) {
if ( JSON.parse( message[ "data" ] )[ 0 ] == "EOSE" ) return;
if ( document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( event.id )[ 0 ] ) return
var isError = checkForChannelSubscriptionErrors( content );
if ( isError ) {
showToast( `${t('unknownError')}` );
} else {
document.getElementsByClassName( "channels" )[ 0 ].firstElementChild.innerHTML += displayChannel( DOMPurify.sanitize( JSON.parse( content )[ "picture" ] ), DOMPurify.sanitize( JSON.parse( content )[ "name" ] ), "", event.id, event.created_at );
getLatestMessage( event.id );
addDmsToSidebar();
var channel = document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( event.id )[ 0 ];
if ( subId.startsWith( "00000000" ) ) {
selectChannel( channel );
}
}
var closer = [ "CLOSE", subId ];
socket.send(JSON.stringify( closer ));
}
if ( kind == 42 && subId.includes( "latest-message" ) ) {
if ( JSON.parse( message[ "data" ] )[ 0 ] == "EOSE" ) return;
if ( localStorage[ "local_pubkey_blacklist" ] ) {
var local_pubkey_blacklist = JSON.parse( localStorage[ "local_pubkey_blacklist" ] );
} else {
var local_pubkey_blacklist = [];
}
if ( local_pubkey_blacklist.includes( event.pubkey ) ) return;
var now = Math.floor( Date.now() / 1000 ) + Number( sessionStorage[ "time-offset" ] );
var event_time = event.created_at;
var how_long_ago = convertHMS( now - event_time );
if ( now - event_time < -600 ) return;
var isMine = ( event.pubkey == pubKey );
if ( !isMine && !array_of_pubkeys.includes( event.pubkey ) ) {
array_of_pubkeys.push( event.pubkey );
}
var events_replied_to = [];
var pubkeys_replied_to = [];
var i; for ( i=0; i<event.tags.length; i++ ) {
if ( event[ "tags" ][ i ][ 0 ] == "e" ) {
events_replied_to.push( event[ "tags" ][ i ][ 1 ] );
}
if ( event[ "tags" ][ i ][ 0 ] == "p" ) {
pubkeys_replied_to.push( event[ "tags" ][ i ][ 1 ] );
}
}
var parent = events_replied_to[ 0 ];
var replyingTo = "";
if ( events_replied_to[ 1 ] ) {
replyingTo = events_replied_to[ 1 ];
array_of_replies.push( [ event.id, replyingTo ] );
}
content = content.replace( /&/g, "&" ).replace( /</g, "<" ).replace( />/g, ">" );
content = DOMPurify.sanitize( content );
var shortened_content = content.replace( /\n/g, " " ).substring( 0, 50 );
if ( content.length > 50 ) {
shortened_content = shortened_content + "...";
}
content = markdownify( content, isMine );
array_of_replies.forEach( function( item ) {
if ( item[ 1 ] == event.id ) {
var replyPubkey = event.pubkey;
if ( event.pubkey in namemap && namemap[ event.pubkey ] != "none" ) {
var person_name = namemap[ event.pubkey ];
} else {
var person_name = replyPubkey.substring( 0, 15 );
}
if ( replyPubkey != pubKey ) {
replyAuthor = `<span class="reply-author ${replyPubkey}">` + person_name + `</span>`;
} else {
replyAuthor = `<span class="reply-author ${replyPubkey}">` + sessionStorage[ "my-name" ] + `</span>`;
}
replyContent = shortened_content;
if ( document.getElementsByClassName( item[ 0 ] )[ 0 ] ) {
document.getElementsByClassName( item[ 0 ] )[ 0 ].getElementsByClassName( "reply-prefix" )[ 0 ].innerHTML = replyAuthor + '<br>' + replyContent;
}
}
});
var color = "";
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "e" || event.pubkey.substring( 0, 1 ) == "f" ) ) {
color = "red";
}
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "c" || event.pubkey.substring( 0, 1 ) == "d" ) ) {
color = "pink";
};
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "a" || event.pubkey.substring( 0, 1 ) == "b" ) ) {
color = "orange";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 10 ) {
color = "yellow";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 8 ) {
color = "green";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 6 ) {
color = "lightblue";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 4 ) {
color = "blue";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 2 ) {
color = "purple";
};
var name = pubkeyToNpub( event.pubkey ).substring( 0, 15 ) + "...";
//var picture = "https://hashvatar.vercel.app/" + event.pubkey;
var picture = "https://www.gravatar.com/avatar/" + event.pubkey + "?s=128&d=retro";
if ( event.pubkey in namemap && namemap[ event.pubkey ] != "none" ) {
name = namemap[ event.pubkey ];
}
name = DOMPurify.sanitize( name );
if ( event.pubkey in imagemap && imagemap[ event.pubkey ] != "none" ) {
picture = imagemap[ event.pubkey ];
}
if ( !msg_cache[ parent ] ) {
msg_cache[ parent ] = [];
}
if ( Number( document.getElementsByClassName( parent )[ 0 ].getElementsByClassName( "message" )[ 0 ].getAttribute( "data-timestamp" ) ) < Number( event_time ) ) {
document.getElementsByClassName( parent )[ 0 ].getElementsByClassName( "message" )[ 0 ].innerText = shortened_content;
document.getElementsByClassName( parent )[ 0 ].getElementsByClassName( "message" )[ 0 ].setAttribute( "data-timestring", how_long_ago )
document.getElementsByClassName( parent )[ 0 ].getElementsByClassName( "message" )[ 0 ].setAttribute( "data-timestamp", event_time )
}
//If the current message is not already in the message cache, add it, and also add to the number of unread messages
var messageIsKnown = false;
msg_cache[ parent ].forEach( function( message, index ) {
if ( Object.keys( message )[ 0 ] == event.id ) {
messageIsKnown = true;
}
});
if ( !messageIsKnown ) {
addMessageToCache( event.pubkey, event.id, event_time, parent, color, name, picture, content, replyingTo );
if ( !document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].classList.contains( "active" ) ) {
var old_num_of_new_messages = Number( document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].getAttribute( "data-num-of-new-messages" ) );
new_num_of_new_messages = old_num_of_new_messages + 1;
document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].setAttribute( "data-num-of-new-messages", String( new_num_of_new_messages ) );
if ( new_num_of_new_messages > 0 ) {
document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].getElementsByClassName( "notification" )[ 0 ].innerText = String( new_num_of_new_messages );
if ( new_num_of_new_messages > 199 ) {
document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].getElementsByClassName( "notification" )[ 0 ].innerText += "+";
}
document.getElementsByClassName( "channels" )[ 0 ].getElementsByClassName( parent )[ 0 ].getElementsByClassName( "notification" )[ 0 ].style.display = "inline";
}
}
}
//if you are currently looking at the channel, add the message to the channel
if ( !document.getElementsByClassName( parent )[ 0 ].classList.contains( "active" ) ) return;
if ( document.getElementsByClassName( event.id )[ 0 ] ) return;
var div = document.createElement( "div" );
div.classList.add( "message" );
div.classList.add( event.id );
div.setAttribute( "data-author", event.pubkey );
var what_for_reply = ( replyingTo ) ? replyingTo:event.pubkey;
div.onclick = function() {msgContext( event.id, event.pubkey, what_for_reply, content );}
if ( isMine ) {
div.align = "right";
}
if ( isMine ) {
var replyclass = "reply-in-blue";
} else {
var replyclass = "reply-in-white";
}
var replyAuthor = `<span class="reply-author">${t('unknown')}</span>`;
var replyPubkey = null;
if ( document.getElementsByClassName( replyingTo )[ 0 ] ) {
replyPubkey = document.getElementsByClassName( replyingTo )[ 0 ].getAttribute( "data-author" );
}
if ( replyPubkey == pubKey ) {
replyAuthor = `<span class="reply-author">${sessionStorage[ "my-name" ]}</span>`;
}
var replyContent = `${t('replyToMessageNotFound')}`;
var replyPrefix = "";
if ( replyingTo && replyingTo != "" ) {
if ( document.getElementsByClassName( replyingTo )[ 0 ] && document.getElementsByClassName( replyingTo )[ 0 ].getElementsByClassName( "bubble" )[ 0 ] && document.getElementsByClassName( replyingTo )[ 0 ].getElementsByClassName( "bubble" )[ 0 ].getElementsByClassName( "msgtext" )[ 0 ] ) {
if ( replyPubkey != pubKey ) {
replyAuthor = `<span class="reply-author ${replyPubkey}">` + document.getElementsByClassName( replyingTo )[ 0 ].getElementsByClassName( "bubble" )[ 0 ].getElementsByClassName( "author" )[ 0 ].innerHTML + `</span>`;
}
replyContent = document.getElementsByClassName( replyingTo )[ 0 ].getElementsByClassName( "bubble" )[ 0 ].getElementsByClassName( "msgtext" )[ 0 ].innerText;
shortened_reply_content = replyContent.replace( /\n/g, " " ).substring( 0, 50 );
if ( replyContent.length > 50 ) {
shortened_reply_content = shortened_reply_content + "...";
}
replyContent = shortened_reply_content;
}
div.setAttribute( "data-replying-to", replyingTo );
replyPrefix = `<div class="reply-prefix ${replyclass}" onclick='document.getElementsByClassName( "${replyingTo}" )[ 0 ].scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});document.getElementsByClassName( "${replyingTo}" )[ 0 ].getElementsByClassName( "bubble" )[ 0 ].style.boxShadow = "0px 0px 25px 2px yellow";setTimeout( function() {document.getElementsByClassName( "${replyingTo}" )[ 0 ].getElementsByClassName( "bubble" )[ 0 ].style.boxShadow = "";}, 600 );event.stopPropagation();'>${replyAuthor}<br>${replyContent}</div>`
}
if ( isMine ) {
div.classList.add( "me" );
var html = `<div class="bubble" align="left">${replyPrefix}<div class="msgtext">${content}</div><div data-timestamp="${event_time}" class="time bubbletime">${how_long_ago}</div></div>`;
div.innerHTML = html;
} else {
var html = `
<span class="picture" style="background-image: url('${picture}')" onclick="event.stopPropagation();showProfile( '${event.pubkey}', '${name}', '${picture}' )"></span>
<div class="bubble"><div class="author ${color} ${event.pubkey}">${name}</div>${replyPrefix}<div class="msgtext">${content}</div><div data-timestamp="${event_time}" class="time bubbletime">${how_long_ago}</div></div>
`;
div.innerHTML = html;
}
document.getElementsByClassName( "message-content" )[ 0 ].append( div );
var last_200_messages = [];
var i; for ( i=0; i<document.getElementsByClassName( "bubble" ).length; i++ ) {
last_200_messages.push( document.getElementsByClassName( "bubble" )[ i ].parentElement );
}
last_200_messages.sort(function( a, b ) {return b.getElementsByClassName( "time" )[ 0 ].getAttribute( "data-timestamp" ) - a.getElementsByClassName( "time" )[ 0 ].getAttribute( "data-timestamp" )});
var i; for ( i=0; i<document.getElementsByClassName( "bubble" ).length; i++ ) {
document.getElementsByClassName( "bubble" )[ i ].parentElement.remove();
i = i - 1;
}
last_200_messages.reverse();
last_200_messages.forEach( function( item ) {
document.getElementsByClassName( "message-content" )[ 0 ].append( item );
document.querySelectorAll( "a" ).forEach( function( a ) {if ( a.rel ) {a.target="_blank"}});
});
//scroll to bottom if near bottom, otherwise don't
if ( window.innerWidth < 601 ) {
var scrollto = document.getElementsByClassName( "content" )[ 0 ].scrollTop;
var offset = 0;
if ( document.getElementsByClassName( "message-content" )[ 0 ] && document.getElementsByClassName( "message-content" )[ 0 ].getElementsByClassName( "message" )[ 0 ] ) {
offset = document.getElementsByClassName( "message-content" )[ 0 ].getElementsByClassName( "message" )[ document.getElementsByClassName( "message-content" )[ 0 ].getElementsByClassName( "message" ).length - 1 ].offsetHeight;
}
if ( offset > 20 ) {
offset = offset - 20;
}
if ( ( document.getElementsByClassName( "content" )[ 0 ].scrollHeight - document.getElementsByClassName( "content" )[ 0 ].scrollTop ) > 1500 ) {
document.getElementsByClassName( "content" )[ 0 ].scrollTo( 0, scrollto + offset );
} else {
document.getElementsByClassName( "content" )[ 0 ].scrollTop = document.getElementsByClassName( "content" )[ 0 ].scrollHeight;
if ( document.getElementsByClassName( "bubbletime" ).length > 0 ) {
document.getElementsByClassName( "bubbletime" )[ document.getElementsByClassName( "bubbletime" ).length - 1 ].scrollIntoView();
}
}
} else {
if ( ( document.getElementsByClassName( "message-content" )[ 0 ].scrollHeight - document.getElementsByClassName( "message-content" )[ 0 ].scrollTop ) > 1500 ) {
return
} else {
document.getElementsByClassName( "message-content" )[ 0 ].scrollTop = document.getElementsByClassName( "message-content" )[ 0 ].scrollHeight;
if ( document.getElementsByClassName( "bubbletime" ).length > 0 ) {
document.getElementsByClassName( "bubbletime" )[ document.getElementsByClassName( "bubbletime" ).length - 1 ].scrollIntoView();
}
}
}
}
//this code runs whenever a dm is clicked, it should also work for "closed dms" where you only see someone's dms if you first add them to your sidebar
if ( kind === 4 && subId.includes( "encrypted-dms" ) ) {
if ( JSON.parse( message[ "data" ] )[ 0 ] == "EOSE" ) return;
if ( document.getElementsByClassName( event.id )[ 0 ] ) return;
if ( localStorage[ "local_pubkey_blacklist" ] ) {
var local_pubkey_blacklist = JSON.parse( localStorage[ "local_pubkey_blacklist" ] );
} else {
var local_pubkey_blacklist = [];
}
if ( local_pubkey_blacklist.includes( event.pubkey ) ) return;
var isMine = ( event.pubkey == pubKey );
if ( !isMine && !array_of_pubkeys.includes( event.pubkey ) ) {
array_of_pubkeys.push( event.pubkey );
}
var now = Math.floor( Date.now() / 1000 ) + Number( sessionStorage[ "time-offset" ] );
var event_time = event.created_at;
var how_long_ago = convertHMS( now - event_time );
if ( now - event_time < -600 ) return;
if ( event.pubkey == pubKey ) {
var otherKey = "";
var i; for ( i=0; i<event.tags.length; i++ ) {
if ( event[ "tags" ][ i ][ 0 ] == "p" && ( !otherKey || otherKey == "" ) ) {
otherKey = event[ "tags" ][ i ][ 1 ];
break;
} else {
otherKey = event.pubkey;
}
}
} else {
otherKey = event.pubkey;
}
if ( !admin && window.nostr ) {
content = await window.nostr.nip04.decrypt( otherKey, content );
} else {
content = await decrypt(privKey, otherKey, content)
}
if ( !document.getElementsByClassName( otherKey.substring( 0, 20 ) )[ 0 ].classList.contains( "active" ) ) return
var color = "";
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "e" || event.pubkey.substring( 0, 1 ) == "f" ) ) {
color = "red";
}
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "c" || event.pubkey.substring( 0, 1 ) == "d" ) ) {
color = "pink";
};
if ( isNaN( event.pubkey.substring( 0, 1 ) ) && ( event.pubkey.substring( 0, 1 ) == "a" || event.pubkey.substring( 0, 1 ) == "b" ) ) {
color = "orange";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 10 ) {
color = "yellow";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 8 ) {
color = "green";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 6 ) {
color = "lightblue";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 4 ) {
color = "blue";
};
if ( !isNaN( event.pubkey.substring( 0, 1 ) ) && event.pubkey.substring( 0, 1 ) < 2 ) {
color = "purple";
};
var name = pubkeyToNpub( event.pubkey ).substring( 0, 15 ) + "...";
//var picture = "https://hashvatar.vercel.app/" + event.pubkey;
var picture = "https://www.gravatar.com/avatar/" + event.pubkey + "?s=128&d=retro";
if ( event.pubkey in namemap && namemap[ event.pubkey ] != "none" ) {
name = namemap[ event.pubkey ];
}
name = DOMPurify.sanitize( name );
if ( event.pubkey in imagemap && imagemap[ event.pubkey ] != "none" ) {
picture = imagemap[ event.pubkey ];
}
content = content.replace( /&/g, "&" ).replace( /</g, "<" ).replace( />/g, ">" );
var shortened_content = content.replace( /\n/g, " " ).substring( 0, 50 );
shortened_content = DOMPurify.sanitize( shortened_content );
if ( content.length > 50 ) {
shortened_content = shortened_content + "...";
}
content = markdownify( content, isMine );
content = DOMPurify.sanitize( content );
var events_replied_to = [];
var pubkeys_replied_to = [];
var i; for ( i=0; i<event.tags.length; i++ ) {
if ( event[ "tags" ][ i ][ 0 ] == "e" ) {
events_replied_to.push( event[ "tags" ][ i ][ 1 ] );
}
if ( event[ "tags" ][ i ][ 0 ] == "p" ) {
pubkeys_replied_to.push( event[ "tags" ][ i ][ 1 ] );
}
}
var replyingTo = "";
if ( events_replied_to[ 0 ] ) {
replyingTo = events_replied_to[ 0 ];
array_of_replies.push( [ event.id, replyingTo ] );
}
array_of_replies.forEach( function( item ) {
if ( item[ 1 ] == event.id ) {
var replyPubkey = event.pubkey;
if ( event.pubkey in namemap && namemap[ event.pubkey ] != "none" ) {
var person_name = namemap[ event.pubkey ];
} else {
var person_name = replyPubkey.substring( 0, 15 );
}
if ( replyPubkey != pubKey ) {
replyAuthor = `<span class="reply-author ${replyPubkey}">` + person_name + `</span>`;
} else {