Files
SnowLuma/packages/webui/src/lib/onebot-config.ts
T
DrSmoothl 196f98e97c feat(webui): global-config settings tab + /api/global-config
Adds a '全局配置' Settings tab that edits config/snowluma.json — the rkey
fallback-server list and the deployment-wide musicSignUrl — backed by a new
bearer-gated GET/POST /api/global-config (load/saveGlobalSettings, then
oneBotManager.reloadGlobalSettings to hot-apply to live instances).

The panel reuses the established settings-panel idiom (Input/Button/Label,
debounced auto-save, flash); rkey servers are an inline add/remove list with
client-side http(s) validation hints. It deliberately does not reconcile inputs
from the server's normalized response (would erase a half-typed URL); a
load-failure shows an error+retry, and the save toast says '无效项已忽略' when
the server would drop an entry.

Removes the now-dead per-UIN musicSignUrl form (moved to global in Phase 2).
Phase 3 of the global-settings split (nav grouping is Phase 4).
2026-06-28 22:16:07 +08:00

69 lines
3.0 KiB
TypeScript

import type { MessageFormat, OneBotConfig, StatusCommandConfig } from '@/types';
const DEFAULT_STATUS_COMMAND: StatusCommandConfig = {
enabled: true,
swallow: false,
cooldownSeconds: 5,
trigger: '#sl',
};
/** 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,
trigger: typeof src.trigger === 'string' && src.trigger.trim().length > 0 && !/[\r\n]/.test(src.trigger)
? src.trigger.trim().slice(0, 32)
: DEFAULT_STATUS_COMMAND.trigger,
};
}
/** 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'],
},
statusCommand: normalizeStatusCommand(cfg.statusCommand),
notifications: normalizeNotifications(cfg.notifications),
};
}