Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): give each media download its own state

Yudhi Armyndharis7d9806352de7
diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsx
index 1e6cff0b6..6f834adcc 100644
--- a/dashboard/src/components/chats/ChatThread.tsx
+++ b/dashboard/src/components/chats/ChatThread.tsx
@@ -54,12 +54,16 @@ function ChatThread({
// MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES — past that a row arrives as `{ omitted: true }`. Without
// this fetch the placeholder is terminal: this thread requests the largest page size and caches it
// with staleTime Infinity, so an older attachment in a media-heavy chat has no other route to it.
- const [mediaFetch, setMediaFetch] = useState<{ id: string; state: 'loading' | 'failed' } | null>(null);
+ // Keyed by message id, not one shared slot: a viewer can click a second placeholder before the
+ // first resolves, and each download owns its own lifecycle. Sharing one slot let the second click
+ // overwrite the first, after which whichever settled first cleared the other's state — re-enabling
+ // a button whose fetch was still open, and landing a failure marker on the wrong bubble.
+ const [mediaFetch, setMediaFetch] = useState<Record<string, 'loading' | 'failed'>>({});
const downloadMedia = useCallback(
async (message: ChatMessageView) => {
const messageId = message.waMessageId;
if (!sessionId || !messageId) return;
- setMediaFetch({ id: messageId, state: 'loading' });
+ setMediaFetch(prev => ({ ...prev, [messageId]: 'loading' }));
let objectUrl: string | null = null;
try {
const blob = await sessionApi.getMessageMediaBlob(sessionId, activeChat.id, messageId);
@@ -70,11 +74,15 @@ function ChatThread({
// downloads from the same thread apart.
link.download = message.metadata?.media?.filename || messageId;
link.click();
- setMediaFetch(null);
+ setMediaFetch(prev => {
+ const next = { ...prev };
+ delete next[messageId];
+ return next;
+ });
} catch {
// 404 covers every "there are no bytes" case the route documents (URL-based send, media
// download disabled, over the inbound cap). Say so on the bubble rather than failing mutely.
- setMediaFetch({ id: messageId, state: 'failed' });
+ setMediaFetch(prev => ({ ...prev, [messageId]: 'failed' }));
} finally {
if (objectUrl) URL.revokeObjectURL(objectUrl);
}
@@ -209,7 +217,7 @@ function ChatThread({
if (mediaInfo.omitted) {
// Not a plain label: the bytes exist behind the per-message media route, so this is the
// only handle the viewer has on them.
- const fetchState = mediaFetch && mediaFetch.id === msg.waMessageId ? mediaFetch.state : null;
+ const fetchState = msg.waMessageId ? mediaFetch[msg.waMessageId] : undefined;
return (
<button
type="button"
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 1f34822d5..68426d46e 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -84,10 +84,37 @@ const OMITTED_MEDIA_MESSAGE: ChatMessage = {
metadata: { media: { mimetype: 'image/jpeg', filename: 'photo.jpg', omitted: true, sizeBytes: 9_000_000 } },
};
+// A second one, so a test can have two downloads open at once and check they do not share state.
+const OMITTED_MEDIA_MESSAGE_2: ChatMessage = {
+ ...OMITTED_MEDIA_MESSAGE,
+ id: 'db-3',
+ waMessageId: 'wamid.3',
+ timestamp: 1_700_000_002,
+ createdAt: new Date(1_700_000_002_000).toISOString(),
+ metadata: { media: { mimetype: 'image/jpeg', filename: 'photo-2.jpg', omitted: true, sizeBytes: 9_000_000 } },
+};
+
/** The per-message media route the omitted marker sends the viewer to. */
-const MEDIA_PATH = `/api/sessions/${SESSION.id}/messages/${encodeURIComponent(CHAT.id)}/${encodeURIComponent(
- OMITTED_MEDIA_MESSAGE.waMessageId as string,
-)}/media`;
+const mediaPathFor = (waMessageId: string): string =>
+ `/api/sessions/${SESSION.id}/messages/${encodeURIComponent(CHAT.id)}/${encodeURIComponent(waMessageId)}/media`;
+const MEDIA_PATH = mediaPathFor(OMITTED_MEDIA_MESSAGE.waMessageId as string);
+const MEDIA_PATH_2 = mediaPathFor(OMITTED_MEDIA_MESSAGE_2.waMessageId as string);
+
+// A media response can be held open, so a test can have two downloads in flight and settle them out
+// of order — the shape in which one fetch's completion can clobber another's state.
+const mediaGates = new Map<string, Promise<void>>();
+
+/** Hold the media response for `path` until the returned release function is called. */
+function holdMedia(path: string): () => void {
+ let release!: () => void;
+ mediaGates.set(
+ path,
+ new Promise<void>(resolve => {
+ release = resolve;
+ }),
+ );
+ return release;
+}
// Contact for the status-compose recipient picker (Baileys requires an explicit allow-list).
const CONTACT = { id: '[email protected]', name: 'Bob', number: '15550002222' };
@@ -159,11 +186,15 @@ function installFetchStub(): void {
return Promise.resolve(jsonResponse([CONTACT]));
}
if (method === 'GET' && path.startsWith(`/api/sessions/${SESSION.id}/messages?`)) {
- return Promise.resolve(jsonResponse({ messages: [DB_MESSAGE, OMITTED_MEDIA_MESSAGE], total: 2 }));
+ return Promise.resolve(
+ jsonResponse({ messages: [DB_MESSAGE, OMITTED_MEDIA_MESSAGE, OMITTED_MEDIA_MESSAGE_2], total: 3 }),
+ );
}
// The media route answers bytes, not JSON — Content-Disposition: attachment.
- if (method === 'GET' && path === MEDIA_PATH) {
- return Promise.resolve(new Response(new Blob(['jpeg-bytes']), { status: 200 }));
+ if (method === 'GET' && (path === MEDIA_PATH || path === MEDIA_PATH_2)) {
+ const bytes = () => new Response(new Blob(['jpeg-bytes']), { status: 200 });
+ const gate = mediaGates.get(path);
+ return gate ? gate.then(bytes) : Promise.resolve(bytes());
}
if (method === 'GET' && /\/messages\/[^/]+\/history/.test(path)) {
return Promise.resolve(jsonResponse([]));
@@ -222,6 +253,8 @@ afterEach(() => {
rtl.cleanup();
queryClient?.clear();
queryClient = undefined;
+ // A gate left held would stall the next test's media fetch forever.
+ mediaGates.clear();
});
function renderChats(): { container: HTMLElement } {
@@ -423,7 +456,8 @@ test('an omitted media bubble fetches the bytes from the per-message media route
fireEvent.click(await screen.findByText('Alice'));
const thread = container.querySelector('.room-messages') as HTMLElement;
- const placeholder = (await within(thread).findByRole('button', { name: /Media/ })) as HTMLButtonElement;
+ // Two omitted rows render, so target the first by its message id rather than by role alone.
+ const placeholder = (await within(thread).findAllByRole('button', { name: /Media/ }))[0] as HTMLButtonElement;
assert.equal(countFetchCalls('GET', MEDIA_PATH), 0, 'the media route must not be hit until asked');
fireEvent.click(placeholder);
@@ -437,3 +471,58 @@ test('an omitted media bubble fetches the bytes from the per-message media route
assert.deepEqual(revokedUrls, createdUrls, 'every object URL created must be revoked');
});
});
+
+/**
+ * Two omitted bubbles can be downloading at once — nothing stops a viewer clicking one, then the
+ * next. Each must own its own lifecycle: with a single shared slot the second click overwrote the
+ * first, and then whichever settled first cleared the other's state, re-enabling a button whose
+ * download was still open and letting a later failure mark the wrong bubble.
+ */
+test('two media downloads in flight do not clobber each other', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ resetFetchCalls();
+
+ const createdUrls: string[] = [];
+ URL.createObjectURL = (): string => {
+ const url = `blob:mock-${createdUrls.length}`;
+ createdUrls.push(url);
+ return url;
+ };
+ URL.revokeObjectURL = (): void => undefined;
+
+ // Hold BOTH responses so the two fetches overlap, then settle them out of order.
+ const releaseA = holdMedia(MEDIA_PATH);
+ holdMedia(MEDIA_PATH_2);
+
+ const { container } = renderChats();
+ fireEvent.click(await screen.findByText('Alice'));
+ const thread = container.querySelector('.room-messages') as HTMLElement;
+ await within(thread).findAllByRole('button', { name: /Media/ });
+
+ // Addressed by message id, not by position: the thread's ordering is not this test's subject.
+ const buttonFor = (waMessageId: string): HTMLButtonElement =>
+ thread.querySelector(`[data-wa-message-id="${waMessageId}"] .message-media-omitted`) as HTMLButtonElement;
+ const a = buttonFor(OMITTED_MEDIA_MESSAGE.waMessageId as string);
+ const b = buttonFor(OMITTED_MEDIA_MESSAGE_2.waMessageId as string);
+
+ fireEvent.click(a);
+ fireEvent.click(b);
+ await waitFor(() => {
+ assert.equal(countFetchCalls('GET', MEDIA_PATH), 1, 'A should have been requested');
+ assert.equal(countFetchCalls('GET', MEDIA_PATH_2), 1, 'B should have been requested');
+ });
+
+ // Settle A while B is still open.
+ releaseA();
+ await waitFor(() => {
+ assert.equal(createdUrls.length, 1, "A's blob should have been handed to the download link");
+ });
+
+ // B is still fetching, so its button must still be disabled. With a shared slot A's completion
+ // cleared it here and B became clickable again mid-download.
+ assert.equal(
+ buttonFor(OMITTED_MEDIA_MESSAGE_2.waMessageId as string).disabled,
+ true,
+ "B's download was still open — A settling must not re-enable it",
+ );
+});