feat(onebot): migrate musicSignUrl from per-UIN to global config

musicSignUrl is a deployment-wide signing-service endpoint (same for every
account), so it belongs in the global config/snowluma.json next to rkey, not in
per-UIN onebot_<uin>.json.

- GlobalSettings gains musicSignUrl (http(s)-validated, '' = built-in default);
  consumption switches to globalSettings.musicSignUrl and hot-reloads via
  applyGlobalSettings.
- One-shot copy-up at boot (migrateGlobalSettings, called first in main()):
  lifts the first VALID legacy value from any config/onebot*.json into the
  global store. Idempotent via a key-PRESENCE guard, so an intentional
  clear-to-'' is never reverted by a stale legacy value.
- Remove musicSignUrl from per-UIN OneBotConfig (type + parse/serialize).

Phase 2 of the global-settings split. (WebUI form follows in Phase 3.)
This commit is contained in:
DrSmoothl committed 2026-06-28 21:38:32 +08:00
1 parent 659970077a
commit ea1c5ff9eb
8 files changed
+185 -23

No files matched your search

-5
View File
@@ -61,7 +61,6 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
}],
wsClients: [],
},
musicSignUrl: '',
statusCommand: makeDefaultStatusCommand(),
notifications: { channelIds: [] },
};
@@ -115,7 +114,6 @@ function toJsonObject(config: OneBotConfig): JsonObject {
wsServers: nets.wsServers.map(wsServerToJson),
wsClients: nets.wsClients.map(wsClientToJson),
},
musicSignUrl: config.musicSignUrl ?? '',
statusCommand: {
enabled: config.statusCommand.enabled,
swallow: config.statusCommand.swallow,
@@ -179,12 +177,10 @@ function wsClientToJson(n: WsClientNetwork): JsonObject {
function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
let legacyFormat: MessageFormat | undefined;
let legacyReport: boolean | undefined;
let musicSignUrl = '';
for (const src of sources) {
const mf = parseMessageFormat(src.messageFormat);
if (mf) legacyFormat = mf;
if (typeof src.reportSelfMessage === 'boolean') legacyReport = src.reportSelfMessage;
if (typeof src.musicSignUrl === 'string') musicSignUrl = src.musicSignUrl;
}
const inheritedFormat: MessageFormat = legacyFormat ?? 'array';
const inheritedReport: boolean = legacyReport ?? false;
@@ -208,7 +204,6 @@ function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
const networks: OneBotNetworks = { httpServers, httpClients, wsServers, wsClients };
return {
networks,
musicSignUrl,
statusCommand: parseStatusCommand(sources),
notifications: parseNotifications(sources),
};
+91 -2
View File
@@ -17,10 +17,13 @@ const GLOBAL_CONFIG_PATH = path.join(CONFIG_DIR, 'snowluma.json');
export interface GlobalSettings {
/** Opt-in remote rkey fallback (see RKeyConfig). Default: servers empty = off. */
rkey: RKeyConfig;
/** Music-card signing service URL. Deployment-wide (same service for every
* account), so global rather than per-UIN. Empty = use the built-in default. */
musicSignUrl: string;
}
export function defaultGlobalSettings(): GlobalSettings {
return { rkey: { fallbackServers: [] } };
return { rkey: { fallbackServers: [] }, musicSignUrl: '' };
}
/** Keep only well-formed, deduped http(s) URLs (must parse + have a host). */
@@ -59,6 +62,11 @@ export function normalizeGlobalSettings(value: unknown): GlobalSettings {
if (isObject(rkey)) {
out.rkey.fallbackServers = normalizeRkeyServers(rkey.fallbackServers);
}
if (typeof value.musicSignUrl === 'string') {
const v = value.musicSignUrl.trim();
// Empty = "use built-in default"; otherwise must be a real http(s) endpoint.
out.musicSignUrl = v && isHttpUrl(v) ? v : '';
}
return out;
}
@@ -85,6 +93,10 @@ export function saveGlobalSettings(incoming: unknown): GlobalSettings {
if (isObject(incoming.rkey)) {
merged.rkey.fallbackServers = normalizeRkeyServers(incoming.rkey.fallbackServers);
}
if (typeof incoming.musicSignUrl === 'string') {
const v = incoming.musicSignUrl.trim();
merged.musicSignUrl = v && isHttpUrl(v) ? v : '';
}
}
fs.mkdirSync(CONFIG_DIR, { recursive: true });
const tmp = GLOBAL_CONFIG_PATH + '.tmp';
@@ -94,9 +106,86 @@ export function saveGlobalSettings(incoming: unknown): GlobalSettings {
}
function toJson(settings: GlobalSettings): JsonObject {
return { rkey: { fallbackServers: settings.rkey.fallbackServers } };
return {
rkey: { fallbackServers: settings.rkey.fallbackServers },
musicSignUrl: settings.musicSignUrl,
};
}
function isObject(value: unknown): value is JsonObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
/**
* One-shot copy-up: `musicSignUrl` used to live in per-UIN config. If the global
* store has none yet, lift the first non-empty value found in any legacy
* `config/onebot*.json` into `config/snowluma.json`. Idempotent — skips once the
* global value is set. (If an operator later clears the global value while a
* stale per-UIN value lingers, a restart would re-copy it; benign and rare.)
*/
export function migrateGlobalSettings(): void {
// One-shot guard keyed on PRESENCE (not truthiness): once snowluma.json carries
// a musicSignUrl key — written by this migration or any global-config save —
// never re-scan. This is what stops an operator's intentional clear-to-'' from
// being reverted by a stale legacy value on the next boot.
if (globalConfigHasMusicSignUrlKey()) return;
const found = scanLegacyMusicSignUrl();
if (!found) return;
saveGlobalSettings({ musicSignUrl: found.value });
const extra = found.others.length ? `; ignored differing value(s) in ${found.others.join(', ')}` : '';
log.info('migrated musicSignUrl to global config from %s: %s%s', found.source, found.value, extra);
}
function globalConfigHasMusicSignUrlKey(): boolean {
if (!fs.existsSync(GLOBAL_CONFIG_PATH)) return false;
try {
const raw = JSON.parse(fs.readFileSync(GLOBAL_CONFIG_PATH, 'utf8')) as unknown;
return isObject(raw) && 'musicSignUrl' in raw;
} catch {
return false;
}
}
interface LegacyMusicSignUrl {
value: string;
source: string;
others: string[];
}
/** Scan config/onebot.json + config/onebot_<uin>.json for a non-empty
* musicSignUrl. Returns the first found plus any other files that carried a
* (differing) value, for logging. */
function scanLegacyMusicSignUrl(): LegacyMusicSignUrl | null {
let dir: string[];
try {
dir = fs.readdirSync(CONFIG_DIR);
} catch {
return null;
}
const files = dir
.filter((f) => f === 'onebot.json' || /^onebot_\d+\.json$/.test(f))
.sort();
let first: { value: string; source: string } | null = null;
const others: string[] = [];
for (const file of files) {
let value: unknown;
try {
const raw = JSON.parse(fs.readFileSync(path.join(CONFIG_DIR, file), 'utf8')) as unknown;
value = isObject(raw) ? raw.musicSignUrl : undefined;
} catch {
continue;
}
if (typeof value !== 'string') continue;
const v = value.trim();
// Skip empty AND invalid here, so "first found" means "first usable" — a
// garbage value in an earlier-sorting file can't shadow a real URL in a
// later one (it would be dropped to '' on save and then locked by the guard).
if (!v || !isHttpUrl(v)) continue;
if (!first) first = { value: v, source: file };
else if (v !== first.value) others.push(file);
}
return first ? { ...first, others } : null;
}
+2 -1
View File
@@ -108,7 +108,7 @@ export class OneBotInstance {
reactionStore: this.reactionStore,
converterCtx: this.converterCtx,
config,
musicSignUrl: config.musicSignUrl,
musicSignUrl: globalSettings.musicSignUrl,
cacheMessageMeta: (messageId, meta) => this.cacheMessageMeta(messageId, meta),
dispatchEvent: (event) => this.dispatchEvent(event),
};
@@ -139,6 +139,7 @@ export class OneBotInstance {
* the manager when config/snowluma.json is saved from the WebUI. */
applyGlobalSettings(globalSettings: GlobalSettings): void {
this.rkeyCache.setFallbackServers(globalSettings.rkey.fallbackServers);
this.ctx.musicSignUrl = globalSettings.musicSignUrl;
}
dispose(): void {
-2
View File
@@ -103,8 +103,6 @@ export interface RKeyConfig {
/** Per-UIN OneBot configuration. */
export interface OneBotConfig {
networks: OneBotNetworks;
/** Music card signing service URL (optional). */
musicSignUrl?: string;
/** Built-in `#sl` status command settings. Always present after normalization. */
statusCommand: StatusCommandConfig;
/** Which GLOBAL notification channels this account opts into (channel ids are