Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): skip replaying events the webhook unsubscribed from
Yudhi Armyndharis80ea658e6a59
diff --git a/src/modules/queue/processors/webhook.processor.ts b/src/modules/queue/processors/webhook.processor.tsindex 1abdc055b..96d07d319 100644--- a/src/modules/queue/processors/webhook.processor.ts+++ b/src/modules/queue/processors/webhook.processor.ts@@ -90,8 +90,8 @@ export class WebhookProcessor extends WorkerHost {try {// The job carries a snapshot taken at enqueue time. Re-read the row before every attempt: a// webhook that was deleted, disabled or unsubscribed from this event since then must not- // receive it (the reconciler skips only deleted and disabled rows; neither re-applies the- // webhook's filters, which need the event data). Completing the job instead of throwing+ // receive it (the reconciler applies the same test; neither re-applies the webhook's filters,+ // which need the event data). Completing the job instead of throwing// stops the retries and files no dead-letter row. Otherwise deliver with the CURRENT url,// headers and secret, as the reconciler's replay does, so a receiver move or a rotated// secret or auth header applies to jobs already waiting in the queue.diff --git a/src/modules/webhook/webhook-reconciler.service.spec.ts b/src/modules/webhook/webhook-reconciler.service.spec.tsindex 5a8637675..b99b18547 100644--- a/src/modules/webhook/webhook-reconciler.service.spec.ts+++ b/src/modules/webhook/webhook-reconciler.service.spec.ts@@ -51,7 +51,7 @@ describe('WebhookReconcilerService', () => {redeliver: jest.fn().mockResolvedValue('delivered'),isLocallyPending: jest.fn().mockReturnValue(false),};- webhooks = { findOne: jest.fn().mockResolvedValue({ id: 'wh-1', active: true }) };+ webhooks = { findOne: jest.fn().mockResolvedValue({ id: 'wh-1', active: true, events: ['*'] }) };service = new WebhookReconcilerService(webhooks as never, outbox as never, delivery as never);});@@ -63,7 +63,7 @@ describe('WebhookReconcilerService', () => {// Deriving a fresh key would make the replay read as a second event at the receiver rather than// a retry of the first, which is the whole reason the key is stored rather than recomputed.expect(delivery.redeliver).toHaveBeenCalledWith(- { id: 'wh-1', active: true },+ { id: 'wh-1', active: true, events: ['*'] },'sess-1','message.received','stored-key_wh-1',@@ -147,6 +147,17 @@ describe('WebhookReconcilerService', () => {expect(stats).toMatchObject({ skipped: 1 });});+ it('does not replay an event the webhook has since unsubscribed from', async () => {+ outbox.findStale.mockResolvedValue([row({ event: 'message.received' })]);+ webhooks.findOne.mockResolvedValue({ id: 'wh-1', active: true, events: ['session.status'] });++ const stats = await service.sweep(OPTS);++ expect(delivery.redeliver).not.toHaveBeenCalled();+ expect(outbox.close).toHaveBeenCalledWith('wh-1', 'stored-key_wh-1', 'failed');+ expect(stats).toMatchObject({ skipped: 1, replayed: 0 });+ });+it('leaves a row alone while this node is still dispatching it', async () => {// A direct delivery with retries, or one parked behind slow receivers, can stay pending past the// grace window. Replaying it would POST alongside the original and spend its budget mid-flight.diff --git a/src/modules/webhook/webhook-reconciler.service.ts b/src/modules/webhook/webhook-reconciler.service.tsindex 8eb9b47ee..a6e324ec1 100644--- a/src/modules/webhook/webhook-reconciler.service.ts+++ b/src/modules/webhook/webhook-reconciler.service.ts@@ -108,9 +108,10 @@ export class WebhookReconcilerService implements OnModuleInit, OnModuleDestroy {continue;}const webhook = await this.webhooks.findOne({ where: { id: row.webhookId } });- if (!webhook || !webhook.active) {- // The subscription is gone or switched off; replaying it would deliver an event the- // operator has already unsubscribed from.+ if (!webhook || !webhook.active || !(webhook.events.includes(row.event) || webhook.events.includes('*'))) {+ // The subscription is gone, switched off or no longer lists this event; replaying it would+ // deliver an event the operator has already unsubscribed from. Same test as the queue+ // processor applies before every attempt.await this.outbox.close(row.webhookId, row.idempotencyKey, 'failed');stats.skipped++;continue;