Sync WhatsApp group subjects into access UI (4.9.1-ops.10).

Refresh missing/forced group titles via groupMetadata and participating catalog so cards show real names instead of bare JIDs.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-05 17:39:25 +08:00
parent a169b164a8
commit f96157da1e
13 changed files with 568 additions and 260 deletions

View file

@ -14,6 +14,7 @@ import { h, localizeText } from './i18n.js';
export const ACCESS_GRANT_ENDPOINT = 'bot.access-grant.set';
export const ACCESS_PENDING_RESOLVE_ENDPOINT = 'bot.access-pending.resolve';
export const ACCESS_GROUP_TITLES_REFRESH_ENDPOINT = 'bot.access-group-titles.refresh';
export const GROUP_SESSION_SCOPE_ENDPOINT = 'bot.group-session-scope.set';
function unwrapRpcResult(result) {
@ -88,10 +89,7 @@ function NicknameHint({ contacts, phone }) {
function groupDisplayName(groupJid, groups) {
const title = groups?.[groupJid]?.title;
if (typeof title === 'string' && title.trim()) return title.trim();
const raw = String(groupJid ?? '');
const id = raw.replace(/@g\.us$/i, '');
if (id.length > 14) return `${id.slice(0, 10)}…@g.us`;
return raw || '未知群';
return '未命名群';
}
function digitsOnly(value) {
@ -300,7 +298,7 @@ export function AccessGrantSettingsPage({ channel, account, rpcCall, onSaved })
const bot = Array.isArray(value?.bots)
? value.bots.find((entry) => entry?.botId === account.botId)
: null;
const grant = normalizeAccessGrant(bot?.accessGrant);
let grant = normalizeAccessGrant(bot?.accessGrant);
if (grant) {
setDraft(cloneGrant(grant, ownerPhone));
onSaved?.(grant);
@ -308,6 +306,30 @@ export function AccessGrantSettingsPage({ channel, account, rpcCall, onSaved })
if (bot?.groupSessionScope) {
setGroupSessionScope(normalizeGroupSessionScope(bot.groupSessionScope));
}
const missingTitles = Object.entries(grant?.groups ?? {})
.filter(([, group]) => !(typeof group?.title === 'string' && group.title.trim()))
.map(([jid]) => jid);
const pendingGroupJids = (grant?.pending ?? [])
.map((entry) => entry.groupJid)
.filter((jid) => typeof jid === 'string' && jid.endsWith('@g.us'));
const toSync = [...new Set([...missingTitles, ...pendingGroupJids])];
if (toSync.length > 0) {
try {
const refreshed = unwrapRpcResult(await rpcCall(ACCESS_GROUP_TITLES_REFRESH_ENDPOINT, {
botId: account.botId,
groupJids: toSync,
}));
if (cancelled) return;
const next = grantFromSnapshot(refreshed, account.botId);
if (next) {
setDraft(cloneGrant(next, ownerPhone));
onSaved?.(next);
}
} catch {
// Title sync is best-effort while the bot is offline.
}
}
} catch (error) {
if (!cancelled) {
setFeedback({
@ -485,20 +507,54 @@ export function AccessGrantSettingsPage({ channel, account, rpcCall, onSaved })
h('div', { className: 'dim-accessGroupHeading' },
h('h3', null, groupDisplayName(groupJid, draft.groups)),
h('code', { className: 'dim-accessGroupJid' }, groupJid)),
h('label', { className: 'dim-accessField' },
h('span', null, '群名称'),
h('input', {
value: group.title ?? '',
maxLength: 128,
placeholder: '填写便于识别的群名称',
onChange: (event) => setDraft((current) => ({
...current,
groups: {
...current.groups,
[groupJid]: { ...group, title: event.target.value },
},
h('div', { className: 'dim-accessGroupTitleRow' },
h('label', { className: 'dim-accessField' },
h('span', null, '群名称'),
h('input', {
value: group.title ?? '',
maxLength: 128,
placeholder: '填写便于识别的群名称',
onChange: (event) => setDraft((current) => ({
...current,
groups: {
...current.groups,
[groupJid]: { ...group, title: event.target.value },
},
})),
})),
})),
h('button', {
type: 'button',
className: 'dim-deliveryButton',
disabled: saving,
onClick: () => void (async () => {
setSaving(true);
setFeedback(null);
try {
const refreshed = unwrapRpcResult(await rpcCall(
ACCESS_GROUP_TITLES_REFRESH_ENDPOINT,
{ botId: account.botId, groupJids: [groupJid] },
));
const next = grantFromSnapshot(refreshed, account.botId);
if (!next) throw new Error('服务没有返回已保存的访问授权。');
setDraft(cloneGrant(next, ownerPhone));
onSaved?.(next);
const title = next.groups?.[groupJid]?.title;
setFeedback({
tone: title ? 'success' : 'error',
message: title
? ['已同步群名:', title].join('')
: '未能从 WhatsApp 读取群名,请确认机器人在线且仍在该群。',
});
} catch (error) {
setFeedback({
tone: 'error',
message: error?.message || '同步群名失败,请稍后重试。',
});
} finally {
setSaving(false);
}
}),
}, '同步群名')),
h(AdminPhones, {
title: '本群管理员',
help: '未配置时,本群申请回落给全局管理员审批(仍只授予本群权)。',

View file

@ -17,6 +17,7 @@ export const WHATSAPP_ENDPOINTS = Object.freeze({
setAccessPolicy: 'bot.access-policy.set',
setAccessGrant: 'bot.access-grant.set',
resolveAccessPending: 'bot.access-pending.resolve',
refreshAccessGroupTitles: 'bot.access-group-titles.refresh',
setGroupSessionScope: 'bot.group-session-scope.set',
setWorkspace: 'bot.workspace.set',
setAgentPreset: SET_AGENT_PRESET_ENDPOINT,

View file

@ -11,6 +11,12 @@ const EN = Object.freeze({
'机器人设置页签': 'Bot settings tabs',
'投递设置': 'Delivery settings',
'访问设置': 'Access settings',
"请提交有效的群名同步请求。": "Submit a valid group-name sync request.",
"同步群名失败,请稍后重试。": "Could not sync the group name. Try again later.",
"未能从 WhatsApp 读取群名,请确认机器人在线且仍在该群。": "Could not read the group name from WhatsApp. Confirm the bot is online and still in the group.",
"已同步群名:": "Synced group name: ",
"同步群名": "Sync group name",
"未命名群": "Unnamed group",
"暂无昵称(私聊/@ 后会自动补齐)": "No nickname yet (filled after DM or @mention)",
"群聊 · ": "Group · ",

View file

@ -483,6 +483,7 @@ const CSS = String.raw`
.dim-accessGroupHeading { min-width: 0; display: grid; gap: 2px; }
.dim-accessGroupHeading h3 { margin: 0; color: var(--dsw-alias-label-primary, #1f2329); font-size: 14px; line-height: 21px; font-weight: 650; }
.dim-accessGroupJid { color: var(--dsw-alias-label-tertiary, #8f959e); font-size: 11px; line-height: 16px; overflow-wrap: anywhere; }
.dim-accessGroupTitleRow { min-width: 0; display: grid; grid-template-columns: minmax(0, 1fr) max-content; gap: 10px; align-items: end; }
.dim-accessNickname { display: block; margin-top: 4px; color: var(--dsw-alias-label-secondary, #646a73); font-size: 12px; line-height: 18px; font-weight: 560; }
.dim-accessNicknameEmpty { color: var(--dsw-alias-label-tertiary, #8f959e); font-weight: 400; }
.dim-accessUserRow { position: relative; z-index: 0; overflow: visible; }

View file

@ -2,6 +2,7 @@ import { validateAccessGrant, normalizeAccessGrant } from '../../../../src/chann
export const SET_ACCESS_GRANT_ENDPOINT = 'bot.access-grant.set';
export const RESOLVE_ACCESS_PENDING_ENDPOINT = 'bot.access-pending.resolve';
export const REFRESH_ACCESS_GROUP_TITLES_ENDPOINT = 'bot.access-group-titles.refresh';
export function validAccessGrantPayload(payload) {
try {
@ -41,6 +42,21 @@ export function validAccessPendingResolvePayload(payload) {
}
}
export function validAccessGroupTitlesRefreshPayload(payload) {
try {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return false;
if (!Object.hasOwn(payload, 'botId') || typeof payload.botId !== 'string'
|| !/^[A-Za-z0-9_-]{1,128}$/.test(payload.botId)) return false;
const keys = Reflect.ownKeys(payload);
if (keys.length === 1) return true;
if (keys.length !== 2 || !Object.hasOwn(payload, 'groupJids')) return false;
if (!Array.isArray(payload.groupJids)) return false;
return payload.groupJids.every((jid) => typeof jid === 'string' && /^\d{5,32}@g\.us$/.test(jid));
} catch {
return false;
}
}
export function publicAccessGrant(grant) {
const normalized = normalizeAccessGrant(grant);
if (!normalized) return null;

View file

@ -3,9 +3,11 @@ import QRCode from 'qrcode';
import { publicConnectionTestResult } from '../../../../src/channels/shared/connection-test.mjs';
import { SET_ACCESS_POLICY_ENDPOINT, validAccessPolicyPayload } from '../shared/access-policy-rpc.mjs';
import {
REFRESH_ACCESS_GROUP_TITLES_ENDPOINT,
RESOLVE_ACCESS_PENDING_ENDPOINT,
SET_ACCESS_GRANT_ENDPOINT,
validAccessGrantPayload,
validAccessGroupTitlesRefreshPayload,
validAccessPendingResolvePayload,
} from '../shared/access-grant-rpc.mjs';
import { SET_GROUP_SESSION_SCOPE_ENDPOINT, validGroupSessionScopePayload } from '../shared/group-session-scope-rpc.mjs';
@ -25,6 +27,7 @@ export const WHATSAPP_ENDPOINTS = Object.freeze({
setAccessPolicy: SET_ACCESS_POLICY_ENDPOINT,
setAccessGrant: SET_ACCESS_GRANT_ENDPOINT,
resolveAccessPending: RESOLVE_ACCESS_PENDING_ENDPOINT,
refreshAccessGroupTitles: REFRESH_ACCESS_GROUP_TITLES_ENDPOINT,
setGroupSessionScope: SET_GROUP_SESSION_SCOPE_ENDPOINT,
setWorkspace: SET_WORKSPACE_ENDPOINT,
setAgentPreset: SET_AGENT_PRESET_ENDPOINT,
@ -76,6 +79,10 @@ function payloadFailure(endpoint, payload) {
return validAccessPendingResolvePayload(payload)
? null : '请提交有效的审批请求。';
}
if (endpoint === WHATSAPP_ENDPOINTS.refreshAccessGroupTitles) {
return validAccessGroupTitlesRefreshPayload(payload)
? null : '请提交有效的群名同步请求。';
}
if (endpoint === WHATSAPP_ENDPOINTS.setGroupSessionScope) {
return validGroupSessionScopePayload(payload)
? null : '请提交有效的群会话策略。';
@ -230,6 +237,14 @@ export function createWhatsappRpcHandler(controller, { encodeQr = qrDataUrl } =
},
(status) => publicStatus(status, cachedEncode),
);
} else if (endpoint === WHATSAPP_ENDPOINTS.refreshAccessGroupTitles) {
if (typeof controller.refreshAccessGroupTitles !== 'function') {
throw new Error('Group title refresh is unavailable');
}
value = await publicStatus(
await controller.refreshAccessGroupTitles(payload.botId, payload.groupJids),
cachedEncode,
);
} else if (endpoint === WHATSAPP_ENDPOINTS.setGroupSessionScope) {
if (typeof controller.updateGroupSessionScope !== 'function') throw new Error('Group session scope update is unavailable');
value = await controller.updateGroupSessionScope(