Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): skip status.received for an expired status
Yudhi Armyndharis4c31b1a37fa2
diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 5cef97b76..247906790 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -750,7 +750,9 @@ export class MessageProjector {// The ingest awaited; a stop()/delete() can retire this engine mid-flight — don't// dispatch for a session that no longer exists (mirrors message.received's re-check).if (!this.engines.isLive(id, engine)) return;- if (created) this.dispatchStatusReceived(id, row);+ // One that expired while the ingest ran (a slow media write) is gone from every read: announcing it+ // would point a consumer at a status the API no longer serves.+ if (created && row.expiresAt > Date.now()) this.dispatchStatusReceived(id, row);}).catch(err =>this.logger.warn('Status ingest failed', {diff --git a/src/modules/session/message-projector.status-expiry.spec.ts b/src/modules/session/message-projector.status-expiry.spec.tsnew file mode 100644index 000000000..d6221bc1a--- /dev/null+++ b/src/modules/session/message-projector.status-expiry.spec.ts@@ -0,0 +1,67 @@+import type { Repository } from 'typeorm';+import { EngineRegistry } from '../../engine/engine-registry.service';+import type { IncomingMessage, IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';+import type { HookManager } from '../../core/hooks';+import type { EventsGateway } from '../events/events.gateway';+import type { Message } from '../message/entities/message.entity';+import type { StatusStoreService } from '../status-store/status-store.service';+import type { StatusUpdate } from '../status-store/entities/status-update.entity';+import type { WebhookService } from '../webhook/webhook.service';+import { MessageProjector } from './message-projector.service';+import type { SessionLidResolver } from './session-lid-resolver.service';+import type { Session } from './entities/session.entity';++const status: IncomingMessage = {+ id: 'st1',+ chatId: 'status@broadcast',+ from: 'status@broadcast',+ to: '[email protected]',+ author: '[email protected]',+ fromMe: false,+ isStatusBroadcast: true,+ body: 'hi',+ type: 'text',+ timestamp: 1_700_000_000,+ isGroup: false,+ kind: 'status',+};++describe('status.received for a status that was ingested', () => {+ const run = async (expiresAt: number, created = true) => {+ const engines = new EngineRegistry();+ const engine = {} as IWhatsAppEngine;+ engines.set('s1', engine);+ const dispatch = jest.fn().mockResolvedValue(undefined);+ const emitStatusReceived = jest.fn();+ const row = { waStatusId: 'st1', contactJid: '[email protected]', type: 'text', mediaOmitted: false, expiresAt };+ const projector = new MessageProjector(+ {} as Repository<Message>,+ {} as Repository<Session>,+ engines,+ { emitStatusReceived } as unknown as EventsGateway,+ { dispatch } as unknown as WebhookService,+ {} as HookManager,+ { ingest: jest.fn().mockResolvedValue({ row: row as StatusUpdate, created }) } as unknown as StatusStoreService,+ {} as SessionLidResolver,+ );+ await projector.handleInboundMessage('s1', engine, status)?.settled;+ return { dispatch, emitStatusReceived };+ };++ it('announces a status that is still live', async () => {+ const { dispatch, emitStatusReceived } = await run(Date.now() + 60_000);+ expect(dispatch).toHaveBeenCalledWith('s1', 'status.received', expect.objectContaining({ statusId: 'st1' }));+ expect(emitStatusReceived).toHaveBeenCalledTimes(1);+ });++ it('announces nothing for a status that expired while it was being ingested', async () => {+ const { dispatch, emitStatusReceived } = await run(Date.now() - 1);+ expect(dispatch).not.toHaveBeenCalled();+ expect(emitStatusReceived).not.toHaveBeenCalled();+ });++ it('announces nothing for a status that was already stored', async () => {+ const { dispatch } = await run(Date.now() + 60_000, false);+ expect(dispatch).not.toHaveBeenCalled();+ });+});