Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): label a live message announced as edited
Yudhi Armyndharisa6f94e3b4e1a
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 0df2716c1..09ad3b58b 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -3089,6 +3089,40 @@ test('a live edit labels the message it changed, and not one whose text it repeaawait within(room).findByText('hello from alice, edited');});+// The gateway announces the edit of a message it had not stored yet before the message, which then arrives with+// the edited text and, where its row is marked, the mark.+test('a live message announced as edited is labelled, though its edit arrived first', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ const { container } = renderChats();+ fireEvent.click(await screen.findByText('Alice'));+ const room = container.querySelector('.room-messages') as HTMLElement;+ await within(room).findByText('hello from alice');+ const labelOf = (text: string) =>+ within(room).getByText(text).closest('.message-bubble')?.querySelector('.message-edited');+ const arrive = (id: string, body: string, edited?: true) => {+ receiveEvent('message.edited', { messageId: id, chatId: CHAT.id, body, timestamp: 1_700_003_001 });+ receiveEvent('message.received', {+ id,+ chatId: CHAT.id,+ from: CHAT.id,+ to: 'me',+ body,+ type: 'text',+ fromMe: false,+ timestamp: 1_700_003_000,+ ...(edited ? { edited } : {}),+ });+ };++ arrive('wamid.live.edited', 'corrected on arrival', true);+ arrive('wamid.live.same', 'sent as it was');+ await within(room).findByText('corrected on arrival');+ await within(room).findByText('sent as it was');++ await waitFor(() => assert.ok(labelOf('corrected on arrival'), 'the message announced as edited was not labelled'));+ assert.equal(labelOf('sent as it was') ?? null, null, 'a message announced without the mark was labelled');+});+test('an edit arriving after the newest message was deleted for everyone does not refill the sidebar preview', async () => {const { screen, fireEvent, within, waitFor } = rtl;const { container } = renderChats();diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 11a931896..032c05670 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -131,6 +131,8 @@ interface IncomingWsMessage {/** Business prompt choices (Baileys); top-level on the live event, folded into metadata for the UI. */buttons?: Array<{ id: string; text: string }>;poll?: { name: string; options: string[]; allowMultipleAnswers: boolean };+ /** Edited before the gateway stored it; its `message.edited` came first, while no thread held it. */+ edited?: boolean;metadata?: ChatMessageView['metadata'];kind?: ChatKind;/** Group poster: `from` is the group JID, so `contact`/`author` identify who actually sent it. */diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.tsindex e069452f8..a52dc847f 100644--- a/dashboard/src/utils/chatMessages.test.ts+++ b/dashboard/src/utils/chatMessages.test.ts@@ -390,6 +390,29 @@ test('applyMessageEdit does not label a message whose text the event repeats', (assert.deepEqual(after[0].metadata?.reactions, { me: 'x' });});+test('liveMessageMetadata: carries the edited mark of a message announced as edited', () => {+ const media = { mimetype: 'image/png', data: 'QUJD' };+ assert.deepEqual(liveMessageMetadata({ edited: true }), { edited: true });+ assert.deepEqual(liveMessageMetadata({ media, edited: true }), { media, edited: true });+ assert.deepEqual(liveMessageMetadata({ metadata: { reactions: { me: 'x' } }, edited: true }), {+ reactions: { me: 'x' },+ edited: true,+ });+ assert.equal(liveMessageMetadata({}), undefined);+});++// The gateway announces the edit of a message it had not stored yet before the message itself, which then carries+// the edited text and the mark.+test('a message announced as edited after its edit missed the thread is labelled edited', () => {+ const missed = applyMessageEdit([], { messageId: 'WA_EDIT_3', body: 'fixed' });+ const live = { body: 'fixed', edited: true as const };+ const after = mergeOrAppend(missed, msg({ id: 'WA_EDIT_3', body: live.body, metadata: liveMessageMetadata(live) }));++ assert.equal(after[0].metadata?.edited, true);+ const echo = mergeOrAppend(after, msg({ id: 'WA_EDIT_3', body: 'fixed', metadata: liveMessageMetadata({}) }));+ assert.equal(echo[0].metadata?.edited, true, 'a later copy without the mark took it away');+});+test('applyMessageEdit is a referential no-op for an empty or unknown target id', () => {const before = [msg({ id: 'm-1', body: 'old' })];assert.equal(applyMessageEdit(before, { messageId: '', body: 'new' }), before);diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.tsindex 1ba29356b..d9fb8a92c 100644--- a/dashboard/src/utils/chatMessages.ts+++ b/dashboard/src/utils/chatMessages.ts@@ -259,7 +259,8 @@ export interface ChatMessageView extends ChatMessage {/*** Metadata for a live `message.received` / `message.sent` WS payload. Prompt `buttons` arrive* top-level on that event (the history route never populates them) and are folded here so the- * thread renders from `metadata.buttons`, matching persisted DB rows.+ * thread renders from `metadata.buttons`, matching persisted DB rows. So is `edited`, set on a+ * message edited before the gateway stored it, whose `message.edited` came while no thread held it.*/export function liveMessageMetadata(msg: {media?: MessageMedia;@@ -267,10 +268,12 @@ export function liveMessageMetadata(msg: {call?: { video: boolean; missed: boolean };buttons?: Array<{ id: string; text: string }>;poll?: { name: string; options: string[]; allowMultipleAnswers: boolean };+ edited?: boolean;metadata?: ChatMessageView['metadata'];}): ChatMessageView['metadata'] {- if (msg.metadata) return msg.metadata;+ if (msg.metadata) return msg.edited ? { ...msg.metadata, edited: true } : msg.metadata;const metadata: NonNullable<ChatMessageView['metadata']> = {};+ if (msg.edited) metadata.edited = true;if (msg.media) metadata.media = msg.media;if (msg.quotedMessage) metadata.quotedMessage = msg.quotedMessage;if (msg.call) metadata.call = msg.call;