Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(automation): apply an edited cooldown to a running quiet period
Yudhi Armyndharis83aaf137db3b
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.tsindex 3ddf29d53..db93b706c 100644--- a/src/modules/automation/automation-rules.service.spec.ts+++ b/src/modules/automation/automation-rules.service.spec.ts@@ -246,6 +246,22 @@ describe('AutomationRulesService', () => {expect(sends.map(s => s.chatId)).toEqual(['[email protected]', '[email protected]']);});+ it('cooldown: an edited cooldownSeconds governs a quiet period that is already running', async () => {+ const rule = await service.create('sessA', { name: 'all', replyText: 'ack', cooldownSeconds: 300 });+ const start = Date.now();+ const now = jest.spyOn(Date, 'now').mockReturnValue(start);+ try {+ await service.evaluateInbound('sessA', inbound());+ await service.update('sessA', rule.id, { cooldownSeconds: 1 });+ now.mockReturnValue(start + 2_000);+ await service.evaluateInbound('sessA', inbound({ id: 'wamid.2' }));+ } finally {+ now.mockRestore();+ }++ expect(sends).toHaveLength(2);+ });+it('cooldownSeconds 0 disables the quiet period', async () => {await service.create('sessA', { name: 'all', replyText: 'ack', cooldownSeconds: 0 });diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.tsindex 5e6ef546f..aa8759913 100644--- a/src/modules/automation/automation-rules.service.ts+++ b/src/modules/automation/automation-rules.service.ts@@ -45,8 +45,12 @@ const OPT_IN_CHAT_KINDS: ReadonlySet<string> = new Set(['channel', 'broadcast',export class AutomationRulesService {private readonly logger = createLogger('AutomationRulesService');- /** `${ruleId}:${chatId}` -> epoch ms until which the rule stays quiet in that chat. Per-process. */- private readonly cooldowns = new Map<string, number>();+ /**+ * `${ruleId}:${chatId}` -> when the rule last fired in that chat, and the expiry its cooldown at+ * that time implied. The quiet period is judged against the rule's CURRENT cooldownSeconds, so an+ * edit takes effect on a window already running; `until` only drives the sweep. Per-process.+ */+ private readonly cooldowns = new Map<string, { firedAt: number; until: number }>();private messagePort?: PluginMessagePort;@@ -218,18 +222,18 @@ export class AutomationRulesService {private inCooldown(rule: AutomationRule, chatId: string): boolean {if (!rule.cooldownSeconds) return false;- const until = this.cooldowns.get(`${rule.id}:${chatId}`);- return until !== undefined && until > Date.now();+ const entry = this.cooldowns.get(`${rule.id}:${chatId}`);+ return entry !== undefined && entry.firedAt + rule.cooldownSeconds * 1000 > Date.now();}private enterCooldown(rule: AutomationRule, chatId: string): void {if (!rule.cooldownSeconds) return;+ const now = Date.now();if (this.cooldowns.size >= COOLDOWN_SWEEP_THRESHOLD) {- const now = Date.now();- for (const [key, until] of this.cooldowns) {+ for (const [key, { until }] of this.cooldowns) {if (until <= now) this.cooldowns.delete(key);}}- this.cooldowns.set(`${rule.id}:${chatId}`, Date.now() + rule.cooldownSeconds * 1000);+ this.cooldowns.set(`${rule.id}:${chatId}`, { firedAt: now, until: now + rule.cooldownSeconds * 1000 });}}