Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): delete for me when a message cannot be deleted for all
Yudhi Armyndharis67dbebc774c5
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex d16bc69c6..0a89bfeb3 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -16,6 +16,7 @@ import {Quotable,} from '../interfaces/whatsapp-engine.interface';import { toEngineParticipants } from './baileys-groups';+import { mapBaileysGroup } from './baileys-group-mapper';import { buildVCard } from './vcard';import { resolveBaileysButtonClick } from './baileys-message-mapper';import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';@@ -533,7 +534,10 @@ export class BaileysMessaging {this.host.ensureReady();const target = await this.requireStored(messageId);this.assertStoredInChat(target, chatId, messageId);- if (forEveryone) {+ // Only the sender, or a group admin, can delete a message for everyone. WhatsApp ignores any other+ // 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.+ if (forEveryone && (target.key.fromMe === true || (await this.selfIsGroupAdmin(target.key.remoteJid)))) {await this.send(await this.toDeliverableJid(chatId), { delete: target.key });return;}@@ -554,6 +558,17 @@ export class BaileysMessaging {);}+ /** Whether the account is an admin of this chat; false for anything that is not a group. */+ private async selfIsGroupAdmin(jid: string | null | undefined): Promise<boolean> {+ if (!jid?.endsWith('@g.us')) return false;+ const metadata = await withQueryDeadline(+ this.sock().groupMetadata(jid),+ this.queryBudgetMs,+ 'WhatsApp did not answer the group metadata query in time',+ );+ return mapBaileysGroup(metadata, this.host.normalizedSelfJid(), id => this.host.toNeutralJid(id)).isAdmin === true;+ }+async editMessage(chatId: string, messageId: string, body: string, mentions?: string[]): Promise<MessageResult> {this.host.ensureReady();const target = await this.requireStored(messageId);diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 9af7639ab..5558bc265 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3914,11 +3914,63 @@ describe('BaileysAdapter store-backed ops', () => {);});- it('deleteMessage revokes via the stored key', async () => {- fakeStore.getMessage.mockResolvedValue(stored);+ it('deleteMessage revokes an own message via the stored key', async () => {+ fakeStore.getMessage.mockResolvedValue(ownStored);+ const adapter = await ready();+ await adapter.deleteMessage('[email protected]', 'TARGET', true);+ expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: ownStored.key });+ });++ it('deleteMessage for everyone of a message the account received deletes it for the account only', async () => {+ // WhatsApp ignores a sender revoke of somebody else's message, so sending one reported a deletion+ // that never happened. WhatsApp Web deletes it for the account instead, and so does this.+ fakeStore.getMessage.mockResolvedValue({ ...stored, messageTimestamp: '1700000007' });const adapter = await ready();await adapter.deleteMessage('[email protected]', 'TARGET', true);- expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: stored.key });+ expect(fakeSock.sendMessage).not.toHaveBeenCalled();+ expect(fakeSock.chatModify).toHaveBeenCalledWith(+ { deleteForMe: { deleteMedia: true, key: stored.key, timestamp: 1700000007 } },+ '[email protected]',+ );+ });++ describe('deleteMessage for everyone of another member message in a group', () => {+ const GROUP = '[email protected]';+ const memberMessage = {+ key: { id: 'TARGET', remoteJid: GROUP, fromMe: false, participant: '[email protected]' },+ message: { conversation: 'hi' },+ messageTimestamp: '1700000007',+ };+ const withSelfRole = (admin: 'admin' | null) =>+ fakeSock.groupMetadata.mockResolvedValue({+ id: GROUP,+ subject: 'G',+ participants: [+ { id: '[email protected]', admin },+ { id: '[email protected]', admin: null },+ ],+ });++ it('revokes it when the account is a group admin', async () => {+ fakeStore.getMessage.mockResolvedValue(memberMessage);+ withSelfRole('admin');+ const adapter = await ready();+ await adapter.deleteMessage(GROUP, 'TARGET', true);+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(GROUP, { delete: memberMessage.key });+ expect(fakeSock.chatModify).not.toHaveBeenCalled();+ });++ it('deletes it for the account only when the account is not an admin', async () => {+ fakeStore.getMessage.mockResolvedValue(memberMessage);+ withSelfRole(null);+ const adapter = await ready();+ await adapter.deleteMessage(GROUP, 'TARGET', true);+ expect(fakeSock.sendMessage).not.toHaveBeenCalled();+ expect(fakeSock.chatModify).toHaveBeenCalledWith(+ { deleteForMe: { deleteMedia: true, key: memberMessage.key, timestamp: 1700000007 } },+ GROUP,+ );+ });});it('media sends honor the chat disappearing timer via the funnel (#473)', async () => {@@ -3945,15 +3997,15 @@ describe('BaileysAdapter store-backed ops', () => {});it('react and delete never carry an ephemeral timer (Baileys does not exclude reactions) (#473)', async () => {- fakeStore.getMessage.mockResolvedValue(stored);+ fakeStore.getMessage.mockResolvedValue(ownStored);const adapter = await ready();fakeSock.fire('chats.upsert', [{ id: '[email protected]', ephemeralExpiration: 604800 }]);await adapter.reactToMessage('[email protected]', 'TARGET', 'π');await adapter.deleteMessage('[email protected]', 'TARGET', true);expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {- react: { text: 'π', key: stored.key },+ react: { text: 'π', key: ownStored.key },});- expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: stored.key });+ expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: ownStored.key });});it('throws when the referenced message is not in the store', async () => {