-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.js
640 lines (572 loc) · 26 KB
/
home.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
//Msg types
//---------
// -2 Noop
// -1 Room welcome
// 1 Regular message
// 3 User room entry announce
$(document).ready(function () {
let hostName = location.hostname;
let hostScheme = window.location.protocol;
let messages = $('div.mesgs');
let inbox = messages.find('div.msg_history');
let isEphemeral = $('input#isEphemeral');
let appIconUrlBase = 'https://podcastindex.org/api/images/';
let pewAudioFile = '/pew.mp3';
let pewAudio = new Audio(pewAudioFile);
const urlParams = new URLSearchParams(window.location.search);
const chat_id = urlParams.get('cid');
var intvlChatPolling = null;
var connection = null;
var lgHasSentMetadata = false;
var lgNostrWaitCycles = 0;
var lgLoggedInWithNostr = false;
var lgUsedNostrBrowserExtension = false;
//Connect to some nostr relays
const nostrRelayPool = new NostrTools.SimplePool();
let nostrRelays = [
'wss://relay.mostr.pub',
'wss://nostr.wine',
'wss://welcome.nostr.wine',
'wss://relay.current.fyi',
'wss://relay.damus.io',
'wss://relay.snort.social',
'wss://relay.nostr.info',
'wss://nos.lol',
'wss://eden.nostr.land',
'wss://brb.io',
'wss://offchain.pub',
'wss://nostr.orangepill.dev'
];
//Get or build a user identity
$('button.msg_submit').prop('disabled', true);
var userProfile = localStorage.getItem('userProfile');
if (userProfile === null) {
var userProfileObject = {};
userProfile = {};
bootbox.dialog({
className: "identityBox",
title: "Who do you want to be?",
closeButton: false,
message: '<form class="bootbox-form">' +
'<input class="bootbox-input form-control imageUpload" disabled>' +
'<input class="bootbox-input bootbox-input-text form-control" type="text" placeholder="Display Name">' +
'<div style="clear:both"> </div>' +
'<input class="imageUploadFilePicker" type="file">' +
'<div class="loginProfileDisplay"><span class="loginAbout"></span></div>' +
'<br>' +
'<input class="bootbox-input bootbox-input-text form-control nostrPrivateKeyInput" ' +
'type="text" ' +
'placeholder="nsec1... paste nostr private key here">' +
'</form>',
buttons: {
Create: {
disabled: "false",
label: "Create",
className: "btn-primary createButton",
callback: function () {
//Get what's in the text box
var username = $('div.bootbox div.modal-body .bootbox-form .bootbox-input-text').val();
//Client side val
if (username.trim() == "") {
console.log("No username given.");
return false;
}
userProfileObject.id = userProfile.pubkey || generateId(63);
userProfileObject.pubkey = userProfile.pubkey || "";
userProfileObject.privkey = userProfile.privkey || "";
userProfileObject.created_at = Date.now() / 1000;
userProfileObject.name = userProfile.name || username;
userProfileObject.display_name = userProfile.display_name || username;
userProfileObject.about = userProfile.about || "";
userProfileObject.picture = userProfile.picture || "";
userProfileObject.banner = userProfile.banner || "";
userProfileObject.website = userProfile.website || "";
//If there isn't a pubkey in the user profile object, create one
//and show it to the user so they can save it
if (userProfileObject.privkey === "" && !lgUsedNostrBrowserExtension) {
userProfileObject.privkey = NostrTools.generatePrivateKey();
}
if (userProfileObject.pubkey === "") {
userProfileObject.pubkey = NostrTools.getPublicKey(userProfileObject.privkey);
}
userProfileObject.id = userProfileObject.pubkey;
//See if the user wants their profile sent to Nostr
if (!lgLoggedInWithNostr) {
bootbox.confirm({
title: 'Identity created!',
message: '<p>Your new identity has been created and save in your browser. Hang on to these' +
' keys in case you ever need to recover it:</p>' +
'<ul class="popupKeyDisplay">' +
'<li>Public: ' + NostrTools.nip19.npubEncode(userProfileObject.pubkey) + '</li>' +
'<li>Private: ' + NostrTools.nip19.nsecEncode(userProfileObject.privkey) + '</li>' +
'</ul>' +
'<p>Do you want to inform the Nostr relay network of your new identity so you can take ' +
'it with you to other places?</p>',
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> No'
},
confirm: {
label: '<i class="fa fa-check"></i> Yes',
className: 'nostrConnect'
}
},
callback: function (result) {
console.log('This was logged in the callback: ' + result);
if (result) {
postNostrMetadata(userProfileObject, nostrRelayPool, nostrRelays).await;
}
}
});
} else {
if ($('div.bootbox input#loginAnnounceNostr').is(':checked')) {
console.log("Announce room to Nostr");
postNostrMessage(
userProfileObject,
"I just joined the live stream chat at: " +
hostScheme + "//" + hostName + "/?cid=" + chat_id,
nostrRelayPool,
nostrRelays);
}
}
//Show the identity keys on the page
displayIdentity(userProfileObject);
//Save the identity we are using
localStorage.setItem('userProfile', JSON.stringify(userProfileObject));
userProfile = userProfileObject;
$('button.msg_submit').prop('disabled', false);
connectWebSocket("[" + truncateUsername(userProfile.display_name) + " just entered the room]");
return true;
}
},
Nostr: {
label: "Nostr",
className: "nostrConnect",
callback: function () {
$('div.bootbox').find('button').prop('disabled', true);
$('div.bootbox').find('button.nostrConnect').empty().html('<i class="fa fa-spin fa-spinner">');
$('div.bootbox').find('.modal-footer').prepend(
'<span class="loadingMessage">Checking for browser extension...</span>'
);
waitForNostr();
return false;
}
}
}
});
$(document).on('change', 'input.imageUploadFilePicker', function () {
let imagedata = encodeImageFileAsURL($(this)[0]);
});
$(document).on('change keyup', 'input.nostrPrivateKeyInput', function () {
let enteredString = $(this).val();
if (enteredString.indexOf("nsec") === 0) {
console.log("Private key manual input", enteredString);
let {type, data} = NostrTools.nip19.decode(enteredString);
console.log("Decoded Nsec key", type, data);
console.log("Re-encoded private key", NostrTools.nip19.nsecEncode(data));
userProfile.privkey = data;
userProfile.pubkey = NostrTools.getPublicKey(data);
getNostrPubkey(nostrRelayPool, nostrRelays);
lgUsedNostrBrowserExtension = false;
$(this).hide();
}
});
} else {
userProfile = JSON.parse(userProfile);
getNostrPubkey(nostrRelayPool, nostrRelays);
displayIdentity(userProfile);
$('button.msg_submit').prop('disabled', false);
}
function encodeImageFileAsURL(element) {
var file = element.files[0];
var reader = new FileReader();
reader.onloadend = function () {
userProfile.picture = reader.result;
$('div.bootbox div.modal-body div.bootbox-body .bootbox-form input.imageUpload')
.css('background-image', 'url(' + userProfile.picture + ')')
.addClass('resolved');
}
reader.readAsDataURL(file);
}
//Wait for window.nostr to show up by looping and checking
//if the object becomes defined
function waitForNostr() {
console.log("Wait for nostr");
lgNostrWaitCycles++;
if (typeof window.nostr !== "undefined") {
//variable exists, do what you want
getNostrPubkey(nostrRelayPool, nostrRelays);
lgUsedNostrBrowserExtension = true;
} else {
if (lgNostrWaitCycles > 8) {
allowPrivateKeyInput();
} else {
setTimeout(waitForNostr, 250);
}
}
}
//Nostr browser extension isn't installed so drop back to private key entry
function allowPrivateKeyInput() {
$('div.bootbox').find('button.nostrConnect').empty().text('Nostr');
$('div.bootbox div.modal-body div.bootbox-body .bootbox-form .bootbox-input.nostrPrivateKeyInput').show();
$('div.bootbox').find('.modal-footer span.loadingMessage').html(
'Not found. <a href="https://chrome.google.com/webstore/detail/nos2x/kpgefcfmnafjgpblomihpgmejjdanjjp">Get one</a>?'
);
$('div.bootbox').find('button').prop('disabled', false);
}
//Send a set_metadata event to the nostr relays for this new identity
async function postNostrMetadata(profileData, pool, relays) {
nostrMetadata = {};
nostrMetadata.name = profileData.name;
nostrMetadata.display_name = profileData.display_name;
nostrMetadata.about = profileData.about;
nostrMetadata.picture = profileData.picture;
nostrMetadata.banner = profileData.banner;
nostrMetadata.website = profileData.website;
let event = {
id: 0,
kind: 0,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: JSON.stringify(nostrMetadata),
pubkey: profileData.pubkey,
"sig": ""
}
event.id = NostrTools.getEventHash(event)
event = await signNostrEvent(event, profileData);
let pubs = pool.publish(relays, event)
pubs.on('ok', () => {
console.log(`relay has accepted our set_metadata event`)
lgHasSentMetadata = true;
})
pubs.on('failed', reason => {
console.log(`failed to publish set_metadata to relay: ${reason}`)
})
}
async function getNostrPubkey(pool, relays) {
if (typeof userProfile.pubkey === "undefined" || userProfile.pubkey == "") {
userProfile.pubkey = await window.nostr.getPublicKey();
}
$('div.bootbox').find('button.nostrConnect').empty().html('<i class="fa fa-spin fa-spinner">');
console.log(userProfile.pubkey);
let sub = pool.sub(
relays,
[
{
authors: [
userProfile.pubkey
],
kinds: [0]
}
]
)
sub.on('event', event => {
console.log('Got the profile metadata:', event);
//Hang on to the user profile data
nostrUserProfile = JSON.parse(event.content);
console.log("Nostr Profile", nostrUserProfile);
userProfile.name = nostrUserProfile.name.trim();
userProfile.about = nostrUserProfile.about.trim();
userProfile.picture = nostrUserProfile.picture.trim();
//Update the displayed identity keys
displayIdentity(userProfile);
//If the identity modal dialog is currently on-screen, take appropriate action
//to fill in it's details with this user's nostr info
if ($('div.bootbox .bootbox-form').length > 0) {
let nameToShow = nostrUserProfile.display_name || userProfile.name || "";
$('div.bootbox .bootbox-form .bootbox-input-text').val(nameToShow);
$('div.bootbox .bootbox-form input.imageUpload')
.css('background-image', 'url(' + userProfile.picture + ')')
.addClass('resolved');
$('div.bootbox .bootbox-form div.loginProfileDisplay .loginAbout').text(userProfile.about);
//Expose the nostr announcement option if it isn't already visible
if ($('div.bootbox div.modal-footer div.loginAnnounceChatToNostr').length === 0) {
$('div.bootbox div.modal-footer span.loadingMessage').remove();
$('div.bootbox div.modal-footer').prepend(
'<div class="loginAnnounceChatToNostr">' +
' <label for="loginAnnounceNostr">Announce this chat to your Nostr feed?</label>' +
' <input id="loginAnnounceNostr" type="checkbox">' +
'</div>'
);
}
$('div.bootbox').find('button.nostrConnect').remove();
$('div.bootbox').find('input.imageUploadFilePicker').remove();
$('div.bootbox').find('button.createButton').text("Login");
$('div.bootbox').find('button').prop('disabled', false);
}
lgLoggedInWithNostr = true;
})
let events = await pool.list(relays, [{kinds: [0]}]);
let event = await pool.get(relays, {authors: [userProfile.pubkey]});
}
//Send a new post to nostr
async function postNostrMessage(profileData, message, pool, relays) {
nostrMetadata = {};
nostrMetadata.name = profileData.name;
nostrMetadata.display_name = profileData.display_name;
nostrMetadata.about = profileData.about;
nostrMetadata.picture = profileData.picture;
nostrMetadata.banner = profileData.banner;
nostrMetadata.website = profileData.website;
let event = {
id: 0,
pubkey: profileData.pubkey,
created_at: Math.floor(Date.now() / 1000),
kind: 1,
tags: [],
content: message + "\n\n#testing #podcasting20 #mkultra",
sig: ""
}
event.id = NostrTools.getEventHash(event);
event = await signNostrEvent(event, profileData);
let pubs = pool.publish(relays, event);
pubs.on('ok', (relay) => {
console.log(`relay: [${relay}] has accepted our message posting event`);
})
pubs.on('failed', reason => {
console.log(`failed to publish message to relay: ${reason}`);
})
}
//_via: https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript?answertab=votes#tab-top
// dec2hex :: Integer -> String
// i.e. 0-255 -> '00'-'ff'
function dec2hex(dec) {
return dec.toString(16).padStart(2, "0");
}
// generateId :: Integer -> String
function generateId(len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
//Submit message
$('textarea.msg_text').keyup(function (event) {
if (event.keyCode === 13) {
if (event.shiftKey === false) {
$('button.msg_submit').click();
}
}
});
$(document).on('click', 'button.msg_submit', function () {
//Get the message text
let messageText = $('textarea.msg_text').val();
//Bail if the message text is empty
if (messageText.trim() == "") {
return false;
}
//Try sending to Nostr if the ephemeral box isn't checked
var sendToNostr = false;
if (!isEphemeral.is(":checked")) {
sendToNostr = true;
}
submitChatMessage(messageText, sendToNostr, 1);
$('textarea.msg_text').val('');
});
async function submitChatMessage(textMessage, withNostr, msgType) {
let lastMsgId = $('div.outgoing_msg:last').data('msgid');
if (typeof lastMsgId === "undefined") {
lastMsgId = 0;
}
//Get a correct name to use
var postingName = userProfile.display_name || userProfile.name;
let event = {
"id": lastMsgId,
"created_at": Math.trunc(Date.now() / 1000),
"pubkey": userProfile.pubkey,
"user_name": postingName.trim().substring(0, 32),
"content": textMessage,
"tags": [],
"chat_id": chat_id,
"picture": userProfile.picture,
"kind": msgType,
"sig": ""
};
event.id = NostrTools.getEventHash(event);
console.log("Event hash", event.id);
//Sign the event
event = await signNostrEvent(event, userProfile);
//Fix the event after signing
delete event.tags;
event.id = lastMsgId;
//Send the event to the websocket
connection.send(JSON.stringify(event));
//Only send posts to Nostr when the ephemeral control checkbox is unchecked
if (withNostr) {
if (!lgHasSentMetadata) {
postNostrMetadata(userProfile, nostrRelayPool, nostrRelays).await;
}
postNostrMessage(userProfile, textMessage, nostrRelayPool, nostrRelays);
}
}
//Websocket testing
window.WebSocket = window.WebSocket || window.MozWebSocket;
wsConnector = setInterval(function () {
//console.log("Checking websocket...");
//console.log(connection);
if (connection === null || connection.readyState === 3) {
connectWebSocket();
}
}, 3000);
function connectWebSocket(announce) {
connection = new WebSocket('wss://' + window.location.hostname + ':8443');
var connectingSpan = document.getElementById("connecting");
console.log("Connecting...");
var postEnterMessage = announce || false;
connection.onopen = function () {
//connectingSpan.style.display =
pollForMessages(connection);
console.log("Websocket connected.");
if (postEnterMessage !== false && userProfile.id !== "") {
submitChatMessage(postEnterMessage, false, 3);
}
};
connection.onerror = function (error) {
//connectingSpan.innerHTML = "Error occured";
console.log("Websocket error.");
};
connection.onmessage = function (message) {
var data = JSON.parse(message.data);
var someNew = false;
if (Array.isArray(data)) {
data.forEach((element, index) => {
if ($('div.outgoing_msg[data-msgid=' + element.id + ']').length == 0) {
if (element.pubkey !== userProfile.pubkey) {
someNew = true;
}
let message = element.content || "";
let msgtype = element.kind || 1;
let userName = element.user_name || "";
let userPicture = element.picture || "";
let dateTime = new Date(element.created_at * 1000).toISOString();
var msgClassName = "";
//If this is the top welcome post
if (msgtype == -1) {
msgClassName = "welcome";
}
//Call out our own messages a bit visually different
var fixedUserName = truncateUsername(userName);
var nameDisplay = ' <h5>' + fixedUserName + ':</h5> ';
if (element.pubkey == userProfile.pubkey) {
msgClassName += " self_msg";
var nameDisplay = "";
}
//Add some modifier classes to the message text if needed
var messageClasses = "";
//Write the message to the screen
inbox.append('' +
'<div class="outgoing_msg message msgtype' + msgtype + ' ' + msgClassName + '" data-msgid="' + element.id + '">' +
' <div class="sent_msg">' +
' <div class="outgoing_msg_img">' +
' <img src="' + userPicture + '">' +
' </div>' +
' <div class="sent_withd_msg">' +
nameDisplay +
' <span class="time_date" data-timestamp="' + dateTime + '">' +
prettyDate(dateTime) + '' +
' </span>' +
' <span class="messageText' + messageClasses + '">' + message + '</span>' +
' </div>' +
' </div>' +
'</div>');
inbox.scrollTop(900719925474099);
}
});
}
if (someNew) {
//pewAudio.play();
}
};
connection.onclose = function () {
console.log("Websocket closed.");
};
pollForMessages = function (conn) {
let lastMsgId = $('div.outgoing_msg:last').data('msgid');
if (typeof lastMsgId === "undefined") {
lastMsgId = 0;
}
// console.log(lastMsgId);
let event = {
"id": lastMsgId,
"created_at": Math.trunc(Date.now() / 1000),
"pubkey": "",
"user_name": "",
"content": "",
"tags": [],
"chat_id": chat_id,
"picture": "",
"kind": -2,
"sig": ""
};
conn.send(JSON.stringify(event));
};
}
connectWebSocket();
//Show the identity of the logged in user at the bottom
function displayIdentity(profileObject) {
if (typeof profileObject.pubkey === "string" && profileObject.pubkey != "") {
$('div.identity_display span.pubkey').text(
NostrTools.nip19.npubEncode(profileObject.pubkey)
);
}
if (typeof profileObject.privkey === "string" && profileObject.privkey != "") {
$('div.identity_display span.privkey').text(
NostrTools.nip19.nsecEncode(profileObject.privkey)
);
}
if (typeof profileObject.picture === "string" && profileObject.picture != "") {
$('img.userAvatarHeader').attr('src', profileObject.picture);
}
}
//Update timestamps
function updateTimeStamps() {
let timestamps = $('div.message span.time_date');
timestamps.each(function (idx) {
let timestamp = $(this).data('timestamp');
$(this).html(prettyDate(timestamp));
});
}
//The profile edit modal
$(document).on('click', 'a#editProfile', function () {
bootbox.confirm({
title: 'Logout?',
message: 'Would you like to log out of your current identity? Be sure you saved your keys.',
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> No'
},
confirm: {
label: '<i class="fa fa-check"></i> Yes',
className: 'btn-danger'
}
},
callback: function (result) {
console.log('This was logged in the callback: ' + result);
if (result) {
localStorage.removeItem('userProfile');
location.reload();
}
}
});
});
function truncateUsername(userName) {
var fixedUserName = userName.trim();
if (fixedUserName.length > 32) {
fixedUserName = fixedUserName.substring(0, 29) + "...";
}
return fixedUserName;
}
async function signNostrEvent(event, profileData) {
console.log("Event", event);
var signedEvent = {};
if (typeof window.nostr !== "undefined") {
console.log(await window.nostr.getPublicKey());
return await window.nostr.signEvent(event);
} else {
event.sig = NostrTools.signEvent(event, profileData.privkey);
return event;
}
}
setInterval(function () {
updateTimeStamps();
}, 59000);
});