@jskit-ai/push-core 0.1.1 → 0.1.3

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - Quiet hours with one end are refused with the empty end in `details.fieldErrors` (`quietFrom` or `quietTo`, codes `push.settings.quietFromRequired` and `push.settings.quietToRequired`), so the field shows it. The refusals of a browser subscription, an unknown push service and an unknown time zone now say what the person can do.
6
+ - The manifest's `jskit.capabilities.requires` lists `runtime.env` and `runtime.logger`, which `PushFeature` requires; without them an app's server stopped at start-up.
5
7
  - A deleted account's devices and settings are forgotten on `account.deleting`.
6
8
  - `push.updateLiveActivity({ token, environment, event, data, ... })` updates or ends an iOS Live Activity through APNs' `liveactivity` push type; logged outside production.
7
9
  - New package: the `push.core` capability (`PushFeature`). A registry of people's devices (`push_devices`: web push subscriptions, APNs and FCM tokens), each person's notification settings (`push_settings`), and `push.send()`, which reaches every device of the people it names through web push (VAPID, `web-push` 4.0.0), APNs (HTTP/2 with a `.p8` key) or FCM HTTP v1 (a service account), honours each person's settings, forgets gone devices and logs instead of sending outside `APP_ENV=production`. Routes for the signed-in person's devices and settings under `/api/push`. Run `npm run db:migrate` for its two tables.
package/locales/en.json CHANGED
@@ -2,13 +2,15 @@
2
2
  "jskit": {
3
3
  "push": {
4
4
  "device": {
5
- "endpointRefused": "This browser's push service is not one the server sends to.",
6
- "keysRequired": "A browser's subscription needs its keys.",
7
- "taken": "This browser's subscription belongs to another account."
5
+ "endpointRefused": "This browser's notification service isn't one this app sends to. Try another browser.",
6
+ "keysRequired": "This browser's notification subscription is incomplete. Turn notifications off and on again.",
7
+ "taken": "Another account gets notifications in this browser. Sign in as that account and turn them off here, then try again."
8
8
  },
9
9
  "settings": {
10
10
  "quietHoursIncomplete": "Quiet hours need both a start and an end.",
11
- "timeZoneInvalid": "This time zone is not one the server knows."
11
+ "quietFromRequired": "Choose when quiet hours start.",
12
+ "quietToRequired": "Choose when quiet hours end.",
13
+ "timeZoneInvalid": "This device's time zone isn't one the server knows, so quiet hours can't be saved. Leave quiet hours empty, or change the device's time zone."
12
14
  }
13
15
  }
14
16
  }
@@ -0,0 +1,22 @@
1
+ // Knex sorts this file just before push_devices_initial.cjs ("-" sorts before "."), so it runs first on every
2
+ // database that has not run it. It runs that released migration with each table it creates in the collation of
3
+ // users, or of the next table listed that exists, instead of the database's default, and repairs a table that
4
+ // default left: see migrateInExistingCollation. The released migration then finds its tables in place.
5
+ const released = require("./push_devices_initial.cjs");
6
+
7
+ exports.up = async function up(knex) {
8
+ const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
9
+ await migrateInExistingCollation(knex, {
10
+ migration: "push_devices_initial.cjs",
11
+ up: released.up,
12
+ tables: ["push_devices"],
13
+ referenceTables: ["users", "auth_local_users"],
14
+ foreignKeys: [
15
+ { tableName: "push_devices", columnName: "user_id", parentTable: "users",
16
+ name: "fk_push_devices_user", onDelete: "CASCADE" }
17
+ ]
18
+ });
19
+ };
20
+
21
+ // The released migration's down drops its tables.
22
+ exports.down = async function down() {};
@@ -0,0 +1,22 @@
1
+ // Knex sorts this file just before push_settings_initial.cjs ("-" sorts before "."), so it runs first on every
2
+ // database that has not run it. It runs that released migration with each table it creates in the collation of
3
+ // users, or of the next table listed that exists, instead of the database's default, and repairs a table that
4
+ // default left: see migrateInExistingCollation. The released migration then finds its tables in place.
5
+ const released = require("./push_settings_initial.cjs");
6
+
7
+ exports.up = async function up(knex) {
8
+ const { migrateInExistingCollation } = await import("@jskit-ai/database-runtime/server/tableCollationMigrations");
9
+ await migrateInExistingCollation(knex, {
10
+ migration: "push_settings_initial.cjs",
11
+ up: released.up,
12
+ tables: ["push_settings"],
13
+ referenceTables: ["users", "auth_local_users"],
14
+ foreignKeys: [
15
+ { tableName: "push_settings", columnName: "user_id", parentTable: "users",
16
+ name: "fk_push_settings_user", onDelete: "CASCADE" }
17
+ ]
18
+ });
19
+ };
20
+
21
+ // The released migration's down drops its tables.
22
+ exports.down = async function down() {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/push-core",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -20,6 +20,8 @@
20
20
  "push.core"
21
21
  ],
22
22
  "requires": [
23
+ "runtime.env",
24
+ "runtime.logger",
23
25
  "runtime.resources"
24
26
  ]
25
27
  },
@@ -84,14 +86,14 @@
84
86
  }
85
87
  },
86
88
  "dependencies": {
87
- "@jskit-ai/resource-core": "0.1.232",
89
+ "@jskit-ai/resource-core": "0.1.234",
88
90
  "json-rest-schema": "^1.0.20",
89
91
  "web-push": "4.0.0"
90
92
  },
91
93
  "peerDependencies": {
92
- "@jskit-ai/database-runtime": "0.1.299",
93
- "@jskit-ai/http-runtime": "0.2.43",
94
- "@jskit-ai/kernel": "0.2.41",
95
- "@jskit-ai/resource-runtime": "0.2.66"
94
+ "@jskit-ai/database-runtime": "0.1.301",
95
+ "@jskit-ai/http-runtime": "0.2.45",
96
+ "@jskit-ai/kernel": "0.2.43",
97
+ "@jskit-ai/resource-runtime": "0.2.68"
96
98
  }
97
99
  }
@@ -54,10 +54,10 @@ function createPush({ devices, settings, senders, vapidPublicKey, environment, l
54
54
  const userId = resolveActorId(options.context);
55
55
  if (input.kind === "webpush") {
56
56
  if (!isPushServiceEndpoint(input.token)) {
57
- throw new AppError(400, "This browser's push service is not one the server sends to.", { code: "push.device.endpointRefused" });
57
+ throw new AppError(400, "This browser's notification service isn't one this app sends to. Try another browser.", { code: "push.device.endpointRefused" });
58
58
  }
59
59
  if (!input.p256dh || !input.auth) {
60
- throw new AppError(400, "A browser's subscription needs its keys.", { code: "push.device.keysRequired" });
60
+ throw new AppError(400, "This browser's notification subscription is incomplete. Turn notifications off and on again.", { code: "push.device.keysRequired" });
61
61
  }
62
62
  }
63
63
  const seenAt = new Date(now()).toISOString();
@@ -74,7 +74,7 @@ function createPush({ devices, settings, senders, vapidPublicKey, environment, l
74
74
  if (existing) {
75
75
  const anotherPerson = String(existing.userId) !== String(userId);
76
76
  if (anotherPerson && input.kind === "webpush" && (existing.p256dh !== input.p256dh || existing.auth !== input.auth)) {
77
- throw new AppError(409, "This browser's subscription belongs to another account.", { code: "push.device.taken" });
77
+ throw new AppError(409, "Another account gets notifications in this browser. Sign in as that account and turn them off here, then try again.", { code: "push.device.taken" });
78
78
  }
79
79
  return deviceOf(await devices.update(existing.id, anotherPerson ? { ...fields, userId } : fields, systemOptions));
80
80
  }
@@ -98,10 +98,14 @@ function createPush({ devices, settings, senders, vapidPublicKey, environment, l
98
98
  /** @param {import("./types.js").PushSettingsInput} input */
99
99
  async function updateSettings(input, options) {
100
100
  if (Boolean(input.quietFrom) !== Boolean(input.quietTo)) {
101
- throw new AppError(400, "Quiet hours need both a start and an end.", { code: "push.settings.quietHoursIncomplete" });
101
+ // The refusal names the empty end, so the person sees it beside that field.
102
+ const details = input.quietFrom
103
+ ? { fieldErrors: { quietTo: "Choose when quiet hours end." }, fieldErrorCodes: { quietTo: { code: "push.settings.quietToRequired", params: {} } } }
104
+ : { fieldErrors: { quietFrom: "Choose when quiet hours start." }, fieldErrorCodes: { quietFrom: { code: "push.settings.quietFromRequired", params: {} } } };
105
+ throw new AppError(400, "Quiet hours need both a start and an end.", { code: "push.settings.quietHoursIncomplete", details });
102
106
  }
103
107
  if (input.timeZone && !validTimeZone(input.timeZone)) {
104
- throw new AppError(400, "This time zone is not one the server knows.", { code: "push.settings.timeZoneInvalid" });
108
+ throw new AppError(400, "This device's time zone isn't one the server knows, so quiet hours can't be saved. Leave quiet hours empty, or change the device's time zone.", { code: "push.settings.timeZoneInvalid" });
105
109
  }
106
110
  return settings.saveMine({
107
111
  kindsOff: [...new Set(input.kindsOff)],
@@ -80,4 +80,4 @@ function createPushSettings({ pushSettings }) {
80
80
  return Object.freeze({ readMine, readForUsers, saveMine, removeForUser });
81
81
  }
82
82
 
83
- export { DEFAULT_SETTINGS, createPushSettings, inQuietHours, settingsOf };
83
+ export { createPushSettings, inQuietHours, settingsOf };
@@ -69,7 +69,14 @@ test("settings save once per person, check quiet hours and time zone, and defaul
69
69
  assert.deepEqual(saved, { kindsOff: ["mention"], showText: false, quietFrom: "22:00", quietTo: "07:00", timeZone: "Europe/Rome" });
70
70
  await push.updateSettings({ kindsOff: [], showText: true }, as(1));
71
71
  assert.deepEqual((await push.readOverview(as(1))).settings.quietFrom, null);
72
- await assert.rejects(push.updateSettings({ kindsOff: [], showText: true, quietFrom: "22:00" }, as(1)), { code: "push.settings.quietHoursIncomplete" });
72
+ // The refusal names the empty end, so the person sees it beside that field.
73
+ await assert.rejects(push.updateSettings({ kindsOff: [], showText: true, quietFrom: "22:00" }, as(1)), {
74
+ code: "push.settings.quietHoursIncomplete",
75
+ details: { fieldErrors: { quietTo: "Choose when quiet hours end." }, fieldErrorCodes: { quietTo: { code: "push.settings.quietToRequired", params: {} } } }
76
+ });
77
+ await assert.rejects(push.updateSettings({ kindsOff: [], showText: true, quietTo: "07:00" }, as(1)), {
78
+ details: { fieldErrors: { quietFrom: "Choose when quiet hours start." }, fieldErrorCodes: { quietFrom: { code: "push.settings.quietFromRequired", params: {} } } }
79
+ });
73
80
  await assert.rejects(push.updateSettings({ kindsOff: [], showText: true, timeZone: "Mars/Olympus" }, as(1)), { code: "push.settings.timeZoneInvalid" });
74
81
  });
75
82