Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): read a chat muted "Always" as muted indefinitely

Yudhi Armyndharisdebf4951e42a
diff --git a/src/engine/adapters/baileys-chat-state.entity.ts b/src/engine/adapters/baileys-chat-state.entity.ts
index a0de455a6..936e91cad 100644
--- a/src/engine/adapters/baileys-chat-state.entity.ts
+++ b/src/engine/adapters/baileys-chat-state.entity.ts
@@ -23,7 +23,7 @@ export class ChatState {
@PrimaryColumn()
chatId!: string;
- /** Epoch MILLISECONDS the mute ends, or null when the chat is not muted. */
+ /** Epoch MILLISECONDS the mute ends, -1 for a mute with no end ("Always"), or null when not muted. */
@Column({
type: 'bigint',
nullable: true,
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.ts
index dc14dddb2..b1d5981f4 100644
--- a/src/engine/adapters/baileys-session-store.spec.ts
+++ b/src/engine/adapters/baileys-session-store.spec.ts
@@ -225,6 +225,10 @@ describe('BaileysSessionStore', () => {
const asLong = { toNumber: () => Date.now() + 60 * 60 * 1000 };
expect(chatFor({ muteEndTime: asLong }).muted).toBe(true);
});
+
+ it('reads a mute set to Always (WhatsApp sends muteEndTime -1) as muted indefinitely', () => {
+ expect(chatFor({ muteEndTime: -1 })).toMatchObject({ muted: true, muteExpiration: 0 });
+ });
});
describe('chat-state persistence (survives a reconnect Baileys cannot resync)', () => {
@@ -287,6 +291,17 @@ describe('BaileysSessionStore', () => {
expect(chatOn(newStore(), { muteEndTime: null }).muteExpiration).toBeUndefined();
});
+ it('persists a mute set to Always as -1 and reads it back as muted indefinitely', () => {
+ expect(chatOn(newStore(), { muteEndTime: -1 })).toMatchObject({ muted: true, muteExpiration: 0 });
+ expect(fake.get(SID, CHAT)?.muteEndTime).toBe(-1);
+ expect(chatOn(newStore(), { name: 'Alice' })).toMatchObject({ muted: true, muteExpiration: 0 });
+ });
+
+ it('reads a mute Always persisted as -1000 by an earlier version as muted indefinitely', async () => {
+ await fake.remember(SID, CHAT, { muteEndTime: -1000 });
+ expect(chatOn(newStore(), { name: 'Alice' })).toMatchObject({ muted: true, muteExpiration: 0 });
+ });
+
it('normalizes a seconds-scale expiry to ms for muteExpiration', () => {
const nowS = Math.floor(Date.now() / 1000);
expect(chatOn(newStore(), { muteEndTime: nowS + 3600 }).muteExpiration).toBe((nowS + 3600) * 1000);
diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.ts
index 4d90f72c3..2b9552157 100644
--- a/src/engine/adapters/baileys-session-store.ts
+++ b/src/engine/adapters/baileys-session-store.ts
@@ -605,11 +605,13 @@ export class BaileysSessionStore {
* `conversationTimestamp` beside it. So it is normalised by magnitude: below 1e12 is seconds (an
* epoch-ms stamp below 1e12 is a date before 2001-09) and is scaled to ms. The current state survives a
* reconnect via {@link persistChatState}, because Baileys re-emits only app-state mutations newer than
- * the persisted version, never an already-applied mute.
+ * the persisted version, never an already-applied mute. A negative value is WhatsApp's "Always"
+ * sentinel (-1, the value WhatsApp Web sends too), a mute with no end.
*/
private isMuted(muteEndTime: number | { toNumber(): number } | null | undefined): boolean {
const raw = this.toUnixSeconds(muteEndTime);
if (!raw) return false;
+ if (raw < 0) return true;
const endMs = raw < 1e12 ? raw * 1000 : raw;
return endMs > Date.now();
}
@@ -617,11 +619,12 @@ export class BaileysSessionStore {
/**
* The expiry instant (epoch ms) for {@link ChatSummary.muteExpiration}, or undefined when the chat
* is not muted. Same normalisation as {@link isMuted}, so the two agree: a value only survives here
- * when it is still in the future.
+ * when it is still in the future. A mute with no end reads 0, the contract's "muted indefinitely".
*/
private muteExpirationMs(muteEndTime: number | { toNumber(): number } | null | undefined): number | undefined {
const raw = this.toUnixSeconds(muteEndTime);
if (!raw) return undefined;
+ if (raw < 0) return 0;
const endMs = raw < 1e12 ? raw * 1000 : raw;
return endMs > Date.now() ? endMs : undefined;
}
@@ -643,10 +646,14 @@ export class BaileysSessionStore {
}
}
- /** Normalise a raw muteEndTime to canonical epoch ms, or null (0/absent = unmuted). See {@link isMuted}. */
+ /**
+ * Normalise a raw muteEndTime to canonical epoch ms, or null (0/absent = unmuted). A mute with no end
+ * keeps the -1 sentinel rather than scaling it. See {@link isMuted}.
+ */
private normalizeMuteEndTime(v: number | { toNumber(): number } | null | undefined): number | null {
const n = this.toUnixSeconds(v);
if (!n) return null;
+ if (n < 0) return -1;
return n < 1e12 ? n * 1000 : n;
}