Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): refuse a group batch larger than the cold allowance
Yudhi Armyndharis4d6d12685015
diff --git a/src/modules/message/send-pacing-cold-query.spec.ts b/src/modules/message/send-pacing-cold-query.spec.tsindex 95d69f342..4d165cc28 100644--- a/src/modules/message/send-pacing-cold-query.spec.ts+++ b/src/modules/message/send-pacing-cold-query.spec.ts@@ -1,4 +1,5 @@import { DataSource } from 'typeorm';+import { HttpException } from '@nestjs/common';import { SEND_PACING_LIMITED, SendPacingService } from './send-pacing.service';import { Message, MessageDirection } from './entities/message.entity';import { Session } from '../session/entities/session.entity';@@ -237,9 +238,22 @@ describe('group reachouts against a real database', () => {// The cost is per stranger, not per call — otherwise one request adding two hundred numbers would// cost exactly as much as adding one, which is the abuse the rule exists to bound.it('charges the batch per new contact, not per call', async () => {- await expect(reachout('s1', ['[email protected]', '[email protected]', '[email protected]', '[email protected]'])).rejects.toMatchObject({- status: 429,- });+ await reachout('s1', ['[email protected]', '[email protected]']);+ await expect(reachout('s1', ['[email protected]', '[email protected]'])).rejects.toMatchObject({ status: 429 });+ });++ // Waiting cannot help a batch larger than a whole day's allowance, so a 429 with a retry hint would+ // send a client that honours it round the same refusal every day. Refuse it as a request to split.+ it('answers 400 without a retry hint for a batch larger than the whole allowance', async () => {+ const error = await service.assertReachoutAllowed('s1', ['[email protected]', '[email protected]', '[email protected]', '[email protected]']).then(+ () => null,+ (e: HttpException) => e,+ );+ expect(error?.getStatus()).toBe(400);+ expect(JSON.stringify(error?.getResponse())).toContain('batches of at most 3');+ expect(JSON.stringify(error?.getResponse())).not.toContain('retryAfterSeconds');+ // Nothing was reserved: a batch that fits is still allowed.+ await expect(reachout('s1', ['[email protected]', '[email protected]', '[email protected]'])).resolves.toBeUndefined();});it('does not charge for contacts the account already knows', async () => {diff --git a/src/modules/message/send-pacing.service.ts b/src/modules/message/send-pacing.service.tsindex 62e1c4b22..7410faa1c 100644--- a/src/modules/message/send-pacing.service.ts+++ b/src/modules/message/send-pacing.service.ts@@ -1,4 +1,4 @@-import { HttpException, HttpStatus, Injectable, Optional } from '@nestjs/common';+import { BadRequestException, HttpException, HttpStatus, Injectable, Optional } from '@nestjs/common';import { ConfigService } from '@nestjs/config';import { InjectRepository } from '@nestjs/typeorm';import { MoreThanOrEqual, Repository } from 'typeorm';@@ -211,6 +211,15 @@ export class SendPacingService {const dayStart = startOfUtcDay(new Date());const ageDays = Math.floor((dayStart.getTime() - startOfUtcDay(session.createdAt).getTime()) / DAY_MS);const allowance = this.allowanceForAge(config.coldSchedule, ageDays);+ // A batch larger than a whole day's allowance cannot pass on any day of this rung, so a 429 with a+ // retry hint would send a client that honours it round the same refusal every day. Nothing is+ // reserved; the caller has to split the request.+ if (coldCount > allowance) {+ throw new BadRequestException(+ `Reaching ${coldCount} new contact(s) exceeds the daily allowance of ${allowance} new ` ++ `conversation(s) for a session ${ageDays} day(s) old; split the request into batches of at most ${allowance}`,+ );+ }// Both sources of the day's reachouts: cold chat messages (persisted rows) and prior group adds// (the in-memory tally). Group adds persist nothing, so without the tally they would not count// against themselves and the cap would reset every request.