Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): do not label an edit that repeats the text

Yudhi Armyndharisfd4752bafb1d
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index a58a9b82a..fd730da4c 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -3013,6 +3013,35 @@ function receiveEvent(event: string, data: Record<string, unknown>, sessionId: s
});
}
+test('a live edit labels the message it changed, and not one whose text it repeats', 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 labels = () => room.querySelectorAll('.message-edited').length;
+ assert.equal(labels(), 0);
+
+ // The backend announces an edit that repeats the stored text but no longer stores a marker for it.
+ receiveEvent('message.edited', {
+ messageId: DB_MESSAGE.waMessageId,
+ chatId: CHAT.id,
+ body: DB_MESSAGE.body,
+ timestamp: 1_700_003_001,
+ });
+ await flush();
+ assert.equal(labels(), 0, 'a repeated text was labelled as edited');
+
+ receiveEvent('message.edited', {
+ messageId: DB_MESSAGE.waMessageId,
+ chatId: CHAT.id,
+ body: 'hello from alice, edited',
+ timestamp: 1_700_003_002,
+ });
+ await waitFor(() => assert.equal(labels(), 1, 'a changed text was not labelled as edited'));
+ await within(room).findByText('hello from alice, edited');
+});
+
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/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index c89af75e7..e069452f8 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -381,6 +381,15 @@ test('applyMessageEdit updates a live row by id', () => {
assert.equal(after[0].body, '');
});
+test('applyMessageEdit does not label a message whose text the event repeats', () => {
+ const before = [msg({ id: 'm-1', waMessageId: 'WA_EDIT_2', body: 'same', metadata: { reactions: { me: 'x' } } })];
+ const after = applyMessageEdit(before, { messageId: 'WA_EDIT_2', body: 'same' });
+ // The same list, so nothing renders again; compared as a boolean, since a failed deepEqual of rows is slow to print.
+ assert.ok(after === before, 'an unchanged text produced a new list');
+ assert.equal(after[0].metadata?.edited, undefined);
+ assert.deepEqual(after[0].metadata?.reactions, { me: 'x' });
+});
+
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.ts
index 4b38d777a..1ba29356b 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -462,7 +462,8 @@ export function findRevokedIndex(list: ChatMessageView[], event: { id: string; r
/**
* Replace the displayed body of a cached WhatsApp message after a `message.edited` event. Both id
* candidates are matched, for the reason given on findRevokedIndex. Returns the original array on
- * a miss.
+ * a miss, and when the event repeats the text the message already has: the gateway announces such an
+ * event but stores no edited marker for it, so labelling it here would show what a reload does not.
*/
export function applyMessageEdit(
list: ChatMessageView[],
@@ -470,7 +471,7 @@ export function applyMessageEdit(
): ChatMessageView[] {
if (!event.messageId) return list;
const idx = list.findIndex(byMessageId(event.messageId));
- if (idx === -1 || list[idx].type === 'revoked') return list;
+ if (idx === -1 || list[idx].type === 'revoked' || list[idx].body === event.body) return list;
const next = list.slice();
next[idx] = { ...next[idx], body: event.body, metadata: { ...next[idx].metadata, edited: true } };
return next;