Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): load every contact into the status recipient picker

Yudhi Armyndharis4d8df4be40f5
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index d4d13ca4e..0e6fbce72 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -312,7 +312,7 @@ function installFetchStub(): void {
if (method === 'GET' && /\/contacts\/[^/]+\/profile-picture$/.test(path)) {
return Promise.resolve(jsonResponse({ url: null }));
}
- if (method === 'GET' && path === `/api/sessions/${SESSION.id}/contacts`) {
+ if (method === 'GET' && path.startsWith(`/api/sessions/${SESSION.id}/contacts?`)) {
return Promise.resolve(jsonResponse([CONTACT]));
}
if (method === 'GET' && path.startsWith(`/api/sessions/${SESSION.id}/messages?`)) {
diff --git a/dashboard/src/services/api.test.ts b/dashboard/src/services/api.test.ts
index 99bd401b7..93f0c46b2 100644
--- a/dashboard/src/services/api.test.ts
+++ b/dashboard/src/services/api.test.ts
@@ -68,3 +68,24 @@ test('a role 403 keeps the key and rejects with the status', async () => {
assert.equal(sessionStorage.getItem('openwa_api_key'), 'stored-key');
assert.deepEqual(navigations, []);
});
+
+test('the contact list walks past the 1000 contacts one response carries', async () => {
+ const { contactApi } = await import('./api.ts');
+ const requested: string[] = [];
+ globalThis.fetch = ((input: RequestInfo | URL) => {
+ const url = String(input);
+ requested.push(url.replace(/^.*\/api/, ''));
+ const count = url.includes('offset=0') ? 1000 : 5;
+ const contacts = Array.from({ length: count }, (_, i) => ({ id: `${i}@c.us`, name: null, number: `${i}` }));
+ return Promise.resolve(
+ new Response(JSON.stringify(contacts), { status: 200, headers: { 'Content-Type': 'application/json' } }),
+ );
+ }) as typeof fetch;
+
+ const contacts = await contactApi.list('s1');
+ assert.equal(contacts.length, 1005);
+ assert.deepEqual(requested, [
+ '/sessions/s1/contacts?limit=1000&offset=0',
+ '/sessions/s1/contacts?limit=1000&offset=1000',
+ ]);
+});
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index 94793a780..29fcc551c 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -3,6 +3,7 @@
import { warnIfInsecureHttpUrl } from '../utils/urlSecurity';
import { isKeyUnusable } from '../utils/authLifecycle';
+import { fetchAllPages } from '../utils/fetchAllPages';
// Resolve the API base URL. By default this is the same-origin relative path '/api',
// correct when the dashboard and API are served from the same origin (the default
@@ -968,7 +969,18 @@ export interface ProfilePictureResponse {
}
export const contactApi = {
- list: (sessionId: string) => request<Contact[]>(`/sessions/${sessionId}/contacts`),
+ // The route caps a response at 1000 contacts; walk the pages so an address book past that is complete.
+ list: async (sessionId: string) =>
+ (
+ await fetchAllPages(
+ async (limit, offset) => {
+ const data = await request<Contact[]>(`/sessions/${sessionId}/contacts?limit=${limit}&offset=${offset}`);
+ // The route answers a bare array with no total: a short page is the last one.
+ return { data, total: data.length < limit ? offset + data.length : Infinity };
+ },
+ { pageSize: 1000 },
+ )
+ ).items,
checkNumber: (sessionId: string, number: string) =>
request<CheckNumberResponse>(`/sessions/${sessionId}/contacts/check/${encodeURIComponent(number)}`),
// Returns the contact/group profile picture URL. Both engines return null when the user hid their