Skip to content

Commit 04c9e75

Browse files
committed
feat(service-messaging): the push channel copies a device row's origin into the push data
PushDeliveryDataSchema.origin (from main) says the push channel copies it from the device row. Each device's send now carries its row's origin when the schema admits it; a row without one, or with a malformed one, sends the data without it. The registry does not store origin yet (UserDeviceSchema.origin stays PLANNED), so today no push carries it. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8c089dd commit 04c9e75

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎packages/services/service-messaging/src/push-channel.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,27 @@ describe('createPushChannel — no transport', () => {
202202
});
203203
});
204204

205+
describe('createPushChannel — the sending server (origin)', () => {
206+
it('copies each device row\'s origin into that device\'s data, and sends none for a row without one', async () => {
207+
const { push, sent } = pushDouble();
208+
const reg = registry([
209+
device({ deviceId: 'd1', token: 'tok-a', origin: 'https://crm.example.com' }),
210+
device({ deviceId: 'd2', token: 'tok-b', origin: 'http://localhost:3000' }),
211+
device({ deviceId: 'd3', token: 'tok-c' }),
212+
device({ deviceId: 'd4', token: 'tok-d', origin: 'https://crm.example.com/path' }),
213+
]);
214+
const channel = createPushChannel({ getPush: () => push, devices: reg.devices });
215+
expect((await channel.send(ctx, delivery())).ok).toBe(true);
216+
const byToken = Object.fromEntries(sent.map((m) => [m.token, m.data]));
217+
expect(byToken['tok-a']).toEqual({ notificationId: 'ntf_1', route: '/opportunity/123', origin: 'https://crm.example.com' });
218+
expect(byToken['tok-b']).toEqual({ notificationId: 'ntf_1', route: '/opportunity/123', origin: 'http://localhost:3000' });
219+
// No stored origin, or one the schema refuses: the data goes without it.
220+
expect(byToken['tok-c']).toEqual({ notificationId: 'ntf_1', route: '/opportunity/123' });
221+
expect(byToken['tok-d']).toEqual({ notificationId: 'ntf_1', route: '/opportunity/123' });
222+
for (const m of sent) expect(PushDeliveryDataSchema.safeParse(m.data).success).toBe(true);
223+
});
224+
});
225+
205226
describe('deliveryData / truncateUtf8', () => {
206227
it('admits both inbox actionUrl shapes as route', () => {
207228
expect(deliveryData('n', '/opportunity/1')).toEqual({ notificationId: 'n', route: '/opportunity/1' });

‎packages/services/service-messaging/src/push-channel.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
22

3-
import { PushRouteSchema } from '@objectstack/spec/api';
3+
import { PushDeliveryDataSchema, PushRouteSchema } from '@objectstack/spec/api';
44
import type { UserDevice } from '@objectstack/spec/system';
55
import type {
66
ChannelAvailability,
@@ -85,8 +85,9 @@ const ROUTE_MAX_BYTES = 1024;
8585
* enabled ones whose vendor the push service has a transport for, and send
8686
* each one the notification's title and body (already in the recipient's
8787
* language: fan-out renders keyed text per recipient) plus its delivery data
88-
* (`PushDeliveryDataSchema`): the notification id and, when it passes
89-
* `PushRouteSchema`, its `actionUrl` as `route`. An `actionUrl` that is not a
88+
* (`PushDeliveryDataSchema`): the notification id, when it passes
89+
* `PushRouteSchema` its `actionUrl` as `route`, and the device row's `origin`
90+
* when it holds one. An `actionUrl` that is not a
9091
* same-server path is left out, never sent; the delivery is not failed for it.
9192
*
9293
* **Dead tokens are retired here.** A device the vendor answers `invalid_token`
@@ -161,7 +162,13 @@ export function createPushChannel(opts: PushChannelOptions): MessagingChannel {
161162
const dead: string[] = [];
162163
const failures: Array<{ error: string; retryable: boolean }> = [];
163164
for (const device of reachable) {
164-
const result = await push.send({ vendor: device.vendor, token: device.token, title, body, data });
165+
const result = await push.send({
166+
vendor: device.vendor,
167+
token: device.token,
168+
title,
169+
body,
170+
data: withOrigin(data, device.origin),
171+
});
165172
if (result.status === 'sent') {
166173
sent.push(result.messageId ?? '');
167174
} else if (result.status === 'invalid_token') {
@@ -228,6 +235,17 @@ export function deliveryData(notificationId: string | undefined, actionUrl: stri
228235
return data;
229236
}
230237

238+
/**
239+
* The device's `origin` added to the delivery data (`PushDeliveryDataSchema.origin`),
240+
* when the row holds one the schema admits: the server the Console registered
241+
* from, so a shell with several saved servers opens the tap on this one. A row
242+
* without one, or with one the schema refuses, sends the data as is.
243+
*/
244+
export function withOrigin(data: Record<string, string>, origin: string | undefined): Record<string, string> {
245+
if (!origin || !PushDeliveryDataSchema.shape.origin.safeParse(origin).success) return data;
246+
return { ...data, origin };
247+
}
248+
231249
/** Collapse whitespace runs: a push shows one plain line of text per member. */
232250
function plain(text: string | undefined): string {
233251
return String(text ?? '').replace(/\s+/g, ' ').trim();

0 commit comments

Comments
 (0)