Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): drop media held in variables from finished batches
Yudhi Armyndharisd7eebc06ef02
diff --git a/src/modules/message/batch-media.util.ts b/src/modules/message/batch-media.util.tsindex 20d00e19a..2e2dcd96d 100644--- a/src/modules/message/batch-media.util.ts+++ b/src/modules/message/batch-media.util.ts@@ -1,16 +1,28 @@import { DataSource, In, QueryDeepPartialEntity } from 'typeorm';import { BatchStatus, MessageBatch } from './entities/message-batch.entity';+/**+ * The names a template body asks for, in the renderer's own syntax: canonical `{{ name }}` and the+ * legacy `{name}` (see PLACEHOLDER_PATTERN in common/utils/template-render.ts, which a spec keeps in step).+ */+export function templateVariableNames(body: string): string[] {+ return Array.from(body.matchAll(/\{\{\s*([\w.-]+)\s*\}\}|\{(\w+)\}/g), match => match[1] ?? match[2]);+}+/*** Drop base64 payloads from a finished batch's stored message list. A completed/cancelled batch is* terminal (never resumed), so the (often multi-MB) base64 in `message_batches.messages` is dead- * weight; the descriptive fields (mimetype/filename/caption/url) are kept.+ * weight; the descriptive fields (mimetype/filename/caption/url) are kept. An item that takes its+ * media from a variable (`base64: '{{pic}}'`) keeps the bytes in `variables`, so those go too.*/export function stripBatchMediaPayloads(messages: MessageBatch['messages']): void {for (const m of messages ?? []) {for (const key of ['image', 'video', 'audio', 'document']) {const media = m.content[key] as { base64?: unknown } | undefined;if (media && typeof media === 'object' && 'base64' in media) {+ if (typeof media.base64 === 'string' && m.variables && typeof m.variables === 'object') {+ for (const name of templateVariableNames(media.base64)) delete m.variables[name];+ }delete media.base64;}}diff --git a/src/modules/message/bulk-media-variables.spec.ts b/src/modules/message/bulk-media-variables.spec.tsnew file mode 100644index 000000000..b8fdbf1dd--- /dev/null+++ b/src/modules/message/bulk-media-variables.spec.ts@@ -0,0 +1,110 @@+import { DataSource } from 'typeorm';+import { HookManager } from '../../core/hooks';+import { EngineRegistry } from '../../engine/engine-registry.service';+import type { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';+import { Session } from '../session/entities/session.entity';+import { BulkMessageService } from './bulk-message.service';+import { MessageBatch, BatchStatus } from './entities/message-batch.entity';+import { Message } from './entities/message.entity';+import type { MessageService } from './message.service';+import { renderTemplate } from '../../common/utils/template-render';+import { templateVariableNames } from './batch-media.util';+import type { SendPacingService } from './send-pacing.service';++describe('bulk media variable cleanup', () => {+ let db: DataSource;+ let bulk: BulkMessageService;+ let send: jest.Mock;+ let hooks: HookManager;+ const payload = 'QUJDREVG';+ const messages = () => [+ {+ chatId: '[email protected]',+ type: 'image' as const,+ content: { image: { base64: '{{picture}}', mimetype: 'image/png' }, caption: 'Photo' },+ variables: { picture: payload, name: 'Alice' },+ },+ ];++ beforeEach(async () => {+ db = await new DataSource({+ type: 'better-sqlite3',+ database: ':memory:',+ entities: [Message, MessageBatch, Session],+ synchronize: true,+ }).initialize();+ await db.getRepository(Session).save({ id: 's1', name: 'sender', config: {} });+ const engines = new EngineRegistry();+ send = jest.fn().mockResolvedValue({ id: 'sent-1', timestamp: 1700000000 });+ engines.set('s1', { sendImageMessage: send, sendTextMessage: send } as unknown as IWhatsAppEngine);+ hooks = new HookManager();+ bulk = new BulkMessageService(+ db.getRepository(MessageBatch),+ engines,+ { saveOutgoingMessage: () => Promise.resolve() } as unknown as MessageService,+ hooks,+ {+ assertSendAllowed: () => Promise.resolve(),+ recordSendFailure: () => undefined,+ recordSendSuccess: () => undefined,+ } as unknown as SendPacingService,+ );+ });++ afterEach(async () => {+ jest.restoreAllMocks();+ await db.destroy();+ });++ it.each([+ ['complete', BatchStatus.COMPLETED],+ ['cancel', BatchStatus.CANCELLED],+ ['startup', BatchStatus.FAILED],+ ] as const)('drops the media variable on %s', async (transition, terminalStatus) => {+ const repository = db.getRepository(MessageBatch);+ let batch: MessageBatch;+ if (transition === 'complete') {+ const processing = jest.spyOn(bulk as unknown as { processBatch: () => Promise<void> }, 'processBatch');+ batch = await bulk.createBatch('s1', { messages: messages() });+ await processing.mock.results[0].value;+ } else {+ batch = await repository.save({+ batchId: 'personalized',+ sessionId: 's1',+ status: BatchStatus.PENDING,+ messages: messages(),+ options: { delayBetweenMessages: 1000, randomizeDelay: false, stopOnError: false },+ progress: { total: 1, pending: 1, sent: 0, failed: 0, cancelled: 0 },+ results: [],+ currentIndex: 0,+ });+ if (transition === 'cancel') await bulk.cancelBatch('s1', batch.batchId);+ else await bulk.onApplicationBootstrap();+ }++ const stored = await repository.findOneByOrFail({ id: batch.id });+ expect(stored.status).toBe(terminalStatus);+ expect(JSON.stringify(stored.messages)).not.toContain(payload);+ expect(stored.messages[0].content).toMatchObject({ image: { mimetype: 'image/png' }, caption: 'Photo' });+ expect(stored.messages[0].variables).toEqual({ name: 'Alice' });+ });++ it('names the same variables the renderer substitutes', () => {+ for (const body of [+ '{{a}}',+ '{b}',+ '{{ c }}',+ '{{d.e-f}}',+ '{{{g}}}',+ '{ h }',+ '{{i}',+ 'a {j} and {{k}}',+ 'plain',+ ]) {+ const names = templateVariableNames(body);+ const vars = Object.fromEntries(names.map(name => [name, '@']));+ const rendered = renderTemplate(body, vars);+ expect(rendered.split('@').length - 1).toBe(names.length);+ }+ });+});