Skip to content

Commit 5f310c3

Browse files
committed
feat(messaging,spec): store a push device's origin and carry it in every push
sys_user_device gains an optional, unbounded origin column. The device registry stores the registration's origin on the row (null without one, replaced by a refresh like app_id and locale) and listDevices answers it, so the push channel's existing copy of device.origin into the push data finally has a value to copy. Spec retires the two [PLANNED — not yet stored] prefixes on RegisterDeviceRequestSchema.origin and UserDeviceSchema.origin, flips the marker pin, and the push server origin docblock names where its pattern is knowingly narrower (underscore labels, trailing-dot hosts) and looser (port digits, the IPv6 arm) than a URL origin, and that IPv4 is accepted. Claude-Session: https://claude.ai/code/session_016WV4czbHcjvoBYWDw2gRAq Co-authored-by: Claude <noreply@anthropic.com>
1 parent b77c160 commit 5f310c3

11 files changed

Lines changed: 136 additions & 17 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/service-messaging': patch
3+
'@objectstack/spec': patch
4+
---
5+
6+
feat(messaging,spec): a push device's `origin` is stored at registration and every push to the device carries it back
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
- **`@objectstack/service-messaging`**: `sys_user_device` gains an optional `origin` column. `POST /api/v1/notifications/devices` stores the registration's `origin` on the device row (`null` when the registration names none, and a refresh replaces it as it does `appId` and `locale`); `listDevices` answers it; and the push channel, which already copied a device's `origin` into the push data, now finds one to copy. A mobile shell with several saved servers can therefore open a tapped push on the server that sent it. Registrations without `origin` behave exactly as before.
12+
- **`@objectstack/spec`**: `RegisterDeviceRequestSchema.origin` and `UserDeviceSchema.origin` drop their `[PLANNED — not yet stored]` prefix, since the server now stores the value. The push server origin docblock names where its pattern is knowingly narrower than a URL origin (an underscore in a label, a trailing-dot host) and looser (a written-out default port, a leading zero or an out-of-range port, any bracketed run of hex digits, colons and dots), and that a dotted IPv4 host is accepted. Description and docblock text only; nothing a caller sends is accepted or refused differently.

‎content/docs/references/api/protocol.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,7 @@ Register or refresh a push device for the signed-in user
27822782
| **vendor** | `Enum<'apns' \| 'fcm'>` | optional | Push vendor the token belongs to: apns or fcm. Absent, the server derives it from platform: ios means apns, android means fcm. web has no default vendor: a web registration names apns or fcm, and one without vendor is refused |
27832783
| **appId** | `string` | optional | App the token was issued to: the iOS bundle ID or the Android application ID |
27842784
| **locale** | `string` | optional | Device locale (BCP 47, for example zh-CN). Stored on the device row; no push renders by it: a push arrives in the recipient's language (sys_user.locale, then the deployment default) |
2785-
| **origin** | `string` | optional | [PLANNED — not yet stored] The origin the Console was loaded from (window.location.origin, for example https://crm.example.com): the address the mobile shell saved for this server. The Console writes it; the server stores it on the device row and every push to the device carries it back, so a shell with several saved servers opens the push on the one that sent it. http or https, a lowercase host and an optional port; a path, another scheme or a bare host is refused |
2785+
| **origin** | `string` | optional | The origin the Console was loaded from (window.location.origin, for example https://crm.example.com): the address the mobile shell saved for this server. The Console writes it; the server stores it on the device row and every push to the device carries it back, so a shell with several saved servers opens the push on the one that sent it. http or https, a lowercase host and an optional port; a path, another scheme or a bare host is refused |
27862786

27872787

27882788
---

‎content/docs/references/system/push-device.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ A push device of one user: one row per app install
3838
| **appId** | `string` | optional | App the token was issued to: the iOS bundle ID or the Android application ID |
3939
| **locale** | `string` | optional | Device locale (BCP 47, for example zh-CN), as the registration named it. Stored for the device; no push renders by it: a push arrives in the recipient's language (sys_user.locale, then the deployment default) |
4040
| **name** | `string` | optional | Device friendly name, as the registration named it |
41-
| **origin** | `string` | optional | [PLANNED — not yet stored] The origin the Console was loaded from, as the registration named it (for example https://crm.example.com). The device registry keeps it on the row and the push channel copies it into every push to this device, where the mobile shell picks the saved server by it. Absent on a device registered without one |
41+
| **origin** | `string` | optional | The origin the Console was loaded from, as the registration named it (for example https://crm.example.com). The device registry keeps it on the row and the push channel copies it into every push to this device, where the mobile shell picks the saved server by it. Absent on a device registered without one |
4242
| **enabled** | `boolean` | ✅ | False once the vendor reported the token dead; nothing is delivered to a disabled device |
4343
| **invalidatedAt** | `string` | optional | When the vendor reported the token dead. Present exactly while enabled is false: absent on an enabled device, and cleared by the registration that re-enables it |
4444
| **lastSeenAt** | `string` | optional | When the device last registered or refreshed. Every registration and refresh sets it, so it is absent only on a row no registration wrote; listDevices answers such a device last |

‎packages/services/service-messaging/src/device-registry.integration.test.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
* - a takeover whose write is refused leaves the previous holder's row in
2626
* place: the delete and the write are one engine transaction;
2727
* - the database refuses a second holder of a token (the unique index), and
28-
* two concurrent registrations of one token end with one row.
28+
* two concurrent registrations of one token end with one row;
29+
* - the registration's `origin` (ruling B on the push delivery decision) is
30+
* stored on the row, `null` without one, replaced by a refresh, answered
31+
* by `listDevices`, and copied into the data of a push to that device.
2932
*
3033
* Refusals assert `code` AND `status` (ADR-0112).
3134
*/
@@ -35,9 +38,11 @@ import { ObjectQL } from '@objectstack/objectql';
3538
import { SqlDriver } from '@objectstack/driver-sql';
3639
import { SysUser } from '@objectstack/platform-objects';
3740
import { validationFailureDetails } from '@objectstack/types';
41+
import { PushDeliveryDataSchema } from '@objectstack/spec/api';
3842
import { UserDevice } from './objects/user-device.object.js';
3943
import { MessagingService } from './messaging-service.js';
4044
import { USER_DEVICE_OBJECT } from './device-registry.js';
45+
import { createPushChannel } from './push-channel.js';
4146
import { installDefaultOrganization } from './default-organization.testkit.js';
4247

4348
const SYS = { context: { isSystem: true } } as any;
@@ -260,3 +265,61 @@ describe('push device registry: INotificationService device members over sys_use
260265
expect(await rows({ token: 'tok-race' })).toHaveLength(1);
261266
});
262267
});
268+
269+
describe('push device registry: the sending server (origin) is stored, read back and pushed', () => {
270+
it('stores the registration\'s origin on the row and listDevices answers it', async () => {
271+
const { service, alice, rows } = await boot();
272+
await service.registerDevice(alice, { token: 'tok-o', platform: 'ios', deviceId: 'dev-o', origin: 'https://crm.example.com' });
273+
274+
const [row] = await rows({ device_id: 'dev-o' });
275+
expect(row.origin).toBe('https://crm.example.com');
276+
const [listed] = await service.listDevices(alice);
277+
expect(listed.origin).toBe('https://crm.example.com');
278+
});
279+
280+
it('stores null for a registration without origin, and the device answers no origin member', async () => {
281+
const { service, alice, rows } = await boot();
282+
await service.registerDevice(alice, { token: 'tok-n', platform: 'android', deviceId: 'dev-n' });
283+
284+
const [row] = await rows({ device_id: 'dev-n' });
285+
expect(row.origin ?? null).toBeNull();
286+
const [listed] = await service.listDevices(alice);
287+
expect(listed).not.toHaveProperty('origin');
288+
});
289+
290+
it('a refresh replaces the stored origin with the one it names, as it does appId and locale', async () => {
291+
const { service, alice, rows } = await boot();
292+
await service.registerDevice(alice, { token: 'tok-r', platform: 'ios', deviceId: 'dev-r', origin: 'https://a.example.com' });
293+
await service.registerDevice(alice, { token: 'tok-r', platform: 'ios', deviceId: 'dev-r', origin: 'http://localhost:3000' });
294+
expect((await rows({ device_id: 'dev-r' }))[0].origin).toBe('http://localhost:3000');
295+
296+
await service.registerDevice(alice, { token: 'tok-r', platform: 'ios', deviceId: 'dev-r' });
297+
expect((await rows({ device_id: 'dev-r' }))[0].origin ?? null).toBeNull();
298+
});
299+
300+
it('a push to the device carries the stored origin in its data; a device without one gets none', async () => {
301+
const { service, alice } = await boot();
302+
await service.registerDevice(alice, { token: 'tok-s', platform: 'ios', deviceId: 'with-origin', origin: 'https://crm.example.com' });
303+
await service.registerDevice(alice, { token: 'tok-t', platform: 'android', deviceId: 'without-origin' });
304+
305+
const sent: Array<{ token: string; data: Record<string, string> }> = [];
306+
const channel = createPushChannel({
307+
getPush: () => ({
308+
vendors: () => ['apns', 'fcm'],
309+
async send(m) { sent.push({ token: m.token, data: m.data }); return { status: 'sent', messageId: m.token }; },
310+
}),
311+
devices: service,
312+
});
313+
const result = await channel.send({ logger: silent } as any, {
314+
channel: 'push',
315+
recipient: alice,
316+
notification: { notificationId: 'ntf_o', title: 'Deal won', body: 'Acme', actionUrl: '/opportunity/1' },
317+
} as any);
318+
319+
expect(result.ok).toBe(true);
320+
const byToken = Object.fromEntries(sent.map((m) => [m.token, m.data]));
321+
expect(byToken['tok-s']).toEqual({ notificationId: 'ntf_o', route: '/opportunity/1', origin: 'https://crm.example.com' });
322+
expect(byToken['tok-t']).toEqual({ notificationId: 'ntf_o', route: '/opportunity/1' });
323+
for (const m of sent) expect(PushDeliveryDataSchema.safeParse(m.data).success).toBe(true);
324+
});
325+
});

‎packages/services/service-messaging/src/device-registry.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export class DeviceRegistry {
186186
platform: request.platform,
187187
app_id: request.appId ?? null,
188188
locale: request.locale ?? null,
189+
origin: request.origin ?? null,
189190
name: request.name ?? null,
190191
enabled: true,
191192
invalidated_at: null,
@@ -296,6 +297,7 @@ function toUserDevice(row: DeviceRow): UserDevice {
296297
};
297298
if (row.app_id) device.appId = String(row.app_id);
298299
if (row.locale) device.locale = String(row.locale);
300+
if (row.origin) device.origin = String(row.origin);
299301
if (row.name) device.name = String(row.name);
300302
const invalidatedAt = iso(row.invalidated_at);
301303
if (!enabled && invalidatedAt) device.invalidatedAt = invalidatedAt;

‎packages/services/service-messaging/src/objects/user-device.object.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('sys_user_device declaration', () => {
3030
expect((UserDeviceSchema as any).shape.platform.options.slice().sort()).toEqual([...admitted].sort());
3131
});
3232

33+
it('has an optional, unbounded origin column for the origin the registration names', () => {
34+
const origin = UserDevice.fields.origin as any;
35+
expect(origin).toMatchObject({ type: 'text', required: false });
36+
// Spec's origin pattern bounds the shape, not the length: a bounded column would
37+
// refuse a value the registration accepts.
38+
expect(origin.maxLength).toBeUndefined();
39+
expect((RegisterDeviceRequestSchema as any).shape.origin).toBeDefined();
40+
expect((UserDeviceSchema as any).shape.origin).toBeDefined();
41+
});
42+
3343
it('carries the two unique indexes the registry relies on', () => {
3444
expect(UserDevice.indexes).toEqual(expect.arrayContaining([
3545
expect.objectContaining({ fields: ['user_id', 'device_id'], unique: 'global' }),

‎packages/services/service-messaging/src/objects/user-device.object.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ export const UserDevice = ObjectSchema.create({
121121
+ 'arrives in the recipient\'s language (their user locale, then the deployment default).',
122122
}),
123123

124+
origin: Field.text({
125+
label: 'Server Origin',
126+
required: false,
127+
// No maxLength: spec's origin pattern (`system/push-server-origin.ts`)
128+
// bounds the shape, not the length, so an unbounded column stores
129+
// every origin the registration accepts.
130+
description: 'The origin the Console was loaded from when it registered the device (for example '
131+
+ 'https://crm.example.com). Every push to the device carries it, so a mobile shell with several saved '
132+
+ 'servers opens the push on this one. Empty for a device registered without one.',
133+
}),
134+
124135
name: Field.text({
125136
label: 'Device Name',
126137
required: false,

‎packages/spec/src/api/protocol.zod.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,7 +3689,7 @@ export const GetPresenceResponseSchema = lazySchema(() => z.object({
36893689
* `DELETE /api/v1/notifications/devices/:deviceId`.
36903690
*
36913691
* One request does both: registering a `deviceId` the user already has
3692-
* refreshes that device (its token, vendor, app and locale) instead of adding
3692+
* refreshes that device (its token, vendor, app, locale and origin) instead of adding
36933693
* a second one. A request without `deviceId` gets one minted by the server
36943694
* and answered in {@link RegisterDeviceResponseSchema}; the client keeps it
36953695
* and sends it on every later registration and on unregister.
@@ -3718,7 +3718,7 @@ export const RegisterDeviceRequestSchema = lazySchema(() => z.object({
37183718
+ 'recipient\'s language (sys_user.locale, then the deployment default)',
37193719
),
37203720
origin: z.string().regex(PUSH_SERVER_ORIGIN_PATTERN, { message: PUSH_SERVER_ORIGIN_MESSAGE }).optional().describe(
3721-
'[PLANNED — not yet stored] The origin the Console was loaded from (window.location.origin, for example '
3721+
'The origin the Console was loaded from (window.location.origin, for example '
37223722
+ 'https://crm.example.com): the address the mobile shell saved for this server. The Console writes it; the server '
37233723
+ 'stores it on the device row and every push to the device carries it back, so a shell with several saved servers '
37243724
+ 'opens the push on the one that sent it. http or https, a lowercase host and an optional port; a path, another '

‎packages/spec/src/system/push-device.zod.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const UserDeviceSchema = lazySchema(() => z.object({
6868
),
6969
name: z.string().optional().describe('Device friendly name, as the registration named it'),
7070
origin: z.string().regex(PUSH_SERVER_ORIGIN_PATTERN, { message: PUSH_SERVER_ORIGIN_MESSAGE }).optional().describe(
71-
'[PLANNED — not yet stored] The origin the Console was loaded from, as the registration named it (for example '
71+
'The origin the Console was loaded from, as the registration named it (for example '
7272
+ 'https://crm.example.com). The device registry keeps it on the row and the push channel copies it into every push '
7373
+ 'to this device, where the mobile shell picks the saved server by it. Absent on a device registered without one',
7474
),

‎packages/spec/src/system/push-server-origin.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ describe('the three origin members are one contract', () => {
103103
expect(new Set(patterns).size).toBe(1);
104104
});
105105

106-
it('marks the device-side members PLANNED until the server stores the origin', () => {
107-
expect(RegisterDeviceRequestSchema.shape.origin.description).toMatch(/^\[PLANNED — not yet stored\] /);
108-
expect(UserDeviceSchema.shape.origin.description).toMatch(/^\[PLANNED — not yet stored\] /);
109-
// The push data is served: the push channel produces it and copies a stored origin (#29041).
106+
it('marks no member PLANNED: the server stores the origin and every push carries it back', () => {
107+
// The device registry stores the registration's origin on sys_user_device and the push
108+
// channel copies it into the push data (service-messaging), so all three members are served.
109+
expect(RegisterDeviceRequestSchema.shape.origin.description).not.toMatch(/PLANNED/);
110+
expect(UserDeviceSchema.shape.origin.description).not.toMatch(/PLANNED/);
111+
expect(PushDeliveryDataSchema.shape.origin.description).not.toMatch(/PLANNED/);
110112
expect(PushDeliveryDataSchema.description).not.toMatch(/PLANNED/);
111113
});
112114
});

0 commit comments

Comments
 (0)