From 6bcc49fc6f694549c572c81cefa1acd25025e95f Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Mon, 2 Nov 2026 17:46:59 +0800 Subject: [PATCH] fix(notifications): link admin emails to admin notification settings --- client/src/pages/admin/useAdmin.test.tsx | 5 ++ .../nest/notifications/channels/builtins.ts | 3 +- .../nest/notifications/mailer/email-html.ts | 3 +- .../notifications/mailer/mailer.service.ts | 3 +- .../unit/nest/notification-transports.test.ts | 51 +++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/client/src/pages/admin/useAdmin.test.tsx b/client/src/pages/admin/useAdmin.test.tsx index 7103519d4..24b196622 100644 --- a/client/src/pages/admin/useAdmin.test.tsx +++ b/client/src/pages/admin/useAdmin.test.tsx @@ -880,6 +880,11 @@ describe('useAdmin', () => { expect(result.current.activeTab).toBe('audit'); }); + it('opens the notifications tab from the admin email manage link', async () => { + const { result } = await mountAdmin('/admin?tab=notifications'); + expect(result.current.activeTab).toBe('notifications'); + }); + /* * A tab id with no panel behind it — a typo, or a link from an install that * has tabs this one does not — used to open the page on an empty content diff --git a/server/src/nest/notifications/channels/builtins.ts b/server/src/nest/notifications/channels/builtins.ts index 94074322a..eba8309ae 100644 --- a/server/src/nest/notifications/channels/builtins.ts +++ b/server/src/nest/notifications/channels/builtins.ts @@ -51,7 +51,8 @@ export function buildBuiltinChannels({ mailer, webhook, ntfy, push }: BuiltinCha async sendToUser(userId, msg) { const email = await mailer.getUserEmail(userId); if (!email) return false; - return mailer.sendEmail(email, msg.title, msg.body, userId, msg.navigateTarget); + const manageTarget = ADMIN_SCOPED_EVENTS.has(msg.event) ? '/admin?tab=notifications' : undefined; + return mailer.sendEmail(email, msg.title, msg.body, userId, msg.navigateTarget, manageTarget); }, async test(userId) { const email = await mailer.getUserEmail(userId); diff --git a/server/src/nest/notifications/mailer/email-html.ts b/server/src/nest/notifications/mailer/email-html.ts index 741abaf88..29291e486 100644 --- a/server/src/nest/notifications/mailer/email-html.ts +++ b/server/src/nest/notifications/mailer/email-html.ts @@ -37,11 +37,12 @@ export function buildEmailHtml( lang: string, navigateTarget?: string, rawBody = false, + manageTarget = '/settings?tab=notifications', ): string { const s = I18N[lang] || I18N.en; const appUrl = getAppUrl(); const ctaHref = escapeHtml(navigateTarget ? `${appUrl}${navigateTarget}` : appUrl || ''); - const manageHref = escapeHtml(`${appUrl}/settings?tab=notifications`); + const manageHref = escapeHtml(`${appUrl}${manageTarget}`); const safeSubject = escapeHtml(subject); const safeBody = rawBody ? body : escapeHtml(body); diff --git a/server/src/nest/notifications/mailer/mailer.service.ts b/server/src/nest/notifications/mailer/mailer.service.ts index 1dbbf0bf4..e1d14bf67 100644 --- a/server/src/nest/notifications/mailer/mailer.service.ts +++ b/server/src/nest/notifications/mailer/mailer.service.ts @@ -213,6 +213,7 @@ export class MailerService { body: string, userId?: number, navigateTarget?: string, + manageTarget?: string, ): Promise { const config = await this.getSmtpConfig(); if (!config) return false; @@ -227,7 +228,7 @@ export class MailerService { to, subject: `TREK — ${subject}`, text: body, - html: buildEmailHtml(subject, body, lang, navigateTarget), + html: buildEmailHtml(subject, body, lang, navigateTarget, false, manageTarget), attachments: [emailLogoAttachment()], }); logInfo(`Email sent to=${to} subject="${subject}"`); diff --git a/server/tests/unit/nest/notification-transports.test.ts b/server/tests/unit/nest/notification-transports.test.ts index 4e483e22a..6337811cd 100644 --- a/server/tests/unit/nest/notification-transports.test.ts +++ b/server/tests/unit/nest/notification-transports.test.ts @@ -260,6 +260,13 @@ describe('buildEmailHtml', () => { expect(html).toContain('Hello world, this is the body!'); }); + it('keeps the email shell fluid on narrow screens', () => { + const html = buildEmailHtml('Subject', 'Body', 'en'); + expect(html).toContain(''); + expect(html).toContain('width="100%"'); + expect(html).toContain('max-width: 480px'); + }); + it('uses English i18n strings for lang=en', () => { const html = buildEmailHtml('Subject', 'Body', 'en'); expect(html).toContain('notifications enabled in TREK'); @@ -272,6 +279,13 @@ describe('buildEmailHtml', () => { ); }); + it('links admin-scoped notification preferences to the admin notifications tab', () => { + const html = buildEmailHtml('Subject', 'Body', 'en', undefined, false, '/admin?tab=notifications'); + expect(html).toMatch( + /]*>Manage preferences in Settings<\/a>/, + ); + }); + it('uses German i18n strings for lang=de', () => { const html = buildEmailHtml('Subject', 'Body', 'de'); expect(html).toContain('TREK aktiviert'); @@ -293,6 +307,43 @@ describe('buildEmailHtml', () => { }); }); +describe('built-in email manage link scope', () => { + it('uses the admin tab for admin-scoped events and the user tab otherwise', async () => { + const mailer = { + getUserEmail: vi.fn(async () => 'person@example.com'), + sendEmail: vi.fn(async () => true), + }; + const emailChannel = buildBuiltinChannels({ + mailer: mailer as never, + webhook: {} as never, + ntfy: {} as never, + push: {} as never, + }).find((channel) => channel.id === 'email')!; + + await emailChannel.sendToUser(1, { event: 'version_available', title: 'Update', body: 'New version' }); + await emailChannel.sendToUser(1, { event: 'trip_invite', title: 'Invite', body: 'Join trip' }); + + expect(mailer.sendEmail).toHaveBeenNthCalledWith( + 1, + 'person@example.com', + 'Update', + 'New version', + 1, + undefined, + '/admin?tab=notifications', + ); + expect(mailer.sendEmail).toHaveBeenNthCalledWith( + 2, + 'person@example.com', + 'Invite', + 'Join trip', + 1, + undefined, + undefined, + ); + }); +}); + // ── SEC: XSS escaping in buildEmailHtml ────────────────────────────────────── describe('buildEmailHtml XSS prevention (SEC-016)', () => {