vybe/client/message.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-04-15 21:52:21 -07:00
import { render, html } from '/uhtml.js';
let msg;
2024-06-12 03:00:50 -07:00
function sendMessage(event) {
event.preventDefault();
2024-04-15 21:52:21 -07:00
if (!msg.value)
return;
2024-06-12 03:00:50 -07:00
window.currentInstance.emit('send_message', {
2024-04-15 21:52:21 -07:00
message: msg.value,
2024-05-05 00:47:42 -07:00
thread: window.currentThread.id
2024-04-15 21:52:21 -07:00
});
msg.value = '';
}
let earliestMessage;
2024-06-12 03:00:50 -07:00
function loadMessages(callback) {
let instance = window.currentInstance;
2024-04-15 21:52:21 -07:00
if (!msg) {
render(document.getElementById('message'), html`
<button id='loadmore' class='hidden' onclick=${loadMessages}>
load more messages
</button>
<div id='messages'></div>
<form id='msginput' onsubmit=${sendMessage}>
<input id='msg' placeholder='write a message...' />
<button type='submit' id='sendmsg'>send</button>
</form>
`);
msg = document.getElementById('msg');
}
const messages = document.getElementById('messages');
if (!this) { // called from chooseThread, initializing thread
messages.innerHTML = '';
earliestMessage = null;
2024-05-16 20:40:09 -07:00
if (window.currentThread.permissions.post)
document.getElementById('msginput').classList.remove('hidden');
else
document.getElementById('msginput').classList.add('hidden');
2024-04-15 21:52:21 -07:00
}
2024-06-12 03:00:50 -07:00
instance.emit('get_history', {
2024-05-27 20:03:45 -07:00
before: earliestMessage,
thread: window.currentThread.id
2024-06-12 03:00:50 -07:00
}, async msg => {
2024-05-27 20:03:45 -07:00
if (!msg.success) {
console.log('get_history failed: ' + msg.message);
return;
}
callback && callback(msg.messages);
if (msg.messages.length > 0) {
earliestMessage = msg.messages[msg.messages.length - 1].id;
2024-06-12 03:00:50 -07:00
let users = {};
for (let message of msg.messages) {
2024-06-17 19:44:11 -07:00
let user;
if (message.user.name)
user = message.user;
2024-06-14 22:24:32 -07:00
else {
2024-06-17 19:44:11 -07:00
user = users[message.user.id];
if (user === undefined)
try {
user = users[message.user.id] = await window.getUser(message.user.id);
}
catch (e) {
console.log(`error getting user ${message.user.id}:`, e);
users[message.user.id] = false;
}
if (user) {
user.id = message.user.id;
user.permissions = message.user.permissions;
2024-06-12 03:00:50 -07:00
}
}
2024-06-17 19:44:11 -07:00
let div = html.node`<div class='message'>: ${message.content}</div>`;
2024-06-19 22:57:57 -07:00
div.prepend(user ? window.makeUser(user, user.id.split?.('@')[1] || instance.url)
2024-06-17 19:44:11 -07:00
: html.node`<span>${message.user.id}</span>`);
messages.prepend(div);
2024-06-12 03:00:50 -07:00
}
2024-04-15 21:52:21 -07:00
}
2024-05-27 20:03:45 -07:00
if (msg.more)
document.getElementById('loadmore').classList.remove('hidden');
else
document.getElementById('loadmore').classList.add('hidden');
messages.scroll(0, messages.scrollHeight - messages.clientHeight);
});
2024-06-12 03:00:50 -07:00
if (!instance.messaged) {
instance.socket.on('new_message', message => {
if (message.thread !== window.currentThread?.id || instance !== window.currentInstance)
return;
const messages = document.getElementById('messages');
let scroll = messages.scrollTop + 10 >= messages.scrollHeight - messages.clientHeight;
2024-06-14 22:24:32 -07:00
let div = html.node`<div class='message'>: ${message.content}</div>`;
2024-06-17 19:44:11 -07:00
div.prepend(window.makeUser(message.user,
2024-06-19 22:57:57 -07:00
message.user.id.split?.('@')[1] || instance.url));
2024-06-14 22:24:32 -07:00
messages.append(div);
2024-06-12 03:00:50 -07:00
if (scroll)
messages.scroll(0, messages.scrollHeight - messages.clientHeight);
if (!earliestMessage)
earliestMessage = message.id;
});
instance.messaged = true;
}
2024-04-15 21:52:21 -07:00
}
export default loadMessages;