Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): report group members the gateway refuses

Yudhi Armyndharisb04af91176ac
diff --git a/dashboard/src/components/chats/GroupInfoDialog.tsx b/dashboard/src/components/chats/GroupInfoDialog.tsx
index ea7769d4d..e39151c00 100644
--- a/dashboard/src/components/chats/GroupInfoDialog.tsx
+++ b/dashboard/src/components/chats/GroupInfoDialog.tsx
@@ -3,7 +3,15 @@ import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { Crown, Info, Loader2, RefreshCw, ShieldCheck, ShieldOff, UserMinus, UserPlus, Users } from 'lucide-react';
import { Modal } from '../Modal';
-import { contactApi, sessionApi, type Chat, type GroupInfo, type Session } from '../../services/api';
+import {
+ contactApi,
+ sessionApi,
+ type Chat,
+ type GroupInfo,
+ type GroupParticipantResult,
+ type GroupParticipantsAck,
+ type Session,
+} from '../../services/api';
import { useProfilePicture } from '../../hooks/useProfilePicture';
import { useRole } from '../../hooks/useRole';
import { useToast } from '../../hooks/useToast';
@@ -27,6 +35,13 @@ interface GroupInfoDialogProps {
const errorText = (err: unknown) => (err instanceof Error ? err.message : undefined);
+/** The participant a membership write refused, with the reason the engine gave. */
+const refusalText = (r: GroupParticipantResult) => {
+ const who = r.id.replace(/@.*/, '');
+ const why = r.message ?? (r.status ? `HTTP ${r.status}` : undefined);
+ return why ? `${who}: ${why}` : who;
+};
+
/** Whether a participant is this session's own account. */
const isOwnAccount = (participant: GroupInfo['participants'][number], session: Session) => {
const own = session.phone?.replace(/\D/g, '');
@@ -124,28 +139,37 @@ function GroupInfoDialog({ session, group, onClose, onRenamed }: GroupInfoDialog
// --- Participants ---
const [pending, setPending] = useState<string | null>(null);
const [newMember, setNewMember] = useState('');
- const runMemberAction = async (key: string, action: () => Promise<unknown>) => {
+ // The gateway answers 200 for a membership write even when it refused some participants, and says
+ // which in `results`. Returns whether every participant was accepted.
+ const runMemberAction = async (key: string, action: () => Promise<GroupParticipantsAck>): Promise<boolean> => {
setPending(key);
try {
- await action();
+ const { results } = await action();
+ const refused = results.filter(r => !r.success);
+ if (refused.length > 0) {
+ showError(t('chats.group.memberActionFailed'), refused.map(refusalText).join('; '));
+ }
await infoQuery.refetch();
+ return refused.length === 0;
} catch (err) {
showError(t('chats.group.memberActionFailed'), errorText(err));
+ return false;
} finally {
setPending(null);
}
};
// A number is checked first, as in the new-chat panel: the gateway resolves it to the id WhatsApp uses.
- const addMember = () =>
- runMemberAction('add', async () => {
+ const addMember = async () => {
+ const added = await runMemberAction('add', async () => {
const digits = newMember.replace(/\D/g, '').replace(/^00/, '');
const check = await contactApi.checkNumber(session.id, digits);
if (!check.exists || !check.whatsappId) {
throw new Error(t('chats.newChat.notOnWhatsApp', { number: formatPhoneForDisplay(digits) ?? `+${digits}` }));
}
- await sessionApi.addGroupParticipants(session.id, group.id, [check.whatsappId]);
- setNewMember('');
+ return sessionApi.addGroupParticipants(session.id, group.id, [check.whatsappId]);
});
+ if (added) setNewMember('');
+ };
const sessionName = session.name;
const subheader = (
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 6948b2214..a7350f9eb 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -66,6 +66,8 @@ let listGroup = false;
let blockedIds: string[] = [];
// Whether the session's own number administers GROUP.
let groupAdmin = true;
+// When true, a membership write answers HTTP 200 and reports every participant as refused.
+let refuseMember = false;
const CONTACT = { id: '[email protected]', name: 'Bob', number: '15550002222', isMyContact: true, isBlocked: false };
const ALICE_CONTACT = { id: CHAT.id, name: 'Alice', number: '15550001111', isMyContact: true, isBlocked: false };
@@ -152,6 +154,17 @@ function installFetchStub(): void {
) {
return Promise.resolve(jsonResponse({ success: !declineWrites }));
}
+ if (path.startsWith(`${groupPath}/participants`)) {
+ const { participants } = body as { participants: string[] };
+ const refusal = refuseMember ? { status: 403, message: 'not-authorized' } : {};
+ return Promise.resolve(
+ jsonResponse({
+ success: true,
+ message: 'ok',
+ results: participants.map(id => ({ id, success: !refuseMember, ...refusal })),
+ }),
+ );
+ }
if (path.startsWith(`${groupPath}/`)) {
return Promise.resolve(jsonResponse({ success: true, message: 'ok' }));
}
@@ -218,6 +231,7 @@ afterEach(async () => {
gates.clear();
listGroup = false;
groupAdmin = true;
+ refuseMember = false;
blockedIds = [];
window.sessionStorage.setItem('openwa_user_role', 'admin');
window.sessionStorage.setItem('openwa_engine_type', 'baileys');
@@ -383,6 +397,32 @@ test('the group dialog saves a new name through the subject route and renames th
assert.ok(within(dialog).getByRole('button', { name: 'Remove: Bob' }), 'an admin was offered no member actions');
});
+test('a participant the gateway refuses is reported although the write answered 200', async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ listGroup = true;
+ renderPage();
+ await screen.findByText('Team');
+ fireEvent.click(screen.getByRole('button', { name: 'Actions for Team' }));
+ fireEvent.click(screen.getByRole('button', { name: 'Group info' }));
+ const dialog = await screen.findByRole('dialog');
+ await within(dialog).findByText(`Managed via ${SESSION.name}`);
+ fireEvent.click(within(dialog).getByRole('tab', { name: 'Participants' }));
+
+ const promotes = () => fetchCalls.filter(c => c.path.endsWith('/participants/promote')).length;
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Make admin: Bob' }));
+ await waitFor(() => assert.equal(promotes(), 1));
+ await waitFor(() =>
+ assert.ok(!(within(dialog).getByRole('button', { name: 'Make admin: Bob' }) as HTMLButtonElement).disabled),
+ );
+ assert.ok(!screen.queryByText('The action failed'), 'an accepted write was reported as failed');
+
+ refuseMember = true;
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Make admin: Bob' }));
+ await screen.findByText('The action failed');
+ await screen.findByText('15550002222: not-authorized');
+ assert.equal(promotes(), 2);
+});
+
test('a member who is no admin sees the group read-only, and a viewer key gets no write actions', async () => {
const { screen, fireEvent, within } = rtl;
listGroup = true;
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index d887f012d..e644d6070 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -458,6 +458,18 @@ export interface GroupAck {
message: string;
}
+/** One participant's outcome of a membership write; the gateway answers 200 even when some are refused. */
+export interface GroupParticipantResult {
+ id: string;
+ success: boolean;
+ status?: number;
+ message?: string;
+}
+
+export interface GroupParticipantsAck extends GroupAck {
+ results: GroupParticipantResult[];
+}
+
export interface Contact {
id: string;
name?: string;
@@ -923,17 +935,17 @@ export const sessionApi = {
body: JSON.stringify({ base64, mimetype }),
}),
addGroupParticipants: (id: string, groupId: string, participants: string[]) =>
- request<GroupAck>(`/sessions/${id}/groups/${encodeURIComponent(groupId)}/participants`, {
+ request<GroupParticipantsAck>(`/sessions/${id}/groups/${encodeURIComponent(groupId)}/participants`, {
method: 'POST',
body: JSON.stringify({ participants }),
}),
removeGroupParticipants: (id: string, groupId: string, participants: string[]) =>
- request<GroupAck>(`/sessions/${id}/groups/${encodeURIComponent(groupId)}/participants`, {
+ request<GroupParticipantsAck>(`/sessions/${id}/groups/${encodeURIComponent(groupId)}/participants`, {
method: 'DELETE',
body: JSON.stringify({ participants }),
}),
setGroupAdmins: (id: string, groupId: string, participants: string[], admin: boolean) =>
- request<GroupAck>(
+ request<GroupParticipantsAck>(
`/sessions/${id}/groups/${encodeURIComponent(groupId)}/participants/${admin ? 'promote' : 'demote'}`,
{ method: 'POST', body: JSON.stringify({ participants }) },
),