diff --git a/src/app/features/settings/notifications/TauriNotificationsApiClient.test.ts b/src/app/features/settings/notifications/TauriNotificationsApiClient.test.ts new file mode 100644 index 000000000..7a34564b0 --- /dev/null +++ b/src/app/features/settings/notifications/TauriNotificationsApiClient.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it } from 'vitest'; +import { buildNotificationExtra } from './TauriNotificationsApiClient'; + +describe('buildNotificationExtra', () => { + it('string-coerces all values of a flat routing payload', () => { + expect( + buildNotificationExtra({ + type: 'm.room.message', + room_id: '!r:example.org', + event_id: '$e:example.org', + user_id: '@u:example.org', + }) + ).toEqual({ + type: 'm.room.message', + room_id: '!r:example.org', + event_id: '$e:example.org', + user_id: '@u:example.org', + }); + }); + + it('drops undefined keys', () => { + expect( + buildNotificationExtra({ type: 'm.room.message', room_id: '!r:x', event_id: undefined }) + ).toEqual({ type: 'm.room.message', room_id: '!r:x' }); + }); + + it('drops null keys', () => { + expect(buildNotificationExtra({ room_id: '!r:x', event_id: null })).toEqual({ + room_id: '!r:x', + }); + }); + + it('string-coerces non-string primitives', () => { + expect(buildNotificationExtra({ count: 5, flagged: true })).toEqual({ + count: '5', + flagged: 'true', + }); + }); + + it.each([undefined, null, 'not-an-object', 42])( + 'returns an empty record for non-object input (%s)', + (input) => { + expect(buildNotificationExtra(input)).toEqual({}); + } + ); +}); diff --git a/src/app/features/settings/notifications/TauriNotificationsApiClient.ts b/src/app/features/settings/notifications/TauriNotificationsApiClient.ts index 6219e3e77..2b07504eb 100644 --- a/src/app/features/settings/notifications/TauriNotificationsApiClient.ts +++ b/src/app/features/settings/notifications/TauriNotificationsApiClient.ts @@ -162,6 +162,20 @@ export type NativeTauriNotification = { icon?: string; }; +// String-coerce `data` into the plugin's Record extra, dropping +// nullish keys and any value that isn't a string/number/boolean (objects would +// stringify to '[object Object]'). +export function buildNotificationExtra(data: unknown): Record { + const extra: Record = {}; + if (data && typeof data === 'object') { + for (const [k, v] of Object.entries(data as Record)) { + if (typeof v === 'string') extra[k] = v; + else if (typeof v === 'number' || typeof v === 'boolean') extra[k] = String(v); + } + } + return extra; +} + export async function sendNativeTauriNotification({ title, body, diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 128d42d11..32fda303f 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -212,14 +212,16 @@ export function BackgroundNotifications() { async function sendNotification(opts: NotifyOptions): Promise { if (isDesktopTauri()) { try { - const { getTauriNotificationsApi } = + const { getTauriNotificationsApi, buildNotificationExtra } = await import('$features/settings/notifications/TauriNotificationsApiClient'); const api = await getTauriNotificationsApi(); + // Attach routing payload so the tap deep-links via NativeNotificationClickRouting. await api.sendNotification({ id: nextDesktopNotificationId(), title: opts.title, body: opts.body, silent: opts.silent ?? false, + extra: buildNotificationExtra(opts.data), }); return; } catch (err) {