|
1 | 1 | // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | | -import { PushRouteSchema } from '@objectstack/spec/api'; |
| 3 | +import { PushDeliveryDataSchema, PushRouteSchema } from '@objectstack/spec/api'; |
4 | 4 | import type { UserDevice } from '@objectstack/spec/system'; |
5 | 5 | import type { |
6 | 6 | ChannelAvailability, |
@@ -85,8 +85,9 @@ const ROUTE_MAX_BYTES = 1024; |
85 | 85 | * enabled ones whose vendor the push service has a transport for, and send |
86 | 86 | * each one the notification's title and body (already in the recipient's |
87 | 87 | * 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 |
90 | 91 | * same-server path is left out, never sent; the delivery is not failed for it. |
91 | 92 | * |
92 | 93 | * **Dead tokens are retired here.** A device the vendor answers `invalid_token` |
@@ -161,7 +162,13 @@ export function createPushChannel(opts: PushChannelOptions): MessagingChannel { |
161 | 162 | const dead: string[] = []; |
162 | 163 | const failures: Array<{ error: string; retryable: boolean }> = []; |
163 | 164 | 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 | + }); |
165 | 172 | if (result.status === 'sent') { |
166 | 173 | sent.push(result.messageId ?? ''); |
167 | 174 | } else if (result.status === 'invalid_token') { |
@@ -228,6 +235,17 @@ export function deliveryData(notificationId: string | undefined, actionUrl: stri |
228 | 235 | return data; |
229 | 236 | } |
230 | 237 |
|
| 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 | + |
231 | 249 | /** Collapse whitespace runs: a push shows one plain line of text per member. */ |
232 | 250 | function plain(text: string | undefined): string { |
233 | 251 | return String(text ?? '').replace(/\s+/g, ' ').trim(); |
|
0 commit comments