diff --git a/js/app.js b/js/app.js index 5e39efa..013b8fb 100644 --- a/js/app.js +++ b/js/app.js @@ -3,14 +3,14 @@ import { waha } from "./waha.js"; import { ui, elements } from "./ui.js"; import { websocket } from "./websocket.js"; import { compensateMessageOrdering, formatTime } from "./utils.js"; -import { fetchChats, getAppUser, getChatMessages, getChats } from "./storage.js"; +import { fetchChats, getAppUser, getChatMessages, getChats, updateOnlineStatus } from "./storage.js"; import { upsertMessages } from "./db.js"; let activeChatState = null; -let userInfo; const messageTone = new Audio("./message.ogg"); document.addEventListener('DOMContentLoaded', async () => { + await updateOnlineStatus(); setupEventListeners(); try { elements.loggedUserName.textContent = await getAppUser().pushName; @@ -137,7 +137,7 @@ function handleIncomingMessage(msg) { } // console.log('[WS] Resolved msgChatId:', msgChatId, '| activeChatState:', activeChatState?.id); - if (msg.fromMe) { + if (!msg.fromMe) { messageTone.play(); } @@ -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, userInfo.id); + ui.appendSingleMessage({ ...msg, chatId: msgChatId }, activeChatState.name, getAppUser().id); ui.scrollToBottom(); } } @@ -215,7 +215,7 @@ async function sendMessage() { status: 'sending' }; - ui.appendSingleMessage(tempMsg, activeChatState.name, userInfo.id); + ui.appendSingleMessage(tempMsg, activeChatState.name, getAppUser().id); ui.scrollToBottom(); try { @@ -284,11 +284,12 @@ async function sendFileMessage(file) { } }; - ui.appendSingleMessage(tempMsg, activeChatState.name, userInfo.id, activeChatState.id, true); + ui.appendSingleMessage(tempMsg, activeChatState.name, getAppUser().id, activeChatState.id, true); ui.scrollToBottom(); const result = await waha.sendFileMessage(activeChatState.id, file); - ui.updateMessageTick(tempId, result.status); + ui.removeChatMessage(tempId); + ui.appendSingleMessage(result.id, activeChatState.name, getAppUser().id, activeChatState.id); } catch (error) { console.error(error.message); } diff --git a/js/storage.js b/js/storage.js index 745567e..1159816 100644 --- a/js/storage.js +++ b/js/storage.js @@ -1,16 +1,23 @@ import { loadChatsSorted, loadLatestMessages, loadMedia, upsertChats, upsertMedia, upsertMessages } from "./db.js"; import { waha } from "./waha.js"; -let user; +let online = false; let chats = []; -export async function fetchChats() { +export async function updateOnlineStatus() { try { - await getRemoteChats() + await waha.getVersion(); + online = true; } catch (error) { - + console.log(error); + online = false; } +} +export async function fetchChats() { + if (online) { + await getRemoteChats() + } chats = await loadChatsSorted(); } @@ -45,27 +52,25 @@ export function getChats() { } export async function getAppUser() { - if (user) { - return user; + if (online) { + const info = await waha.getMyInfo(); + localStorage.setItem('pandora-last-username', info.name); + localStorage.setItem('pandora-last-userid', info.id); + return info; } else { - try { - return await waha.getMyInfo(); - } catch (error) { - console.error(error); - return { - pushName: "Unknown", - id: "Unknown" + return { + pushName: localStorage.getItem('pandora-last-username') || 'Unknown', + id: localStorage.getItem('pandora-last-userid') || 'Unknown' } - } } } export async function getMessage(chatId, msgId, downloadMedia) { - try { + if (online) { const newMessage = await waha.getSingleChatMessage(chatId, msgId, downloadMedia); upsertMessages([newMessage]); return newMessage; - } catch (error) { + } else { return { id: `${Date.now()}-temp`, body: "You're offline", @@ -76,33 +81,40 @@ export async function getMessage(chatId, msgId, downloadMedia) { } export async function getMedia(reqId) { - try { - const media = await waha.downloadMedia(reqId); - upsertMedia(reqId, media.blob, media.filename); - return media; - } catch (error) { const cached = await loadMedia(reqId); if (cached) { return cached; } - throw error; - } + + try { + if (online) { + const media = await waha.downloadMedia(reqId); + upsertMedia(reqId, media.blob, media.filename); + return media; + } + } catch (error) { + return; + } } export async function getChatMessages(chatId) { - try { + if (online) { const newMessages = await waha.getChatMessages(chatId); upsertMessages(newMessages); return newMessages; - } catch (error) { + } else { return await loadLatestMessages(chatId); } } export async function getChatPicture(chatId) { - try { + if (online) { return await waha.getChatPicture(chatId); - } catch (error) { - return ""; + } else { + return { url: "" }; } +} + +export function isOnline() { + return online; } \ No newline at end of file diff --git a/js/ui.js b/js/ui.js index c0c5ff5..cdda2e4 100644 --- a/js/ui.js +++ b/js/ui.js @@ -110,7 +110,7 @@ export const ui = { ${timeStr}