diff --git a/server/public/home.html b/server/public/home.html index 8bedb1d..f36eaee 100644 --- a/server/public/home.html +++ b/server/public/home.html @@ -788,7 +788,7 @@
- @@ -2228,7 +2228,29 @@ let chatWs=null, _chatConnectedOnce=false; // handled by boot's loadSidebar, so we skip the resync then. function resyncChat(){ try{ loadSidebar(); }catch(_){} - if(selected && currentTab()==='chat'){ const s=selected; try{ openConvo(s.kind, s.id); }catch(_){} } + refreshOpenThread(); // NON-destructive catch-up (never wipes the open thread — see below) +} +// Re-fetch the open conversation and MERGE: add messages missed while disconnected + refresh +// read/delivered/seen/edited/deleted flags, but NEVER remove messages already shown. openConvo +// replaced THREAD wholesale, which could momentarily blank a chat (e.g. a just-sent message) if the +// reload raced or returned stale — the cause of the "messages disappear after sending" report. +async function refreshOpenThread(){ + if(!selected || currentTab()!=='chat') return; + const kind=selected.kind, id=selected.id; + const url=kind==='group'?('/api/messages/thread?group='+encodeURIComponent(id)):('/api/messages/thread?with='+encodeURIComponent(id)); + let msgs; try{ msgs=await fetch(url).then(r=>r.json()); }catch(_){ return; } + if(!Array.isArray(msgs)) return; + if(!selected||selected.kind!==kind||selected.id!==id) return; // switched away while fetching + const byId=new Map(THREAD.map(m=>[m.id,m])); let changed=false; + msgs.forEach(nm=>{ const ex=byId.get(nm.id); + if(!ex){ THREAD.push(nm); changed=true; return; } + if(nm.read_at&&!ex.read_at){ ex.read_at=nm.read_at; changed=true; } + if(nm.delivered_at&&!ex.delivered_at){ ex.delivered_at=nm.delivered_at; changed=true; } + if(nm.deleted&&!ex.deleted){ ex.deleted=true; ex.body=''; ex.attachment=null; ex.reactions=[]; changed=true; } + if(nm.edited_at&&nm.edited_at!==ex.edited_at){ ex.edited_at=nm.edited_at; ex.body=nm.body; changed=true; } + if(JSON.stringify(nm.seenBy||[])!==JSON.stringify(ex.seenBy||[])){ ex.seenBy=nm.seenBy||[]; changed=true; } + }); + if(changed){ THREAD.sort((a,b)=>(a.created_at||0)-(b.created_at||0)); THREAD_CACHE.set(kind+':'+id, THREAD.slice()); renderThread(); } } function connectChatWs(){ try{