Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not mark the previous chat read after a session switch (#1801)
Yudhi Armyndharis1d61a7f79ee2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6ac6f66a8..e8781120b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Stop the dashboard Chats page from marking the open chat read on a newly selected session.- Refuse bulk batches once shutdown begins, and fail a batch saved during shutdown instead of sending it.- Disable plugins whose enable is still running at shutdown, and refuse new plugin enables during teardown.- Stop in-flight webhook replay, ingress replay and pending-message sweeps at shutdown.diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 21d250344..508305712 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -1203,6 +1203,46 @@ test("a reconnect refetch that settles after a session switch leaves the new ses}});+test('switching session with a chat open marks only the chats opened on the new session read', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ twoSessions = true;+ // Session 2 lists Alice too (a contact both accounts share). The stub folds session 2's routes onto+ // session 1's, so its mark-as-read bodies are collected before that.+ chatsResponder = sessionId =>+ Promise.resolve(+ jsonResponse(sessionId === SESSION.id ? [CHAT] : [{ ...CHAT, lastMessage: 'alice on two' }, CHAT_2]),+ );+ const readsOnTwo: unknown[] = [];+ const stub = globalThis.fetch;+ globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {+ if (String(input).endsWith(`/api/sessions/${SESSION_2.id}/chats/read`))+ readsOnTwo.push(JSON.parse(String(init?.body)));+ return stub(input, init);+ }) as typeof fetch;+ try {+ const { container, unmount } = renderChats();+ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');++ fireEvent.change(container.querySelector('select.session-selector') as HTMLSelectElement, {+ target: { value: SESSION_2.id },+ });+ await screen.findByText('alice on two');+ fireEvent.click(await screen.findByText('Carol'));+ await waitFor(() => assert.ok(container.querySelector('.room-messages')));++ // Unmounting flushes the reads still queued for session 2, so none can be missed by a timer.+ unmount();+ await flush();+ await flush();+ assert.deepEqual(readsOnTwo, [{ chatId: CHAT_2.id }]);+ } finally {+ globalThis.fetch = stub;+ twoSessions = false;+ }+});+test('a read-only key opening a chat sends no mark-as-read', async () => {const { screen, fireEvent, within, waitFor } = rtl;resetFetchCalls();diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex dd8cbf6be..8b5a821d2 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -420,11 +420,14 @@ export function Chats() {);// Marking a chat read is an operator write; a read-only key would only collect 403 toasts.+ // A session switch rebuilds the coalescer for the new session one render before the open chat is+ // cleared, so until the new session's list is on screen the open chat is still the previous+ // account's and nothing is marked read.const markChatRead = useCallback((chatId: string) => {- if (canWrite) markReadCoalescer.call(chatId);+ if (canWrite && listedSessionRef.current === selectedSessionId) markReadCoalescer.call(chatId);},- [markReadCoalescer, canWrite],+ [markReadCoalescer, canWrite, selectedSessionId],);// 3. WebSocket integration for real-time messages