Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): clear a deleted message from the chat list preview

Yudhi Armyndharis351df0358306
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 0e6fbce72..894622e69 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -1783,6 +1783,50 @@ test('every message for a chat the sidebar does not list refetches the list, and
await screen.findByText('Dave');
});
+function revoke(chatId: string, id: string): void {
+ const socket = lastSocket();
+ assert.ok(socket, 'expected the page to have opened a socket');
+ socket.receive('message', {
+ type: 'event',
+ timestamp: new Date(1_700_003_000_000).toISOString(),
+ payload: {
+ event: 'message.revoked',
+ sessionId: SESSION.id,
+ data: { id, revokedId: id, chatId, from: chatId, to: 'me', body: '', type: 'revoked', timestamp: 1_700_003_000 },
+ },
+ });
+}
+
+test("a message deleted for everyone leaves the sidebar preview when it was the chat's newest", async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ const { container } = renderChats();
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+
+ const room = container.querySelector('.room-messages') as HTMLElement;
+ const sidebar = container.querySelector('.chats-sidebar') as HTMLElement;
+
+ // An older row going leaves the preview, which shows the newest one.
+ revoke(CHAT.id, DB_MESSAGE.waMessageId as string);
+ await waitFor(() => assert.ok(!within(room).queryByText('hello from alice'), 'the deleted row kept its text'));
+ within(sidebar).getByText('hello from alice');
+
+ revoke(CHAT.id, OMITTED_MEDIA_MESSAGE_2.waMessageId as string);
+ await waitFor(() =>
+ assert.ok(!within(sidebar).queryByText('hello from alice'), 'the sidebar still previews the deleted message'),
+ );
+});
+
+test('a message deleted for everyone in a chat never opened refetches the chat list', async () => {
+ const { screen, waitFor } = rtl;
+ renderChats();
+ await screen.findByText('Carol');
+ resetFetchCalls();
+
+ revoke(CHAT_2.id, 'wamid.carol.1');
+ await waitFor(() => assert.equal(countFetchCalls('GET', `/api/sessions/${SESSION.id}/chats`), 1));
+});
+
// A global-search hit in the third session, on Alice's chat.
const THIRD_SESSION_HIT: SearchHit = {
messageId: 'db-9',
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index def3c6a4a..b798adeda 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -538,14 +538,21 @@ export function Chats() {
);
const handleIncomingMessageRevoked = useCallback(
- (event: { sessionId: string; id: string; revokedId?: string; type: string }) => {
+ (event: { sessionId: string; id: string; revokedId?: string; chatId: string; type: string }) => {
if (event.sessionId !== selectedSessionId) return;
// Walk every cached chat under this session, find the deleted message and zero it — the
// backend emits an empty body; the localized "deleted" label is rendered below. Matching is
// in findRevokedIndex: the event carries two candidate ids and wwebjs's `id` alone can miss.
const revoked = (m: ChatMessageView): boolean => findRevokedIndex([m], event) !== -1;
- for (const [key] of cachedSessionThreads(queryClient, event.sessionId, revoked)) {
+ let matchedCachedMessage = false;
+ let revokedLastMessage = false;
+ for (const [key, thread] of cachedSessionThreads(queryClient, event.sessionId, revoked)) {
+ matchedCachedMessage = true;
+ // The sidebar previews the newest row, so only deleting that one changes it.
+ if (key[2] === event.chatId && findRevokedIndex(thread, event) === thread.length - 1) {
+ revokedLastMessage = true;
+ }
updateCachedMessages(queryClient, key, list => {
const idx = findRevokedIndex(list, event);
if (idx === -1) return list;
@@ -554,8 +561,15 @@ export function Chats() {
return next;
});
}
+ if (revokedLastMessage) {
+ setChats(previous => previous.map(chat => (chat.id === event.chatId ? { ...chat, lastMessage: '' } : chat)));
+ } else if (!matchedCachedMessage) {
+ // No cached thread proves whether the deleted message was the chat's newest; refresh the
+ // summaries, as an edit does.
+ void loadChats(selectedSessionId, { background: true });
+ }
},
- [selectedSessionId, queryClient],
+ [selectedSessionId, queryClient, loadChats],
);
const handleIncomingMessageEdited = useCallback(