Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): match an edit or delete to its stored copy by alt jid
Yudhi Armyndharisb84ddd978cc8
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex a680e8b46..5afec9346 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -607,14 +607,19 @@ export class BaileysEvents {* checks neither the chat nor the sender of either (Utils/process-message.js), so the stored copy* changes only for one from the same chat and from the message's author. A group admin may delete* anyone's message, which nothing here can verify, so a group delete skips the author check.- * Compared in the neutral dialect, so a chat or sender seen by lid once and by phone once matches.+ * Compared in the neutral dialect, and through each key's alt twin as targetsForeignMessage does, so+ * a chat or sender seen by lid once and by phone once matches even when the pair is not yet known.*/private mayChange(target: WAMessageKey, envelope: WAMessageKey, isDelete: boolean): boolean {- const chat = (key: WAMessageKey): string => this.host.toNeutralJid(key.remoteJid ?? '');- const author = (key: WAMessageKey): string =>- key.fromMe === true ? 'fromMe' : this.host.toNeutralJid(key.participant || key.remoteJid || '');- if (chat(target) !== chat(envelope)) return false;- return (isDelete && parseWaId(chat(target)).kind === 'group') || author(target) === author(envelope);+ const neutral = (jids: Array<string | null | undefined>): string[] =>+ jids.filter((jid): jid is string => !!jid).map(jid => this.host.toNeutralJid(jid));+ const chat = (key: WAMessageKey): string[] => neutral([key.remoteJid, key.remoteJidAlt]);+ const author = (key: WAMessageKey): string[] =>+ key.fromMe === true ? ['fromMe'] : key.participant ? neutral([key.participant, key.participantAlt]) : chat(key);+ const overlap = (a: string[], b: string[]): boolean => a.some(jid => b.includes(jid));+ if (!overlap(chat(target), chat(envelope))) return false;+ const inGroup = chat(target).some(jid => parseWaId(jid).kind === 'group');+ return (isDelete && inGroup) || overlap(author(target), author(envelope));}handleMessagesUpdate(updates: Array<{ key?: { id?: string | null }; update?: { status?: number | null } }>): void {diff --git a/src/engine/adapters/baileys-quote-on-receipt.spec.ts b/src/engine/adapters/baileys-quote-on-receipt.spec.tsindex a1fc4f494..fb0a25ae2 100644--- a/src/engine/adapters/baileys-quote-on-receipt.spec.ts+++ b/src/engine/adapters/baileys-quote-on-receipt.spec.ts@@ -203,6 +203,26 @@ describe('quoting a Baileys message the moment it is announced', () => {expect(options.quoted?.message?.conversation).toBe('after the edit');});+ it('empties a message stored under its lid when the delete names the chat by phone', async () => {+ const revoked = jest.fn();+ const { events, messaging } = build(() => undefined, { revoked });+ const byLid = inbound('TARGET');+ byLid.key = { ...byLid.key, remoteJid: '99887@lid', remoteJidAlt: CHAT };+ events.handleMessagesUpsert({ messages: [byLid], type: 'notify' });+ await ticks();+ release();+ await ticks();++ events.handleMessagesUpsert({ messages: [change({ type: 0 })], type: 'notify' });+ await ticks();++ expect(revoked).toHaveBeenCalledTimes(1);+ expect((await store.getMessage('s1', 'TARGET'))?.message).toBeNull();+ await expect(messaging.forwardMessage(CHAT, '[email protected]', 'TARGET')).rejects.toBeInstanceOf(+ MessageNotFoundError,+ );+ });+it('refuses a consumer of the delete that quotes the deleted message at once', async () => {let reply: Promise<unknown> | undefined;const { events, messaging, sock } = build(() => undefined, {