Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): bound muteUntil to the last JavaScript date
Yudhi Armyndharis1e6c877807bf
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex b5da3eebe..2251d2f67 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -1126,7 +1126,7 @@ Mute a chat's notifications until a given moment, or unmute it.| Field | Type | Required | Constraints | Description || ----------- | -------------- | -------- | -------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- || `chatId` | string | Yes | `@IsString`; `@IsNotEmpty`; `@MaxLength(255)`; `@Matches(/^[^\s@]+@[^\s@]+$/)` (localpart@host, no whitespace) | Engine-native JID, e.g. `[email protected]` |-| `muteUntil` | number \| null | Yes | `@IsInt`; `@Min(1)` when not `null` | Epoch **milliseconds** the mute expires at, or `null` to unmute |+| `muteUntil` | number \| null | Yes | `@IsInt`; `@Min(1)`; `@Max(8640000000000000)` when not `null` | Epoch **milliseconds** the mute expires at, or `null` to unmute |```json{ "chatId": "[email protected]", "muteUntil": 1800000000000 }@@ -1145,7 +1145,8 @@ Mute a chat's notifications until a given moment, or unmute it.> **`muteUntil` is required, and `null` is not the same as omitting it.** The two plausible readings> of a missing field — unmute, or mute forever — are opposites, so the endpoint rejects the omission> with a `400` instead of guessing. Send `null` to unmute. To mute indefinitely, send a far-future-> timestamp: neither engine exposes a portable "forever" value (whatsapp-web.js uses `-1` internally,+> timestamp, at most `8640000000000000` (the last instant a JavaScript date holds; a later one is a+> `400`): neither engine exposes a portable "forever" value (whatsapp-web.js uses `-1` internally,> Baileys has none), so the API keeps one well-defined shape.> **Milliseconds, not seconds.** This was measured against a live WhatsApp account rather than readdiff --git a/openapi.json b/openapi.jsonindex df683c706..c7eeb3449 100644--- a/openapi.json+++ b/openapi.json@@ -19214,10 +19214,11 @@},"muteUntil": {"type": "integer",- "description": "Absolute epoch-MILLISECONDS timestamp at which the mute expires, or `null` to unmute now. Required — omitting it is rejected rather than guessed, because the two plausible readings (unmute vs mute forever) are opposites. To mute indefinitely, send a far-future timestamp. Milliseconds, not seconds: a seconds-scale value is an instant in 1970, so the mute expires immediately while the request still answers 200.",+ "description": "Absolute epoch-MILLISECONDS timestamp at which the mute expires, or `null` to unmute now. Required — omitting it is rejected rather than guessed, because the two plausible readings (unmute vs mute forever) are opposites. To mute indefinitely, send a far-future timestamp, no later than 8640000000000000 (the last instant a JavaScript date holds). Milliseconds, not seconds: a seconds-scale value is an instant in 1970, so the mute expires immediately while the request still answers 200.","example": 1800000000000,"nullable": true,- "minimum": 1+ "minimum": 1,+ "maximum": 8640000000000000}},"required": [diff --git a/src/modules/session/dto/mute-chat.dto.spec.ts b/src/modules/session/dto/mute-chat.dto.spec.tsindex 940914c53..2738f7eb0 100644--- a/src/modules/session/dto/mute-chat.dto.spec.ts+++ b/src/modules/session/dto/mute-chat.dto.spec.ts@@ -1,13 +1,36 @@+import 'reflect-metadata';import { DECORATORS } from '@nestjs/swagger';+import { plainToInstance } from 'class-transformer';+import { validate } from 'class-validator';import { MuteChatDto } from './mute-chat.dto';describe('MuteChatDto published schema', () => {- // @nestjs/swagger derives nothing from @IsInt/@Min, so the decorator has to state both.- it('publishes muteUntil as a nullable integer of at least 1', () => {+ // @nestjs/swagger derives nothing from @IsInt/@Min/@Max, so the decorator has to state all of them.+ it('publishes muteUntil as a nullable integer between 1 and the last JavaScript date', () => {expect(Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, MuteChatDto.prototype, 'muteUntil')).toMatchObject({type: 'integer',nullable: true,minimum: 1,+ maximum: 8_640_000_000_000_000,});});});++// whatsapp-web.js converts the value through Date, which is NaN past 8.64e15 and so became a mute with no+// end, and Baileys writes it into an int64 that wraps past 2^63 (1e300 encodes as 0). Both engines take every+// instant up to that bound exactly, and the dashboard's "always" and the documented far-future example sit+// far below it.+describe('MuteChatDto muteUntil bound', () => {+ const muteUntilConstraints = async (muteUntil: unknown): Promise<string[]> => {+ const errors = await validate(plainToInstance(MuteChatDto, { chatId: '[email protected]', muteUntil }));+ return errors.filter(e => e.property === 'muteUntil').flatMap(e => Object.keys(e.constraints ?? {}));+ };++ it.each([null, 1, 1_800_000_000_000, Date.UTC(2100, 0, 1), 8_640_000_000_000_000])('accepts %s', async value => {+ expect(await muteUntilConstraints(value)).toEqual([]);+ });++ it.each([8_640_000_000_000_001, Number.MAX_SAFE_INTEGER, 1e16, 1e300])('refuses %s', async value => {+ expect(await muteUntilConstraints(value)).toEqual(['max']);+ });+});diff --git a/src/modules/session/dto/mute-chat.dto.ts b/src/modules/session/dto/mute-chat.dto.tsindex d43ff3ee1..fc4c58296 100644--- a/src/modules/session/dto/mute-chat.dto.ts+++ b/src/modules/session/dto/mute-chat.dto.ts@@ -1,7 +1,14 @@import { ApiProperty } from '@nestjs/swagger';-import { IsInt, IsNotEmpty, IsString, Matches, Min, ValidateIf, MaxLength } from 'class-validator';+import { IsInt, IsNotEmpty, IsString, Matches, Max, Min, ValidateIf, MaxLength } from 'class-validator';import { CHAT_ID_MAX_LENGTH } from '../../message/dto/send-message.dto';+/**+ * The last instant a JavaScript Date can hold. whatsapp-web.js converts `muteUntil` through Date, which is+ * NaN past it and then becomes a mute with no end, and Baileys writes the number into an int64 that wraps+ * past 2^63 (1e300 encodes as 0). Both take every instant up to this bound exactly.+ */+export const MUTE_UNTIL_MAX = 8_640_000_000_000_000;+export class MuteChatDto {@ApiProperty({maxLength: CHAT_ID_MAX_LENGTH,@@ -22,18 +29,21 @@ export class MuteChatDto {description:'Absolute epoch-MILLISECONDS timestamp at which the mute expires, or `null` to unmute now. ' +'Required — omitting it is rejected rather than guessed, because the two plausible readings ' +- '(unmute vs mute forever) are opposites. To mute indefinitely, send a far-future timestamp. ' ++ '(unmute vs mute forever) are opposites. To mute indefinitely, send a far-future timestamp, no later ' ++ 'than 8640000000000000 (the last instant a JavaScript date holds). ' +'Milliseconds, not seconds: a seconds-scale value is an instant in 1970, so the mute expires ' +'immediately while the request still answers 200.',example: 1800000000000,type: 'integer',nullable: true,minimum: 1,+ maximum: MUTE_UNTIL_MAX,})// `null` is a real instruction (unmute), so it skips the numeric checks rather than failing them.// A missing field is NOT null, so it still falls through to @IsInt and is rejected with a 400.@ValidateIf((o: MuteChatDto) => o.muteUntil !== null)@IsInt()@Min(1)+ @Max(MUTE_UNTIL_MAX)muteUntil!: number | null;}