displayname fix

main
jerl 2024-05-27 22:03:45 -05:00
parent 7deeb82a08
commit 5798d9030c
2 changed files with 32 additions and 37 deletions

View File

@ -23,7 +23,7 @@ window.socket.on('new_message', message => {
messages.appendChild(html.node` messages.appendChild(html.node`
<div class='message'> <div class='message'>
<strong>${message.name}: </strong> <strong>${message.name}: </strong>
${message.message} ${message.content}
</div>`); </div>`);
if (scroll) if (scroll)
messages.scroll(0, messages.scrollHeight - messages.clientHeight); messages.scroll(0, messages.scrollHeight - messages.clientHeight);
@ -54,36 +54,32 @@ function loadMessages(firstRender, callback) {
else else
document.getElementById('msginput').classList.add('hidden'); document.getElementById('msginput').classList.add('hidden');
} }
window.emit( window.emit('get_history', {
'get_history', before: earliestMessage,
{ thread: window.currentThread.id
before: earliestMessage, }, msg => {
thread: window.currentThread.id if (!msg.success) {
}, console.log('get_history failed: ' + msg.message);
msg => { return;
if (!msg.success) {
console.log('get_history failed: ' + msg.message);
return;
}
callback && callback(msg.messages);
if (firstRender && messages.hasChildNodes())
return;
if (msg.messages.length > 0) {
earliestMessage = msg.messages[msg.messages.length - 1].id;
for (let message of msg.messages)
messages.prepend(html.node`
<div class='message'>
<strong>${message.displayname}: </strong>
${message.message}
</div>`);
}
if (msg.more)
document.getElementById('loadmore').classList.remove('hidden');
else
document.getElementById('loadmore').classList.add('hidden');
messages.scroll(0, messages.scrollHeight - messages.clientHeight);
} }
); callback && callback(msg.messages);
if (firstRender && messages.hasChildNodes())
return;
if (msg.messages.length > 0) {
earliestMessage = msg.messages[msg.messages.length - 1].id;
for (let message of msg.messages)
messages.prepend(html.node`
<div class='message'>
<strong>${message.name}: </strong>
${message.content}
</div>`);
}
if (msg.more)
document.getElementById('loadmore').classList.remove('hidden');
else
document.getElementById('loadmore').classList.add('hidden');
messages.scroll(0, messages.scrollHeight - messages.clientHeight);
});
} }
export default loadMessages; export default loadMessages;

View File

@ -37,8 +37,8 @@ async function send_message(msg, respond) {
s.emit('new_message', { s.emit('new_message', {
id: id.rows[0].id, id: id.rows[0].id,
username: msg.auth_user.name, username: msg.auth_user.name,
displayname: msg.auth_user.displayname, name: msg.auth_user.displayname,
message: msg.message, content: msg.message,
thread: msg.thread thread: msg.thread
}); });
} }
@ -70,9 +70,10 @@ async function get_history(msg, respond) {
}); });
} }
const messages = await db.query( const messages = await db.query(
`select user.name, post.id, content from post `select coalesce(displayname, name) as name, post.id, content
from post
join user on post.user = user.id join user on post.user = user.id
${msg.before ? 'where post.id < ? and' : 'where'} where ${msg.before ? 'post.id < ? and' : ''}
thread = ? thread = ?
order by post.created desc order by post.created desc
limit 101`, limit 101`,
@ -80,9 +81,7 @@ async function get_history(msg, respond) {
); );
return respond({ return respond({
success: true, success: true,
messages: messages.rows messages: messages.rows.slice(0, 100),
.slice(0, 100)
.map((i) => ({ id: i.id, name: i.name, message: i.content })),
more: messages.rows.length > 100 more: messages.rows.length > 100
}); });
} }