Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): deliver API mutation events (#1780)

Yudhi Armyndharisa80dbfa27891
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7958ae654..76c386f3c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Deliver Baileys API edit and revoke events to webhook and WebSocket consumers.
- Preserve chat-scoped WebSocket event order while resolving chat identities.
- Preserve poll choices across dashboard echoes and keep revoked messages cleared.
- Serialize explicitly selected empty Go redrive slices as empty arrays.
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 5492f8818..b4301450c 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -601,18 +601,15 @@ export class BaileysMessaging {
// revoke, yet the send resolves, so it would report a deletion that never happened. WhatsApp Web
// deletes such a message for the account alone instead, and so does this. A group whose member
// list shows no row for the account proves nothing either way, so the revoke still goes out there.
- // Whichever branch runs, the text leaves this account's view, so it must not stay the chat
- // preview. The echo of an own revoke is skipped as an own send, so the inbound path never clears it.
+ // Whichever branch runs, the text leaves this account's view, so clear the preview immediately.
const chatJid = target.key.remoteJid ?? chatId;
// The preview of a received broadcast-list message is kept in its sender's chat.
const previewJid = baileysChatJid(chatJid, target.key.participant, target.key.fromMe === true);
if (forEveryone && (target.key.fromMe === true || (await this.selfIsGroupAdmin(target.key.remoteJid)) !== false)) {
await this.send(await this.toDeliverableJid(chatId), { delete: target.key });
this.host.recordMessageEdit(previewJid, messageId, '', 'revoked');
- // The echo of this delete is skipped as an own send, so the stored copy is emptied here, as
- // processInboundMessage does for a delete made from the phone or by the other side. Recorded
- // first, so the message stays deleted even if the store write fails or a repeat delivery of the
- // original is stored after it.
+ // Clear the stored copy before the buffered echo arrives. Record the deletion first so it
+ // survives a failed store write or a repeat delivery of the original.
this.host.markDeletedForEveryone(messageId);
await this.changeStored(messageId, stored => ({ ...stored, message: null }));
return;
@@ -678,10 +675,9 @@ export class BaileysMessaging {
const editContent = { text: body, ...this.withMentions(mentions), edit: target.key };
const b = await this.host.loadLib();
await this.send(jid, this.previewSafe(editContent), this.previewSafeOptions(editContent));
- // The edit's echo is skipped as an own send, so the chat preview follows it from here.
+ // Update the preview and raw copy immediately, ahead of the buffered echo.
this.host.recordMessageEdit(target.key.remoteJid ?? chatId, messageId, body);
- // Same reason as deleteMessage: the stored copy is what a later quote carries, and this edit's
- // echo never reaches processInboundMessage.
+ // A later quote must carry the updated text even before the edit's echo arrives.
await this.changeStored(messageId, stored => {
const content = b.normalizeMessageContent(stored.message ?? undefined);
return content && setBaileysText(content, body) ? stored : null;
@@ -866,8 +862,8 @@ export class BaileysMessaging {
if (this.host.getSocketOrNull() !== sock) throw new EngineNotReadyError();
throw error;
}
- // Reactions are announced by their library echo; no local message.sent callback replaces it.
- if (!('react' in content)) this.host.rememberOwnSend(sent?.key?.id);
+ // Mutations are announced by their library echo; no local callback replaces it.
+ if (!('react' in content || 'delete' in content || 'edit' in content)) this.host.rememberOwnSend(sent?.key?.id);
return sent;
}
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 092b98cb2..1b3bd08d1 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -2663,6 +2663,66 @@ describe('BaileysAdapter inbound fan-out', () => {
expect(onMessageCreate).not.toHaveBeenCalled();
});
+ it.each([
+ ['delete', '[email protected]', true],
+ ['edit', '[email protected]', true],
+ ['delete', '123456789@lid', true],
+ ['delete', '[email protected]', false],
+ ] as const)('delivers an API %s echo in %s', async (operation, chatId, fromMe) => {
+ jest
+ .requireMock<Record<string, jest.Mock>>('@whiskeysockets/baileys')
+ .getContentType.mockImplementation(realGetContentType);
+ fakeSock.user = { id: '[email protected]' };
+ fakeSock.groupMetadata.mockResolvedValue({
+ id: chatId,
+ participants: [{ id: fakeSock.user.id, admin: 'admin' }],
+ });
+ const target = {
+ key: { remoteJid: chatId, fromMe, id: 'TARGET', participant: '[email protected]' },
+ message: { conversation: 'hello' },
+ messageTimestamp: 1700000000,
+ };
+ const echo = {
+ key: { remoteJid: chatId, fromMe: true, id: 'MUTATION_ECHO' },
+ message: {
+ protocolMessage: {
+ key: target.key,
+ type: operation === 'delete' ? 0 : 14,
+ ...(operation === 'edit' ? { editedMessage: { conversation: 'updated' } } : {}),
+ },
+ },
+ messageTimestamp: 1700000001,
+ };
+ fakeStore.getMessage.mockResolvedValue(target);
+ fakeSock.sendMessage.mockResolvedValue(echo);
+ const onMessageRevoked = jest.fn();
+ const onMessageEdited = jest.fn();
+ const onMessageCreate = jest.fn();
+ const onMessage = jest.fn();
+ const adapter = newAdapter();
+ await adapter.initialize({ onMessageRevoked, onMessageEdited, onMessageCreate, onMessage });
+ fakeSock.fire('connection.update', { connection: 'open' });
+ if (operation === 'delete') await adapter.deleteMessage(chatId, 'TARGET', true);
+ else await adapter.editMessage(chatId, 'TARGET', 'updated');
+ expect(onMessageRevoked).not.toHaveBeenCalled();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ fakeSock.fire('messages.upsert', { type: 'append', messages: [echo] });
+ await new Promise(resolve => setImmediate(resolve));
+ const callback = operation === 'delete' ? onMessageRevoked : onMessageEdited;
+ expect(callback).toHaveBeenCalledTimes(1);
+ expect(callback).toHaveBeenCalledWith(
+ expect.objectContaining({
+ chatId: chatId.replace('@s.whatsapp.net', '@c.us'),
+ ...(operation === 'delete'
+ ? { id: 'TARGET', revokedId: 'TARGET', body: '' }
+ : { messageId: 'TARGET', body: 'updated' }),
+ }),
+ );
+ expect(operation === 'delete' ? onMessageEdited : onMessageRevoked).not.toHaveBeenCalled();
+ expect(onMessageCreate).not.toHaveBeenCalled();
+ expect(onMessage).not.toHaveBeenCalled();
+ });
+
// The account's own phone kept working while the gateway was down. WhatsApp replays what it sent
// in that window through the same 'append' tag as the API echo above, and only the id says it is
// not ours. It is an outgoing message the session never saw, so it goes out as onMessageCreate.