feat(notifications): default body template → Server酱 JSON (title/desp) (#108)

- Change DEFAULT_BODY_TEMPLATE from plaintext to Server酱-style JSON
- Add JSON-safe escaping in renderTemplate for backslashes and quotes
- Update frontend DEFAULT_TEMPLATE to match backend
- Add tests for JSON escaping behavior

Fixes #106 (targets dev branch)
This commit is contained in:
渡辺鳩P authored and GitHub committed 2026-06-18 16:10:09 +08:00
1 parent c637329884
commit 06c8954881
3 files changed
+36 -7

No files matched your search

+23 -6
View File
@@ -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, string>): 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) ──────────
@@ -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', () => {
@@ -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 {