Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): do not hold a null picture for an hour

Yudhi Armyndharis3f2010c84646
diff --git a/dashboard/src/hooks/useProfilePictures.test.ts b/dashboard/src/hooks/useProfilePictures.test.ts
index 6fb56c2e8..7d57c0176 100644
--- a/dashboard/src/hooks/useProfilePictures.test.ts
+++ b/dashboard/src/hooks/useProfilePictures.test.ts
@@ -367,6 +367,51 @@ test('signing out drops the held pictures, so the next actor is shown none of th
await rtl.waitFor(() => assert.equal(Object.keys(second.result.current.data ?? {}).length, 2));
});
+test('an id that came back null is asked for again after a minute, the pictures held are not', async t => {
+ let clock = 1_800_000_000_000;
+ t.mock.method(Date, 'now', () => clock);
+ // The batch route answers null for a hidden picture and for a lookup that failed, and the two look alike.
+ answer = (sessionId, ids) =>
+ Promise.resolve(
+ json({ pictures: Object.fromEntries(ids.map(id => [id, id === 'b' ? null : urlFor(sessionId, id)])) }),
+ );
+ const { result, rerender } = renderPictures('s1', ['a', 'b']);
+ await rtl.waitFor(() => assert.ok('b' in (result.current.data ?? {})));
+
+ // Within the minute the null is held, as before.
+ clock += 30_000;
+ rerender({ sessionId: 's1', ids: ['a', 'b', 'c'] });
+ await rtl.waitFor(() => assert.equal(result.current.data?.c, urlFor('s1', 'c')));
+ assert.deepEqual(asked(), [['a', 'b'], ['c']]);
+
+ // Past it, the next change of ids asks for b again, now that it can be answered.
+ answer = everyUrl;
+ clock += 40_000;
+ rerender({ sessionId: 's1', ids: ['a', 'b', 'c', 'd'] });
+ await rtl.waitFor(() => assert.equal(result.current.data?.b, urlFor('s1', 'b')));
+ assert.deepEqual(asked().at(-1), ['b', 'd']);
+});
+
+test('an aged null does not make the batch stale, so coming back to the window asks for nothing', async t => {
+ let clock = 1_800_000_000_000;
+ t.mock.method(Date, 'now', () => clock);
+ answer = (sessionId, ids) =>
+ Promise.resolve(
+ json({ pictures: Object.fromEntries(ids.map(id => [id, id === 'b' ? null : urlFor(sessionId, id)])) }),
+ );
+ const { result } = renderPictures('s1', ['a', 'b']);
+ await rtl.waitFor(() => assert.ok('b' in (result.current.data ?? {})));
+
+ clock += 10 * 60_000;
+ rtl.act(() => {
+ focusManager.setFocused(false);
+ focusManager.setFocused(true);
+ });
+ await new Promise(resolve => setTimeout(resolve, 50));
+ assert.equal(requests.length, 1, 'a null that was a minute old made the whole batch refetch on focus');
+ assert.equal(result.current.isStale, false);
+});
+
test('asks for nothing without a session or without ids', async () => {
const none = renderPictures(undefined, ['a']);
const empty = renderPictures('s1', []);
diff --git a/dashboard/src/hooks/useProfilePictures.ts b/dashboard/src/hooks/useProfilePictures.ts
index 92f107340..f6e24c35d 100644
--- a/dashboard/src/hooks/useProfilePictures.ts
+++ b/dashboard/src/hooks/useProfilePictures.ts
@@ -3,6 +3,10 @@ import { contactApi } from '../services/api';
// Signed CDN URLs rotate: a held picture is reused for an hour, then asked for again.
const PICTURE_TTL_MS = 60 * 60 * 1000;
+// The batch route answers null for a hidden or missing picture and also for a lookup that failed (throttled, past
+// its per-id deadline), and the two look alike. A null is held for a minute only: a failure heals on the next
+// change of ids after that, and a real null costs one more lookup then.
+const NULL_TTL_MS = 60 * 1000;
interface ResolvedPicture {
url: string | null;
@@ -37,8 +41,8 @@ function heldFor(client: QueryClient, sessionId: string): Held {
* query key uses the sorted id set so reordering the sidebar doesn't refetch.
*
* A change of membership (a message from a chat outside the top 50 pushes one out) is a new key, but
- * only the ids not held yet are asked for: every answer is kept per session with the time it arrived,
- * ids that came back null (hidden or no picture) included, and reused for an hour. What is held lives
+ * only the ids not held yet are asked for: every answer is kept per session with the time it arrived and
+ * reused for an hour, a null (hidden, no picture, or a lookup that failed) for a minute. What is held lives
* in the query cache, so a page that mounts again with the same ids asks for nothing. The data returned
* covers the requested ids, and the previous data of the same session stays while a smaller batch is
* out, so the rows keep their pictures instead of dropping to the generic icon. An invalidated batch (a
@@ -67,7 +71,7 @@ export function useProfilePictures(sessionId: string | undefined, contactIds: st
const invalidated = client.getQueryState(queryKey)?.isInvalidated ?? false;
const missing = requestIds.filter(id => {
const entry = held.get(id);
- return invalidated || !entry || now - entry.at >= PICTURE_TTL_MS;
+ return invalidated || !entry || now - entry.at >= (entry.url === null ? NULL_TTL_MS : PICTURE_TTL_MS);
});
if (missing.length > 0) {
try {
@@ -87,7 +91,8 @@ export function useProfilePictures(sessionId: string | undefined, contactIds: st
enabled: Boolean(sessionId && requestIds.length > 0),
// Fresh until the oldest requested picture is an hour old. A missing one (an id the answer left out, a
// failed top-up) or an expired one makes the query stale at once, so the next focus or mount asks again
- // for just those, instead of a failed attempt counting as a fresh hour.
+ // for just those, instead of a failed attempt counting as a fresh hour. A null's shorter clock is for the
+ // next change of ids only: counted here, it would send every focus back for all the pictures nobody has.
staleTime: query => {
const held = sessionId ? queryClient.getQueryData<Held>(heldKey(sessionId)) : undefined;
let expiresAt = Infinity;