vybe/client/message.js

102 lines
2.9 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) {
if (!message.name) {
try {
message.name = (users[message.userid] || (
users[message.userid] = await window.getUser(message.userid)
)).displayname;
}
catch (e) {
console.log(`error getting user ${message.userid}`, e);
message.name = message.userid;
}
}
2024-05-27 20:03:45 -07:00
messages.prepend(html.node`
<div class='message'>
<strong>${message.name}: </strong>
${message.content}
</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;
messages.appendChild(html.node`
<div class='message'>
<strong>${message.name}: </strong>
${message.content}
</div>`);
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;