Files
SnowLuma/packages/webui/src/lib/onebot-config.ts
T
DrSmoothl 0b7fb0c69d feat(notifications): P4 WebUI — channel manager, per-account opt-in, deliveries widget
- API client: notifications surface (getConfig/saveConfig/recent/test) + types.
- Settings → 通知 tab: global channel CRUD (id/name/url/body-template/enabled) +
  global debounce window, per-channel test-send, dirty-tracked save; invalid
  channels (bad slug / non-http URL) are dropped server-side and surfaced.
- Per-account 通用设置: opt-in toggles bound to notifications.channelIds, riding
  the ConfigPage's existing debounced autosave; stale opt-ins preserved.
- Overview: opt-in 最近投递 widget (polls /api/notifications/recent, read-only).
  Hidden-by-default set is now DERIVED from the catalogue (defaultVisible) and
  shared by desktop + mobile reconcile — replaces the hardcoded STATIC_WIDGET_IDS
  so a new opt-in widget can't reappear unbidden on existing mobile dashboards.

Typecheck + build green. Adversarial review: 1 should-fix (mobile hidden-set) +
2 nits found, fixed/triaged, and re-verified clean.
2026-06-16 19:53:31 +08:00

62 lines
2.9 KiB
TypeScript

import type { MessageFormat, OneBotConfig, StatusCommandConfig } from '@/types';
const DEFAULT_STATUS_COMMAND: StatusCommandConfig = { enabled: true, swallow: false, cooldownSeconds: 5 };
/** Fill the `statusCommand` block with defaults when the backend omits or
* partially supplies it (older configs predate the feature). */
function normalizeStatusCommand(raw: unknown): StatusCommandConfig {
const src = (raw ?? {}) as Record<string, unknown>;
return {
enabled: typeof src.enabled === 'boolean' ? src.enabled : DEFAULT_STATUS_COMMAND.enabled,
swallow: typeof src.swallow === 'boolean' ? src.swallow : DEFAULT_STATUS_COMMAND.swallow,
cooldownSeconds:
typeof src.cooldownSeconds === 'number' && Number.isFinite(src.cooldownSeconds) && src.cooldownSeconds >= 0
? Math.trunc(src.cooldownSeconds)
: DEFAULT_STATUS_COMMAND.cooldownSeconds,
};
}
/** Keep only string channel ids (the server re-validates slugs on save). */
function normalizeNotifications(raw: unknown): { channelIds: string[] } {
const src = (raw ?? {}) as Record<string, unknown>;
const channelIds = Array.isArray(src.channelIds)
? src.channelIds.filter((x): x is string => typeof x === 'string')
: [];
return { channelIds };
}
/**
* Anti-corruption layer for the per-UIN config payload. Older backends emit
* `messageFormat` / `reportSelfMessage` at the top level instead of per
* adapter, and may omit them on adapters entirely. This collapses both shapes
* into the canonical {@link OneBotConfig} the editor expects.
*/
export function normalizeOneBotConfig(raw: unknown): OneBotConfig {
const cfg = (raw ?? {}) as Record<string, unknown>;
const nets = (cfg.networks as Record<string, unknown> | undefined) ?? {};
const legacyFormat: MessageFormat = cfg.messageFormat === 'string' ? 'string' : 'array';
const legacyReport = !!cfg.reportSelfMessage;
const normalize = (item: Record<string, unknown>): Record<string, unknown> => ({
...item,
messageFormat: item.messageFormat === 'string' ? 'string' : legacyFormat,
reportSelfMessage:
typeof item.reportSelfMessage === 'boolean' ? item.reportSelfMessage : legacyReport,
});
const list = (x: unknown): Record<string, unknown>[] =>
Array.isArray(x) ? x.map((it) => normalize(it as Record<string, unknown>)) : [];
return {
networks: {
httpServers: list(nets.httpServers) as unknown as OneBotConfig['networks']['httpServers'],
httpClients: list(nets.httpClients) as unknown as OneBotConfig['networks']['httpClients'],
wsServers: list(nets.wsServers) as unknown as OneBotConfig['networks']['wsServers'],
wsClients: list(nets.wsClients) as unknown as OneBotConfig['networks']['wsClients'],
},
musicSignUrl: typeof cfg.musicSignUrl === 'string' ? cfg.musicSignUrl : undefined,
statusCommand: normalizeStatusCommand(cfg.statusCommand),
notifications: normalizeNotifications(cfg.notifications),
};
}