Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): stop stamping a disappearing timer the chat turned off
Yudhi Armyndharis785fbab50350
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.tsindex adcd53aa7..8161b3b76 100644--- a/src/engine/adapters/baileys-session-store.spec.ts+++ b/src/engine/adapters/baileys-session-store.spec.ts@@ -529,6 +529,59 @@ describe('BaileysSessionStore', () => {expect(store.getEphemeralExpiration('[email protected]')).toBe(86400);});+ it('drops a message-learned timer once chats.update turns disappearing messages off', () => {+ // Baileys reports the EPHEMERAL_SETTING change as chats.update with ephemeralExpiration null; later+ // messages carry no expiration, so only this update can retire the timer the cache learned.+ store.recordMessage({+ key: { remoteJid: '[email protected]', fromMe: false, id: 'M1' },+ message: { extendedTextMessage: { text: 'hi', contextInfo: { expiration: 86400 } } },+ messageTimestamp: 100,+ });+ store.upsertChats([{ id: '[email protected]', ephemeralSettingTimestamp: 200, ephemeralExpiration: null }]);+ store.recordMessage({+ key: { remoteJid: '[email protected]', fromMe: false, id: 'M2' },+ message: { conversation: 'later' },+ messageTimestamp: 300,+ });+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();+ });++ it('takes a changed timer from chats.update over the one learned from messages', () => {+ store.recordMessage({+ key: { remoteJid: '[email protected]', fromMe: false, id: 'M1' },+ message: { extendedTextMessage: { text: 'hi', contextInfo: { expiration: 86400 } } },+ messageTimestamp: 100,+ });+ store.upsertChats([{ id: '[email protected]', ephemeralExpiration: 604800 }]);+ expect(store.getEphemeralExpiration('[email protected]')).toBe(604800);+ });++ it('applies a lid-keyed chats.update to the timer an own send cached under the phone twin', () => {+ store.addLidMappings([{ lid: '111@lid', pn: '[email protected]' }]);+ store.upsertChats([{ id: '111@lid', ephemeralExpiration: 86400 }]);+ store.recordMessage({+ key: { remoteJid: '[email protected]', fromMe: true, id: 'S1' },+ message: { extendedTextMessage: { text: 'sent', contextInfo: { expiration: 86400 } } },+ messageTimestamp: 100,+ });+ store.upsertChats([{ id: '111@lid', ephemeralExpiration: 604800 }]);+ expect(store.getEphemeralExpiration('[email protected]')).toBe(604800);+ store.upsertChats([{ id: '111@lid', ephemeralExpiration: null }]);+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();+ });++ it('keeps a message-learned timer when a chat update does not carry ephemeralExpiration', () => {+ store.recordMessage({+ key: { remoteJid: '[email protected]', fromMe: false, id: 'M1' },+ message: { extendedTextMessage: { text: 'hi', contextInfo: { expiration: 86400 } } },+ messageTimestamp: 100,+ });+ store.upsertChats([{ id: '[email protected]', unreadCount: 2 }]);+ expect(store.getEphemeralExpiration('[email protected]')).toBe(86400);+ });+it('prefers the message-learned timer over a stale Chat.ephemeralExpiration', () => {store.upsertChats([{ id: '[email protected]', ephemeralExpiration: 604800 }]);store.recordMessage({diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.tsindex 1c43bd6fa..1157dcfef 100644--- a/src/engine/adapters/baileys-session-store.ts+++ b/src/engine/adapters/baileys-session-store.ts@@ -302,6 +302,20 @@ export class BaileysSessionStore {const existing = this.chats.get(r.id) ?? { id: r.id };this.chats.set(r.id, { ...existing, ...r });this.persistChatState(r.id, r);+ // A timer the chat itself reports (Baileys emits the EPHEMERAL_SETTING change as chats.update) is+ // newer than the one learned from messages, and turning the timer off produces no stamped message+ // that could clear it. Own key only, as in persistChatState: history-sync proto defaults are not news.+ // Every twin: own sends cache the timer under the phone spelling of a chat Baileys keys by its lid.+ if (Object.hasOwn(r, 'ephemeralExpiration')) {+ const exp = r.ephemeralExpiration;+ for (const key of new Set(this.chatTwins(r.id).flatMap(k => [k, this.toNeutralJid(k)]))) {+ if (typeof exp === 'number' && exp > 0) {+ this.ephemeralByChat.set(key, exp);+ } else {+ this.ephemeralByChat.delete(key);+ }+ }+ }}}