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:
3 files changed
+36
-7
No files matched your search
@@ -48,9 +48,13 @@ export interface NotificationsConfig {
|
|||||||
channels: NotificationChannel[];
|
channels: NotificationChannel[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A neutral plaintext starting point; the WebUI ships per-vendor presets
|
/** Default body template — Server酱-style JSON ({title}/{desp}). The WebUI
|
||||||
* (钉钉/Discord/…) as frontend constants the operator can drop in. */
|
* ships additional per-vendor presets (钉钉/Discord/…) as frontend constants
|
||||||
export const DEFAULT_BODY_TEMPLATE = '{nickname}({uin}) {event} @ {time}';
|
* 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 {
|
export function defaultNotificationsConfig(): NotificationsConfig {
|
||||||
return {
|
return {
|
||||||
@@ -67,12 +71,25 @@ export function defaultNotificationsConfig(): NotificationsConfig {
|
|||||||
* A key present in `vars` is replaced by its value; an unknown `{key}` is left
|
* 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
|
* untouched (原样) so a typo'd placeholder is visible rather than silently
|
||||||
* blanked. Pure + total (a non-string template yields '').
|
* 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 {
|
export function renderTemplate(template: string, vars: Record<string, string>): string {
|
||||||
if (typeof template !== 'string') return '';
|
if (typeof template !== 'string') return '';
|
||||||
return template.replace(/\{(\w+)\}/g, (match, key: string) =>
|
let isJson = false;
|
||||||
Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : match,
|
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) ──────────
|
// ─── 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.
|
// to a recursive/loop replace would break this while passing every other test.
|
||||||
expect(renderTemplate('{a}', { a: '{b}', b: 'NESTED' })).toBe('{b}');
|
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', () => {
|
describe('normalizeNotificationsConfig — total normalize', () => {
|
||||||
|
|||||||
@@ -16,7 +16,10 @@ import { cn } from '@/lib/utils';
|
|||||||
import type { NotificationChannel, NotificationsConfig } from '@/types';
|
import type { NotificationChannel, NotificationsConfig } from '@/types';
|
||||||
import { NotificationChannelDialog } from './notification-channel-dialog';
|
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. */
|
/** A fresh channel with a non-colliding default id. */
|
||||||
function blankChannel(existing: NotificationChannel[]): NotificationChannel {
|
function blankChannel(existing: NotificationChannel[]): NotificationChannel {
|
||||||
|
|||||||
Reference in new issue
Block a user