fix: honor whatsapp mediaMaxMb (#505) (thanks @koala73)
This commit is contained in:
@@ -23,6 +23,7 @@ export type ResolvedWhatsAppAccount = {
|
||||
groupPolicy?: GroupPolicy;
|
||||
dmPolicy?: DmPolicy;
|
||||
textChunkLimit?: number;
|
||||
mediaMaxMb?: number;
|
||||
blockStreaming?: boolean;
|
||||
groups?: WhatsAppAccountConfig["groups"];
|
||||
};
|
||||
@@ -120,6 +121,7 @@ export function resolveWhatsAppAccount(params: {
|
||||
groupPolicy: accountCfg?.groupPolicy ?? params.cfg.whatsapp?.groupPolicy,
|
||||
textChunkLimit:
|
||||
accountCfg?.textChunkLimit ?? params.cfg.whatsapp?.textChunkLimit,
|
||||
mediaMaxMb: accountCfg?.mediaMaxMb ?? params.cfg.whatsapp?.mediaMaxMb,
|
||||
blockStreaming:
|
||||
accountCfg?.blockStreaming ?? params.cfg.whatsapp?.blockStreaming,
|
||||
groups: accountCfg?.groups ?? params.cfg.whatsapp?.groups,
|
||||
|
||||
@@ -788,6 +788,7 @@ export async function monitorWebProvider(
|
||||
groupAllowFrom: account.groupAllowFrom,
|
||||
groupPolicy: account.groupPolicy,
|
||||
textChunkLimit: account.textChunkLimit,
|
||||
mediaMaxMb: account.mediaMaxMb,
|
||||
blockStreaming: account.blockStreaming,
|
||||
groups: account.groups,
|
||||
},
|
||||
@@ -1305,6 +1306,7 @@ export async function monitorWebProvider(
|
||||
verbose,
|
||||
accountId: account.accountId,
|
||||
authDir: account.authDir,
|
||||
mediaMaxMb: account.mediaMaxMb,
|
||||
onMessage: async (msg) => {
|
||||
handledMessages += 1;
|
||||
lastMessageAt = Date.now();
|
||||
|
||||
@@ -3,12 +3,21 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
afterAll,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
vi,
|
||||
} from "vitest";
|
||||
|
||||
const readAllowFromStoreMock = vi.fn().mockResolvedValue([]);
|
||||
const upsertPairingRequestMock = vi
|
||||
.fn()
|
||||
.mockResolvedValue({ code: "PAIRCODE", created: true });
|
||||
const saveMediaBufferSpy = vi.fn();
|
||||
|
||||
vi.mock("../config/config.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||
@@ -33,6 +42,19 @@ vi.mock("../pairing/pairing-store.js", () => ({
|
||||
upsertPairingRequestMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../media/store.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../media/store.js")>();
|
||||
return {
|
||||
...actual,
|
||||
saveMediaBuffer: vi.fn(
|
||||
async (...args: Parameters<typeof actual.saveMediaBuffer>) => {
|
||||
saveMediaBufferSpy(...args);
|
||||
return actual.saveMediaBuffer(...args);
|
||||
},
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
const HOME = path.join(
|
||||
os.tmpdir(),
|
||||
`clawdbot-inbound-media-${crypto.randomUUID()}`,
|
||||
@@ -87,6 +109,10 @@ vi.mock("./session.js", () => {
|
||||
import { monitorWebInbox } from "./inbound.js";
|
||||
|
||||
describe("web inbound media saves with extension", () => {
|
||||
beforeEach(() => {
|
||||
saveMediaBufferSpy.mockClear();
|
||||
});
|
||||
|
||||
beforeAll(async () => {
|
||||
await fs.rm(HOME, { recursive: true, force: true });
|
||||
});
|
||||
@@ -182,4 +208,44 @@ describe("web inbound media saves with extension", () => {
|
||||
|
||||
await listener.close();
|
||||
});
|
||||
|
||||
it("passes mediaMaxMb to saveMediaBuffer", async () => {
|
||||
const onMessage = vi.fn();
|
||||
const listener = await monitorWebInbox({
|
||||
verbose: false,
|
||||
onMessage,
|
||||
mediaMaxMb: 1,
|
||||
});
|
||||
const { createWaSocket } = await import("./session.js");
|
||||
const realSock = await (
|
||||
createWaSocket as unknown as () => Promise<{
|
||||
ev: import("node:events").EventEmitter;
|
||||
}>
|
||||
)();
|
||||
|
||||
const upsert = {
|
||||
type: "notify",
|
||||
messages: [
|
||||
{
|
||||
key: { id: "img3", fromMe: false, remoteJid: "222@s.whatsapp.net" },
|
||||
message: { imageMessage: { mimetype: "image/jpeg" } },
|
||||
messageTimestamp: 1_700_000_003,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
realSock.ev.emit("messages.upsert", upsert);
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (onMessage.mock.calls.length > 0) break;
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
}
|
||||
|
||||
expect(onMessage).toHaveBeenCalledTimes(1);
|
||||
expect(saveMediaBufferSpy).toHaveBeenCalled();
|
||||
const lastCall = saveMediaBufferSpy.mock.calls.at(-1);
|
||||
expect(lastCall?.[3]).toBe(1 * 1024 * 1024);
|
||||
|
||||
await listener.close();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -376,7 +376,11 @@ export async function monitorWebInbox(options: {
|
||||
try {
|
||||
const inboundMedia = await downloadInboundMedia(msg, sock);
|
||||
if (inboundMedia) {
|
||||
const maxBytes = (options.mediaMaxMb ?? 50) * 1024 * 1024;
|
||||
const maxMb =
|
||||
typeof options.mediaMaxMb === "number" && options.mediaMaxMb > 0
|
||||
? options.mediaMaxMb
|
||||
: 50;
|
||||
const maxBytes = maxMb * 1024 * 1024;
|
||||
const saved = await saveMediaBuffer(
|
||||
inboundMedia.buffer,
|
||||
inboundMedia.mimetype,
|
||||
|
||||
Reference in New Issue
Block a user