Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): learn the account's own lid when the connection opens
Yudhi Armyndharis018aaed1ee3c
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.tsindex 0f21914d7..f28ee79ad 100644--- a/src/engine/adapters/baileys-lifecycle.ts+++ b/src/engine/adapters/baileys-lifecycle.ts@@ -26,6 +26,7 @@ import {import { differentWaIds, type BaileysEvents } from './baileys-events';import type { BaileysHistory } from './baileys-history';import type { BaileysSessionStore } from './baileys-session-store';+import { userPart } from '../identity/wa-id';/** Linked-device identity shown in WhatsApp (Settings → Linked Devices). The display name is* operator-brandable via BAILEYS_BROWSER_NAME; it only applies to pairings made after the change. */@@ -568,6 +569,14 @@ export class BaileysLifecycle {this.qrCode = null;this.phoneNumber = this.host.extractPhone(this.sock?.user?.id);this.pushName = this.sock?.user?.name ?? null;+ // The account's own lid<->phone pair. Baileys stores it in its own mapping without emitting+ // lid-mapping.update, and an account whose only traffic is API sends never sees it on a message+ // key either, so a lid-addressed group's `<lid>@lid` row for the account stayed unresolved and+ // every self-admin check read it as somebody else.+ const me = this.sock?.user;+ if (me?.id && me.lid) {+ this.host.addLidMappings([{ lid: `${userPart(me.lid)}@lid`, pn: `${userPart(me.id)}@s.whatsapp.net` }]);+ }// I4: reset the reconnect counter on a successful connection.this.reconnectAttempts = 0;this.setStatus(EngineStatus.READY);diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 9f9ed3109..8c64458fe 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3982,6 +3982,44 @@ describe('BaileysAdapter store-backed ops', () => {GROUP,);});++ // A lid-addressed group lists every member, the account included, as `<lid>@lid`, and WhatsApp+ // withholds the phone twin, so the account's own row carries nothing but its lid.+ const lidMemberMessage = { ...memberMessage, key: { ...memberMessage.key, participant: '44455566@lid' } };+ const withLidAddressedSelfRole = (admin: 'admin' | null) =>+ fakeSock.groupMetadata.mockResolvedValue({+ id: GROUP,+ subject: 'G',+ addressingMode: 'lid',+ participants: [+ { id: '11122233@lid', admin },+ { id: '44455566@lid', admin: null },+ ],+ });++ it('revokes it in a lid-addressed group when the account is an admin there', async () => {+ // WhatsApp hands the account its own lid in the creds on connect.+ fakeSock.user = { id: '628999:[email protected]', lid: '11122233:1@lid', name: 'Me' };+ fakeStore.getMessage.mockResolvedValue(lidMemberMessage);+ withLidAddressedSelfRole('admin');+ const adapter = await ready();+ await adapter.deleteMessage(GROUP, 'TARGET', true);+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(GROUP, { delete: lidMemberMessage.key });+ expect(fakeSock.chatModify).not.toHaveBeenCalled();+ });++ it('deletes it for the account only in a lid-addressed group where the account is not an admin', async () => {+ fakeSock.user = { id: '628999:[email protected]', lid: '11122233:1@lid', name: 'Me' };+ fakeStore.getMessage.mockResolvedValue(lidMemberMessage);+ withLidAddressedSelfRole(null);+ const adapter = await ready();+ await adapter.deleteMessage(GROUP, 'TARGET', true);+ expect(fakeSock.sendMessage).not.toHaveBeenCalled();+ expect(fakeSock.chatModify).toHaveBeenCalledWith(+ { deleteForMe: { deleteMedia: true, key: lidMemberMessage.key, timestamp: 1700000007 } },+ GROUP,+ );+ });});it('media sends honor the chat disappearing timer via the funnel (#473)', async () => {@@ -4428,6 +4466,26 @@ describe('BaileysAdapter group management', () => {]);});+ it('recognises the account by its own lid in a lid-addressed group', async () => {+ // The own row is `<lid>@lid` with no phone twin; the lid comes from the creds on connect.+ fakeSock.user = { id: '628999:[email protected]', lid: '11122233:1@lid', name: 'Me' };+ const lidMeta = {+ id: '[email protected]',+ subject: 'G',+ announce: true,+ participants: [+ { id: '11122233@lid', admin: 'admin' },+ { id: '44455566@lid', admin: null },+ ],+ };+ fakeSock.groupFetchAllParticipating.mockResolvedValue({ '[email protected]': lidMeta });+ fakeSock.groupMetadata.mockResolvedValueOnce(lidMeta);+ const adapter = await ready();+ expect(await adapter.getGroups()).toEqual([expect.objectContaining({ id: '[email protected]', isAdmin: true })]);+ // An admin of an announce-only group can still post there.+ expect(await adapter.getGroupInfo('[email protected]')).toMatchObject({ isAnnounce: true, isReadOnly: false });+ });+it('getGroupInfo maps groupMetadata, and returns null only for a server refusal (401/403/404)', async () => {fakeSock.groupMetadata.mockResolvedValueOnce(META);const adapter = await ready();