fix(notifications): attach routing payload to desktop background notifications

This commit is contained in:
Erwan Leboucher committed 2026-07-26 21:01:52 +02:00
1 parent a6e0b9ed15
commit 5b4e3228c7
3 files changed
+63 -1

No files matched your search

@@ -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({});
}
);
});
@@ -162,6 +162,20 @@ export type NativeTauriNotification = {
icon?: string;
};
// String-coerce `data` into the plugin's Record<string, string> 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<string, string> {
const extra: Record<string, string> = {};
if (data && typeof data === 'object') {
for (const [k, v] of Object.entries(data as Record<string, unknown>)) {
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,
@@ -212,14 +212,16 @@ export function BackgroundNotifications() {
async function sendNotification(opts: NotifyOptions): Promise<void> {
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) {