diff --git a/packages/core/src/notifications/config.ts b/packages/core/src/notifications/config.ts index eb410eef..2ec24437 100644 --- a/packages/core/src/notifications/config.ts +++ b/packages/core/src/notifications/config.ts @@ -48,9 +48,13 @@ export interface NotificationsConfig { channels: NotificationChannel[]; } -/** A neutral plaintext starting point; the WebUI ships per-vendor presets - * (钉钉/Discord/…) as frontend constants the operator can drop in. */ -export const DEFAULT_BODY_TEMPLATE = '{nickname}({uin}) {event} @ {time}'; +/** Default body template — Server酱-style JSON ({title}/{desp}). The WebUI + * ships additional per-vendor presets (钉钉/Discord/…) as frontend constants + * the operator can drop in. */ +export const DEFAULT_BODY_TEMPLATE = `{ + "title": "账号状态通知:{event}", + "desp": "您的账号状态发生了改变。\\n\\n**昵称**:{nickname}\\n**QQ号**:{uin}\\n**当前状态**:{event}\\n**时间**:{time}" +}`; export function defaultNotificationsConfig(): NotificationsConfig { return { @@ -67,12 +71,25 @@ export function defaultNotificationsConfig(): NotificationsConfig { * A key present in `vars` is replaced by its value; an unknown `{key}` is left * untouched (原样) so a typo'd placeholder is visible rather than silently * blanked. Pure + total (a non-string template yields ''). + * + * **JSON safety**: When the template is valid JSON (parseable by JSON.parse), + * values are escaped to prevent breaking the JSON structure — backslashes + * become `\\` and double-quotes become `\"`. */ export function renderTemplate(template: string, vars: Record): string { if (typeof template !== 'string') return ''; - return template.replace(/\{(\w+)\}/g, (match, key: string) => - Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : match, - ); + let isJson = false; + try { + JSON.parse(template); + isJson = true; + } catch { + // not JSON — plain text mode + } + return template.replace(/\{(\w+)\}/g, (match, key: string) => { + if (!Object.prototype.hasOwnProperty.call(vars, key)) return match; + const val = vars[key]; + return isJson ? val.replace(/\\/g, '\\\\').replace(/"/g, '\\"') : val; + }); } // ─── Normalization helpers (replicated from ui-config conventions) ────────── diff --git a/packages/core/tests/notifications-config.test.ts b/packages/core/tests/notifications-config.test.ts index d81827b9..29bde880 100644 --- a/packages/core/tests/notifications-config.test.ts +++ b/packages/core/tests/notifications-config.test.ts @@ -47,6 +47,15 @@ describe('renderTemplate — mechanical {key} substitution', () => { // to a recursive/loop replace would break this while passing every other test. expect(renderTemplate('{a}', { a: '{b}', b: 'NESTED' })).toBe('{b}'); }); + + it('escapes backslashes and quotes when template is JSON-like', () => { + const tmpl = '{"text": "{val}"}'; + expect(renderTemplate(tmpl, { val: 'a"b\\c' })).toBe('{"text": "a\\"b\\\\c"}'); + }); + + it('does NOT escape when template is plain text', () => { + expect(renderTemplate('{val}', { val: 'a"b\\c' })).toBe('a"b\\c'); + }); }); describe('normalizeNotificationsConfig — total normalize', () => { diff --git a/packages/webui/src/components/settings/notifications-panel.tsx b/packages/webui/src/components/settings/notifications-panel.tsx index dc2bdee0..67b0dff3 100644 --- a/packages/webui/src/components/settings/notifications-panel.tsx +++ b/packages/webui/src/components/settings/notifications-panel.tsx @@ -16,7 +16,10 @@ import { cn } from '@/lib/utils'; import type { NotificationChannel, NotificationsConfig } from '@/types'; import { NotificationChannelDialog } from './notification-channel-dialog'; -const DEFAULT_TEMPLATE = '{nickname}({uin}) {event} @ {time}'; +const DEFAULT_TEMPLATE = `{ + "title": "账号状态通知:{event}", + "desp": "您的账号状态发生了改变。\\n\\n**昵称**:{nickname}\\n**QQ号**:{uin}\\n**当前状态**:{event}\\n**时间**:{time}" +}`; /** A fresh channel with a non-colliding default id. */ function blankChannel(existing: NotificationChannel[]): NotificationChannel {