Remove usage of userInfo in app.js; make storage aware of connection status to avoid connecting to the server in offline mode; fix updateChaInList querying for non-existent elements and using them blindly; make websocket also check connection status on reload.

This commit is contained in:
天クマ 2026-07-16 19:36:25 -03:00
commit a021ca821f
4 changed files with 66 additions and 39 deletions

View file

@ -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;
}