Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): name the messages an automatic read acknowledges
Yudhi Armyndharis2dbbe3eb5ed1
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex a57e45562..19ce05d3b 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -110,6 +110,21 @@ let listDuplicateContact = false;let failContacts = false;// What the chat list reports for Alice, which a test changes to stand for the phone changing her chat.const aliceNow: Partial<Chat> = {};+// The messages the gateway holds for Alice's chat, oldest first.+let aliceThread: Array<Record<string, unknown>> = [];+const threadRow = (n: number, direction: 'incoming' | 'outgoing' = 'incoming') => ({+ id: `row-${n}`,+ waMessageId: `wamid.${direction === 'incoming' ? 'in' : 'out'}.${n}`,+ chatId: CHAT.id,+ from: direction === 'incoming' ? CHAT.id : 'me',+ to: direction === 'incoming' ? 'me' : CHAT.id,+ body: `message ${n}`,+ type: 'text',+ direction,+ status: 'sent',+ timestamp: 1_700_000_000 + n,+ createdAt: new Date((1_700_000_000 + n) * 1000).toISOString(),+});// The number the new-chat tests type, and the id the gateway resolves it to.const TYPED_NUMBER = '+1 555 000 9999';const RESOLVED_ID = '[email protected]';@@ -231,7 +246,8 @@ function installFetchStub(): void {);}if (method === 'GET' && path.startsWith(`${base}/messages?`)) {- return Promise.resolve(jsonResponse({ messages: [], total: 0 }));+ // The gateway answers newest first.+ return gated('thread', () => jsonResponse({ messages: [...aliceThread].reverse(), total: aliceThread.length }));}if (method === 'GET' && /\/messages\/[^/]+\/history/.test(path)) return Promise.resolve(jsonResponse([]));if (method === 'POST' && path === `${base}/chats/read`) return gated('read', () => jsonResponse({ success: true }));@@ -280,6 +296,7 @@ afterEach(async () => {listDuplicateContact = false;failContacts = false;for (const key of Object.keys(aliceNow)) delete aliceNow[key as keyof Chat];+ aliceThread = [];declineWrites = false;for (const id of Object.keys(pictureFor)) delete pictureFor[id];gates.clear();@@ -1275,6 +1292,71 @@ test('Mark as unread waits for an automatic read already sent, so the unread marassert.equal(readsOf(CHAT.id).length, 1, 'another automatic read followed the unread mark');});+// ── Baileys acknowledges the messages a read names, so the page names them. ──++const readBodies = (chatId: string) => readsOf(chatId).map(c => c.body);++test('on Baileys the automatic read names the incoming messages of the thread, newest included', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ aliceThread = [threadRow(1), threadRow(2, 'outgoing'), threadRow(3), threadRow(4)];+ renderPage();+ fireEvent.click(await screen.findByText('Alice'));+ await waitFor(() => assert.ok(readsOf(CHAT.id).length >= 1, 'opening the chat did not mark it read'), {+ timeout: 3_000,+ });+ await screen.findByText('message 4');+ await waitFor(() => assert.ok(readBodies(CHAT.id).at(-1) !== undefined), { timeout: 3_000 });+ assert.deepEqual(readBodies(CHAT.id).at(-1), {+ chatId: CHAT.id,+ messageIds: ['wamid.in.1', 'wamid.in.3', 'wamid.in.4'],+ });+});++test('a message that arrives later is the only one the next read names', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ aliceThread = [threadRow(1), threadRow(2)];+ renderPage();+ fireEvent.click(await screen.findByText('Alice'));+ await screen.findByText('message 2');+ await waitFor(+ () => assert.deepEqual(readBodies(CHAT.id).at(-1), { chatId: CHAT.id, messageIds: ['wamid.in.1', 'wamid.in.2'] }),+ {+ timeout: 3_000,+ },+ );+ fetchCalls.length = 0;++ receiveFromAlice('wamid.live.1', 'a live one');+ await waitFor(() => assert.equal(readsOf(CHAT.id).length, 1), { timeout: 3_000 });+ assert.deepEqual(readBodies(CHAT.id), [{ chatId: CHAT.id, messageIds: ['wamid.live.1'] }]);+});++test('a thread that loads after the opening read is read again with its ids', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ aliceThread = [threadRow(1), threadRow(2)];+ const release = hold('thread');+ renderPage();+ fireEvent.click(await screen.findByText('Alice'));+ // The opening read is due before the thread has come, so it names nothing.+ await waitFor(() => assert.deepEqual(readBodies(CHAT.id), [{ chatId: CHAT.id }]), { timeout: 3_000 });++ release();+ await screen.findByText('message 2');+ await waitFor(() => assert.equal(readsOf(CHAT.id).length, 2), { timeout: 3_000 });+ assert.deepEqual(readBodies(CHAT.id).at(-1), { chatId: CHAT.id, messageIds: ['wamid.in.1', 'wamid.in.2'] });+});++test('on whatsapp-web.js the read names no message, since the engine reads the whole chat', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ window.sessionStorage.setItem('openwa_engine_type', 'whatsapp-web.js');+ aliceThread = [threadRow(1), threadRow(2)];+ renderPage();+ fireEvent.click(await screen.findByText('Alice'));+ await screen.findByText('message 2');+ await waitFor(() => assert.ok(readsOf(CHAT.id).length >= 1), { timeout: 3_000 });+ assert.deepEqual(readBodies(CHAT.id), [{ chatId: CHAT.id }]);+});+/** Make the document read as hidden (a background tab) or visible again, as the browser does before `visibilitychange`. */function setTabHidden(hidden: boolean): void {if (hidden) {diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 0697f1241..f6449e20f 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -1248,9 +1248,10 @@ test('switching session with a chat open marks only the chats opened on the newunmount();await flush();await flush();- assert.deepEqual(readsOnTwo, [{ chatId: CHAT_2.id }]);+ const chatIdsOf = (reads: unknown[]) => reads.map(read => (read as { chatId: string }).chatId);+ assert.deepEqual(chatIdsOf(readsOnTwo), [CHAT_2.id]);// The read queued for the session that was left still reaches that session.- assert.deepEqual(readsOnOne, [{ chatId: CHAT.id }]);+ assert.deepEqual(chatIdsOf(readsOnOne), [CHAT.id]);} finally {globalThis.fetch = stub;twoSessions = false;diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 8db9f2582..f62e698ef 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -50,6 +50,7 @@ import {getMediaSrc,liveMessageMetadata,stripMentionDelimiters,+ unacknowledgedIncomingIds,type ChatMessageView,type MessageMedia,} from '../utils/chatMessages';@@ -65,6 +66,8 @@ import {messagesQueryKey,updateCachedMessages,cachedSessionThreads,+ flattenMessagePages,+ type MessagesData,} from '../hooks/useChatMessages';import { useChannelMessages } from '../hooks/useChannelMessages';import { useContactStatuses } from '../hooks/useContactStatuses';@@ -89,6 +92,8 @@ import './Chats.css';// Quiet window for coalescing mark-as-read RPCs (see markReadCoalescer below).const MARK_READ_DEBOUNCE_MS = 750;+// Most messages the gateway takes in one read (MarkChatReadDto).+const MARK_READ_MESSAGE_IDS_MAX = 100;const readKey = (sessionId: string, chatId: string) => `${sessionId}\u0000${chatId}`;// Longest one background chat-list walk may take before it is abandoned. A walk is a dozen small requests, and@@ -542,6 +547,9 @@ export function Chats() {const [readsInFlight] = useState(() => new Map<string, Promise<void>>());const [keptUnread] = useState(() => new Set<string>());const [withheldReads] = useState(() => new Set<string>());+ // Baileys acknowledges the messages a read names, and without any only the newest one it holds. A read names the+ // incoming messages of the thread that an earlier read has not acknowledged (the ones it named, per chat).+ const [acknowledged] = useState(() => new Map<string, Set<string>>());const markReadCoalescer = useMemo(() =>createTrailingCoalescer<string>(chatId => {@@ -550,8 +558,22 @@ export function Chats() {withheldReads.add(key);return;}- const read = sessionApi.markChatRead(selectedSessionId, chatId).then(- () => undefined,+ let named: string[] = [];+ if (engineType === 'baileys') {+ const thread = queryClient.getQueryData<MessagesData>(messagesQueryKey(selectedSessionId, chatId));+ named = unacknowledgedIncomingIds(+ thread ? flattenMessagePages(thread) : [],+ acknowledged.get(key) ?? new Set(),+ MARK_READ_MESSAGE_IDS_MAX,+ );+ }+ const read = sessionApi.markChatRead(selectedSessionId, chatId, named).then(+ result => {+ if (!result.success || named.length === 0) return;+ const sent = acknowledged.get(key) ?? new Set<string>();+ for (const id of named) sent.add(id);+ acknowledged.set(key, sent);+ },err => {showWarningToast(t('chats.errors.markRead'), err instanceof Error ? err.message : undefined);},@@ -561,7 +583,7 @@ export function Chats() {if (readsInFlight.get(key) === read) readsInFlight.delete(key);});}, MARK_READ_DEBOUNCE_MS),- [selectedSessionId, t, showWarningToast, readsInFlight, withheldReads],+ [selectedSessionId, t, showWarningToast, readsInFlight, withheldReads, acknowledged, engineType, queryClient],);// Flush pending trailing calls on unmount / session switch: the mark-as-read POST is@@ -1027,6 +1049,15 @@ export function Chats() {// eslint-disable-next-line react-hooks/exhaustive-deps}, [activeChat?.id, markChatRead, canWrite]);+ // On Baileys the read of an opened chat falls due before its thread has loaded, so it names no message. Once+ // the thread is there the chat is read again, naming what it holds (the coalescer merges the two when close).+ useEffect(() => {+ if (activeChat && engineType === 'baileys' && !loadingMessages && messages.length > 0) {+ markChatRead(activeChat.id);+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [activeChat?.id, loadingMessages, engineType]);+// --- Global search: jump to a hit's chat (and best-effort scroll to the message) ---// A cross-session hit switches session, which asynchronously reloads the chats list — so the// target chat may not be available at click time. pendingHitRef carries the intent across thatdiff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex f99c3d18e..645523c20 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -979,10 +979,12 @@ export const sessionApi = {return true;});},- markChatRead: (id: string, chatId: string) =>+ // `messageIds` are the messages to acknowledge. Baileys acknowledges them one by one and, without any, only the+ // newest it holds; whatsapp-web.js reads the whole chat and ignores them.+ markChatRead: (id: string, chatId: string, messageIds?: string[]) =>request<{ success: boolean }>(`/sessions/${id}/chats/read`, {method: 'POST',- body: JSON.stringify({ chatId }),+ body: JSON.stringify({ chatId, ...(messageIds?.length ? { messageIds } : {}) }),}),// Both also delete the gateway's stored copy of the chat's messages (rows, media, search entries).clearChatMessages: (id: string, chatId: string) =>diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.tsindex b1f307eea..c89af75e7 100644--- a/dashboard/src/utils/chatMessages.test.ts+++ b/dashboard/src/utils/chatMessages.test.ts@@ -8,6 +8,7 @@ import {mergeChatMessages,mergeReactionSnapshot,liveMessageMetadata,+ unacknowledgedIncomingIds,type EngineHistoryMessage,} from './chatMessages.ts';import { MENTION_CLOSE, MENTION_OPEN } from './messageFormatter.ts';@@ -792,3 +793,23 @@ test('a later message echo preserves the edited marker', () => {assert.equal(after[0].metadata?.edited, true);assert.deepEqual(after[0].metadata?.reactions, { alice: '👍' });});++test('unacknowledgedIncomingIds names the newest incoming messages that no read has acknowledged', () => {+ const incoming = (n: number, over: Partial<ChatMessage> = {}): ChatMessage =>+ db({ id: `row-${n}`, waMessageId: `wamid.${n}`, direction: 'incoming', timestamp: n, ...over });+ const thread = [+ incoming(1),+ incoming(2, { direction: 'outgoing' }),+ incoming(3),+ incoming(4, { waMessageId: undefined }),+ // The gateway refuses an id with whitespace, and the whole read with it.+ incoming(5, { waMessageId: 'has space' }),+ incoming(6),+ ];++ assert.deepEqual(unacknowledgedIncomingIds(thread, new Set(), 100), ['wamid.1', 'wamid.3', 'wamid.6']);+ assert.deepEqual(unacknowledgedIncomingIds(thread, new Set(['wamid.3']), 100), ['wamid.1', 'wamid.6']);+ // Past the cap it is the newest that are named, in the order they were sent.+ assert.deepEqual(unacknowledgedIncomingIds(thread, new Set(), 2), ['wamid.3', 'wamid.6']);+ assert.deepEqual(unacknowledgedIncomingIds([], new Set(), 100), []);+});diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.tsindex bb849aa83..4b38d777a 100644--- a/dashboard/src/utils/chatMessages.ts+++ b/dashboard/src/utils/chatMessages.ts@@ -475,3 +475,23 @@ export function applyMessageEdit(next[idx] = { ...next[idx], body: event.body, metadata: { ...next[idx].metadata, edited: true } };return next;}++/**+ * The ids a read of a chat should name on an engine that acknowledges messages one by one: the incoming messages+ * of the thread that no read has acknowledged yet, at most `max` of the newest, oldest first. An id the gateway+ * would refuse (whitespace, or longer than 128 characters) is left out, since one of them fails the whole read.+ */+export function unacknowledgedIncomingIds(+ thread: ChatMessageView[],+ acknowledged: ReadonlySet<string>,+ max: number,+): string[] {+ const ids: string[] = [];+ for (let i = thread.length - 1; i >= 0 && ids.length < max; i--) {+ const { direction, waMessageId } = thread[i];+ if (direction === 'incoming' && waMessageId && /^\S{1,128}$/.test(waMessageId) && !acknowledged.has(waMessageId)) {+ ids.push(waMessageId);+ }+ }+ return ids.reverse();+}