Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): reread a group when its timer changes

Yudhi Armyndharis1880e1eeebaa
diff --git a/src/engine/adapters/baileys-group-metadata-cache.ts b/src/engine/adapters/baileys-group-metadata-cache.ts
index 0d1a09fb1..3c428f197 100644
--- a/src/engine/adapters/baileys-group-metadata-cache.ts
+++ b/src/engine/adapters/baileys-group-metadata-cache.ts
@@ -103,7 +103,7 @@ export class GroupMetadataCache {
}
/** Forget one group: its membership or settings changed, so the next send reads it again. */
- invalidate(jid: string | undefined): void {
+ invalidate(jid: string | null | undefined): void {
if (!jid) return;
this.entries.delete(jid);
this.inFlight.delete(jid);
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index ed9c6623d..9ee99cc7f 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -540,6 +540,12 @@ export class BaileysLifecycle {
event: 'update',
count: updates?.length ?? 0,
});
+ // A disappearing-messages timer changed on the phone (or turned off, which reads null) reaches us as a
+ // chat update only, and the cached list's timer is what a group send stamps on its stanza. The same
+ // own key makes upsertChats take the new timer.
+ for (const update of updates ?? []) {
+ if (Object.hasOwn(update, 'ephemeralExpiration')) this.groupMetadata.invalidate(update.id);
+ }
this.host.upsertChats(updates);
});
sock.ev.on('chats.delete', ids => {
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index a21b522fa..b6b4fca5f 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -10506,6 +10506,10 @@ describe('BaileysAdapter group metadata cache', () => {
['a participant is added or removed', 'group-participants.update', { id: GROUP, action: 'add', participants: [] }],
['the group changes a setting', 'groups.update', [{ id: GROUP, announce: true }]],
['the account is added to the group again', 'groups.upsert', [{ id: GROUP, participants: [] }]],
+ // Baileys reports a disappearing-messages change made on the phone as a chat update only, and the
+ // timer on the cached list is what a group send stamps on its stanza.
+ ['its disappearing-messages timer is changed', 'chats.update', [{ id: GROUP, ephemeralExpiration: 86400 }]],
+ ['its disappearing-messages timer is switched off', 'chats.update', [{ id: GROUP, ephemeralExpiration: null }]],
])('reads the group again after %s', async (_label, event, payload) => {
await ready();
const { cachedGroupMetadata } = socketConfig();
@@ -10517,6 +10521,17 @@ describe('BaileysAdapter group metadata cache', () => {
expect(fakeSock.groupMetadata).toHaveBeenCalledTimes(2);
});
+ it('keeps a cached group through the chat updates that every message raises', async () => {
+ await ready();
+ const { cachedGroupMetadata } = socketConfig();
+ await cachedGroupMetadata(GROUP);
+
+ fakeSock.fire('chats.update', [{ id: GROUP, unreadCount: 3, conversationTimestamp: 1_700_000_000 }]);
+ await cachedGroupMetadata(GROUP);
+
+ expect(fakeSock.groupMetadata).toHaveBeenCalledTimes(1);
+ });
+
it('refreshes a cached group from a whole snapshot without another query', async () => {
await ready();
const { cachedGroupMetadata } = socketConfig();