Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): keep chat state when history sync omits the fields
Yudhi Armyndharis9422d324d40b
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.tsindex dfdea3a7b..8bd4b7c97 100644--- a/src/engine/adapters/baileys-session-store.spec.ts+++ b/src/engine/adapters/baileys-session-store.spec.ts@@ -282,6 +282,35 @@ describe('BaileysSessionStore', () => {expect(chatOn(s, { name: 'Renamed' })).toMatchObject({ muted: true, archived: true });});+ // A decoded proto.Conversation (what history sync hands over) carries archived, pinned and+ // muteEndTime as null defaults on its PROTOTYPE, and sets an own property only for a field that+ // was on the wire. Modelled here without importing the ESM-only WAProto.+ const historyChat = (own: Record<string, unknown>) =>+ Object.assign(Object.create({ archived: null, pinned: null, muteEndTime: null }) as object, {+ id: CHAT,+ name: 'Alice',+ ...own,+ }) as Record<string, unknown>;++ it('a history chat that omits the fields does not reset persisted pin, archive and mute', () => {+ chatOn(newStore(), { muteEndTime: -1, archived: true, pinned: 2 });+ for (const s of [newStore(), newStore()]) {+ s.upsertChats([historyChat({})]);+ expect(s.listChats()[0]).toMatchObject({ muted: true, archived: true, pinned: true });+ }+ expect(fake.get(SID, CHAT)).toEqual({ muteEndTime: -1, archived: true, pinned: true });+ });++ it('a history chat that omits the fields writes no row for a chat never seen', () => {+ newStore().upsertChats([historyChat({})]);+ expect(fake.rows.size).toBe(0);+ });++ it('a history chat that carries a field on the wire still persists it', () => {+ newStore().upsertChats([historyChat({ pinned: 5 })]);+ expect(fake.get(SID, CHAT)).toEqual({ muteEndTime: null, archived: false, pinned: true });+ });+it('normalizes a seconds-scale muteEndTime to ms on persist', () => {const nowS = Math.floor(Date.now() / 1000);chatOn(newStore(), { muteEndTime: nowS + 3600 });diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.tsindex 139fa6b9a..b8352434c 100644--- a/src/engine/adapters/baileys-session-store.ts+++ b/src/engine/adapters/baileys-session-store.ts@@ -733,17 +733,20 @@ export class BaileysSessionStore {}/**- * Write mute/archive/pin through to the persisted store when a chat update carries them. Key presence,- * not truthiness, is the trigger: a history-sync or name-hydration partial that omits these keys must- * not overwrite persisted state, and a live unmute arrives as `muteEndTime: null` (key present) that- * must persist as null. A no-op when this session has no store wired (unit tests, wwjs).+ * Write mute/archive/pin through to the persisted store when a chat update carries them. Own-key+ * presence, not truthiness, is the trigger: a history-sync or name-hydration partial that omits these+ * keys must not overwrite persisted state, and a live unmute arrives as `muteEndTime: null` (an own+ * key) that must persist as null. It has to be an OWN key: history sync hands over decoded+ * `proto.Conversation` instances, whose prototype defaults all three fields to null, so the `in`+ * operator would read every history chunk as a reset to unpinned, unarchived and unmuted. A no-op+ * when this session has no store wired (unit tests, wwjs).*/private persistChatState(id: string, r: Partial<Chat>): void {if (!this.chatStateStore || !this.sessionId) return;const patch: Partial<ChatStateValue> = {};- if ('muteEndTime' in r) patch.muteEndTime = this.normalizeMuteEndTime(r.muteEndTime);- if ('archived' in r) patch.archived = Boolean(r.archived);- if ('pinned' in r) patch.pinned = Boolean(r.pinned);+ if (Object.hasOwn(r, 'muteEndTime')) patch.muteEndTime = this.normalizeMuteEndTime(r.muteEndTime);+ if (Object.hasOwn(r, 'archived')) patch.archived = Boolean(r.archived);+ if (Object.hasOwn(r, 'pinned')) patch.pinned = Boolean(r.pinned);if (Object.keys(patch).length) {void this.chatStateStore.remember(this.sessionId, id, patch);}