Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): revoke when the account's own group row is unknown
Yudhi Armyndharisba30c26dc407
diff --git a/src/engine/adapters/baileys-group-mapper.ts b/src/engine/adapters/baileys-group-mapper.tsindex 743fb6cac..d5aa33cde 100644--- a/src/engine/adapters/baileys-group-mapper.ts+++ b/src/engine/adapters/baileys-group-mapper.ts@@ -25,13 +25,19 @@ function preferPhoneDialect(jid: string, phoneTwin: string | undefined, normalizreturn normalizeJid(phoneTwin ?? jid);}-function isSelfAdmin(metadata: GroupMetadata, selfJid: string, normalizeJid: NormalizeJid): boolean {+/** The account's own participant row, or undefined when no row can be identified as the account. */+export function findSelfParticipant(+ metadata: GroupMetadata,+ selfJid: string,+ normalizeJid: NormalizeJid,+): GroupMetadata['participants'][number] | undefined {const self = userPart(normalizeJid(selfJid));- return metadata.participants.some(- p =>- userPart(preferPhoneDialect(p.id, p.phoneNumber, normalizeJid)) === self &&- (p.admin === 'admin' || p.admin === 'superadmin'),- );+ return metadata.participants.find(p => userPart(preferPhoneDialect(p.id, p.phoneNumber, normalizeJid)) === self);+}++function isSelfAdmin(metadata: GroupMetadata, selfJid: string, normalizeJid: NormalizeJid): boolean {+ const admin = findSelfParticipant(metadata, selfJid, normalizeJid)?.admin;+ return admin === 'admin' || admin === 'superadmin';}/** Map a Baileys GroupMetadata to the neutral summary {@link Group}. `selfJid` flags whether WE are an admin. */diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex 3df5b0103..48b6b0fb7 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -16,7 +16,7 @@ import {Quotable,} from '../interfaces/whatsapp-engine.interface';import { toEngineParticipants } from './baileys-groups';-import { mapBaileysGroup } from './baileys-group-mapper';+import { findSelfParticipant } from './baileys-group-mapper';import { buildVCard } from './vcard';import { resolveBaileysButtonClick } from './baileys-message-mapper';import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';@@ -539,8 +539,9 @@ export class BaileysMessaging {this.assertStoredInChat(target, chatId, messageId);// 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)))) {+ // 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.+ if (forEveryone && (target.key.fromMe === true || (await this.selfIsGroupAdmin(target.key.remoteJid)) !== false)) {await this.send(await this.toDeliverableJid(chatId), { delete: target.key });return;}@@ -561,15 +562,19 @@ 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> {+ /**+ * Whether the account is an admin of this chat: false for anything that is not a group, and+ * undefined when no participant row can be identified as the account.+ */+ private async selfIsGroupAdmin(jid: string | null | undefined): Promise<boolean | undefined> {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;+ const self = findSelfParticipant(metadata, this.host.normalizedSelfJid(), id => this.host.toNeutralJid(id));+ return self === undefined ? undefined : self.admin === 'admin' || self.admin === 'superadmin';}async editMessage(chatId: string, messageId: string, body: string, mentions?: string[]): Promise<MessageResult> {diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 8c64458fe..2f6da964a 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -4020,6 +4020,17 @@ describe('BaileysAdapter store-backed ops', () => {GROUP,);});++ it('revokes it when no participant can be identified as the account', async () => {+ // No own lid on the creds and no phone twin on any row: nothing shows the account is not an+ // admin, so the revoke goes out as it always did rather than quietly becoming a local delete.+ fakeStore.getMessage.mockResolvedValue(lidMemberMessage);+ withLidAddressedSelfRole(null);+ const adapter = await ready();+ await adapter.deleteMessage(GROUP, 'TARGET', true);+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(GROUP, { delete: lidMemberMessage.key });+ expect(fakeSock.chatModify).not.toHaveBeenCalled();+ });});it('media sends honor the chat disappearing timer via the funnel (#473)', async () => {