Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): read a group again after a group listing
Yudhi Armyndharis63367a136b6d
diff --git a/.env.example b/.env.exampleindex 2fbcc2b83..478600709 100644--- a/.env.example+++ b/.env.example@@ -303,8 +303,8 @@ OPENWA_PIDS_LIMIT=2048# BAILEYS_CHAT_STATE_CACHE_MAX=5000# How long, in milliseconds, a Baileys group send reuses the group's participant list instead of querying-# it for every message. A membership or settings event, a reconnect and a write to the group each drop-# the cached list early. 0 queries for every send. Default 300000 (5 minutes).+# it for every message. A membership or settings event, a refresh of the group listing, a reconnect and a+# write to the group each drop the cached list early. 0 queries for every send. Default 300000 (5 minutes).# BAILEYS_GROUP_METADATA_CACHE_TTL_MS=300000# Per-session cap on the Baileys message rows kept in the database: the newest N survive and olderdiff --git a/docs/29-engine-capability-matrix.md b/docs/29-engine-capability-matrix.mdindex a7bf5109c..70486a73a 100644--- a/docs/29-engine-capability-matrix.md+++ b/docs/29-engine-capability-matrix.md@@ -1020,7 +1020,8 @@ adapter boundary — none silently stubs.unknown and the revoke is still sent.- **Group sends (baileys) reuse the group's participant list.** Rather than query it for everymessage, the adapter keeps each group's list for `BAILEYS_GROUP_METADATA_CACHE_TTL_MS` (default 5- minutes; `0` queries for every send). A membership or settings event for the group, a write to it+ minutes; `0` queries for every send). A membership or settings event for the group, a refresh of+ the all-groups listing (Baileys runs one on its own and `GET /groups` another), a write to itthrough the API, and every reconnect drop the cached list early. A member added without the adapterreceiving the event gets the group's messages once the entry expires or its client asks for aretry. whatsapp-web.js has no counterpart: the page keeps the member list itself.diff --git a/src/engine/adapters/baileys-group-metadata-cache.spec.ts b/src/engine/adapters/baileys-group-metadata-cache.spec.tsindex 1ff5d8223..4631297ce 100644--- a/src/engine/adapters/baileys-group-metadata-cache.spec.ts+++ b/src/engine/adapters/baileys-group-metadata-cache.spec.ts@@ -279,18 +279,36 @@ describe('GroupMetadataCache', () => {expect(fetch.mock.calls.map(([jid]) => jid)).toEqual([GROUP]);});- it('refreshes a cached group from a whole snapshot, restarting its TTL', async () => {+ it('drops a cached group a whole snapshot names, so only the single-group query writes an entry', async () => {const cache = build();- const fetch = jest.fn().mockResolvedValue(group(GROUP, ['[email protected]']));+ const queried = {+ ...group(GROUP, ['159442138038327@lid']),+ addressingMode: 'lid',+ ephemeralDuration: 86400,+ } as unknown as GroupMetadata;+ const fetch = jest.fn().mockResolvedValue(queried);await cache.read(GROUP, fetch);- clock += 900;- cache.applyUpdates([group(GROUP, ['[email protected]', '[email protected]'])]);- clock += 900;+ // A snapshot of the all-groups listing: phone-form ids, no addressing mode, no disappearing timer.+ cache.applyUpdates([group(GROUP, ['[email protected]']), group(OTHER)]);- const hit = await cache.read(GROUP, fetch);- expect(hit?.participants).toHaveLength(2);- expect(fetch).toHaveBeenCalledTimes(1);+ expect(cache.peek(GROUP)).toBeUndefined();+ expect(cache.size).toBe(0);+ expect(await cache.read(GROUP, fetch)).toBe(queried);+ expect(fetch).toHaveBeenCalledTimes(2);+ });++ it('does not keep a read that a snapshot overtook', async () => {+ const cache = build();+ let answer!: (metadata: GroupMetadata) => void;+ const fetch = jest.fn(() => new Promise<GroupMetadata>(resolve => (answer = resolve)));+ const read = cache.read(GROUP, fetch);++ cache.applyUpdates([group(GROUP, ['[email protected]'])]);+ answer(group(GROUP));+ await read;++ expect(cache.size).toBe(0);});it('does not add a group nobody has sent to from a snapshot', () => {diff --git a/src/engine/adapters/baileys-group-metadata-cache.ts b/src/engine/adapters/baileys-group-metadata-cache.tsindex 0b943f4de..9c922c77f 100644--- a/src/engine/adapters/baileys-group-metadata-cache.ts+++ b/src/engine/adapters/baileys-group-metadata-cache.ts@@ -96,14 +96,6 @@ export class GroupMetadataCache {return pending;}- /**- * Replace the copy of a group that is already cached with metadata that arrived whole. A group nobody- * has sent to is not added: the cache holds what sends use, not every group the account is in.- */- refresh(metadata: GroupMetadata): void {- if (this.enabled && metadata.id && this.entries.has(metadata.id)) this.store(metadata.id, metadata);- }-/** Forget one group: its membership or settings changed, so the next send reads it again. */invalidate(jid: string | null | undefined): void {if (!jid) return;@@ -120,16 +112,15 @@ export class GroupMetadataCache {}/**- * Apply a `groups.update` batch. A whole-metadata entry (the result of a full group fetch, which- * Baileys also emits through this event) refreshes the cached copy; a partial one is a real change to- * some field, so the group is read again on the next send.+ * Apply a `groups.update` batch: every group it names is read again on the next send. A partial entry+ * is a real change to some field. A whole-metadata entry (the result of the all-groups listing, which+ * Baileys also emits through this event, on every dirty-groups notice as well as on GET /groups) comes+ * from a different query than a send uses, and may name participants in another id form or leave out+ * the addressing mode and the disappearing timer, so it never becomes the cached copy. The cost is one+ * single-group query per cached group after such a listing.*/applyUpdates(updates: ReadonlyArray<Partial<GroupMetadata>> | undefined): void {- for (const update of updates ?? []) {- if (!update?.id) continue;- if (Array.isArray(update.participants)) this.refresh(update as GroupMetadata);- else this.invalidate(update.id);- }+ for (const update of updates ?? []) this.invalidate(update?.id);}private store(jid: string, metadata: GroupMetadata): void {diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.tsindex 193199ea2..efcbff818 100644--- a/src/engine/adapters/baileys-lifecycle.ts+++ b/src/engine/adapters/baileys-lifecycle.ts@@ -556,8 +556,8 @@ export class BaileysLifecycle {});this.host.removeChats(ids);});- // The same events keep the group metadata cache honest: a membership change or a (re)join drops that- // group, a whole-metadata snapshot refreshes it, and any other change makes it read again.+ // The same events keep the group metadata cache honest: a membership change, a (re)join, a settings+ // change or a whole-metadata snapshot drops that group, so the next send reads it again.sock.ev.on('group-participants.update', event => {this.groupMetadata.invalidate(event.id);this.host.handleGroupParticipantsUpdate(event);diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex ef5a708ab..c946f197f 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -10537,16 +10537,24 @@ describe('BaileysAdapter group metadata cache', () => {expect(fakeSock.groupMetadata).toHaveBeenCalledTimes(1);});- it('refreshes a cached group from a whole snapshot without another query', async () => {+ it('builds the next send from the single-group query, not from a whole snapshot of the group list', async () => {+ const queried = {+ ...meta(['159442138038327@lid']),+ addressingMode: 'lid',+ ephemeralDuration: 86400,+ };+ fakeSock.groupMetadata.mockResolvedValue(queried);await ready();const { cachedGroupMetadata } = socketConfig();await cachedGroupMetadata(GROUP);+ // The all-groups listing can name participants in another id form and leave out the addressing mode+ // and the disappearing timer; it must not become what the next group send is built from.fakeSock.fire('groups.update', [meta(['[email protected]', '[email protected]'])]);- const refreshed = (await cachedGroupMetadata(GROUP)) as { participants: unknown[] };+ const next = await cachedGroupMetadata(GROUP);- expect(refreshed.participants).toHaveLength(2);- expect(fakeSock.groupMetadata).toHaveBeenCalledTimes(1);+ expect(next).toBe(queried);+ expect(fakeSock.groupMetadata).toHaveBeenCalledTimes(2);});it('still reports the group events to consumers while it keeps the cache', async () => {