feat(notifications): P1 config layer — global channel store + per-UIN opt-in

Global channel store at config/notifications.json, mirroring ui-config's
total-normalize + atomic-write + cached-load conventions: NotificationsConfig
(version, debounceSeconds clamp [0,3600] default 30, channels[]), a total
normalizeNotificationsConfig (drops malformed/non-http channels, dedupes by
id, strips unknown fields), and the pure renderTemplate ({key} substitution;
missing keys left as-is; no re-expansion of substituted values).

Per-UIN opt-in: OneBotConfig.notifications.channelIds (validated slugs,
last-write-wins merge across global+per-uin, deduped) in the onebot package.

Tests: 19 (core notifications-config) + 12 (onebot config), all green.
Adversarial review: 0 blockers / 0 should-fix.
This commit is contained in:
DrSmoothl committed 2026-06-16 19:02:01 +08:00
1 parent bf5978fb51
commit 34d05f41f5
5 files changed
+509 -1

No files matched your search

+37 -1
View File
@@ -56,6 +56,7 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
},
musicSignUrl: '',
statusCommand: makeDefaultStatusCommand(),
notifications: { channelIds: [] },
};
}
@@ -113,6 +114,7 @@ function toJsonObject(config: OneBotConfig): JsonObject {
swallow: config.statusCommand.swallow,
cooldownSeconds: config.statusCommand.cooldownSeconds,
},
notifications: { channelIds: config.notifications?.channelIds ?? [] },
};
}
@@ -196,7 +198,41 @@ function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
}
const networks: OneBotNetworks = { httpServers, httpClients, wsServers, wsClients };
return { networks, musicSignUrl, statusCommand: parseStatusCommand(sources) };
return {
networks,
musicSignUrl,
statusCommand: parseStatusCommand(sources),
notifications: parseNotifications(sources),
};
}
/** 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
* deliberately: core depends on onebot, so onebot cannot import from core. */
function parseNotifications(sources: JsonObject[]): { channelIds: string[] } {
let channelIds: string[] = [];
for (const src of sources) {
const raw = src.notifications;
if (!isObject(raw)) continue;
if (Array.isArray(raw.channelIds)) channelIds = normalizeChannelIds(raw.channelIds);
}
return { channelIds };
}
function normalizeChannelIds(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 || v.length > 64 || !/^[\w.-]+$/.test(v)) continue;
if (seen.has(v)) continue;
seen.add(v);
out.push(v);
}
return out;
}
/** Last-write-wins merge of `statusCommand` across config sources, with
+4
View File
@@ -86,6 +86,10 @@ export interface OneBotConfig {
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
* validated slugs; channels themselves live in config/notifications.json).
* Always present after normalization. */
notifications?: { channelIds: string[] };
}
export interface MessageMeta {
+50
View File
@@ -26,6 +26,7 @@ describe('makeDefaultOneBotConfig', () => {
expect(config.networks.wsClients).toEqual([]);
expect(config.musicSignUrl).toBe('');
expect(config.statusCommand).toEqual({ enabled: true, swallow: false, cooldownSeconds: 5 });
expect(config.notifications).toEqual({ channelIds: [] });
});
});
@@ -222,3 +223,52 @@ describe('loadOneBotConfig', () => {
});
});
describe('OneBotConfig.notifications (per-UIN channel opt-in)', () => {
let tempDir: string;
let prevCwd: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-onebot-notif-'));
prevCwd = process.cwd();
process.chdir(tempDir);
});
afterEach(() => {
process.chdir(prevCwd);
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('defaults to an empty channelIds list when absent', () => {
const config = loadOneBotConfig('20001');
expect(config.notifications).toEqual({ channelIds: [] });
});
it('validates, dedupes and drops bad channel ids', () => {
const uin = '20002';
const dir = path.join(tempDir, 'config');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(
path.join(dir, `onebot_${uin}.json`),
JSON.stringify({
networks: { httpServers: [], httpClients: [], wsServers: [], wsClients: [] },
notifications: { channelIds: ['dingtalk', 'dingtalk', 'bad id!', '', 42, 'feishu'] },
}),
);
const config = loadOneBotConfig(uin);
expect(config.notifications?.channelIds).toEqual(['dingtalk', 'feishu']);
});
it('round-trips channelIds through save/load and persists them', () => {
const uin = '20003';
const config = makeDefaultOneBotConfig();
config.notifications = { channelIds: ['discord', 'serverchan'] };
saveOneBotConfig(uin, config);
const reloaded = loadOneBotConfig(uin);
expect(reloaded.notifications?.channelIds).toEqual(['discord', 'serverchan']);
const onDisk = JSON.parse(fs.readFileSync(path.join(tempDir, 'config', `onebot_${uin}.json`), 'utf8'));
expect(onDisk.notifications).toEqual({ channelIds: ['discord', 'serverchan'] });
});
});