@meith/notifications 0.16.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.
- package/LICENSE.md +165 -0
- package/package.json +26 -0
- package/src/deliver-push.ts +116 -0
- package/src/deliver.ts +48 -0
- package/src/index.ts +64 -0
- package/src/kinds.ts +189 -0
- package/src/mail.ts +73 -0
- package/src/push.ts +236 -0
- package/src/render.ts +275 -0
- package/src/service.ts +276 -0
- package/src/types.ts +133 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meith/notifications",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"license": "LGPL-3.0-or-later",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/meith-dev/meith.git",
|
|
8
|
+
"directory": "packages/notifications"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./src/index.ts",
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/*.test.*",
|
|
16
|
+
"!src/**/*.type-test.*"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@meith/core": "0.16.0",
|
|
23
|
+
"@meith/i18n": "0.16.0",
|
|
24
|
+
"@meith/mail": "0.16.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { EN_CATALOG, sourceTranslator } from '@meith/i18n'
|
|
2
|
+
|
|
3
|
+
import type { DeliveryOutcome, NotificationTranslatorResolver } from './deliver'
|
|
4
|
+
import { PUSH_PAYLOAD_LIMIT, sendWebPush, type VapidDetails } from './push'
|
|
5
|
+
import { renderNotification } from './render'
|
|
6
|
+
import type { NotificationRepository } from './types'
|
|
7
|
+
|
|
8
|
+
const BODY_LIMIT = 300
|
|
9
|
+
|
|
10
|
+
export interface PushDeliveryResult {
|
|
11
|
+
readonly outcome: DeliveryOutcome
|
|
12
|
+
readonly sent: number
|
|
13
|
+
readonly pruned: number
|
|
14
|
+
readonly failed: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function pushPayload(input: {
|
|
18
|
+
readonly notificationId: number
|
|
19
|
+
readonly title: string
|
|
20
|
+
readonly body: string
|
|
21
|
+
readonly href: string | null
|
|
22
|
+
readonly badge: number
|
|
23
|
+
}): string {
|
|
24
|
+
const payload = JSON.stringify({
|
|
25
|
+
id: input.notificationId,
|
|
26
|
+
title: input.title,
|
|
27
|
+
body: input.body.replace(/\s+/g, ' ').trim().slice(0, BODY_LIMIT),
|
|
28
|
+
href: input.href,
|
|
29
|
+
badge: input.badge,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
return Buffer.byteLength(payload, 'utf8') > PUSH_PAYLOAD_LIMIT
|
|
33
|
+
? JSON.stringify({
|
|
34
|
+
id: input.notificationId,
|
|
35
|
+
title: input.title.slice(0, BODY_LIMIT),
|
|
36
|
+
body: '',
|
|
37
|
+
href: input.href,
|
|
38
|
+
badge: input.badge,
|
|
39
|
+
})
|
|
40
|
+
: payload
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function deliverNotificationPush(deps: {
|
|
44
|
+
readonly notifications: NotificationRepository
|
|
45
|
+
readonly vapid: VapidDetails
|
|
46
|
+
readonly notificationId: number
|
|
47
|
+
readonly translatorForLocale?: NotificationTranslatorResolver
|
|
48
|
+
readonly now?: () => Date
|
|
49
|
+
readonly send?: typeof sendWebPush
|
|
50
|
+
}): Promise<PushDeliveryResult> {
|
|
51
|
+
const nothing = { sent: 0, pruned: 0, failed: 0 }
|
|
52
|
+
|
|
53
|
+
const deliverable = await deps.notifications.findForDelivery(deps.notificationId)
|
|
54
|
+
if (deliverable === null) return { outcome: 'missing', ...nothing }
|
|
55
|
+
if (deliverable.pushSentAt !== null) return { outcome: 'already-sent', ...nothing }
|
|
56
|
+
if (!deliverable.pushEnabled) return { outcome: 'declined', ...nothing }
|
|
57
|
+
|
|
58
|
+
const subscriptions = await deps.notifications.pushSubscriptionsFor(deliverable.recipient.userId)
|
|
59
|
+
if (subscriptions.length === 0) return { outcome: 'unsubscribed', ...nothing }
|
|
60
|
+
|
|
61
|
+
const t = await (deps.translatorForLocale ?? (() => sourceTranslator(EN_CATALOG)))(
|
|
62
|
+
deliverable.recipient.locale,
|
|
63
|
+
)
|
|
64
|
+
const view = renderNotification(deliverable.notification, t)
|
|
65
|
+
const now = deps.now ?? (() => new Date())
|
|
66
|
+
const send = deps.send ?? sendWebPush
|
|
67
|
+
|
|
68
|
+
const payload = pushPayload({
|
|
69
|
+
notificationId: view.id,
|
|
70
|
+
title: view.subject,
|
|
71
|
+
body: view.body,
|
|
72
|
+
href: view.href,
|
|
73
|
+
badge: await deps.notifications.unreadCount(deliverable.recipient.userId),
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
let sent = 0
|
|
77
|
+
let pruned = 0
|
|
78
|
+
let failed = 0
|
|
79
|
+
|
|
80
|
+
for (const subscription of subscriptions) {
|
|
81
|
+
const result = await send({
|
|
82
|
+
subscription: {
|
|
83
|
+
endpoint: subscription.endpoint,
|
|
84
|
+
p256dh: subscription.p256dh,
|
|
85
|
+
auth: subscription.auth,
|
|
86
|
+
},
|
|
87
|
+
payload,
|
|
88
|
+
vapid: deps.vapid,
|
|
89
|
+
now: now(),
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
if (result.outcome === 'sent') {
|
|
93
|
+
sent += 1
|
|
94
|
+
await deps.notifications.touchPushSubscription(subscription.id, now())
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (result.outcome === 'gone') {
|
|
99
|
+
pruned += 1
|
|
100
|
+
await deps.notifications.prunePushSubscription(subscription.id)
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
failed += 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (sent === 0 && failed > 0) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Every push service refused notification ${deps.notificationId} (${failed} endpoint(s)).`,
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
await deps.notifications.markPushSent(deps.notificationId, now())
|
|
114
|
+
|
|
115
|
+
return { outcome: sent > 0 ? 'sent' : 'unsubscribed', sent, pruned, failed }
|
|
116
|
+
}
|
package/src/deliver.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { MailDriver } from '@meith/core'
|
|
2
|
+
import { EN_CATALOG, sourceTranslator, type Translator } from '@meith/i18n'
|
|
3
|
+
|
|
4
|
+
import { type MailBrand, renderNotificationMail } from './mail'
|
|
5
|
+
import { renderNotification } from './render'
|
|
6
|
+
import type { NotificationRepository } from './types'
|
|
7
|
+
|
|
8
|
+
export type DeliveryOutcome = 'sent' | 'missing' | 'declined' | 'already-sent' | 'unsubscribed'
|
|
9
|
+
|
|
10
|
+
export type NotificationTranslatorResolver = (locale: string) => Promise<Translator> | Translator
|
|
11
|
+
|
|
12
|
+
export async function deliverNotificationEmail(deps: {
|
|
13
|
+
readonly notifications: NotificationRepository
|
|
14
|
+
readonly mail: MailDriver
|
|
15
|
+
readonly brand: MailBrand
|
|
16
|
+
readonly notificationId: number
|
|
17
|
+
readonly translatorForLocale?: NotificationTranslatorResolver
|
|
18
|
+
readonly now?: () => Date
|
|
19
|
+
}): Promise<DeliveryOutcome> {
|
|
20
|
+
const deliverable = await deps.notifications.findForDelivery(deps.notificationId)
|
|
21
|
+
if (deliverable === null) return 'missing'
|
|
22
|
+
if (deliverable.emailSentAt !== null) return 'already-sent'
|
|
23
|
+
if (!deliverable.emailEnabled) return 'declined'
|
|
24
|
+
|
|
25
|
+
const t = await (deps.translatorForLocale ?? (() => sourceTranslator(EN_CATALOG)))(
|
|
26
|
+
deliverable.recipient.locale,
|
|
27
|
+
)
|
|
28
|
+
const mail = renderNotificationMail({
|
|
29
|
+
view: renderNotification(deliverable.notification, t),
|
|
30
|
+
brand: deps.brand,
|
|
31
|
+
recipientName: deliverable.recipient.username,
|
|
32
|
+
t,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const fromName = deps.brand.fromName ?? ''
|
|
36
|
+
|
|
37
|
+
await deps.mail.send({
|
|
38
|
+
to: deliverable.recipient.email,
|
|
39
|
+
subject: mail.subject,
|
|
40
|
+
text: mail.text,
|
|
41
|
+
html: mail.html,
|
|
42
|
+
...(fromName === '' ? {} : { fromName }),
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
await deps.notifications.markEmailSent(deps.notificationId, (deps.now ?? (() => new Date()))())
|
|
46
|
+
|
|
47
|
+
return 'sent'
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type DeliveryOutcome,
|
|
3
|
+
deliverNotificationEmail,
|
|
4
|
+
type NotificationTranslatorResolver,
|
|
5
|
+
} from './deliver'
|
|
6
|
+
export {
|
|
7
|
+
deliverNotificationPush,
|
|
8
|
+
type PushDeliveryResult,
|
|
9
|
+
pushPayload,
|
|
10
|
+
} from './deliver-push'
|
|
11
|
+
export {
|
|
12
|
+
configurableKindsFor,
|
|
13
|
+
isNotificationKind,
|
|
14
|
+
NOTIFICATION_KINDS,
|
|
15
|
+
type NotificationAudience,
|
|
16
|
+
type NotificationKind,
|
|
17
|
+
type NotificationKindSpec,
|
|
18
|
+
notificationKind,
|
|
19
|
+
type RegisteredNotificationKind,
|
|
20
|
+
} from './kinds'
|
|
21
|
+
export {
|
|
22
|
+
type MailBrand,
|
|
23
|
+
type RenderedMail,
|
|
24
|
+
renderNotificationMail,
|
|
25
|
+
} from './mail'
|
|
26
|
+
export {
|
|
27
|
+
encryptPushPayload,
|
|
28
|
+
generateVapidKeys,
|
|
29
|
+
isVapidPrivateKey,
|
|
30
|
+
isVapidPublicKey,
|
|
31
|
+
PUSH_PAYLOAD_LIMIT,
|
|
32
|
+
PUSH_TTL_SECONDS,
|
|
33
|
+
type PushSendOutcome,
|
|
34
|
+
type PushSendResult,
|
|
35
|
+
type PushSubscriptionKeys,
|
|
36
|
+
pushAudience,
|
|
37
|
+
pushOutcomeFor,
|
|
38
|
+
sendWebPush,
|
|
39
|
+
type VapidDetails,
|
|
40
|
+
type VapidKeyPair,
|
|
41
|
+
vapidAuthorization,
|
|
42
|
+
} from './push'
|
|
43
|
+
export { type NotificationView, renderNotification } from './render'
|
|
44
|
+
export {
|
|
45
|
+
MAX_STAFF_FANOUT,
|
|
46
|
+
NO_NOTIFICATION_AUDIT,
|
|
47
|
+
NOTIFICATIONS_PAGE_SIZE,
|
|
48
|
+
type NotificationAudit,
|
|
49
|
+
type NotificationPreferenceView,
|
|
50
|
+
NotificationService,
|
|
51
|
+
} from './service'
|
|
52
|
+
export type {
|
|
53
|
+
DeliverableNotification,
|
|
54
|
+
NotificationChannel,
|
|
55
|
+
NotificationChannelPreference,
|
|
56
|
+
NotificationData,
|
|
57
|
+
NotificationPage,
|
|
58
|
+
NotificationRecord,
|
|
59
|
+
NotificationRepository,
|
|
60
|
+
PushSubscriptionRecord,
|
|
61
|
+
RaiseInput,
|
|
62
|
+
RaiseResult,
|
|
63
|
+
SavePushSubscriptionInput,
|
|
64
|
+
} from './types'
|
package/src/kinds.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { NotificationChannel } from './types'
|
|
2
|
+
|
|
3
|
+
export type NotificationAudience = 'member' | 'staff'
|
|
4
|
+
|
|
5
|
+
interface NotificationKindBase {
|
|
6
|
+
readonly title: string
|
|
7
|
+
readonly description: string
|
|
8
|
+
readonly titleKey?: string
|
|
9
|
+
readonly descriptionKey?: string
|
|
10
|
+
readonly audience: NotificationAudience
|
|
11
|
+
readonly emailByDefault: boolean
|
|
12
|
+
readonly emailConfigurable: boolean
|
|
13
|
+
readonly pushByDefault: boolean
|
|
14
|
+
readonly pushConfigurable: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const NOTIFICATION_KINDS = [
|
|
18
|
+
{
|
|
19
|
+
id: 'warning.received',
|
|
20
|
+
titleKey: 'notification.warning.received.title',
|
|
21
|
+
descriptionKey: 'notification.warning.received.description',
|
|
22
|
+
title: 'You receive a warning',
|
|
23
|
+
description:
|
|
24
|
+
'A moderator has warned you. The notification records what it was for ' +
|
|
25
|
+
'and how many points it carried.',
|
|
26
|
+
audience: 'member',
|
|
27
|
+
emailByDefault: true,
|
|
28
|
+
emailConfigurable: true,
|
|
29
|
+
pushByDefault: true,
|
|
30
|
+
pushConfigurable: true,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'report.actioned',
|
|
34
|
+
titleKey: 'notification.report.actioned.title',
|
|
35
|
+
descriptionKey: 'notification.report.actioned.description',
|
|
36
|
+
title: 'A report you filed is closed',
|
|
37
|
+
description:
|
|
38
|
+
'A moderator has resolved or rejected something you reported. The ' +
|
|
39
|
+
"moderator's private note is never included.",
|
|
40
|
+
audience: 'member',
|
|
41
|
+
emailByDefault: false,
|
|
42
|
+
emailConfigurable: true,
|
|
43
|
+
pushByDefault: false,
|
|
44
|
+
pushConfigurable: true,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: 'subscription.reply',
|
|
48
|
+
titleKey: 'notification.subscription.reply.title',
|
|
49
|
+
descriptionKey: 'notification.subscription.reply.description',
|
|
50
|
+
title: 'A thread you follow gets a reply',
|
|
51
|
+
description:
|
|
52
|
+
'Somebody replies in a thread or forum you follow and asked to hear ' +
|
|
53
|
+
'about as it happens.',
|
|
54
|
+
audience: 'member',
|
|
55
|
+
emailByDefault: true,
|
|
56
|
+
emailConfigurable: true,
|
|
57
|
+
pushByDefault: true,
|
|
58
|
+
pushConfigurable: true,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'subscription.digest',
|
|
62
|
+
titleKey: 'notification.subscription.digest.title',
|
|
63
|
+
descriptionKey: 'notification.subscription.digest.description',
|
|
64
|
+
title: 'Your daily or weekly digest is ready',
|
|
65
|
+
description:
|
|
66
|
+
'A summary of what arrived in the threads and forums you follow on a ' + 'digest cadence.',
|
|
67
|
+
audience: 'member',
|
|
68
|
+
emailByDefault: true,
|
|
69
|
+
emailConfigurable: true,
|
|
70
|
+
pushByDefault: false,
|
|
71
|
+
pushConfigurable: true,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: 'post.mentioned',
|
|
75
|
+
titleKey: 'notification.post.mentioned.title',
|
|
76
|
+
descriptionKey: 'notification.post.mentioned.description',
|
|
77
|
+
title: 'Somebody mentions you in a post',
|
|
78
|
+
description:
|
|
79
|
+
'Somebody writes @your-name in a post. The notification links to the ' +
|
|
80
|
+
'post so you can read it in place.',
|
|
81
|
+
audience: 'member',
|
|
82
|
+
emailByDefault: true,
|
|
83
|
+
emailConfigurable: true,
|
|
84
|
+
pushByDefault: true,
|
|
85
|
+
pushConfigurable: true,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: 'post.quoted',
|
|
89
|
+
titleKey: 'notification.post.quoted.title',
|
|
90
|
+
descriptionKey: 'notification.post.quoted.description',
|
|
91
|
+
title: 'Somebody quotes your post',
|
|
92
|
+
description:
|
|
93
|
+
'Somebody carries your words into their reply with the quote button. ' +
|
|
94
|
+
'The notification links to the reply that quotes you.',
|
|
95
|
+
audience: 'member',
|
|
96
|
+
emailByDefault: false,
|
|
97
|
+
emailConfigurable: true,
|
|
98
|
+
pushByDefault: false,
|
|
99
|
+
pushConfigurable: true,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: 'pm.received',
|
|
103
|
+
titleKey: 'notification.pm.received.title',
|
|
104
|
+
descriptionKey: 'notification.pm.received.description',
|
|
105
|
+
title: 'You receive a private message',
|
|
106
|
+
description:
|
|
107
|
+
'Somebody sends you a private message. The notification carries who ' +
|
|
108
|
+
'from and the subject, never the message itself.',
|
|
109
|
+
audience: 'member',
|
|
110
|
+
emailByDefault: true,
|
|
111
|
+
emailConfigurable: true,
|
|
112
|
+
pushByDefault: true,
|
|
113
|
+
pushConfigurable: true,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: 'pm.receipt',
|
|
117
|
+
titleKey: 'notification.pm.receipt.title',
|
|
118
|
+
descriptionKey: 'notification.pm.receipt.description',
|
|
119
|
+
title: 'A message you asked to track is read',
|
|
120
|
+
description:
|
|
121
|
+
'Somebody opens a private message you sent with a read receipt ' +
|
|
122
|
+
'requested. Raised once, on the first read.',
|
|
123
|
+
audience: 'member',
|
|
124
|
+
emailByDefault: false,
|
|
125
|
+
emailConfigurable: true,
|
|
126
|
+
pushByDefault: false,
|
|
127
|
+
pushConfigurable: true,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'system.task_failed',
|
|
131
|
+
titleKey: 'notification.system.task_failed.title',
|
|
132
|
+
descriptionKey: 'notification.system.task_failed.description',
|
|
133
|
+
title: 'A scheduled task fails',
|
|
134
|
+
description:
|
|
135
|
+
'A background task raised an error. Counters, digests and search ' +
|
|
136
|
+
'indexing all run this way, so a task failing silently is how a board ' +
|
|
137
|
+
'drifts without anybody noticing.',
|
|
138
|
+
audience: 'staff',
|
|
139
|
+
emailByDefault: true,
|
|
140
|
+
emailConfigurable: true,
|
|
141
|
+
pushByDefault: false,
|
|
142
|
+
pushConfigurable: true,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: 'marketplace.update_available',
|
|
146
|
+
titleKey: 'notification.marketplace.update_available.title',
|
|
147
|
+
descriptionKey: 'notification.marketplace.update_available.description',
|
|
148
|
+
title: 'An installed plugin has a newer listed version',
|
|
149
|
+
description:
|
|
150
|
+
'The daily catalog fetch finds a newer, compatible version of a plugin this ' +
|
|
151
|
+
'board runs. Sent once per version, whether or not anybody installs it.',
|
|
152
|
+
audience: 'staff',
|
|
153
|
+
emailByDefault: false,
|
|
154
|
+
emailConfigurable: true,
|
|
155
|
+
pushByDefault: false,
|
|
156
|
+
pushConfigurable: true,
|
|
157
|
+
},
|
|
158
|
+
] as const satisfies readonly (NotificationKindBase & { readonly id: string })[]
|
|
159
|
+
|
|
160
|
+
export type NotificationKind = (typeof NOTIFICATION_KINDS)[number]['id']
|
|
161
|
+
|
|
162
|
+
export type NotificationKindSpec = NotificationKindBase & { readonly id: NotificationKind }
|
|
163
|
+
|
|
164
|
+
export interface RegisteredNotificationKind extends NotificationKindBase {
|
|
165
|
+
readonly id: string
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const BY_ID = new Map<string, NotificationKindSpec>(
|
|
169
|
+
NOTIFICATION_KINDS.map((kind) => [kind.id, kind as NotificationKindSpec]),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
export function notificationKind(id: string): NotificationKindSpec | undefined {
|
|
173
|
+
return BY_ID.get(id)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function isNotificationKind(id: string): id is NotificationKind {
|
|
177
|
+
return BY_ID.has(id)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function configurableKindsFor(
|
|
181
|
+
audience: NotificationAudience,
|
|
182
|
+
channel: NotificationChannel = 'email',
|
|
183
|
+
): readonly NotificationKindSpec[] {
|
|
184
|
+
return NOTIFICATION_KINDS.filter(
|
|
185
|
+
(kind) =>
|
|
186
|
+
kind.audience === audience &&
|
|
187
|
+
(channel === 'email' ? kind.emailConfigurable : kind.pushConfigurable),
|
|
188
|
+
) as readonly NotificationKindSpec[]
|
|
189
|
+
}
|
package/src/mail.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EN_CATALOG, sourceTranslator, type Translator } from '@meith/i18n'
|
|
2
|
+
import {
|
|
3
|
+
absoluteUrl,
|
|
4
|
+
type MailBody,
|
|
5
|
+
type MailBrand,
|
|
6
|
+
type MailFooterLine,
|
|
7
|
+
type RenderedMail,
|
|
8
|
+
renderMail,
|
|
9
|
+
} from '@meith/mail'
|
|
10
|
+
|
|
11
|
+
import type { NotificationView } from './render'
|
|
12
|
+
|
|
13
|
+
export type { MailBrand, RenderedMail }
|
|
14
|
+
|
|
15
|
+
const PREFERENCES_PATH = '/notifications/preferences'
|
|
16
|
+
|
|
17
|
+
const UNSUBSCRIBE_PATH = '/unsubscribe'
|
|
18
|
+
|
|
19
|
+
function footer(input: {
|
|
20
|
+
readonly boardName: string
|
|
21
|
+
readonly preferences: string | null
|
|
22
|
+
readonly unsubscribe: string | null
|
|
23
|
+
readonly t: Translator
|
|
24
|
+
}): readonly MailFooterLine[] {
|
|
25
|
+
const { t } = input
|
|
26
|
+
const lines: MailFooterLine[] = [
|
|
27
|
+
{ text: t.t('mail.footer.account', { board: input.boardName }) },
|
|
28
|
+
input.preferences === null
|
|
29
|
+
? { text: t.t('mail.footer.preferencesNoLink') }
|
|
30
|
+
: { text: t.t('mail.footer.preferences'), href: input.preferences },
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
if (input.unsubscribe !== null) {
|
|
34
|
+
lines.push({ text: t.t('mail.footer.unsubscribe'), href: input.unsubscribe })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return lines
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function renderNotificationMail(input: {
|
|
41
|
+
readonly view: NotificationView
|
|
42
|
+
readonly brand: MailBrand
|
|
43
|
+
readonly recipientName: string
|
|
44
|
+
readonly t?: Translator
|
|
45
|
+
}): RenderedMail {
|
|
46
|
+
const { view, brand, recipientName } = input
|
|
47
|
+
const t = input.t ?? sourceTranslator(EN_CATALOG)
|
|
48
|
+
|
|
49
|
+
const boardName =
|
|
50
|
+
brand.boardName === '' ? t.t('notification.mail.boardFallback') : brand.boardName
|
|
51
|
+
|
|
52
|
+
const target = absoluteUrl(brand.boardUrl, view.href)
|
|
53
|
+
const preferences = absoluteUrl(brand.boardUrl, PREFERENCES_PATH)
|
|
54
|
+
const unsubscribe =
|
|
55
|
+
view.unsubscribeToken === null
|
|
56
|
+
? null
|
|
57
|
+
: absoluteUrl(
|
|
58
|
+
brand.boardUrl,
|
|
59
|
+
`${UNSUBSCRIBE_PATH}?token=${encodeURIComponent(view.unsubscribeToken)}`,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
const body: MailBody = {
|
|
63
|
+
title: view.subject,
|
|
64
|
+
greeting: t.t('notification.mail.greeting', { username: recipientName }),
|
|
65
|
+
paragraphs: view.body === '' ? [] : view.body.split(/\n{2,}/),
|
|
66
|
+
...(target === null
|
|
67
|
+
? {}
|
|
68
|
+
: { action: { label: t.t('mail.notification.action', { board: boardName }), href: target } }),
|
|
69
|
+
footer: footer({ boardName, preferences, unsubscribe, t }),
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return renderMail({ brand: { ...brand, boardName }, body, t })
|
|
73
|
+
}
|