Dedupe sent messages in addMessage to prevent duplicate display

This commit is contained in:
Clay 2026-12-03 14:17:32 -05:00
parent b80e262c78
commit 2d7b6f08ab
2 changed files with 8 additions and 2 deletions

View file

@ -39,9 +39,14 @@ function ensureConversation(type, name) {
return state.conversations[key]; return state.conversations[key];
} }
// NOTE: Dedupe is required here. Sent messages can be added optimistically and also
// arrive via SSE replay/broadcast (e.g. TOC sentIM buffer). Never remove this guard
// or duplicates will reappear in the UI.
function addMessage(type, name, msg) { function addMessage(type, name, msg) {
if (!name || name === 'unknown') return; if (!name || name === 'unknown') return;
const conv = ensureConversation(type, name); const conv = ensureConversation(type, name);
if (msg?.msgId && conv.messages.some((m) => m.msgId && m.msgId === msg.msgId)) return;
if (msg?.text && conv.messages.slice(-6).some((m) => m.text === msg.text && normalizeName(m.from) === normalizeName(msg.from) && !!m.mine === !!msg.mine)) return;
conv.messages.push(msg); conv.messages.push(msg);
if (!state.active) state.active = conv.key; if (!state.active) state.active = conv.key;
ui.renderConversations(state); ui.renderConversations(state);
@ -300,6 +305,7 @@ document.getElementById('messageForm').addEventListener('submit', async (e) => {
const res = await api.sendChat(conv.roomId || conv.name, text); const res = await api.sendChat(conv.roomId || conv.name, text);
msgId = res?.data?.msgId; msgId = res?.data?.msgId;
} }
// addMessage dedupes by msgId/text - see note in addMessage().
addMessage(conv.type, conv.roomId || conv.name, { addMessage(conv.type, conv.roomId || conv.name, {
from: state.screenName, from: state.screenName,
text, text,

View file

@ -4,7 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Web OSCAR Chat</title> <title>Web OSCAR Chat</title>
<link rel="stylesheet" href="/style.css?v=22"> <link rel="stylesheet" href="/style.css?v=23">
</head> </head>
<body> <body>
<div id="app"> <div id="app">
@ -86,6 +86,6 @@
</main> </main>
</div> </div>
<script type="module" src="/app.js?v=22"></script> <script type="module" src="/app.js?v=23"></script>
</body> </body>
</html> </html>