-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
786 lines (761 loc) · 32.2 KB
/
chat.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
// Toggle logs for testing
const enableLogging = false;
// Variables to store user data and state
let userAccount = null;
let userAddress = null;
let userPublicKey = null;
let userName = null;
let groupMessages = [];
let isLoggedIn = false;
let refreshIntervalId = null;
// Cache for address & avatar mapping
let addressNameMap = {};
let avatarCache = {};
// To keep track of selected users
let selectedUsers = new Set();
// Declare a global variable to store the attached file
let attachedFile = null;
// Mapping of displayed messages
let displayedMessages = {};
document.getElementById('attach-button').style.display = 'none';
// Initialize the application
async function init() {
document.getElementById('login-button').addEventListener('click', login);
document.getElementById('send-button').addEventListener('click', sendMessage);
document.getElementById('message-input').addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
event.preventDefault(); // Prevents adding a newline
sendMessage();
}
});
document.getElementById('toggle-user-list-button').addEventListener('click', function() {
const sidebar = document.getElementById('sidebar-right');
if (sidebar.style.display === 'none' || sidebar.style.display === '') {
sidebar.style.display = 'block';
} else {
sidebar.style.display = 'none';
}
// Mobile compatibility
if (sidebar.classList.contains('show')) {
sidebar.classList.remove('show');
} else {
sidebar.classList.add('show');
}
});
// Add event listener for the "Attach" button
document.getElementById('attach-button').addEventListener('click', function() {
// Create a hidden file input if it doesn't exist
let fileInput = document.getElementById('file-input');
if (!fileInput) {
fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.id = 'file-input';
fileInput.style.display = 'none';
fileInput.addEventListener('change', handleFileSelect);
document.body.appendChild(fileInput);
}
fileInput.value = null; // Reset the file input
fileInput.click();
});
// Load messages and users
await loadMessagesAndUsers();
// Periodically refresh messages and users
refreshIntervalId = setInterval(() => {
loadMessagesAndUsers().catch(error => console.error(error)); // Refresh every 15 seconds
}, 15000);
}
// Base58 encoding and decoding functions
(function () {
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const ALPHABET_MAP = {};
for (let i = 0; i < ALPHABET.length; i++) {
ALPHABET_MAP[ALPHABET.charAt(i)] = i;
}
const BASE = 58;
function base58Encode(buffer) {
if (buffer.length === 0) return '';
let digits = [0];
for (let i = 0; i < buffer.length; i++) {
let carry = buffer[i];
for (let j = 0; j < digits.length; j++) {
const x = digits[j] * 256 + carry;
digits[j] = x % BASE;
carry = Math.floor(x / BASE);
}
while (carry > 0) {
digits.push(carry % BASE);
carry = Math.floor(carry / BASE);
}
}
// Deal with leading zeros
for (let i = 0; i < buffer.length && buffer[i] === 0; i++) {
digits.push(0);
}
return digits.reverse().map(d => ALPHABET[d]).join('');
}
function base58Decode(string) {
if (string.length === 0) return new Uint8Array(0);
let bytes = [0];
for (let i = 0; i < string.length; i++) {
const c = string[i];
const value = ALPHABET_MAP[c];
if (value === undefined) {
throw new Error('Invalid base58 character');
}
let carry = value;
for (let j = 0; j < bytes.length; j++) {
const x = bytes[j] * BASE + carry;
bytes[j] = x & 0xff;
carry = x >> 8;
}
while (carry > 0) {
bytes.push(carry & 0xff);
carry >>= 8;
}
}
// Deal with leading zeros
for (let i = 0; i < string.length && string[i] === ALPHABET[0]; i++) {
bytes.push(0);
}
return new Uint8Array(bytes.reverse());
}
// Expose the functions globally
window.base58Encode = base58Encode;
window.base58Decode = base58Decode;
})();
// Combined function to load messages and active users
async function loadMessagesAndUsers() {
try {
const chatMessages = document.getElementById('chat-messages');
// Check if the user is at the bottom before updating messages
const isAtBottom = chatMessages.scrollHeight - chatMessages.clientHeight - chatMessages.scrollTop <= 50;
// Fetch messages for groupId 0
const messages = await qortalRequest({
action: 'SEARCH_CHAT_MESSAGES',
txGroupId: 0
});
groupMessages = messages;
// Build message map to handle edits and replies
let messageMap = {};
for (let msg of groupMessages) {
let messageId = msg.chatReference || msg.signature;
let existingMessage = messageMap[messageId];
if (!existingMessage || msg.timestamp > existingMessage.timestamp) {
messageMap[messageId] = msg;
}
}
// Get all messages and sort them by timestamp
let allMessages = Object.values(messageMap);
allMessages.sort((a, b) => a.timestamp - b.timestamp);
// Build set of all active users from all messages (before filtering)
let allActiveUsers = new Set();
for (let msg of allMessages) {
allActiveUsers.add(msg.sender);
}
// Now filter messagesToDisplay based on selected users
let messagesToDisplay = allMessages;
if (selectedUsers.size > 0) {
messagesToDisplay = messagesToDisplay.filter(msg => selectedUsers.has(msg.sender));
}
// Build a set of current message signatures
let currentMessageSignatures = new Set(messagesToDisplay.map(msg => msg.signature));
// Remove messages that are no longer in messagesToDisplay
for (let signature in displayedMessages) {
if (!currentMessageSignatures.has(signature)) {
// Remove from DOM
let messageElement = displayedMessages[signature];
if (messageElement && messageElement.parentNode) {
messageElement.parentNode.removeChild(messageElement);
}
// Remove from displayedMessages
delete displayedMessages[signature];
}
}
// For each message in messagesToDisplay
for (let msg of messagesToDisplay) {
const messageId = msg.signature;
const existingMessageElement = displayedMessages[messageId];
if (!existingMessageElement) {
// Message not displayed yet, create and append it
const messageElement = await createMessageElement(msg, messageMap);
chatMessages.appendChild(messageElement);
displayedMessages[messageId] = messageElement;
} else {
// Message already displayed, check if it needs to be updated
if (msg.timestamp > existingMessageElement.dataset.timestamp) {
// Update message content
const newMessageElement = await createMessageElement(msg, messageMap);
chatMessages.replaceChild(newMessageElement, existingMessageElement);
displayedMessages[messageId] = newMessageElement;
}
}
}
// If the user was at the bottom, scroll to the bottom
if (isAtBottom) {
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Update the user list with all active users
updateUserList(allActiveUsers);
} catch (error) {
console.error('Error loading messages and users:', error);
displayLogMessage(`Error loading messages and users: ${error}`);
}
}
async function createMessageElement(msg, messageMap) {
const messageElement = document.createElement('div');
messageElement.classList.add('message-item');
const senderName = await getNameForAddress(msg.sender);
// Assign a unique ID to each message element
messageElement.id = 'msg-' + msg.signature;
// Store the timestamp as a data attribute
messageElement.dataset.timestamp = msg.timestamp;
// Highlight messages from the logged-in user
if (isLoggedIn && msg.sender === userAddress) {
messageElement.classList.add('highlighted-message');
}
// Create avatar image
const avatarImg = document.createElement('img');
const name = addressNameMap[msg.sender] || msg.sender;
avatarImg.src = avatarCache[name] || 'default-avatar.png';
// Create message content
const messageContent = document.createElement('div');
messageContent.classList.add('message-content');
let messageText = '';
if (msg.isEncrypted) {
messageText = '[Encrypted Message]';
} else {
try {
const decodedBytes = base58Decode(msg.data);
const jsonString = new TextDecoder().decode(decodedBytes);
const messageObject = JSON.parse(jsonString);
// Extract text from messageText
messageText = extractTextFromMessage(messageObject.messageText);
// Handle replies
if (messageObject.repliedTo) {
const repliedToSignature = messageObject.repliedTo;
const repliedToMsg = messageMap[repliedToSignature];
if (repliedToMsg) {
let repliedToText = '';
if (repliedToMsg.isEncrypted) {
repliedToText = '[Encrypted Message]';
} else {
try {
const decodedRepliedBytes = base58Decode(repliedToMsg.data);
const repliedJsonString = new TextDecoder().decode(decodedRepliedBytes);
const repliedMessageObject = JSON.parse(repliedJsonString);
repliedToText = extractTextFromMessage(repliedMessageObject.messageText);
} catch (e) {
repliedToText = '[Unable to decode replied message]';
}
}
// Create a div for the replied-to message
const repliedToElement = document.createElement('div');
repliedToElement.classList.add('replied-to-message');
repliedToElement.innerHTML = `<strong>${await getNameForAddress(repliedToMsg.sender)}</strong>: ${repliedToText}`;
// Add the replied-to message above the current message content
messageContent.insertBefore(repliedToElement, messageContent.firstChild);
} else {
const repliedToElement = document.createElement('div');
repliedToElement.classList.add('replied-to-message');
repliedToElement.textContent = '[Original message not found]';
messageContent.insertBefore(repliedToElement, messageContent.firstChild);
}
}
// Handle embedded images
if (messageObject.images && Array.isArray(messageObject.images) && messageObject.images[0] !== "") {
for (let image of messageObject.images) {
const imageUrl = `/arbitrary/${image.service}/${image.name}/${image.identifier}`;
messageText += `<br><img src="${imageUrl}" alt="Embedded Image">`;
}
}
// Process qortal:// links within the message text
messageText = processQortalLinks(messageText);
} catch (e) {
messageText = '[Unable to decode message]';
}
}
messageContent.innerHTML += `<strong>${senderName}</strong> <span style="color: gray; font-size: 0.8em;">${formatTimestamp(msg.timestamp)}</span><br>${messageText}`;
// Append avatar and content to message element
messageElement.appendChild(avatarImg);
messageElement.appendChild(messageContent);
return messageElement;
}
function updateUserList(activeUsers) {
const userList = document.getElementById('user-list');
userList.innerHTML = '';
for (let user of activeUsers) {
const userItem = document.createElement('li');
userItem.classList.add('user-item');
const userName = addressNameMap[user] || user;
// Highlight selected users
if (selectedUsers.has(user)) {
userItem.classList.add('selected-user');
}
const avatarImg = document.createElement('img');
avatarImg.src = avatarCache[userName] || 'default-avatar.png';
const userNameSpan = document.createElement('span');
userNameSpan.textContent = userName;
userItem.appendChild(avatarImg);
userItem.appendChild(userNameSpan);
// Add click event listener
userItem.addEventListener('click', () => {
if (selectedUsers.has(user)) {
selectedUsers.delete(user);
userItem.classList.remove('selected-user');
} else {
selectedUsers.add(user);
userItem.classList.add('selected-user');
}
// Reload messages with filtering
loadMessagesAndUsers();
});
userList.appendChild(userItem);
}
}
// Function to display log messages in the chat window
function displayLogMessage(message) {
if (enableLogging) {
const chatMessages = document.getElementById('chat-messages');
const logElement = document.createElement('div');
logElement.textContent = `[Log]: ${message}`;
logElement.style.color = 'gray';
logElement.style.fontStyle = 'italic';
chatMessages.appendChild(logElement);
}
}
// Function to display log messages in the user list
function displayUserLogMessage(message) {
if (enableLogging) {
const userList = document.getElementById('user-list');
const logElement = document.createElement('li');
logElement.textContent = `[Log]: ${message}`;
logElement.style.color = 'gray';
logElement.style.fontStyle = 'italic';
userList.appendChild(logElement);
}
}
// Handle user login
async function login() {
if (isLoggedIn) {
// Logout
userAccount = null;
userAddress = null;
userPublicKey = null;
userName = null;
isLoggedIn = false;
// Update login button text
document.getElementById('login-button').textContent = 'Login';
document.getElementById('attach-button').style.display = 'none';
displayLogMessage('Logged out.');
} else {
// Login
try {
const account = await qortalRequest({
action: "GET_USER_ACCOUNT"
});
userAccount = account;
userAddress = account.address;
userPublicKey = account.publicKey;
isLoggedIn = true;
// Get user name
userName = await getNameForAddress(userAddress);
// Update login button text
document.getElementById('login-button').textContent = 'Logout';
document.getElementById('attach-button').style.display = 'block';
// Display logging message: Login successful
displayLogMessage(`Logged in as ${userName}.`);
} catch (error) {
console.error('Error during login:', error);
displayLogMessage(`Error during login: ${error}`);
}
}
}
// Function to get the name for an address
async function getNameForAddress(address) {
if (addressNameMap[address]) {
return addressNameMap[address];
} else {
try {
const names = await qortalRequest({
action: 'GET_ACCOUNT_NAMES',
address: address
});
if (names && names.length > 0) {
const name = names[0].name;
addressNameMap[address] = name;
// Fetch avatar
fetchAvatarForName(name);
return name;
} else {
const truncatedAddress = address.substring(0, 5) + '...' + address.substring(address.length - 5);
addressNameMap[address] = truncatedAddress;
return truncatedAddress;
}
} catch (error) {
console.error('Error fetching name for address:', error);
const truncatedAddress = address.substring(0, 5) + '...' + address.substring(address.length - 5);
addressNameMap[address] = truncatedAddress;
return truncatedAddress;
}
}
}
function fetchAvatarForName(name) {
if (avatarCache[name]) return; // Avatar already cached
const avatarUrl = `/arbitrary/THUMBNAIL/${name}/qortal_avatar`;
const img = new Image();
img.src = avatarUrl;
img.onload = function() {
avatarCache[name] = avatarUrl;
};
img.onerror = function() {
// Use a default avatar or do nothing
avatarCache[name] = 'default-avatar.png'; // Ensure you have a default avatar image
};
}
// Send a message to groupId 0
function sendMessage() {
const messageInput = document.getElementById('message-input');
let message = messageInput.value.trim();
// If no message text and no attached file, do nothing
if (message === '' && !attachedFile) return;
// Stop the periodic refresh
clearInterval(refreshIntervalId);
// Disable the send button and message input
document.getElementById('send-button').disabled = true;
messageInput.disabled = true;
// Dim the input area
document.getElementById('chat-input').style.opacity = '0.5';
// Show a note saying "Sending..."
let sendingNote = document.getElementById('sending-note');
if (!sendingNote) {
sendingNote = document.createElement('div');
sendingNote.id = 'sending-note';
sendingNote.textContent = 'Sending...';
sendingNote.style.color = 'gray';
sendingNote.style.fontStyle = 'italic';
document.getElementById('chat-input').appendChild(sendingNote);
}
// Display logging message: Sending message
displayLogMessage('Sending message...');
// Function to actually send the message after handling the file (if any)
async function proceedToSendMessage() {
// Send group message to groupId 0
try {
const response = await qortalRequest({
action: "SEND_CHAT_MESSAGE",
groupId: "0", // send to groupId 0
message: message
});
if (response && !response.error) {
// Clear the input field upon successful send
messageInput.value = '';
// Display logging message: Message sent
displayLogMessage('Message sent successfully.');
// Log the response for further investigation
console.log('Response from SEND_CHAT_MESSAGE:', response);
displayLogMessage(`Response: ${JSON.stringify(response)}`);
// Reload messages and users
loadMessagesAndUsers();
} else {
// Handle unexpected response
console.error('Unexpected response from SEND_CHAT_MESSAGE:', response);
displayLogMessage(`Unexpected response: ${JSON.stringify(response)}`);
alert('Error sending message: Unexpected response from server.');
}
} catch (error) {
console.error('Error sending message:', error);
displayLogMessage(`Error sending message: ${error}`);
alert('Error sending message. Please try again.');
} finally {
// Re-enable the send button and message input
document.getElementById('send-button').disabled = false;
messageInput.disabled = false;
// Restore the opacity
document.getElementById('chat-input').style.opacity = '1';
// Remove the "Sending..." note
let sendingNote = document.getElementById('sending-note');
if (sendingNote) {
sendingNote.remove();
}
// Clear the attached file and filename display
attachedFile = null;
document.getElementById('selected-file-name').textContent = '';
// Restart the periodic refresh
refreshIntervalId = setInterval(loadMessagesAndUsers, 15000);
}
}
// Check if there is an attached file
if (attachedFile) {
// Determine the service type based on the file type
let serviceType = '';
const fileType = attachedFile.type;
if (fileType.startsWith('image/')) {
serviceType = 'QCHAT_IMAGE';
} else if (fileType.startsWith('audio/')) {
serviceType = 'QCHAT_AUDIO';
} else if (fileType.startsWith('video/')) {
serviceType = 'VIDEO';
} else if (fileType === 'text/plain' || fileType === '') {
serviceType = 'DOCUMENT';
} else {
alert('Unsupported file type.');
// Re-enable the send button and message input
document.getElementById('send-button').disabled = false;
messageInput.disabled = false;
// Restore the opacity
document.getElementById('chat-input').style.opacity = '1';
// Remove the "Sending..." note
let sendingNote = document.getElementById('sending-note');
if (sendingNote) {
sendingNote.remove();
}
return;
}
// Generate a random 6-character identifier
const identifierSuffix = generateRandomString(6);
// Construct the identifier
const identifier = `qchat_group_0_${identifierSuffix}`;
// Prepare the publish request
const publishRequest = {
action: "PUBLISH_QDN_RESOURCE",
name: userName,
service: serviceType,
identifier: identifier,
file: attachedFile,
filename: attachedFile.name
};
// Publish the file
qortalRequest(publishRequest).then(response => {
if (response && !response.error) {
// Construct the qortal link
const qortalLink = `qortal://${serviceType}/${userName}/${identifier}`;
// Prepend the qortal link and a line break to the message
message = `${qortalLink}\n${message}`;
// Proceed to send the message
messageInput.value = message;
proceedToSendMessage();
} else {
console.error('Error publishing file:', response);
displayLogMessage(`Error publishing file: ${JSON.stringify(response)}`);
alert('Error publishing file. Please try again.');
// Re-enable the send button and message input
document.getElementById('send-button').disabled = false;
messageInput.disabled = false;
// Restore the opacity
document.getElementById('chat-input').style.opacity = '1';
// Remove the "Sending..." note
let sendingNote = document.getElementById('sending-note');
if (sendingNote) {
sendingNote.remove();
}
}
}).catch(error => {
console.error('Error publishing file:', error);
displayLogMessage(`Error publishing file: ${error}`);
alert('Error publishing file. Please try again.');
// Re-enable the send button and message input
document.getElementById('send-button').disabled = false;
messageInput.disabled = false;
// Restore the opacity
document.getElementById('chat-input').style.opacity = '1';
// Remove the "Sending..." note
let sendingNote = document.getElementById('sending-note');
if (sendingNote) {
sendingNote.remove();
}
});
} else {
// No attached file, proceed to send the message
proceedToSendMessage();
}
}
function extractTextFromMessage(node) {
let resultText = '';
function traverse(node) {
if (node.type === 'text') {
let text = node.text;
if (node.marks) {
for (let mark of node.marks) {
if (mark.type === 'bold') {
text = '<b>' + text + '</b>';
} else if (mark.type === 'italic') {
text = '<i>' + text + '</i>';
} else if (mark.type === 'underline') {
text = '<u>' + text + '</u>';
}
// Add more formatting as needed
}
}
// Process qortal:// links in the text
text = processQortalLinks(text);
resultText += text;
} else if (node.type === 'paragraph') {
if (node.content && Array.isArray(node.content)) {
node.content.forEach(childNode => {
traverse(childNode);
});
}
resultText += '<br>';
} else if (node.type === 'hardBreak') {
resultText += '<br>';
} else if (node.type === 'heading') {
let level = node.attrs && node.attrs.level ? node.attrs.level : 1;
resultText += `<h${level}>`;
if (node.content && Array.isArray(node.content)) {
node.content.forEach(childNode => {
traverse(childNode);
});
}
resultText += `</h${level}>`;
} else if (node.type === 'codeBlock') {
resultText += '<pre>';
if (node.content && Array.isArray(node.content)) {
node.content.forEach(childNode => {
traverse(childNode);
});
}
resultText += '</pre>';
} else if (node.content && Array.isArray(node.content)) {
node.content.forEach(childNode => {
traverse(childNode);
});
}
}
traverse(node);
return resultText;
}
function processQortalLinks(text) {
// Use a regular expression to find qortal:// links
return text.replace(/qortal:\/\/[^\s<>"']+/g, function(match) {
const displayText = escapeHtml(match);
const encodedLink = encodeURIComponent(match);
// Extract the service, name, and identifier
const path = match.substring('qortal://'.length);
const parts = path.split('/');
const service = parts[0];
const name = parts[1];
const identifier = parts.slice(2).join('/'); // In case identifier contains slashes
// Handle 'use-group/action-join/groupid-XXX' links
if (service === 'use-group' && name === 'action-join') {
return `<a href="#" style="color: dodgerblue; text-decoration: underline;" onclick="joinGroup('${encodedLink}'); return false;">${displayText}</a>`;
}
// For APP and WEBSITE service types
else if (service === 'APP' || service === 'WEBSITE') {
return `<a href="#" style="color: dodgerblue; text-decoration: underline;" onclick="openQortalLink('${encodedLink}'); return false;">${displayText}</a>`;
}
// For embeddable service types
else if (['THUMBNAIL', 'QCHAT_IMAGE', 'IMAGE', 'VIDEO', 'AUDIO', 'QCHAT_AUDIO', 'VOICE', 'BLOG', 'BLOG_POST', 'BLOG_COMMENT', 'DOCUMENT'].includes(service)) {
const url = `/arbitrary/${service}/${name}/${identifier}`;
// Decide how to embed based on service type
if (['IMAGE', 'THUMBNAIL', 'QCHAT_IMAGE'].includes(service)) {
return `<img src="${url}" alt="${displayText}">`;
} else if (['VIDEO'].includes(service)) {
return `<video controls src="${url}"></video>`;
} else if (['AUDIO', 'QCHAT_AUDIO', 'VOICE'].includes(service)) {
return `<audio controls src="${url}"></audio>`;
} else if (['BLOG', 'BLOG_POST', 'BLOG_COMMENT', 'DOCUMENT'].includes(service)) {
return `<embed type="text/html" src="${url}"></embed>`;
} else {
return `<a href="#" onclick="openQortalLink('${encodedLink}'); return false;">${displayText}</a>`;
}
} else {
// Default action for other service types
return `<a href="#" style="color: dodgerblue; text-decoration: underline;" onclick="openQortalLink('${encodedLink}'); return false;">${displayText}</a>`;
}
});
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function openQortalLink(encodedQortalLink) {
const qortalLink = decodeURIComponent(encodedQortalLink);
qortalRequest({
action: 'OPEN_NEW_TAB',
qortalLink: qortalLink
}).then(response => {
// Optionally handle the response
}).catch(error => {
console.error('Error opening Qortal link:', error);
});
}
function joinGroup(encodedQortalLink) {
const qortalLink = decodeURIComponent(encodedQortalLink);
const path = qortalLink.substring('qortal://'.length);
const parts = path.split('/');
const service = parts[0]; // Should be 'use-group'
const action = parts[1]; // Should be 'action-join'
const groupIdPart = parts[2]; // Should be 'groupid-XXX'
// Extract groupId from 'groupid-XXX'
const groupIdMatch = groupIdPart.match(/groupid-(\d+)/);
if (groupIdMatch && groupIdMatch[1]) {
const groupId = parseInt(groupIdMatch[1], 10);
// Call the JOIN_GROUP action
qortalRequest({
action: "JOIN_GROUP",
groupId: groupId
}).then(response => {
if (response === true || (response && !response.error)) {
alert(`Successfully joined group ${groupId}.`);
} else {
console.error('Error joining group:', response);
alert('Error joining group. Please try again.');
}
}).catch(error => {
console.error('Error joining group:', error);
alert('Error joining group. Please try again.');
});
} else {
alert('Invalid group link.');
}
}
function formatTimestamp(timestamp) {
let date = new Date(timestamp);
return date.toLocaleTimeString(); // Adjust format as needed
}
// Base58 encoding and decoding using bs58
function base58Decode(string) {
return bs58.decode(string);
}
function base58Encode(buffer) {
return bs58.encode(buffer);
}
// Convert hex string to Uint8Array
function hexToUint8Array(hexString) {
if (hexString.length % 2 !== 0) {
hexString = '0' + hexString;
}
const byteArray = new Uint8Array(hexString.length / 2);
for (let i = 0; i < byteArray.length; i++) {
byteArray[i] = parseInt(hexString.substr(i * 2, 2), 16);
}
return byteArray;
}
// Define the handleFileSelect function to handle file selection
function handleFileSelect(event) {
attachedFile = event.target.files[0];
if (attachedFile) {
document.getElementById('selected-file-name').textContent = `Attached: ${attachedFile.name}`;
} else {
document.getElementById('selected-file-name').textContent = '';
}
}
// Utility function to generate a random 6-character string
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;
const cryptoObj = window.crypto || window.msCrypto; // For IE 11
const randomValues = new Uint32Array(length);
cryptoObj.getRandomValues(randomValues);
for (let i = 0; i < length; i++) {
result += characters.charAt(randomValues[i] % charactersLength);
}
return result;
}
// Start the application
init();