Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): let the newest inline media through even when it exceeds the budget

Yudhi Armyndharis73e629f01827
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.ts
index b9761a1f0..8e8c87c7e 100644
--- a/src/modules/message/message.service.spec.ts
+++ b/src/modules/message/message.service.spec.ts
@@ -2061,4 +2061,42 @@ describe('spendInlineMediaBudget', () => {
spendInlineMediaBudget(rows, 0);
expect(mediaOf(rows[0]).sizeBytes).toBe(4242);
});
+
+ /**
+ * A payload bigger than the whole budget was omitted even as the only media on the page, so a
+ * single large photo or video — well inside the 50 MiB the gateway stores inline — could never be
+ * read back through this route. The dashboard's thread has no other media source and fetches with
+ * staleTime: Infinity, so the user saw a permanent 📎 placeholder for an image WhatsApp shows.
+ *
+ * The newest payload is therefore always let through when inlining is enabled at all. The budget
+ * still bounds everything after it, and a budget of 0 still means "no inline media", so an
+ * operator who switched inlining off does not get one payload back.
+ */
+ it('lets the newest payload through even when it alone exceeds the budget', () => {
+ const rows = [row('huge', 5000)];
+ spendInlineMediaBudget(rows, 1000);
+
+ expect(mediaOf(rows[0]).data).toHaveLength(5000);
+ expect(mediaOf(rows[0]).omitted).toBeUndefined();
+ });
+
+ // Negative twin: the allowance is for the FIRST payload only — it must not become a blanket pass.
+ it('still omits the rows after an oversized newest payload', () => {
+ const rows = [row('huge', 5000), row('next', 10), row('later', 10)];
+ spendInlineMediaBudget(rows, 1000);
+
+ expect(mediaOf(rows[0]).data).toHaveLength(5000);
+ expect(mediaOf(rows[1]).data).toBeUndefined();
+ expect(mediaOf(rows[1]).omitted).toBe(true);
+ expect(mediaOf(rows[2]).omitted).toBe(true);
+ });
+
+ // A budget of 0 is an explicit "do not inline", not a small budget — no allowance applies.
+ it('grants no allowance when inlining is switched off entirely', () => {
+ const rows = [row('huge', 5000)];
+ spendInlineMediaBudget(rows, 0);
+
+ expect(mediaOf(rows[0]).data).toBeUndefined();
+ expect(mediaOf(rows[0]).omitted).toBe(true);
+ });
});
diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.ts
index 6f9213135..ebce10fee 100644
--- a/src/modules/message/message.service.ts
+++ b/src/modules/message/message.service.ts
@@ -73,7 +73,14 @@ export function spendInlineMediaBudget(messages: Message[], budgetBytes: number)
if (!media || typeof media.data !== 'string' || MEDIA_URL_POINTER.test(media.data)) continue;
const encoded = Buffer.byteLength(media.data, 'utf8');
- if (spent + encoded <= budgetBytes) {
+ // The newest payload is always let through when inlining is enabled at all. Without this an
+ // item larger than the whole budget was omitted even as the ONLY media on the page, so a single
+ // large photo or video — well inside the bytes the gateway stores inline — could never be read
+ // back through this route: the dashboard thread has no other media source and caches with
+ // staleTime: Infinity, leaving a permanent placeholder for media WhatsApp displays.
+ // A budget of 0 means "do not inline", not "a very small budget", so it grants no allowance.
+ const allowanceApplies = spent === 0 && budgetBytes > 0;
+ if (spent + encoded <= budgetBytes || allowanceApplies) {
spent += encoded;
continue;
}