Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): keep a turned-off timer when older messages arrive
Yudhi Armyndharisf81a7189d296
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.tsindex 38bd9aab2..34403abdc 100644--- a/src/engine/adapters/baileys-session-store.spec.ts+++ b/src/engine/adapters/baileys-session-store.spec.ts@@ -556,6 +556,30 @@ describe('BaileysSessionStore', () => {expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();});+ it.each([+ ['[email protected]', '[email protected]'],+ ['111@lid', '[email protected]'],+ ['[email protected]', '111@lid'],+ ])('keeps a timer the %s chat turned off when an older %s message arrives later', (chat, from) => {+ // A reconnect flush delivers chats.update before messages.upsert, and history sync applies the chats+ // before their messages, so a message stamped under the old timer can be recorded after the change.+ store.addLidMappings([{ lid: '111@lid', pn: '[email protected]' }]);+ store.upsertChats([{ id: chat, ephemeralSettingTimestamp: 200, ephemeralExpiration: null }]);+ store.recordMessage({+ key: { remoteJid: from, fromMe: false, id: 'M1' },+ message: { extendedTextMessage: { text: 'hi', contextInfo: { expiration: 86400 } } },+ messageTimestamp: 100,+ });+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();+ expect(store.getEphemeralExpiration('111@lid')).toBeUndefined();+ store.recordMessage({+ key: { remoteJid: from, fromMe: false, id: 'M2' },+ message: { extendedTextMessage: { text: 'on again', contextInfo: { expiration: 604800 } } },+ messageTimestamp: 300,+ });+ expect(store.getEphemeralExpiration('[email protected]')).toBe(604800);+ });+it('takes a changed timer from chats.update over the one learned from messages', () => {store.recordMessage({key: { remoteJid: '[email protected]', fromMe: false, id: 'M1' },diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.tsindex e2b152c2c..37ae54eda 100644--- a/src/engine/adapters/baileys-session-store.ts+++ b/src/engine/adapters/baileys-session-store.ts@@ -491,12 +491,22 @@ export class BaileysSessionStore {* JID and its neutral form, so {@link getEphemeralExpiration} hits regardless of which dialect the caller* sends to. A non-positive/absent value means "no live timer on this message" and is left untouched (a* single non-ephemeral message must not clear a known timer; WhatsApp keeps stamping it while on).+ * A message no newer than the chat's last timer change (`ephemeralSettingTimestamp`, on any twin) is+ * skipped: it carries the old timer, and a reconnect flush or history sync records it after the change.*/private recordEphemeralFromMessage(chatId: string, msg: WAMessage): void {const duration = this.extractEphemeralDuration(msg);if (duration === undefined) {return;}+ const setAt = Math.max(+ ...[this.chatKey(chatId), ...this.chatTwins(chatId)].map(k =>+ this.toUnixSeconds(this.chats.get(k)?.ephemeralSettingTimestamp),+ ),+ );+ if (setAt > 0 && this.toUnixSeconds(msg.messageTimestamp) <= setAt) {+ return;+ }this.ephemeralByChat.set(chatId, duration);this.ephemeralByChat.set(this.toNeutralJid(chatId), duration);}