From b3b74b04fceda9196558fda645c98bda346e667d Mon Sep 17 00:00:00 2001 From: Adrian Victor Date: Thu, 16 Jul 2026 20:03:32 -0300 Subject: [PATCH] Fix bug in updateChatInChatList that would make outgoing messages show up in the user self chat; fix app awaiting for getAppUser on wrong scope. --- js/app.js | 14 +++++++------- js/ui.js | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/js/app.js b/js/app.js index 013b8fb..66e63fa 100644 --- a/js/app.js +++ b/js/app.js @@ -13,7 +13,7 @@ document.addEventListener('DOMContentLoaded', async () => { await updateOnlineStatus(); setupEventListeners(); try { - elements.loggedUserName.textContent = await getAppUser().pushName; + elements.loggedUserName.textContent = (await getAppUser()).pushName; loadChats(); checkWahaStatus(); initWebSocket(); @@ -122,7 +122,7 @@ function normalizeChatId(raw) { return raw; } -function handleIncomingMessage(msg) { +async function handleIncomingMessage(msg) { if (!msg) return; // console.log('[WS] handleIncomingMessage payload:', msg); @@ -145,7 +145,7 @@ function handleIncomingMessage(msg) { const msgId = normalizeChatId(msg.id) || msg.id; const exists = document.getElementById(msgId); if (!exists) { - ui.appendSingleMessage({ ...msg, chatId: msgChatId }, activeChatState.name, getAppUser().id); + ui.appendSingleMessage({ ...msg, chatId: msgChatId }, activeChatState.name, (await getAppUser()).id); ui.scrollToBottom(); } } @@ -193,7 +193,7 @@ async function selectChat(chat) { try { const rawMessages = await getChatMessages(chat.id); const processedMessages = compensateMessageOrdering(rawMessages); - ui.renderMessages(processedMessages, chat.name, getAppUser().id, chat.id); + ui.renderMessages(processedMessages, chat.name, (await getAppUser()).id, chat.id); } catch (error) { console.error('Failed to load messages:', error); elements.messagesContainer.innerHTML = '
Error loading messages
'; @@ -215,7 +215,7 @@ async function sendMessage() { status: 'sending' }; - ui.appendSingleMessage(tempMsg, activeChatState.name, getAppUser().id); + ui.appendSingleMessage(tempMsg, activeChatState.name, (await getAppUser()).id); ui.scrollToBottom(); try { @@ -284,12 +284,12 @@ async function sendFileMessage(file) { } }; - ui.appendSingleMessage(tempMsg, activeChatState.name, getAppUser().id, activeChatState.id, true); + ui.appendSingleMessage(tempMsg, activeChatState.name, (await getAppUser()).id, activeChatState.id, true); ui.scrollToBottom(); const result = await waha.sendFileMessage(activeChatState.id, file); ui.removeChatMessage(tempId); - ui.appendSingleMessage(result.id, activeChatState.name, getAppUser().id, activeChatState.id); + ui.appendSingleMessage(result.id, activeChatState.name, (await getAppUser()).id, activeChatState.id); } catch (error) { console.error(error.message); } diff --git a/js/ui.js b/js/ui.js index cdda2e4..6dd6185 100644 --- a/js/ui.js +++ b/js/ui.js @@ -122,7 +122,7 @@ export const ui = { }, async updateChatInChatList(msg) { - const span = document.querySelector(`.chat-item-msg[data-chatid="${msg.from}"]`); + const span = document.querySelector(`.chat-item-msg[data-chatid="${msg.fromMe ? msg.to : msg.from}"]`); if (span) { span.innerText = msg.body; } @@ -240,7 +240,7 @@ export const ui = { a.removeEventListener('click', clickListener); a.innerText = `[Downloading]`; const mediaMsg = msg.media ? msg : await getMessage(chatId, msg.id, true); - if (!mediaMsg.media.url) { + if (!mediaMsg || !mediaMsg?.media.url) { a.addEventListener('click', clickListener); a.innerText = `[Error, click to try again]` return;