Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): skip the media download on an own status post echo

Yudhi Armyndharisca194d3607e8
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index d884e8fb5..dae5de622 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -3274,6 +3274,49 @@ describe('WhatsAppWebJsAdapter inbound media (MEDIA_DOWNLOAD_ENABLED=false)', ()
expect(msg.media?.omitted).toBeUndefined();
});
+ it('does not download the media of an own status post echo', async () => {
+ // The echo consumer drops status posts, so the blob was fetched in full, held an inbound limiter
+ // slot, and was thrown away.
+ process.env[ENV] = 'true';
+ const adapter = new WhatsAppWebJsAdapter({
+ sessionId: 'sess-echo-status',
+ sessionDataPath: './data/sessions',
+ puppeteer: {},
+ });
+ const client = Object.assign(new EventEmitter(), {
+ info: { wid: { user: '628123' }, pushname: 'Tester' },
+ getState: jest.fn().mockResolvedValue(WAState.CONNECTED),
+ pupPage: { evaluate: jest.fn().mockResolvedValue(true) },
+ });
+ (adapter as unknown as { client: unknown }).client = client;
+ const onMessageCreate = jest.fn();
+ (adapter as unknown as { callbacks: unknown }).callbacks = { onMessageCreate };
+ (adapter as unknown as { setupEventHandlers: () => void }).setupEventHandlers();
+
+ const mockMsg = {
+ id: { _serialized: 'OWN_STATUS_1' },
+ to: 'status@broadcast',
+ body: '',
+ type: 'image',
+ timestamp: 1700000072,
+ fromMe: true,
+ hasMedia: true,
+ _data: { mimetype: 'image/png', size: 3 },
+ downloadMedia: jest.fn().mockResolvedValue({ mimetype: 'image/png', data: 'QUJD', filename: 'a.png' }),
+ hasQuotedMsg: false,
+ };
+
+ client.emit('message_create', mockMsg);
+ await new Promise(r => setImmediate(r));
+ await new Promise(r => setImmediate(r));
+
+ expect(onMessageCreate).toHaveBeenCalledTimes(1);
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ expect((onMessageCreate.mock.calls[0][0] as { isStatusBroadcast?: boolean }).isStatusBroadcast).toBe(true);
+ expect(mockMsg.downloadMedia).not.toHaveBeenCalled();
+ });
+
it('emits the echo with the omitted marker when the own-send media download fails', async () => {
// Pinned on: the disabled exit builds an identical marker, so an ambient 'false' would let this
// pass without ever attempting a download. The describe's afterEach restores it.
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index a52955a54..b22877387 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -100,8 +100,9 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
const incomingMessage = buildIncomingMessageBase(msg);
// Enrich with the media payload through the same capped path the incoming handler uses β€”
// the base builder is sync and carries none, so a phone-sent image would otherwise persist
- // and render as a bare πŸ“Ž marker even though the media is downloadable right here.
- if (msg.hasMedia) {
+ // and render as a bare πŸ“Ž marker even though the media is downloadable right here. Not for an own
+ // status post: the echo consumer drops those, so the download would only hold a limiter slot.
+ if (msg.hasMedia && !incomingMessage.isStatusBroadcast) {
try {
incomingMessage.media = await host.capInboundMediaFor(msg);
} catch (error) {