Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): pace the privacy-id lookups of a typed number
Yudhi Armyndharis36ee2fdddd6a
diff --git a/dashboard/src/hooks/useResolvedPhone.ts b/dashboard/src/hooks/useResolvedPhone.tsindex 11207f6fc..bcd649863 100644--- a/dashboard/src/hooks/useResolvedPhone.ts+++ b/dashboard/src/hooks/useResolvedPhone.ts@@ -1,4 +1,4 @@-import { useQuery, type Query } from '@tanstack/react-query';+import { useQuery, type Query, type QueryClient } from '@tanstack/react-query';import { contactApi } from '../services/api';const DAY_MS = 24 * 60 * 60 * 1000;@@ -19,7 +19,6 @@ export function useResolvedPhone(sessionId: string | undefined, contactId: strinreturn useQuery<string | null, Error>({...resolvedPhoneQuery(sessionId!, contactId!),enabled: Boolean(sessionId && contactId),- gcTime: 60 * 60 * 1000,retry: false,});}@@ -31,5 +30,15 @@ export function resolvedPhoneQuery(sessionId: string, contactId: string) {queryFn: () => contactApi.resolvePhone(sessionId, contactId).then(r => r.phone),// lid→phone mappings don't churn, so a phone is held for the day; a null is not (see NULL_STALE_MS).staleTime: (query: Query<string | null, Error>) => (query.state.data === null ? NULL_STALE_MS : DAY_MS),+ // Without an observer a query is collected after five minutes, which would drop the answers of an id that is+ // only ever asked about through fetchQuery.+ gcTime: DAY_MS,};}++/** True when the page holds a current answer for the id, so asking again costs the gateway nothing. */+export function isResolvedPhoneHeld(queryClient: QueryClient, sessionId: string, contactId: string): boolean {+ const { queryKey, staleTime } = resolvedPhoneQuery(sessionId, contactId);+ const query = queryClient.getQueryCache().find<string | null, Error>({ queryKey });+ return query?.state.status === 'success' && !query.isStaleByTime(staleTime(query));+}diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex 999685637..94a562781 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -114,6 +114,10 @@ let failLidPhones = false;const failingLidIds = new Set<string>();// When true, it answers a phone that is no string, which nothing in the page expects.let garbleLidPhones = false;+// When true, the lookups answer 429 once more than ten arrive within a second, as the gateway's short throttle tier+// does for one route and client; `phoneLookupTimes` is when each arrived.+let throttlePhoneLookups = false;+const phoneLookupTimes: number[] = [];// When true, the number check answers that the number is not on WhatsApp.let numberNotRegistered = false;// How many more privacy-id chats the list holds, none of which resolves to a phone.@@ -290,6 +294,12 @@ function installFetchStub(): void {}if (method === 'GET' && /\/contacts\/[^/]+\/phone$/.test(path)) {const id = decodeURIComponent(path.split('/contacts/')[1].split('/phone')[0]);+ if (throttlePhoneLookups) {+ const now = Date.now();+ while (phoneLookupTimes.length > 0 && now - phoneLookupTimes[0] >= 1000) phoneLookupTimes.shift();+ phoneLookupTimes.push(now);+ if (phoneLookupTimes.length > 10) return Promise.resolve(jsonResponse({ message: 'Too Many Requests' }, 429));+ }if (failLidPhones || failingLidIds.has(id)) return Promise.resolve(jsonResponse({ message: 'busy' }, 503));if (garbleLidPhones) return Promise.resolve(jsonResponse({ contactId: id, phone: 15550009999 }));return Promise.resolve(jsonResponse({ contactId: id, phone: lidPhones[id] ?? null }));@@ -367,6 +377,8 @@ afterEach(async () => {failingLidIds.clear();garbleLidPhones = false;numberNotRegistered = false;+ throttlePhoneLookups = false;+ phoneLookupTimes.length = 0;extraLidChats = 0;for (const id of Object.keys(lidPhones)) delete lidPhones[id];for (const key of Object.keys(aliceNow)) delete aliceNow[key as keyof Chat];@@ -629,9 +641,45 @@ test('only the newest privacy-id chats are asked about, not every one the list hawait screen.findByText('Lid 0');await startChatWithTypedNumber();- await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the chat did not open'));- assert.ok(phoneRequests().length > 0, 'no privacy id was asked about');- assert.ok(phoneRequests().length < extraLidChats, `all ${extraLidChats} privacy ids were asked about`);+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the chat did not open'), { timeout: 8000 });+ assert.equal(phoneRequests().length, 30, 'the newest 30 privacy ids, no more and no fewer, were asked about');+});++test('privacy ids are asked about at a pace the gateway accepts, and the search ends at the match', async () => {+ const { screen, waitFor } = rtl;+ extraLidChats = 40;+ throttlePhoneLookups = true;+ // The thirteenth newest holds the number: it is asked about in the third group of five, after ten requests.+ lidPhones[extraLidChat(12).id] = '15550009999';+ const { container } = renderPage();+ await screen.findByText('Lid 0');+ const before = listedChatCards(container);+ await startChatWithTypedNumber();++ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the chat did not open'), { timeout: 8000 });+ assert.equal(container.querySelector('.room-header .room-contact-info h3')?.textContent, 'Lid 12');+ assert.equal(listedChatCards(container), before, 'a second chat was added beside the one the list holds');+ assert.equal(phoneRequests().length, 15, 'ids after the match were asked about, or a group was asked twice');+});++test('privacy ids the page already holds answers for are kept for a day and neither asked again nor waited for', async () => {+ const { screen, waitFor } = rtl;+ extraLidChats = 10;+ const { container } = renderPage();+ await screen.findByText('Lid 0');+ const { resolvedPhoneQuery } = await import('../hooks/useResolvedPhone.ts');+ const client = queryClients[0];+ for (let n = 0; n < extraLidChats; n++) await client.fetchQuery(resolvedPhoneQuery(SESSION.id, extraLidChat(n).id));+ const held = client.getQueryCache().find({ queryKey: ['resolvedPhone', SESSION.id, extraLidChat(0).id] });+ assert.equal(held?.gcTime, 24 * 60 * 60 * 1000, 'an answer nothing observes would be collected after minutes');+ fetchCalls.length = 0;+ await startChatWithTypedNumber();++ // Two groups of five that reached the gateway would be a second apart.+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the chat did not open in time'), {+ timeout: 800,+ });+ assert.equal(phoneRequests().length, 0, 'an answer the page holds was asked for again');});for (const reactStrictMode of [false, true]) {diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex acae361c8..6808dbfaf 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -26,7 +26,7 @@ import {} from 'lucide-react';import { useProfilePicture } from '../hooks/useProfilePicture';import { useProfilePictures } from '../hooks/useProfilePictures';-import { useResolvedPhone, resolvedPhoneQuery } from '../hooks/useResolvedPhone';+import { useResolvedPhone, resolvedPhoneQuery, isResolvedPhoneHeld } from '../hooks/useResolvedPhone';import { formatPhoneForDisplay } from '../utils/formatPhone';import {sessionApi,@@ -97,12 +97,15 @@ const MARK_READ_DEBOUNCE_MS = 750;const MARK_READ_MESSAGE_IDS_MAX = 100;const readKey = (sessionId: string, chatId: string) => `${sessionId}\u0000${chatId}`;-// How many privacy-id chats a typed number is looked for among, and how many are asked about at once. The engine-// answers one id per request, so searching the whole list would cost a request for every chat in it. Only the-// newest chats are searched: an older conversation opens as a new chat, as it does for a number the list has no-// row for. Raise the cap, or have the gateway name the privacy id of a phone, if that proves too few.+// How many privacy-id chats a typed number is looked for among, how many are asked about at once, and the least+// time between two such groups. The engine answers one id per request, so searching the whole list would cost a+// request for every chat in it. Only the newest chats are searched: an older conversation opens as a new chat, as+// it does for a number the list has no row for. Raise the cap, or have the gateway name the privacy id of a phone,+// if that proves too few. The gateway lets one client ask this route ten times a second, and a request it refuses+// reads as no phone, so a group of five follows the one before it by a second: half the rate it allows.const LID_CHATS_SEARCHED = 30;const LID_LOOKUPS_AT_ONCE = 5;+const LID_LOOKUP_GAP_MS = 1000;// Longest one background chat-list walk may take before it is abandoned. A walk is a dozen small requests, and// the gateway answers a stalled engine at 180 s, so this is far above a healthy walk and below that.@@ -1271,8 +1274,16 @@ export function Chats() {const privacyChats = chatsRef.current.filter(c => c.kind === 'individual' && c.id.endsWith('@lid')).slice(0, LID_CHATS_SEARCHED);+ let lastAskedAt = 0;for (let i = 0; i < privacyChats.length; i += LID_LOOKUPS_AT_ONCE) {const batch = privacyChats.slice(i, i + LID_LOOKUPS_AT_ONCE);+ // A group the page already holds answers for goes ahead at once; only one that reaches the gateway waits.+ if (batch.some(c => !isResolvedPhoneHeld(queryClient, sessionId, c.id))) {+ const wait = lastAskedAt + LID_LOOKUP_GAP_MS - Date.now();+ if (wait > 0) await new Promise(resolve => setTimeout(resolve, wait));+ if (selectedSessionRef.current !== sessionId) return null;+ lastAskedAt = Date.now();+ }const phones = await Promise.all(batch.map(c => queryClient.fetchQuery(resolvedPhoneQuery(sessionId, c.id)).catch(() => null)),);