Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): drop a staged reply when another chat is opened
Yudhi Armyndharis5b24b0519415
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9963c5f0b..75ab763af 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The dashboard Chats page drops a staged reply when another chat or session is opened, so a text send there no longer fails with `404` and a media send no longer quotes the previous chat's message.- whatsapp-web.js chat history resolves each message's sender through `getContact()`, as the live `message` handler already does, so a group participant outside the account's contacts gets a sender label in history too. Thanks @TanmayChachra.- A WebSocket client that emits without an ack callback now receives command replies at all. The gateway answered by returning a frame, which Socket.IO delivers through the ack callback and nowhere else, so a client written to the documented `message` event saw no subscribe confirmation, no pong, and none of the refusals, including the session-scope denial. Replies now go out on `message` as well as through the ack.- A Baileys message the account sent from its phone is no longer lost when the message store cannot be read. The repeat-delivery check threw on a locked database or an unparseable row and the message was dropped with it, and WhatsApp does not re-deliver one it has already acked; the check now fails open, so at worst such a message is reported twice rather than never.diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 2320d1a97..9738d8a91 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -32,6 +32,11 @@ const SESSION: Session = {updatedAt: '2026-01-01T00:00:00.000Z',};+// A second session, served only when a test turns `twoSessions` on. Its routes answer with the first+// session's fixtures (see installFetchStub), so a test can switch sessions without a second data set.+const SESSION_2: Session = { ...SESSION, id: 'session-2', name: 'Second', phone: '15559876543' };+let twoSessions = false;+const CHAT: Chat = {id: '[email protected]',name: 'Alice',@@ -234,7 +239,9 @@ function installFetchStub(): void {globalThis.fetch = (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;const method = init?.method ?? 'GET';- const path = url.replace(/^https?:\/\/[^/]+/, '');+ const path = url+ .replace(/^https?:\/\/[^/]+/, '')+ .replace(`/api/sessions/${SESSION_2.id}/`, `/api/sessions/${SESSION.id}/`);let body: unknown;if (typeof init?.body === 'string') {@@ -246,7 +253,9 @@ function installFetchStub(): void {}fetchCalls.push({ method, path, body });- if (method === 'GET' && path === '/api/sessions') return Promise.resolve(jsonResponse([SESSION]));+ if (method === 'GET' && path === '/api/sessions') {+ return Promise.resolve(jsonResponse(twoSessions ? [SESSION, SESSION_2] : [SESSION]));+ }if (method === 'GET' && path === '/api/infra/engines/current') {return Promise.resolve(jsonResponse({ engineType: 'baileys' }));}@@ -741,6 +750,71 @@ test('a staged attachment is dropped when a different chat is opened', async ());});+test('a staged reply is kept on reopening its chat and dropped when a different chat is opened', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ resetFetchCalls();+ const { container } = renderChats();++ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ let row: HTMLElement | null = await within(container.querySelector('.room-messages') as HTMLElement).findByText(+ 'hello from alice',+ );+ while (row && !row.querySelector('button[title="Reply"]')) row = row.parentElement;+ fireEvent.click(row?.querySelector('button[title="Reply"]') as HTMLElement);+ await waitFor(() => assert.ok(container.querySelector('.replying-preview-banner')));++ // Close and reopen the same room: the staged reply survives, like a staged attachment does.+ fireEvent.click(screen.getByRole('button', { name: 'Back' }));+ fireEvent.click(screen.getByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');+ assert.ok(container.querySelector('.replying-preview-banner'), 'the reply was lost on reopening its own chat');++ // A reply carried into another chat would quote Alice's message, text and number, to Carol.+ // Compared as text: a failing assertion on the DOM node itself stalls the runner.+ fireEvent.click(screen.getByText('Carol'));+ await within(container.querySelector('.room-header') as HTMLElement).findByText('Carol');+ assert.equal(+ container.querySelector('.replying-preview-banner')?.textContent ?? null,+ null,+ "Alice's reply followed the user into Carol's room",+ );+});++test('a staged reply is dropped when another session is opened', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ twoSessions = true;+ try {+ resetFetchCalls();+ const { container } = renderChats();++ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ let row: HTMLElement | null = await within(container.querySelector('.room-messages') as HTMLElement).findByText(+ 'hello from alice',+ );+ while (row && !row.querySelector('button[title="Reply"]')) row = row.parentElement;+ fireEvent.click(row?.querySelector('button[title="Reply"]') as HTMLElement);+ await waitFor(() => assert.ok(container.querySelector('.replying-preview-banner')));++ // The session switch closes the room. The chat opened next has the same id in the other session+ // (a contact both accounts share), so only the session switch, not a change of chat id, can drop it.+ fireEvent.change(container.querySelector('select.session-selector') as HTMLSelectElement, {+ target: { value: SESSION_2.id },+ });+ await waitFor(() => assert.equal(container.querySelector('.room-header'), null));+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-header') as HTMLElement).findByText('Alice');+ assert.equal(+ container.querySelector('.replying-preview-banner')?.textContent ?? null,+ null,+ "Alice's reply followed the user into the other session",+ );+ } finally {+ twoSessions = false;+ }+});+/*** The server-side inline-media budget replaces an over-budget payload with `{ omitted: true }`. This* thread requests the largest page size and caches it with staleTime Infinity, so without a fetch ofdiff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 86bb45d5d..eae65fe05 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -216,10 +216,11 @@ export function Chats() {return () => URL.revokeObjectURL(previewUrl);}, [previewUrl]);- // Drop a staged attachment when the user moves to a DIFFERENT chat. Closing the room- // (`activeChat` → null) deliberately keeps it, so close/reopen is a lossless round trip; only an- // actual change of conversation clears. The composer invalidates its in-flight FileReader on the- // same transition, so a late read cannot re-stage the file against the new chat.+ // Drop a staged attachment and a staged reply when the user moves to a DIFFERENT chat. Closing the+ // room (`activeChat` → null) deliberately keeps them, so close/reopen is a lossless round trip; only+ // an actual change of conversation clears. A reply carried across would quote the previous chat's+ // message, text and sender included, into the new one. The composer invalidates its in-flight+ // FileReader on the same transition, so a late read cannot re-stage the file against the new chat.const lastRoomIdRef = useRef<string | null>(null);useEffect(() => {const current = activeChat?.id ?? null;@@ -229,6 +230,7 @@ export function Chats() {if (previous === null || previous === current) return;setAttachment(null);setPreviewUrl(null);+ setReplyingTo(null);}, [activeChat]);// Per-chat scroll-position memory + auto-scroll heuristic.@@ -316,12 +318,13 @@ export function Chats() {setActiveChat(null);setActiveChannel(null);setActiveStatusContactId(null);- // A staged attachment belongs to a chat in the session being left, so it is dropped here+ // A staged attachment or reply belongs to a chat in the session being left, so it is dropped here// rather than carried across — the close/reopen round trip that preserves it is scoped to a// single session. Clearing previewUrl runs the revoke effect's cleanup; the composer// unmounts with the closed room and invalidates its own in-flight FileReader.setAttachment(null);setPreviewUrl(null);+ setReplyingTo(null);lastRoomIdRef.current = null;}}, [selectedSessionId, loadChats]);