@frigear-nu/push 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.d.mts +17 -2
- package/dist/module.json +1 -1
- package/dist/runtime/server/db/schema.postgresql.d.ts +35 -1
- package/dist/runtime/server/db/schema.postgresql.js +11 -2
- package/dist/runtime/server/db/schema.sqlite.d.ts +39 -1
- package/dist/runtime/server/db/schema.sqlite.js +11 -2
- package/dist/runtime/server/utils/push-devices.d.ts +23 -7
- package/dist/runtime/server/utils/push-devices.js +36 -7
- package/dist/runtime/server/utils/push-send.d.ts +8 -2
- package/dist/runtime/server/utils/push-send.js +18 -2
- package/dist/types.d.mts +1 -1
- package/package.json +4 -4
package/dist/module.d.mts
CHANGED
|
@@ -71,9 +71,20 @@ type PushRuntimeConfig = {
|
|
|
71
71
|
sandbox: boolean | string;
|
|
72
72
|
};
|
|
73
73
|
};
|
|
74
|
+
/**
|
|
75
|
+
* What owns a device that is not a user's (a shared phone): `type` names the kind, in the registering module's terms
|
|
76
|
+
* (`sms-device`), and `id` the record. `user` is not a valid `type`: a user's devices are registered by user id.
|
|
77
|
+
*/
|
|
78
|
+
type PushDeviceOwner = {
|
|
79
|
+
type: string;
|
|
80
|
+
id: string;
|
|
81
|
+
};
|
|
74
82
|
type PushDeviceDto = {
|
|
75
83
|
id: string;
|
|
76
|
-
|
|
84
|
+
/** The user the device belongs to; `null` for a device of another owner (`owner`). */
|
|
85
|
+
userId: number | null;
|
|
86
|
+
/** The owner of a device that is not a user's; `null` for a user's device. */
|
|
87
|
+
owner: PushDeviceOwner | null;
|
|
77
88
|
platform: PushPlatform;
|
|
78
89
|
/** Where its pushes go. `null` only for a device registered before providers were stored: `push.ios` decides. */
|
|
79
90
|
provider: PushProvider | null;
|
|
@@ -115,6 +126,10 @@ type PushMessage = {
|
|
|
115
126
|
/** Extra string data delivered with the notification. */
|
|
116
127
|
data?: Record<string, string>;
|
|
117
128
|
};
|
|
129
|
+
/** {@link PushMessage} for {@link PushDeviceOwner}s instead of users (`sendPushToOwners`). */
|
|
130
|
+
type PushOwnerMessage = Omit<PushMessage, 'userIds'> & {
|
|
131
|
+
owners: PushDeviceOwner[];
|
|
132
|
+
};
|
|
118
133
|
type SendPushResult = {
|
|
119
134
|
/** Devices the provider accepted. */
|
|
120
135
|
sent: number;
|
|
@@ -148,4 +163,4 @@ declare module 'nitropack' {
|
|
|
148
163
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
149
164
|
|
|
150
165
|
export { _default as default };
|
|
151
|
-
export type { ModuleOptions, PushDeviceDto, PushDeviceRegisteredContext, PushMessage, PushPlatform, PushProvider, PushPublicConfig, PushRuntimeConfig, RegisterDeviceInput, SendPushResult };
|
|
166
|
+
export type { ModuleOptions, PushDeviceDto, PushDeviceOwner, PushDeviceRegisteredContext, PushMessage, PushOwnerMessage, PushPlatform, PushProvider, PushPublicConfig, PushRuntimeConfig, RegisterDeviceInput, SendPushResult };
|
package/dist/module.json
CHANGED
|
@@ -26,7 +26,7 @@ export declare const pushDevices: import("drizzle-orm/pg-core").PgTableWithColum
|
|
|
26
26
|
columnType: "PgInteger";
|
|
27
27
|
data: number;
|
|
28
28
|
driverParam: string | number;
|
|
29
|
-
notNull:
|
|
29
|
+
notNull: false;
|
|
30
30
|
hasDefault: false;
|
|
31
31
|
isPrimaryKey: false;
|
|
32
32
|
isAutoincrement: false;
|
|
@@ -36,6 +36,40 @@ export declare const pushDevices: import("drizzle-orm/pg-core").PgTableWithColum
|
|
|
36
36
|
identity: undefined;
|
|
37
37
|
generated: undefined;
|
|
38
38
|
}, {}, {}>;
|
|
39
|
+
ownerType: import("drizzle-orm/pg-core").PgColumn<{
|
|
40
|
+
name: "ownerType";
|
|
41
|
+
tableName: "push_devices";
|
|
42
|
+
dataType: "string";
|
|
43
|
+
columnType: "PgText";
|
|
44
|
+
data: string;
|
|
45
|
+
driverParam: string;
|
|
46
|
+
notNull: false;
|
|
47
|
+
hasDefault: false;
|
|
48
|
+
isPrimaryKey: false;
|
|
49
|
+
isAutoincrement: false;
|
|
50
|
+
hasRuntimeDefault: false;
|
|
51
|
+
enumValues: [string, ...string[]];
|
|
52
|
+
baseColumn: never;
|
|
53
|
+
identity: undefined;
|
|
54
|
+
generated: undefined;
|
|
55
|
+
}, {}, {}>;
|
|
56
|
+
ownerId: import("drizzle-orm/pg-core").PgColumn<{
|
|
57
|
+
name: "ownerId";
|
|
58
|
+
tableName: "push_devices";
|
|
59
|
+
dataType: "string";
|
|
60
|
+
columnType: "PgText";
|
|
61
|
+
data: string;
|
|
62
|
+
driverParam: string;
|
|
63
|
+
notNull: false;
|
|
64
|
+
hasDefault: false;
|
|
65
|
+
isPrimaryKey: false;
|
|
66
|
+
isAutoincrement: false;
|
|
67
|
+
hasRuntimeDefault: false;
|
|
68
|
+
enumValues: [string, ...string[]];
|
|
69
|
+
baseColumn: never;
|
|
70
|
+
identity: undefined;
|
|
71
|
+
generated: undefined;
|
|
72
|
+
}, {}, {}>;
|
|
39
73
|
platform: import("drizzle-orm/pg-core").PgColumn<{
|
|
40
74
|
name: "platform";
|
|
41
75
|
tableName: "push_devices";
|
|
@@ -2,7 +2,15 @@ import { pgTable, text, integer, timestamp, index } from "drizzle-orm/pg-core";
|
|
|
2
2
|
export const pushDevices = pgTable("push_devices", {
|
|
3
3
|
/** `psd_` + random id. */
|
|
4
4
|
id: text().primaryKey(),
|
|
5
|
-
|
|
5
|
+
/** The user the device belongs to; `null` for a device of another owner (`ownerType`). */
|
|
6
|
+
userId: integer(),
|
|
7
|
+
/**
|
|
8
|
+
* What owns a device that is not a user's, named by the module that registers it (`sms-device`); `null` for a user's
|
|
9
|
+
* device. Never `user`.
|
|
10
|
+
*/
|
|
11
|
+
ownerType: text(),
|
|
12
|
+
/** The owner's id, in the owning module's terms; set exactly when `ownerType` is. */
|
|
13
|
+
ownerId: text(),
|
|
6
14
|
/** `ios | android | web`. */
|
|
7
15
|
platform: text().notNull(),
|
|
8
16
|
/** The FCM registration token (android, and ios through FCM) or the APNs device token (ios through APNs). */
|
|
@@ -22,5 +30,6 @@ export const pushDevices = pgTable("push_devices", {
|
|
|
22
30
|
/** Pushes the provider refused for this device in a row (0 after a delivered one): revoked at the limit. */
|
|
23
31
|
failures: integer().notNull().default(0)
|
|
24
32
|
}, (t) => [
|
|
25
|
-
index("push_device_user_idx").on(t.userId)
|
|
33
|
+
index("push_device_user_idx").on(t.userId),
|
|
34
|
+
index("push_device_owner_idx").on(t.ownerType, t.ownerId)
|
|
26
35
|
]);
|
|
@@ -28,7 +28,7 @@ export declare const pushDevices: import("drizzle-orm/sqlite-core").SQLiteTableW
|
|
|
28
28
|
columnType: "SQLiteInteger";
|
|
29
29
|
data: number;
|
|
30
30
|
driverParam: number;
|
|
31
|
-
notNull:
|
|
31
|
+
notNull: false;
|
|
32
32
|
hasDefault: false;
|
|
33
33
|
isPrimaryKey: false;
|
|
34
34
|
isAutoincrement: false;
|
|
@@ -38,6 +38,44 @@ export declare const pushDevices: import("drizzle-orm/sqlite-core").SQLiteTableW
|
|
|
38
38
|
identity: undefined;
|
|
39
39
|
generated: undefined;
|
|
40
40
|
}, {}, {}>;
|
|
41
|
+
ownerType: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
42
|
+
name: "ownerType";
|
|
43
|
+
tableName: "push_devices";
|
|
44
|
+
dataType: "string";
|
|
45
|
+
columnType: "SQLiteText";
|
|
46
|
+
data: string;
|
|
47
|
+
driverParam: string;
|
|
48
|
+
notNull: false;
|
|
49
|
+
hasDefault: false;
|
|
50
|
+
isPrimaryKey: false;
|
|
51
|
+
isAutoincrement: false;
|
|
52
|
+
hasRuntimeDefault: false;
|
|
53
|
+
enumValues: [string, ...string[]];
|
|
54
|
+
baseColumn: never;
|
|
55
|
+
identity: undefined;
|
|
56
|
+
generated: undefined;
|
|
57
|
+
}, {}, {
|
|
58
|
+
length: undefined;
|
|
59
|
+
}>;
|
|
60
|
+
ownerId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
61
|
+
name: "ownerId";
|
|
62
|
+
tableName: "push_devices";
|
|
63
|
+
dataType: "string";
|
|
64
|
+
columnType: "SQLiteText";
|
|
65
|
+
data: string;
|
|
66
|
+
driverParam: string;
|
|
67
|
+
notNull: false;
|
|
68
|
+
hasDefault: false;
|
|
69
|
+
isPrimaryKey: false;
|
|
70
|
+
isAutoincrement: false;
|
|
71
|
+
hasRuntimeDefault: false;
|
|
72
|
+
enumValues: [string, ...string[]];
|
|
73
|
+
baseColumn: never;
|
|
74
|
+
identity: undefined;
|
|
75
|
+
generated: undefined;
|
|
76
|
+
}, {}, {
|
|
77
|
+
length: undefined;
|
|
78
|
+
}>;
|
|
41
79
|
platform: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
42
80
|
name: "platform";
|
|
43
81
|
tableName: "push_devices";
|
|
@@ -2,7 +2,15 @@ import { sqliteTable, text, integer, index } from "drizzle-orm/sqlite-core";
|
|
|
2
2
|
export const pushDevices = sqliteTable("push_devices", {
|
|
3
3
|
/** `psd_` + random id. */
|
|
4
4
|
id: text().primaryKey(),
|
|
5
|
-
|
|
5
|
+
/** The user the device belongs to; `null` for a device of another owner (`ownerType`). */
|
|
6
|
+
userId: integer(),
|
|
7
|
+
/**
|
|
8
|
+
* What owns a device that is not a user's, named by the module that registers it (`sms-device`); `null` for a user's
|
|
9
|
+
* device. Never `user`.
|
|
10
|
+
*/
|
|
11
|
+
ownerType: text(),
|
|
12
|
+
/** The owner's id, in the owning module's terms; set exactly when `ownerType` is. */
|
|
13
|
+
ownerId: text(),
|
|
6
14
|
/** `ios | android | web`. */
|
|
7
15
|
platform: text().notNull(),
|
|
8
16
|
/** The FCM registration token (android, and ios through FCM) or the APNs device token (ios through APNs). */
|
|
@@ -22,5 +30,6 @@ export const pushDevices = sqliteTable("push_devices", {
|
|
|
22
30
|
/** Pushes the provider refused for this device in a row (0 after a delivered one): revoked at the limit. */
|
|
23
31
|
failures: integer().notNull().default(0)
|
|
24
32
|
}, (t) => [
|
|
25
|
-
index("push_device_user_idx").on(t.userId)
|
|
33
|
+
index("push_device_user_idx").on(t.userId),
|
|
34
|
+
index("push_device_owner_idx").on(t.ownerType, t.ownerId)
|
|
26
35
|
]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { schema } from '@nuxthub/db';
|
|
2
2
|
import type { H3Event } from 'h3';
|
|
3
|
-
import type { PushDeviceDto, RegisterDeviceInput } from '../../../types.js';
|
|
3
|
+
import type { PushDeviceDto, PushDeviceOwner, RegisterDeviceInput } from '../../../types.js';
|
|
4
4
|
type DeviceRow = typeof schema.pushDevices.$inferSelect;
|
|
5
5
|
export declare function toPushDeviceDto(row: DeviceRow): PushDeviceDto;
|
|
6
6
|
/** SHA-256 (hex) of an installation secret: the only form it is stored in. */
|
|
@@ -8,14 +8,30 @@ export declare const hashPushInstallationSecret: (secret: string) => Promise<str
|
|
|
8
8
|
/**
|
|
9
9
|
* Registers a device for `userId`. Registering is idempotent: the same user registering the same token again (with or
|
|
10
10
|
* without the installation secret) keeps the row and makes it active again. The row is bound to the installation by
|
|
11
|
-
* `input.secret` (stored as a hash): a token that is registered to another user moves to this one only when
|
|
12
|
-
* carries the same secret (the phone changed hands); otherwise it answers 409 `device_owned` and nothing
|
|
13
|
-
* provider is stored resolved (what the device said, else FCM on Android and `push.ios` on iOS), and
|
|
14
|
-
* updates it and clears `revokedAt` and the failure count. `input.replaces` is the token the
|
|
15
|
-
* before (its APNs token, now that it registers its FCM token): the caller's device with it is
|
|
16
|
-
* to the same secret (or to none), so the installation is not notified twice. Fires
|
|
11
|
+
* `input.secret` (stored as a hash): a token that is registered to another user (or owner) moves to this one only when
|
|
12
|
+
* the request carries the same secret (the phone changed hands); otherwise it answers 409 `device_owned` and nothing
|
|
13
|
+
* changes. The provider is stored resolved (what the device said, else FCM on Android and `push.ios` on iOS), and
|
|
14
|
+
* registering again updates it and clears `revokedAt` and the failure count. `input.replaces` is the token the
|
|
15
|
+
* installation registered before (its APNs token, now that it registers its FCM token): the caller's device with it is
|
|
16
|
+
* removed when it is bound to the same secret (or to none), so the installation is not notified twice. Fires
|
|
17
|
+
* `push:device:registered`.
|
|
17
18
|
*/
|
|
18
19
|
export declare function registerPushDevice(event: H3Event, userId: number, input: RegisterDeviceInput): Promise<PushDeviceDto>;
|
|
20
|
+
/**
|
|
21
|
+
* Registers a device that belongs to `owner` instead of a user (a shared phone another module pairs, owner type
|
|
22
|
+
* `sms-device`), with the rules of {@link registerPushDevice}: idempotent per owner, bound to `input.secret`, moved
|
|
23
|
+
* from another owner or user only with that secret (else 409 `device_owned`), `input.replaces` removed, and
|
|
24
|
+
* `push:device:registered` fired (`device.userId` is `null`, `device.owner` is set). The calling module checks that
|
|
25
|
+
* the caller may act for `owner`: push has no routes for these devices. Throws for an owner type `user` or an empty one.
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerPushDeviceForOwner(event: H3Event, owner: PushDeviceOwner, input: RegisterDeviceInput): Promise<PushDeviceDto>;
|
|
28
|
+
/** The owner's active devices, newest first. */
|
|
29
|
+
export declare function listPushDevicesOfOwner(owner: PushDeviceOwner): Promise<PushDeviceDto[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Removes the owner's devices (its record was deleted or revoked: there is no foreign key, so nothing cascades). With
|
|
32
|
+
* `id`, only that device of the owner.
|
|
33
|
+
*/
|
|
34
|
+
export declare function removePushDevicesOfOwner(owner: PushDeviceOwner, id?: string): Promise<void>;
|
|
19
35
|
/** The user's active devices, newest first. */
|
|
20
36
|
export declare function listPushDevices(userId: number): Promise<PushDeviceDto[]>;
|
|
21
37
|
/** Every device of the user, revoked ones included, newest first (the staff view). */
|
|
@@ -8,6 +8,7 @@ export function toPushDeviceDto(row) {
|
|
|
8
8
|
return {
|
|
9
9
|
id: row.id,
|
|
10
10
|
userId: row.userId,
|
|
11
|
+
owner: row.ownerType && row.ownerId ? { type: row.ownerType, id: row.ownerId } : null,
|
|
11
12
|
platform: row.platform,
|
|
12
13
|
provider: isPushProvider(row.provider) ? row.provider : null,
|
|
13
14
|
appId: row.appId,
|
|
@@ -26,15 +27,26 @@ function iosProviderOf(config) {
|
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
export const hashPushInstallationSecret = (secret) => sha256Hex(secret);
|
|
29
|
-
|
|
30
|
+
const userHolder = (userId) => ({ userId, ownerType: null, ownerId: null });
|
|
31
|
+
function ownerHolder(owner) {
|
|
32
|
+
if (!owner.type || !owner.id || owner.type === "user") {
|
|
33
|
+
throw new Error(`[@frigear-nu/push] Invalid push device owner ${JSON.stringify(owner)}: it needs an id and a type other than "user".`);
|
|
34
|
+
}
|
|
35
|
+
return { userId: null, ownerType: owner.type, ownerId: owner.id };
|
|
36
|
+
}
|
|
37
|
+
function heldBy(holder) {
|
|
38
|
+
const table = schema.pushDevices;
|
|
39
|
+
return holder.userId !== null ? eq(table.userId, holder.userId) : and(eq(table.ownerType, holder.ownerType), eq(table.ownerId, holder.ownerId));
|
|
40
|
+
}
|
|
41
|
+
async function upsertPushDevice(event, holder, input) {
|
|
30
42
|
const now = /* @__PURE__ */ new Date();
|
|
31
43
|
const provider = resolveDeviceProvider(input, iosProviderOf(useRuntimeConfig().push)) ?? "fcm";
|
|
32
44
|
const secretHash = input.secret ? await hashPushInstallationSecret(input.secret) : null;
|
|
33
45
|
const table = schema.pushDevices;
|
|
34
|
-
const owned = secretHash ? or(
|
|
46
|
+
const owned = secretHash ? or(heldBy(holder), eq(table.secretHash, secretHash)) : heldBy(holder);
|
|
35
47
|
const [row] = await db.insert(table).values({
|
|
36
48
|
id: newDeviceId(),
|
|
37
|
-
|
|
49
|
+
...holder,
|
|
38
50
|
platform: input.platform,
|
|
39
51
|
token: input.token,
|
|
40
52
|
provider,
|
|
@@ -47,7 +59,7 @@ export async function registerPushDevice(event, userId, input) {
|
|
|
47
59
|
}).onConflictDoUpdate({
|
|
48
60
|
target: table.token,
|
|
49
61
|
set: {
|
|
50
|
-
|
|
62
|
+
...holder,
|
|
51
63
|
platform: input.platform,
|
|
52
64
|
provider,
|
|
53
65
|
appId: input.appId ?? null,
|
|
@@ -62,12 +74,26 @@ export async function registerPushDevice(event, userId, input) {
|
|
|
62
74
|
}).returning();
|
|
63
75
|
if (!row) throw apiError(409, "device_owned", "This device is registered to another account.");
|
|
64
76
|
if (input.replaces && input.replaces !== input.token && input.secret) {
|
|
65
|
-
await
|
|
77
|
+
await removeHeldDeviceByToken(holder, input.replaces, input.secret);
|
|
66
78
|
}
|
|
67
79
|
const device = toPushDeviceDto(row);
|
|
68
80
|
await useNitroApp().hooks.callHook("push:device:registered", { event, device });
|
|
69
81
|
return device;
|
|
70
82
|
}
|
|
83
|
+
export function registerPushDevice(event, userId, input) {
|
|
84
|
+
return upsertPushDevice(event, userHolder(userId), input);
|
|
85
|
+
}
|
|
86
|
+
export function registerPushDeviceForOwner(event, owner, input) {
|
|
87
|
+
return upsertPushDevice(event, ownerHolder(owner), input);
|
|
88
|
+
}
|
|
89
|
+
export async function listPushDevicesOfOwner(owner) {
|
|
90
|
+
const rows = await db.select().from(schema.pushDevices).where(and(heldBy(ownerHolder(owner)), isNull(schema.pushDevices.revokedAt))).orderBy(desc(schema.pushDevices.createdAt), desc(schema.pushDevices.lastSeenAt));
|
|
91
|
+
return rows.map(toPushDeviceDto);
|
|
92
|
+
}
|
|
93
|
+
export async function removePushDevicesOfOwner(owner, id) {
|
|
94
|
+
const held = heldBy(ownerHolder(owner));
|
|
95
|
+
await db.delete(schema.pushDevices).where(id ? and(held, eq(schema.pushDevices.id, id)) : held);
|
|
96
|
+
}
|
|
71
97
|
export async function listPushDevices(userId) {
|
|
72
98
|
const rows = await db.select().from(schema.pushDevices).where(and(eq(schema.pushDevices.userId, userId), isNull(schema.pushDevices.revokedAt))).orderBy(desc(schema.pushDevices.createdAt), desc(schema.pushDevices.lastSeenAt));
|
|
73
99
|
return rows.map(toPushDeviceDto);
|
|
@@ -83,8 +109,11 @@ export async function findOwnPushDevice(userId, id) {
|
|
|
83
109
|
export async function removePushDevice(userId, id) {
|
|
84
110
|
await db.delete(schema.pushDevices).where(and(eq(schema.pushDevices.id, id), eq(schema.pushDevices.userId, userId)));
|
|
85
111
|
}
|
|
86
|
-
export
|
|
112
|
+
export function removePushDeviceByToken(userId, token, secret) {
|
|
113
|
+
return removeHeldDeviceByToken(userHolder(userId), token, secret);
|
|
114
|
+
}
|
|
115
|
+
async function removeHeldDeviceByToken(holder, token, secret) {
|
|
87
116
|
const table = schema.pushDevices;
|
|
88
117
|
const secretHash = await hashPushInstallationSecret(secret);
|
|
89
|
-
await db.delete(table).where(and(eq(table.token, token),
|
|
118
|
+
await db.delete(table).where(and(eq(table.token, token), heldBy(holder), or(isNull(table.secretHash), eq(table.secretHash, secretHash))));
|
|
90
119
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { H3Event } from 'h3';
|
|
2
|
-
import type { PushMessage, SendPushResult } from '../../../types.js';
|
|
2
|
+
import type { PushMessage, PushOwnerMessage, SendPushResult } from '../../../types.js';
|
|
3
3
|
import type { Http2Transport } from '../../providers/types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Replaces the transport APNs requests go through (`node:http2` by default). For a relay on a runtime without an HTTP/2
|
|
@@ -16,12 +16,18 @@ export declare function setPushHttp2Transport(transport: Http2Transport | undefi
|
|
|
16
16
|
* `title` and `body` can be one text per locale: each device gets the text for the locale it registered with.
|
|
17
17
|
*
|
|
18
18
|
* With the `log` delivery (the default until credentials are configured, see `push.settings.delivery`) the push is only
|
|
19
|
-
* written to the server log (title and
|
|
19
|
+
* written to the server log (title and owner, never the body) and counted as sent.
|
|
20
20
|
*
|
|
21
21
|
* Feature code sends push through notify's `dispatchPushNotification`: this module is notify's push driver
|
|
22
22
|
* (`runtime/server/notify-driver.ts`), which calls this. `sendPush` stays for push's own routes.
|
|
23
23
|
*/
|
|
24
24
|
export declare function sendPush(_event: H3Event | undefined, message: PushMessage): Promise<SendPushResult>;
|
|
25
|
+
/**
|
|
26
|
+
* {@link sendPush} for devices that belong to an owner other than a user (`registerPushDeviceForOwner`): every active
|
|
27
|
+
* device of `message.owners`, with the same delivery, locale, revocation and failure rules. For the module that owns
|
|
28
|
+
* the devices (a shared phone's outbox wake-up); notify's dispatcher only targets users.
|
|
29
|
+
*/
|
|
30
|
+
export declare function sendPushToOwners(_event: H3Event | undefined, message: PushOwnerMessage): Promise<SendPushResult>;
|
|
25
31
|
/** Consecutive refused pushes after which a device is revoked (`push_devices.failures`). */
|
|
26
32
|
export declare const PUSH_DEVICE_FAILURE_LIMIT = 3;
|
|
27
33
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { db, schema } from "@nuxthub/db";
|
|
2
|
-
import { and, gt, gte, inArray, isNull, sql } from "drizzle-orm";
|
|
2
|
+
import { and, eq, gt, gte, inArray, isNull, sql } from "drizzle-orm";
|
|
3
3
|
import { chunkParams } from "@frigear-nu/module-kit/server";
|
|
4
4
|
import { createProviderSenders, resolveIosProvider, resolvePushDelivery } from "../../providers/config.js";
|
|
5
5
|
import { deliverToDevices } from "../../providers/deliver.js";
|
|
@@ -28,6 +28,22 @@ export async function sendPush(_event, message) {
|
|
|
28
28
|
const userIds = [...new Set(message.userIds)];
|
|
29
29
|
if (!userIds.length) return { sent: 0, failed: 0, revoked: 0 };
|
|
30
30
|
const devices = (await Promise.all(chunkParams(userIds).map((ids) => db.select().from(schema.pushDevices).where(and(inArray(schema.pushDevices.userId, ids), isNull(schema.pushDevices.revokedAt)))))).flat();
|
|
31
|
+
return deliverPush(devices, message);
|
|
32
|
+
}
|
|
33
|
+
export async function sendPushToOwners(_event, message) {
|
|
34
|
+
const table = schema.pushDevices;
|
|
35
|
+
const idsByType = /* @__PURE__ */ new Map();
|
|
36
|
+
for (const owner of message.owners) {
|
|
37
|
+
if (!owner.type || !owner.id || owner.type === "user") continue;
|
|
38
|
+
const ids = idsByType.get(owner.type) ?? /* @__PURE__ */ new Set();
|
|
39
|
+
idsByType.set(owner.type, ids.add(owner.id));
|
|
40
|
+
}
|
|
41
|
+
const queries = [...idsByType].flatMap(([type, ids]) => chunkParams([...ids]).map((chunk) => db.select().from(table).where(and(eq(table.ownerType, type), inArray(table.ownerId, chunk), isNull(table.revokedAt)))));
|
|
42
|
+
const devices = (await Promise.all(queries)).flat();
|
|
43
|
+
return deliverPush(devices, message);
|
|
44
|
+
}
|
|
45
|
+
const describeOwner = (device) => device.userId !== null ? `user ${device.userId}` : `${device.ownerType} ${device.ownerId}`;
|
|
46
|
+
async function deliverPush(devices, message) {
|
|
31
47
|
if (!devices.length) return { sent: 0, failed: 0, revoked: 0 };
|
|
32
48
|
const text = (value, locale) => readTranslated(value, locale ?? "en");
|
|
33
49
|
const config = pushConfig();
|
|
@@ -46,7 +62,7 @@ export async function sendPush(_event, message) {
|
|
|
46
62
|
console.warn("[@frigear-nu/push] The log delivery is active in production: pushes are only logged, not sent. Set the FCM / APNs credentials (NUXT_PUSH_*) or `push.settings.delivery`.");
|
|
47
63
|
}
|
|
48
64
|
for (const device of devices) {
|
|
49
|
-
console.info(`[@frigear-nu/push] (log delivery) push "${text(message.title, device.locale)}" to
|
|
65
|
+
console.info(`[@frigear-nu/push] (log delivery) push "${text(message.title, device.locale)}" to ${describeOwner(device)}, ${device.platform} device ${device.id}`);
|
|
50
66
|
}
|
|
51
67
|
return { sent: devices.length, failed: 0, revoked: 0 };
|
|
52
68
|
}
|
package/dist/types.d.mts
CHANGED
|
@@ -2,4 +2,4 @@ export { type Translated } from '@frigear-nu/notify/types'
|
|
|
2
2
|
|
|
3
3
|
export { default } from './module.mjs'
|
|
4
4
|
|
|
5
|
-
export { type ModuleOptions, type PushDeviceDto, type PushDeviceRegisteredContext, type PushMessage, type PushPlatform, type PushProvider, type PushPublicConfig, type PushRuntimeConfig, type RegisterDeviceInput, type SendPushResult } from './module.mjs'
|
|
5
|
+
export { type ModuleOptions, type PushDeviceDto, type PushDeviceOwner, type PushDeviceRegisteredContext, type PushMessage, type PushOwnerMessage, type PushPlatform, type PushProvider, type PushPublicConfig, type PushRuntimeConfig, type RegisterDeviceInput, type SendPushResult } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigear-nu/push",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Push notifications for the frigear ecosystem: device registration, FCM and APNs delivery (iOS through FCM too), as the push driver of @frigear-nu/notify. Runs in server and client mode.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"frigear-module",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"playground"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@frigear-nu/auth": "^0.6.
|
|
42
|
+
"@frigear-nu/auth": "^0.6.10",
|
|
43
43
|
"@frigear-nu/module-kit": "^1.3.13",
|
|
44
|
-
"@frigear-nu/notify": "^0.3.
|
|
44
|
+
"@frigear-nu/notify": "^0.3.3",
|
|
45
45
|
"@frigear-nu/ui": "^0.2.12",
|
|
46
46
|
"@frigear-nu/users": "^3.1.6",
|
|
47
47
|
"@libsql/client": "^0.18.0",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"@capacitor/push-notifications": ">=8.0.0",
|
|
58
58
|
"@frigear-nu/admin": ">=0.0.0",
|
|
59
59
|
"@frigear-nu/menu": "^0.4.2",
|
|
60
|
-
"@frigear-nu/mobile": "^0.6.
|
|
60
|
+
"@frigear-nu/mobile": "^0.6.6",
|
|
61
61
|
"@frigear-nu/widget": "^0.4.4",
|
|
62
62
|
"@nuxt/ui": "^4.12.0",
|
|
63
63
|
"@nuxthub/core": "^0.10.12"
|