Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): clear revoked history previews

Yudhi Armyndharisdd1605bebc96
diff --git a/src/engine/adapters/baileys-history.spec.ts b/src/engine/adapters/baileys-history.spec.ts
index b68d290a1..a248a6aae 100644
--- a/src/engine/adapters/baileys-history.spec.ts
+++ b/src/engine/adapters/baileys-history.spec.ts
@@ -1,5 +1,62 @@
-import type { WASocket } from '@whiskeysockets/baileys';
+import type { WAMessage, WASocket } from '@whiskeysockets/baileys';
import { BaileysHistory, BaileysHistoryHost } from './baileys-history';
+import { BaileysSessionStore } from './baileys-session-store';
+
+describe('history chat previews', () => {
+ it.each([false, true])('clears the revoked target in either batch order (reversed=%s)', async reversed => {
+ const store = new BaileysSessionStore(undefined, 's1');
+ store.addLidMappings([{ lid: '99@lid', pn: '[email protected]' }]);
+ store.upsertChats([{ id: '[email protected]' }]);
+ const messages = [
+ {
+ key: { id: 'M1', remoteJid: '[email protected]', fromMe: false },
+ message: { conversation: 'secret' },
+ messageTimestamp: 100,
+ },
+ {
+ key: { id: 'REVOKE', remoteJid: '99@lid', fromMe: false },
+ message: { protocolMessage: { type: 0, key: { id: 'M1', remoteJid: '[email protected]' } } },
+ messageTimestamp: 200,
+ },
+ ] as WAMessage[];
+ const host = {
+ loadLib: () =>
+ Promise.resolve({
+ normalizeMessageContent: (m: unknown) => m,
+ getContentType: (m: object) => Object.keys(m)[0],
+ proto: { Message: { ProtocolMessage: { Type: { REVOKE: 0 } } } },
+ }),
+ normalizedSelfJid: () => '[email protected]',
+ toNeutralJid: (id: string) => store.toNeutralJid(id),
+ extractEphemeralDuration: (msg: WAMessage) => store.extractEphemeralDuration(msg),
+ recordMessage: (msg: WAMessage, type: Parameters<typeof store.recordMessage>[1]) =>
+ store.recordMessage(msg, type),
+ recordMessageEdit: (...args: Parameters<typeof store.recordMessageEdit>) => store.recordMessageEdit(...args),
+ upsertContacts: () => undefined,
+ getOnHistoryMessages: () => () => undefined,
+ } as unknown as BaileysHistoryHost;
+ const history = new BaileysHistory(host);
+ await history.captureHistoryMessages(reversed ? [...messages].reverse() : messages);
+ expect(store.listChats()[0]).toMatchObject({ lastMessage: '', lastMessageType: 'revoked' });
+
+ // A revoke of an older message cannot replace a newer preview or its timestamp.
+ await history.captureHistoryMessages([
+ { key: { id: 'M2', remoteJid: '[email protected]' }, message: { conversation: 'newer' }, messageTimestamp: 300 },
+ messages[1],
+ {
+ key: { id: 'REACTION', remoteJid: '[email protected]' },
+ message: { reactionMessage: { text: 'ok', key: { id: 'M2' } } },
+ messageTimestamp: 400,
+ },
+ {
+ key: { id: 'PROTOCOL', remoteJid: '[email protected]' },
+ message: { protocolMessage: { type: 3 } },
+ messageTimestamp: 500,
+ },
+ ] as WAMessage[]);
+ expect(store.listChats()[0]).toMatchObject({ lastMessage: 'newer', lastMessageType: 'text' });
+ });
+});
/**
* `groupFetchAllParticipating` yields `{}` for BOTH an unanswered query and an account with no groups —
diff --git a/src/engine/adapters/baileys-history.ts b/src/engine/adapters/baileys-history.ts
index 8458f351c..78c0af604 100644
--- a/src/engine/adapters/baileys-history.ts
+++ b/src/engine/adapters/baileys-history.ts
@@ -3,6 +3,7 @@ import type { Chat, Contact as BaileysContact, WAMessage, WASocket } from '@whis
import { EngineEventCallbacks, IncomingMessage, MessageType } from '../interfaces/whatsapp-engine.interface';
import {
BAILEYS_NON_CONTENT_TYPES,
+ baileysChatJid,
buildIncomingMessageFromBaileys,
extractBaileysBody,
extractBaileysButtonReply,
@@ -30,6 +31,7 @@ export interface BaileysHistoryHost {
loadLib(): Promise<typeof BaileysLib>;
/** Seed the chat's last-message preview + sort time from a history/live message. */
recordMessage(msg: WAMessage, type?: MessageType): void;
+ recordMessageEdit(chatId: string, messageId: string, text: string, type?: MessageType): void;
upsertContacts(records: Partial<BaileysContact>[]): void;
upsertChats(records: Partial<Chat>[]): void;
/**
@@ -105,6 +107,7 @@ export class BaileysHistory {
const b = await this.host.loadLib();
const nameUpdates: { id: string; notify: string }[] = [];
const mapped: IncomingMessage[] = [];
+ const revokes: { chatId: string; messageId: string }[] = [];
for (const msg of messages) {
if (msg.key?.fromMe !== true && msg.pushName) {
const sender = msg.key?.participant ?? msg.key?.remoteJid;
@@ -117,8 +120,19 @@ export class BaileysHistory {
// Only content messages replace the preview; reactions and protocol updates do not.
this.host.recordMessage(msg, incoming.type);
mapped.push(incoming);
+ } else if (msg.key?.remoteJid) {
+ const content = b.normalizeMessageContent(msg.message) ?? msg.message;
+ const protocol = content?.protocolMessage;
+ if (protocol && protocol.type === b.proto.Message.ProtocolMessage.Type.REVOKE && protocol.key?.id) {
+ revokes.push({
+ chatId: baileysChatJid(msg.key.remoteJid, msg.key.participant, msg.key.fromMe === true),
+ messageId: protocol.key.id,
+ });
+ }
}
}
+ // History batches can put a revoke before its target. Update only the target preview after all content.
+ for (const { chatId, messageId } of revokes) this.host.recordMessageEdit(chatId, messageId, '', 'revoked');
if (nameUpdates.length) {
this.host.upsertContacts(nameUpdates);
}