fix(push): record device results under the owning user

Test devices register under live sessions, which delivery now requires.
This commit is contained in:
Juan Castro committed 2026-10-24 16:07:59 -04:00
1 parent dc027f9f0e
commit 3da39b7e35
3 files changed
+25 -6

No files matched your search

@@ -104,7 +104,21 @@ const makeApp = async (
);
};
const registerDevice = (
/** One live session per user, so devices count as active. */
const sessions = new WeakMap<PuterServer, Map<number, string>>();
const sessionOf = async (server: PuterServer, userId: number) => {
const mine = sessions.get(server) ?? new Map<number, string>();
sessions.set(server, mine);
let uid = mine.get(userId);
if (!uid) {
uid = ((await server.stores.session.create(userId)) as { uuid: string })
.uuid;
mine.set(userId, uid);
}
return uid;
};
const registerDevice = async (
server: PuterServer,
userId: number,
token: string,
@@ -112,7 +126,7 @@ const registerDevice = (
) =>
server.stores.pushDevice.register({
userId,
sessionUid: `sess-${uuidv4()}`,
sessionUid: await sessionOf(server, userId),
platform: opts.platform ?? 'web',
token,
appUid: opts.appUid ?? null,
@@ -100,7 +100,11 @@ const makePerson = async (): Promise<Person> => {
);
await env.server.stores.pushDevice.register({
userId: user.id,
sessionUid: `sess-${uuidv4()}`,
sessionUid: (
(await env.server.stores.session.create(user.id)) as {
uuid: string;
}
).uuid,
platform: 'web',
token: `tok-${uuidv4()}`,
});
+4 -3
View File
@@ -181,7 +181,7 @@ export class PushService extends PuterService {
return;
}
await this.#send(devices, message);
await this.#send(userId, devices, message);
}
/** The name the server stamped on the message, else the app row's. */
@@ -211,6 +211,7 @@ export class PushService extends PuterService {
/** One call per platform batch; each device's result is recorded once. */
async #send(
userId: number,
devices: PushDeliveryTarget[],
message: PushMessage,
): Promise<void> {
@@ -285,8 +286,8 @@ export class PushService extends PuterService {
});
}
await Promise.all([
this.stores.pushDevice.touch(delivered),
this.stores.pushDevice.recordFailure(failed),
this.stores.pushDevice.touch(userId, delivered),
this.stores.pushDevice.recordFailure(userId, failed),
this.stores.pushDevice.removeByTokenHashes(unregistered),
]);
}