feat(onebot): opt-in remote rkey fallback server (#156)

On accounts where the native OIDB 0x9067_202 rkey fetch persistently returns
nothing, every image was served as a bare multimedia.nt.qq.com.cn URL the CDN
rejects with invalid rkey — SnowLuma had no fallback, unlike NapCat.

Add config.rkey.fallbackServers (default []: OFF; no third-party server is
contacted unless the operator configures one). When the native fetch leaves the
cache without a usable image rkey, each configured HTTP endpoint is tried in
turn for { group_rkey, private_rkey, expired_time } (NapCat rkey-server format;
an OneBot { retcode, data } wrapper is also accepted), populating the group/
private image rkey types. Values keep any leading &rkey=/?rkey= since
resolveImageUrl strips before re-appending.

WebUI has no form yet but round-trips rkey.fallbackServers verbatim so a hand-
edited value survives an unrelated config save. Part B of #156 (cooldown is A).
This commit is contained in:
DrSmoothl committed 2026-06-28 01:37:58 +08:00
1 parent 219d60410a
commit 12b01907bc
8 files changed
+265 -7

No files matched your search

+33
View File
@@ -9,6 +9,7 @@ import type {
MessageFormat,
OneBotConfig,
OneBotNetworks,
RKeyConfig,
StatusCommandConfig,
WsClientNetwork,
WsRole,
@@ -64,6 +65,7 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
musicSignUrl: '',
statusCommand: makeDefaultStatusCommand(),
notifications: { channelIds: [] },
rkey: { fallbackServers: [] },
};
}
@@ -123,6 +125,7 @@ function toJsonObject(config: OneBotConfig): JsonObject {
trigger: config.statusCommand.trigger,
},
notifications: { channelIds: config.notifications?.channelIds ?? [] },
rkey: { fallbackServers: config.rkey?.fallbackServers ?? [] },
};
}
@@ -211,9 +214,39 @@ function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
musicSignUrl,
statusCommand: parseStatusCommand(sources),
notifications: parseNotifications(sources),
rkey: parseRKeyConfig(sources),
};
}
/** Last-write-wins merge of `rkey.fallbackServers` across config sources. Only
* http(s) URLs are kept; the list is deduped. Default empty (feature off). */
function parseRKeyConfig(sources: JsonObject[]): RKeyConfig {
let fallbackServers: string[] = [];
for (const src of sources) {
const raw = src.rkey;
if (!isObject(raw)) continue;
if (Array.isArray(raw.fallbackServers)) {
fallbackServers = normalizeRkeyServers(raw.fallbackServers);
}
}
return { fallbackServers };
}
function normalizeRkeyServers(value: unknown): string[] {
if (!Array.isArray(value)) return [];
const seen = new Set<string>();
const out: string[] = [];
for (const item of value) {
if (typeof item !== 'string') continue;
const v = item.trim();
if (!v || !/^https?:\/\//i.test(v)) continue;
if (seen.has(v)) continue;
seen.add(v);
out.push(v);
}
return out;
}
/** Last-write-wins merge of `notifications.channelIds` across config sources,
* each id validated as a slug + deduped. Mirrors the channel-id rule in
* packages/core/src/notifications/config.ts (CHANNEL_ID_RE) — duplicated