@oblax/notifications 0.0.0-stage → 2.1.0

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.
@@ -0,0 +1,197 @@
1
+ import { OblaxError, buildQuery as query, filterPairs, pathSegment as seg } from '@oblax/core';
2
+ import { RealtimeModule } from '@oblax/realtime';
3
+ import { CategoriesSubmodule } from './categories.js';
4
+ import { PreferencesSubmodule } from './preferences.js';
5
+ import { SendsSubmodule } from './sends.js';
6
+ import { NotificationsWatch } from './watch.js';
7
+ /** The longest delay setTimeout keeps: a longer one fires at once. */
8
+ const MAX_DELAY = 2147483647;
9
+ /** The realtime tuning `Notifications(config)` and `onChange()` take for the bell's own stream. */
10
+ const TUNING = ['reconnectDelay', 'maxReconnectDelay', 'idleTimeout'];
11
+ const DEFAULTS = {
12
+ /** The docs' bell snippet refetches once more 1.5 s after a ring. */
13
+ recheckDelay: 1500,
14
+ };
15
+ /**
16
+ * The Notifications module: the signed-in user's inbox, the bell, and sends.
17
+ *
18
+ * The inbox is on the module itself (`list`, `count`, `read`, `unread`,
19
+ * `archive`, `delete`, `readAll`), and so are `send()` (app admins) and
20
+ * `onChange()` (the bell); the rest is in sub-modules:
21
+ *
22
+ * - `preferences` — the user's settings screen: which categories they get, and how.
23
+ * - `categories` — the project's kinds of notification (writes: app admins).
24
+ * - `sends` — a send read back, and its recipients per channel (app admins).
25
+ *
26
+ * Every inbox route acts on the signed-in user: someone else's notification
27
+ * answers 404, as one that doesn't exist.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const bell = oblax.notifications.onChange({
32
+ * handler: async () => setBadge((await oblax.notifications.count()).count),
33
+ * })
34
+ *
35
+ * const { data, next } = await oblax.notifications.list({ limit: 20 })
36
+ * await oblax.notifications.read({ id: data[0].id })
37
+ * await oblax.notifications.readAll({ subject: { type: 'workflows.item', id: '981' } })
38
+ * ```
39
+ */
40
+ export class NotificationsModule {
41
+ constructor(ctx, config) {
42
+ this.ctx = ctx;
43
+ this.recheckDelay = checkDelay(config.recheckDelay ?? DEFAULTS.recheckDelay, 'Notifications module');
44
+ // realtime checks the tuning (positive, at most MAX_DELAY, max ≥ first): fail fast here
45
+ this.realtime = rename(() => new RealtimeModule(ctx, tuningOf(config)), 'Realtime module', 'Notifications module');
46
+ this.preferences = new PreferencesSubmodule(ctx);
47
+ this.categories = new CategoriesSubmodule(ctx);
48
+ this.sends = new SendsSubmodule(ctx);
49
+ }
50
+ // ─── The inbox ─────────────────────────────────────────────────────────────
51
+ /**
52
+ * A page of the user's notifications, newest first. Expired ones are never
53
+ * listed; archived ones only with `archived: true`. Read from a replica.
54
+ */
55
+ async list(params = {}) {
56
+ const filters = [];
57
+ if (params.unread !== undefined)
58
+ filters.push({ field: 'unread', op: 'eq', value: params.unread });
59
+ if (params.critical !== undefined)
60
+ filters.push({ field: 'critical', op: 'eq', value: params.critical });
61
+ const qs = query({
62
+ after: params.after,
63
+ limit: params.limit,
64
+ category: params.category,
65
+ organization_id: params.organizationId,
66
+ subject_type: params.subjectType,
67
+ subject_id: params.subjectId,
68
+ archived: params.archived,
69
+ }, filterPairs(filters));
70
+ return this.ctx.http.get(`/notifications${qs}`);
71
+ }
72
+ /**
73
+ * The user's unread count for the badge: not archived, not expired; of one
74
+ * category with `category`. A small query: call it whenever the bell rings.
75
+ */
76
+ async count(params = {}) {
77
+ return this.ctx.http.get(`/notifications/count${query({ category: params.category })}`);
78
+ }
79
+ /** Marks one notification read (also when it already was). Acknowledges a critical one. */
80
+ async read(params) {
81
+ return this.ctx.http.post(`/notifications/${seg(params.id)}/read`);
82
+ }
83
+ /** Marks one notification unread. A critical one stays acknowledged. */
84
+ async unread(params) {
85
+ return this.ctx.http.post(`/notifications/${seg(params.id)}/unread`);
86
+ }
87
+ /** Hides one notification from the default list and the count. There's no un-archive. */
88
+ async archive(params) {
89
+ return this.ctx.http.post(`/notifications/${seg(params.id)}/archive`);
90
+ }
91
+ /** Deletes one notification from the user's lists; deleting it again is 404. */
92
+ async delete(params) {
93
+ return this.ctx.http.delete(`/notifications/${seg(params.id)}`);
94
+ }
95
+ /**
96
+ * Marks the user's unread notifications read: every one, a category's, a
97
+ * subject's, or a subject's in a category. Mark by subject when the user
98
+ * opens the thing. Answers how many it marked.
99
+ */
100
+ async readAll(params = {}) {
101
+ return this.ctx.http.post('/notifications/read', {
102
+ category: params.category,
103
+ subject: params.subject,
104
+ });
105
+ }
106
+ // ─── Sending ───────────────────────────────────────────────────────────────
107
+ /**
108
+ * Sends a notification: one per person in `to`, written before the call
109
+ * returns, and a ring on their open streams. App admins only (403
110
+ * otherwise). `data` is sent as-is. Like every POST, a send is resent only
111
+ * once, after a 401 and a successful token refresh; a 503 may still have
112
+ * written it, which a `dedupKey` makes harmless to send again.
113
+ */
114
+ async send(params) {
115
+ return this.ctx.http.post('/notifications', {
116
+ to: params.to,
117
+ category: params.category,
118
+ title: params.title,
119
+ body: params.body,
120
+ link: params.link,
121
+ data: params.data,
122
+ actor_id: params.actorId,
123
+ organization_id: params.organizationId,
124
+ subject: params.subject,
125
+ dedup_key: params.dedupKey,
126
+ critical: params.critical,
127
+ expires_at: params.expiresAt,
128
+ });
129
+ }
130
+ // ─── The bell ──────────────────────────────────────────────────────────────
131
+ /**
132
+ * Calls `handler` whenever the user's inbox may have changed, so the app
133
+ * can refetch the count: when the stream opens (the first time and every
134
+ * reconnect, as a ring may have been missed), on every
135
+ * `oblax.notifications.changed`, and once more `recheckDelay` after the
136
+ * last ring (the count is read from a replica that can lag the ring).
137
+ * Each call has a `seq`: refetches can answer out of order, so keep the
138
+ * answer of the latest one.
139
+ *
140
+ * It opens its own stream with `realtime.connect()` (tuned by the module's
141
+ * config, then by `reconnectDelay`, `maxReconnectDelay`, `idleTimeout`
142
+ * given here), or listens on the `stream` passed; tuning given with
143
+ * `stream` throws. The stream reconnects on its own; when it closes for
144
+ * good (`watch.stream.onClose`, `watch.closed`), poll the count instead.
145
+ *
146
+ * @returns the watch: `close()` it on sign-out.
147
+ */
148
+ onChange(params) {
149
+ const where = 'notifications.onChange()';
150
+ if (typeof params?.handler !== 'function') {
151
+ throw new OblaxError(`${where}: \`handler\` must be a function.`);
152
+ }
153
+ const recheckDelay = params.recheckDelay === undefined ? this.recheckDelay : checkDelay(params.recheckDelay, where);
154
+ const tuning = tuningOf(params);
155
+ const settings = { recheckDelay, signal: params.signal, onError: params.onError };
156
+ if (params.stream) {
157
+ const given = Object.keys(tuning);
158
+ if (given.length > 0) {
159
+ throw new OblaxError(`${where}: ${given.map((k) => `\`${k}\``).join(', ')} can't be given with \`stream\`: ` +
160
+ 'a shared stream keeps its own tuning.');
161
+ }
162
+ return new NotificationsWatch(params.stream, params.handler, { ...settings, owned: false });
163
+ }
164
+ const stream = rename(() => this.realtime.connect(tuning), 'realtime.connect()', where);
165
+ return new NotificationsWatch(stream, params.handler, { ...settings, owned: true });
166
+ }
167
+ }
168
+ /** The realtime tuning given, without the keys left out. */
169
+ function tuningOf(config) {
170
+ const out = {};
171
+ for (const key of TUNING) {
172
+ const value = config[key];
173
+ if (value !== undefined)
174
+ out[key] = value;
175
+ }
176
+ return out;
177
+ }
178
+ /** Runs `fn`, naming this module in the OblaxError realtime's checks throw. */
179
+ function rename(fn, from, to) {
180
+ try {
181
+ return fn();
182
+ }
183
+ catch (error) {
184
+ if (error instanceof OblaxError && error.message.startsWith(from)) {
185
+ throw new OblaxError(to + error.message.slice(from.length));
186
+ }
187
+ throw error;
188
+ }
189
+ }
190
+ /** Fails fast on a delay that isn't a number of milliseconds from 0 up. */
191
+ function checkDelay(value, where) {
192
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > MAX_DELAY) {
193
+ throw new OblaxError(`${where}: \`recheckDelay\` must be 0 (off) or a positive number of milliseconds, at most ${MAX_DELAY}.`);
194
+ }
195
+ return value;
196
+ }
197
+ //# sourceMappingURL=notifications-module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notifications-module.js","sourceRoot":"","sources":["../src/notifications-module.ts"],"names":[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,KAAa;IAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,SAAS,EAAE,CAAC;QAC3F,MAAM,IAAI,UAAU,CAClB,GAAG,KAAK,oFAAoF,SAAS,GAAG,CACzG,CAAA;IACH,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC"}
@@ -0,0 +1,33 @@
1
+ import type { OblaxContext } from '@oblax/core';
2
+ import type { NotificationsList, PreferenceCategory, SetPreferencesParams } from './types.js';
3
+ /**
4
+ * The signed-in user's settings screen: every category, `general` first,
5
+ * with the user's effective value for each channel.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const { data } = await oblax.notifications.preferences.get()
10
+ *
11
+ * // a settings screen can send all its switches at once: only the changed ones are written
12
+ * const saved = await oblax.notifications.preferences.set({
13
+ * changes: [
14
+ * { category: 'assignments', channel: 'email', enabled: true },
15
+ * { category: 'assignments', channel: 'push', enabled: false },
16
+ * ],
17
+ * })
18
+ * ```
19
+ */
20
+ export declare class PreferencesSubmodule {
21
+ private readonly ctx;
22
+ constructor(ctx: OblaxContext);
23
+ /** The settings screen. Show a locked category's switches disabled. */
24
+ get(): Promise<NotificationsList<PreferenceCategory>>;
25
+ /**
26
+ * Sets up to 500 switches; one is `changes: [change]`. Answers the screen
27
+ * as saved. A refused request writes nothing: 400 `VALIDATION_FAILED` names
28
+ * each broken change in `validation_errors` (`category`/`channel`/`enabled`
29
+ * for a list of one, `<i>.category`, … for a longer list).
30
+ */
31
+ set(params: SetPreferencesParams): Promise<NotificationsList<PreferenceCategory>>;
32
+ }
33
+ //# sourceMappingURL=preferences.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preferences.d.ts","sourceRoot":"","sources":["../src/preferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAoB,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAE/G;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,oBAAoB;IACnB,OAAO,CAAC,QAAQ,CAAC,GAAG;gBAAH,GAAG,EAAE,YAAY;IAE9C,uEAAuE;IACjE,GAAG,IAAI,OAAO,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;IAI3D;;;;;OAKG;IACG,GAAG,CAAC,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;CAMxF"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The signed-in user's settings screen: every category, `general` first,
3
+ * with the user's effective value for each channel.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * const { data } = await oblax.notifications.preferences.get()
8
+ *
9
+ * // a settings screen can send all its switches at once: only the changed ones are written
10
+ * const saved = await oblax.notifications.preferences.set({
11
+ * changes: [
12
+ * { category: 'assignments', channel: 'email', enabled: true },
13
+ * { category: 'assignments', channel: 'push', enabled: false },
14
+ * ],
15
+ * })
16
+ * ```
17
+ */
18
+ export class PreferencesSubmodule {
19
+ constructor(ctx) {
20
+ this.ctx = ctx;
21
+ }
22
+ /** The settings screen. Show a locked category's switches disabled. */
23
+ async get() {
24
+ return this.ctx.http.get('/notifications/preferences');
25
+ }
26
+ /**
27
+ * Sets up to 500 switches; one is `changes: [change]`. Answers the screen
28
+ * as saved. A refused request writes nothing: 400 `VALIDATION_FAILED` names
29
+ * each broken change in `validation_errors` (`category`/`channel`/`enabled`
30
+ * for a list of one, `<i>.category`, … for a longer list).
31
+ */
32
+ async set(params) {
33
+ return this.ctx.http.put('/notifications/preferences', params.changes.map(toApi));
34
+ }
35
+ }
36
+ /** Only the change's own keys: the user is always the token's. */
37
+ function toApi(c) {
38
+ return { category: c.category, channel: c.channel, enabled: c.enabled };
39
+ }
40
+ //# sourceMappingURL=preferences.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preferences.js","sourceRoot":"","sources":["../src/preferences.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAO,oBAAoB;IAC/B,YAA6B,GAAiB;QAAjB,QAAG,GAAH,GAAG,CAAc;IAAG,CAAC;IAElD,uEAAuE;IACvE,KAAK,CAAC,GAAG;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAwC,4BAA4B,CAAC,CAAA;IAC/F,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,GAAG,CAAC,MAA4B;QACpC,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CACtB,4BAA4B,EAC5B,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAC1B,CAAA;IACH,CAAC;CACF;AAED,kEAAkE;AAClE,SAAS,KAAK,CAAC,CAAmB;IAChC,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAA;AACzE,CAAC"}
@@ -0,0 +1,34 @@
1
+ import type { OblaxContext } from '@oblax/core';
2
+ import type { IdParams, RecipientsPage, RecipientsParams, Send } from './types.js';
3
+ /**
4
+ * Sends read back, for app admins: a send's content and counts, and who has
5
+ * a channel on, to deliver email, push or SMS from a backend.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const { id } = await oblax.notifications.send({ to: { roles: ['editor'] }, category: 'general', title: 'Hi' })
10
+ *
11
+ * let after = ''
12
+ * do {
13
+ * const page = await oblax.notifications.sends.recipients({ id, channel: 'email', after, limit: 500 })
14
+ * await mailEach(page.users)
15
+ * after = page.next
16
+ * } while (after !== '')
17
+ * ```
18
+ */
19
+ export declare class SendsSubmodule {
20
+ private readonly ctx;
21
+ constructor(ctx: OblaxContext);
22
+ /**
23
+ * A send's content and counts, never its `to`. Read from a replica: right
24
+ * after the send it can be 404 for a moment.
25
+ */
26
+ get(params: IdParams): Promise<Send>;
27
+ /**
28
+ * A page of the send's recipients whose switch for `channel` is on now, in
29
+ * id order. A page may hold fewer users than `limit`, even none, while
30
+ * `next` is set: page on until `next` is `''`.
31
+ */
32
+ recipients(params: RecipientsParams): Promise<RecipientsPage>;
33
+ }
34
+ //# sourceMappingURL=sends.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sends.d.ts","sourceRoot":"","sources":["../src/sends.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAElF;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,GAAG;gBAAH,GAAG,EAAE,YAAY;IAE9C;;;OAGG;IACG,GAAG,CAAC,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1C;;;;OAIG;IACG,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,cAAc,CAAC;CAIpE"}
package/dist/sends.js ADDED
@@ -0,0 +1,39 @@
1
+ import { buildQuery as query, pathSegment as seg } from '@oblax/core';
2
+ /**
3
+ * Sends read back, for app admins: a send's content and counts, and who has
4
+ * a channel on, to deliver email, push or SMS from a backend.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * const { id } = await oblax.notifications.send({ to: { roles: ['editor'] }, category: 'general', title: 'Hi' })
9
+ *
10
+ * let after = ''
11
+ * do {
12
+ * const page = await oblax.notifications.sends.recipients({ id, channel: 'email', after, limit: 500 })
13
+ * await mailEach(page.users)
14
+ * after = page.next
15
+ * } while (after !== '')
16
+ * ```
17
+ */
18
+ export class SendsSubmodule {
19
+ constructor(ctx) {
20
+ this.ctx = ctx;
21
+ }
22
+ /**
23
+ * A send's content and counts, never its `to`. Read from a replica: right
24
+ * after the send it can be 404 for a moment.
25
+ */
26
+ async get(params) {
27
+ return this.ctx.http.get(`/notifications/sends/${seg(params.id)}`);
28
+ }
29
+ /**
30
+ * A page of the send's recipients whose switch for `channel` is on now, in
31
+ * id order. A page may hold fewer users than `limit`, even none, while
32
+ * `next` is set: page on until `next` is `''`.
33
+ */
34
+ async recipients(params) {
35
+ const qs = query({ channel: params.channel, after: params.after, limit: params.limit });
36
+ return this.ctx.http.get(`/notifications/sends/${seg(params.id)}/recipients${qs}`);
37
+ }
38
+ }
39
+ //# sourceMappingURL=sends.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sends.js","sourceRoot":"","sources":["../src/sends.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,WAAW,IAAI,GAAG,EAAE,MAAM,aAAa,CAAA;AAGrE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,cAAc;IACzB,YAA6B,GAAiB;QAAjB,QAAG,GAAH,GAAG,CAAc;IAAG,CAAC;IAElD;;;OAGG;IACH,KAAK,CAAC,GAAG,CAAC,MAAgB;QACxB,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAO,wBAAwB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA;IAC1E,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU,CAAC,MAAwB;QACvC,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;QACvF,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAiB,wBAAwB,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,CAAC,CAAA;IACpG,CAAC;CACF"}