@meith/runtime 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 +42 -0
- package/src/event-handlers.ts +98 -0
- package/src/groups.ts +9 -0
- package/src/index.ts +14 -0
- package/src/mail-brand.ts +83 -0
- package/src/plugin-context.ts +56 -0
- package/src/plugin-lifecycle.ts +41 -0
- package/src/plugin-rendering.ts +60 -0
- package/src/plugin-tasks.ts +52 -0
- package/src/scheduler-bundle.ts +373 -0
- package/src/task-workers.ts +293 -0
- package/src/visible-forums.ts +23 -0
- package/src/webhook-delivery.ts +117 -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,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meith/runtime",
|
|
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/runtime"
|
|
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/api": "0.16.0",
|
|
23
|
+
"@meith/attachments": "0.16.0",
|
|
24
|
+
"@meith/core": "0.16.0",
|
|
25
|
+
"@meith/avatars": "0.16.0",
|
|
26
|
+
"@meith/drivers": "0.16.0",
|
|
27
|
+
"@meith/i18n": "0.16.0",
|
|
28
|
+
"@meith/accounts": "0.16.0",
|
|
29
|
+
"@meith/events": "0.16.0",
|
|
30
|
+
"@meith/groups": "0.16.0",
|
|
31
|
+
"@meith/marketplace": "0.16.0",
|
|
32
|
+
"@meith/mail": "0.16.0",
|
|
33
|
+
"@meith/db": "0.16.0",
|
|
34
|
+
"@meith/markdown": "0.16.0",
|
|
35
|
+
"@meith/notifications": "0.16.0",
|
|
36
|
+
"@meith/moderation": "0.16.0",
|
|
37
|
+
"@meith/plugin-kit": "0.16.0",
|
|
38
|
+
"@meith/tasks": "0.16.0",
|
|
39
|
+
"@meith/settings": "0.16.0",
|
|
40
|
+
"@meith/theme-default": "0.16.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { EventRegistry } from '@meith/events'
|
|
2
|
+
|
|
3
|
+
export interface EventHandlerDeps {
|
|
4
|
+
readonly counters: {
|
|
5
|
+
rollUpAncestors(postId: number): Promise<boolean>
|
|
6
|
+
applyVisibilityChange(postId: number): Promise<boolean>
|
|
7
|
+
}
|
|
8
|
+
readonly notifications?: {
|
|
9
|
+
deliverEmail?(notificationId: number): Promise<void>
|
|
10
|
+
deliverPush?(notificationId: number): Promise<void>
|
|
11
|
+
}
|
|
12
|
+
readonly attachments?: {
|
|
13
|
+
process(attachmentId: number): Promise<unknown>
|
|
14
|
+
}
|
|
15
|
+
readonly avatars?: {
|
|
16
|
+
process(userId: number): Promise<unknown>
|
|
17
|
+
}
|
|
18
|
+
readonly massMail?: {
|
|
19
|
+
send(input: { massMailId: number; userId: number; email: string }): Promise<void>
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function buildEventRegistry(deps: EventHandlerDeps): EventRegistry {
|
|
24
|
+
const registry = new EventRegistry()
|
|
25
|
+
.register({
|
|
26
|
+
id: 'counters.rollup',
|
|
27
|
+
event: 'post.created',
|
|
28
|
+
async handle(payload) {
|
|
29
|
+
await deps.counters.rollUpAncestors(payload.postId)
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
.register({
|
|
33
|
+
id: 'counters.visibility',
|
|
34
|
+
event: 'post.visibility_changed',
|
|
35
|
+
async handle(payload) {
|
|
36
|
+
await deps.counters.applyVisibilityChange(payload.postId)
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
if (deps.attachments !== undefined) {
|
|
41
|
+
registry.register({
|
|
42
|
+
id: 'attachments.process',
|
|
43
|
+
event: 'attachment.uploaded',
|
|
44
|
+
async handle(payload) {
|
|
45
|
+
await deps.attachments!.process(payload.attachmentId)
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (deps.avatars !== undefined) {
|
|
51
|
+
registry.register({
|
|
52
|
+
id: 'avatars.process',
|
|
53
|
+
event: 'avatar.uploaded',
|
|
54
|
+
async handle(payload) {
|
|
55
|
+
await deps.avatars!.process(payload.userId)
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (deps.massMail !== undefined) {
|
|
61
|
+
registry.register({
|
|
62
|
+
id: 'admin.mass_mail',
|
|
63
|
+
event: 'admin.mass_mail_queued',
|
|
64
|
+
async handle(payload) {
|
|
65
|
+
await deps.massMail!.send({
|
|
66
|
+
massMailId: payload.massMailId,
|
|
67
|
+
userId: payload.userId,
|
|
68
|
+
email: payload.email,
|
|
69
|
+
})
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const notifications = deps.notifications
|
|
75
|
+
if (notifications === undefined) return registry
|
|
76
|
+
|
|
77
|
+
if (notifications.deliverEmail !== undefined) {
|
|
78
|
+
registry.register({
|
|
79
|
+
id: 'notifications.email',
|
|
80
|
+
event: 'notification.created',
|
|
81
|
+
async handle(payload) {
|
|
82
|
+
await notifications.deliverEmail!(payload.notificationId)
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (notifications.deliverPush !== undefined) {
|
|
88
|
+
registry.register({
|
|
89
|
+
id: 'notifications.push',
|
|
90
|
+
event: 'notification.created',
|
|
91
|
+
async handle(payload) {
|
|
92
|
+
await notifications.deliverPush!(payload.notificationId)
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return registry
|
|
98
|
+
}
|
package/src/groups.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { buildEventRegistry, type EventHandlerDeps } from './event-handlers'
|
|
2
|
+
export { SEED_GROUP } from './groups'
|
|
3
|
+
export {
|
|
4
|
+
DEFAULT_THEME_KEY,
|
|
5
|
+
type MailBrandDeps,
|
|
6
|
+
resolveMailBrand,
|
|
7
|
+
SHIPPED_TOKENS,
|
|
8
|
+
type ThemeTokenRegistry,
|
|
9
|
+
} from './mail-brand'
|
|
10
|
+
export { PLUGIN_STATEMENT_TIMEOUT_MS, pluginRuntimeContext } from './plugin-context'
|
|
11
|
+
export { type PluginLifecyclePhase, runPluginLifecycle } from './plugin-lifecycle'
|
|
12
|
+
export { pluginTasks } from './plugin-tasks'
|
|
13
|
+
export { buildSchedulerBundle, type SchedulerBundle } from './scheduler-bundle'
|
|
14
|
+
export { defaultPromotionGuards, type TaskWorkerDeps, taskWorkers } from './task-workers'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { env, logger } from '@meith/core'
|
|
2
|
+
import { type Database, PostgresSettingsRepository, PostgresThemeRepository } from '@meith/db'
|
|
3
|
+
import {
|
|
4
|
+
buildMailBrand,
|
|
5
|
+
type MailBrand,
|
|
6
|
+
mergeTokenOverrides,
|
|
7
|
+
type ThemeTokenSet,
|
|
8
|
+
} from '@meith/mail'
|
|
9
|
+
import { resolveBoardUrl, SettingsSnapshot } from '@meith/settings'
|
|
10
|
+
import { DARK_TOKENS, LIGHT_TOKENS } from '@meith/theme-default'
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_THEME_KEY = 'default'
|
|
13
|
+
|
|
14
|
+
export type ThemeTokenRegistry = Readonly<Record<string, ThemeTokenSet>>
|
|
15
|
+
|
|
16
|
+
export const SHIPPED_TOKENS: ThemeTokenRegistry = {
|
|
17
|
+
[DEFAULT_THEME_KEY]: { light: LIGHT_TOKENS, dark: DARK_TOKENS },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface MailBrandDeps {
|
|
21
|
+
readonly db: Database
|
|
22
|
+
readonly themeKey?: string
|
|
23
|
+
readonly themeTokens?: ThemeTokenRegistry
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function themeTokens(deps: MailBrandDeps): Promise<ThemeTokenSet | null> {
|
|
27
|
+
const registry = deps.themeTokens ?? SHIPPED_TOKENS
|
|
28
|
+
const buildKey = deps.themeKey ?? DEFAULT_THEME_KEY
|
|
29
|
+
|
|
30
|
+
const rows = await new PostgresThemeRepository(deps.db).listRuntime()
|
|
31
|
+
const claimed = rows.find(
|
|
32
|
+
(row) => row.isDefault && row.enabled && registry[row.key] !== undefined,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const key = claimed?.key ?? buildKey
|
|
36
|
+
const baseline = registry[key] ?? registry[DEFAULT_THEME_KEY] ?? SHIPPED_TOKENS[DEFAULT_THEME_KEY]
|
|
37
|
+
if (baseline === undefined) return null
|
|
38
|
+
|
|
39
|
+
const overrides = rows.find((row) => row.key === key)?.tokenOverrides ?? null
|
|
40
|
+
|
|
41
|
+
return mergeTokenOverrides(baseline, overrides)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function resolveMailBrand(deps: MailBrandDeps): Promise<MailBrand> {
|
|
45
|
+
let boardName = ''
|
|
46
|
+
let fromName = ''
|
|
47
|
+
let boardUrl = ''
|
|
48
|
+
let logo: { lightKey: string; darkKey: string; alt: string } | undefined
|
|
49
|
+
let tokens: ThemeTokenSet | null = null
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const overrides = await new PostgresSettingsRepository(deps.db).loadAll()
|
|
53
|
+
const settings = SettingsSnapshot.fromOverrides(new Map(overrides))
|
|
54
|
+
|
|
55
|
+
boardName = settings.get('board.name')
|
|
56
|
+
fromName = settings.get('mail.from_name')
|
|
57
|
+
boardUrl = resolveBoardUrl({ environment: env, settings }).url
|
|
58
|
+
|
|
59
|
+
const alt = settings.get('board.logo_alt').trim()
|
|
60
|
+
logo = {
|
|
61
|
+
lightKey: settings.get('board.logo_light'),
|
|
62
|
+
darkKey: settings.get('board.logo_dark'),
|
|
63
|
+
alt: alt === '' ? boardName : alt,
|
|
64
|
+
}
|
|
65
|
+
} catch (err) {
|
|
66
|
+
logger({ module: 'mail-brand' }).warn({ err }, 'could not read board name for mail')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
tokens = await themeTokens(deps)
|
|
71
|
+
} catch (err) {
|
|
72
|
+
logger({ module: 'mail-brand' }).warn({ err }, 'could not read theme tokens for mail')
|
|
73
|
+
tokens = SHIPPED_TOKENS[DEFAULT_THEME_KEY] ?? null
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return buildMailBrand({
|
|
77
|
+
boardName,
|
|
78
|
+
fromName,
|
|
79
|
+
boardUrl,
|
|
80
|
+
tokens,
|
|
81
|
+
...(logo === undefined ? {} : { logo }),
|
|
82
|
+
})
|
|
83
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { logger, readPluginEnv } from '@meith/core'
|
|
2
|
+
import {
|
|
3
|
+
type Database,
|
|
4
|
+
PostgresNotificationRepository,
|
|
5
|
+
PostgresSettingsRepository,
|
|
6
|
+
pluginData,
|
|
7
|
+
pluginGrants,
|
|
8
|
+
pluginUsers,
|
|
9
|
+
} from '@meith/db'
|
|
10
|
+
import { NotificationService } from '@meith/notifications'
|
|
11
|
+
import {
|
|
12
|
+
type PluginDefinition,
|
|
13
|
+
type PluginRuntimeContext,
|
|
14
|
+
pluginNotificationKindSpecs,
|
|
15
|
+
pluginNotify,
|
|
16
|
+
resolvePluginSettings,
|
|
17
|
+
} from '@meith/plugin-kit'
|
|
18
|
+
|
|
19
|
+
export const PLUGIN_STATEMENT_TIMEOUT_MS = 30_000
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The context every plugin callback is handed: its resolved settings, a logger
|
|
23
|
+
* that says which plugin is talking, and the four runtime capabilities. Built
|
|
24
|
+
* in one place so a task, an install and a purge cannot drift into offering a
|
|
25
|
+
* plugin different powers depending on which door it came through.
|
|
26
|
+
*/
|
|
27
|
+
export async function pluginRuntimeContext(input: {
|
|
28
|
+
readonly db: Database
|
|
29
|
+
readonly plugin: PluginDefinition
|
|
30
|
+
readonly component: string
|
|
31
|
+
readonly overrides?: ReadonlyMap<string, string>
|
|
32
|
+
readonly statementTimeoutMs?: number
|
|
33
|
+
}): Promise<PluginRuntimeContext> {
|
|
34
|
+
const overrides = input.overrides ?? (await new PostgresSettingsRepository(input.db).loadAll())
|
|
35
|
+
const log = logger({ component: input.component, plugin: input.plugin.key })
|
|
36
|
+
|
|
37
|
+
const notifications = new NotificationService({
|
|
38
|
+
notifications: new PostgresNotificationRepository(input.db),
|
|
39
|
+
extraKinds: pluginNotificationKindSpecs(input.plugin.key, input.plugin.notifications ?? []),
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
settings: resolvePluginSettings(input.plugin, overrides, readPluginEnv),
|
|
44
|
+
logger: {
|
|
45
|
+
info: (message, detail) => log.info(detail ?? {}, message),
|
|
46
|
+
warn: (message, detail) => log.warn(detail ?? {}, message),
|
|
47
|
+
error: (message, detail) => log.error(detail ?? {}, message),
|
|
48
|
+
},
|
|
49
|
+
grants: pluginGrants(input.db, input.plugin.key),
|
|
50
|
+
data: pluginData(input.db, input.plugin.key, {
|
|
51
|
+
statementTimeoutMs: input.statementTimeoutMs ?? PLUGIN_STATEMENT_TIMEOUT_MS,
|
|
52
|
+
}),
|
|
53
|
+
users: pluginUsers(input.db),
|
|
54
|
+
notify: pluginNotify(input.plugin.key, input.plugin.notifications ?? [], notifications),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Database } from '@meith/db'
|
|
2
|
+
import type { PluginDefinition } from '@meith/plugin-kit'
|
|
3
|
+
|
|
4
|
+
import { pluginRuntimeContext } from './plugin-context'
|
|
5
|
+
|
|
6
|
+
export type PluginLifecyclePhase = 'install' | 'enable' | 'disable' | 'uninstall'
|
|
7
|
+
|
|
8
|
+
const CALLBACKS: Readonly<Record<PluginLifecyclePhase, keyof PluginDefinition>> = {
|
|
9
|
+
install: 'onInstall',
|
|
10
|
+
enable: 'onEnable',
|
|
11
|
+
disable: 'onDisable',
|
|
12
|
+
uninstall: 'onUninstall',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Run one lifecycle callback, or report that the plugin declares none.
|
|
17
|
+
*
|
|
18
|
+
* It throws what the callback throws, deliberately. These do not run inside the
|
|
19
|
+
* host's try/catch, because that isolation exists to keep a page rendering and
|
|
20
|
+
* none of these is on a page: an install that half-worked should stop the
|
|
21
|
+
* deploy rather than be swallowed, and each caller states its own policy.
|
|
22
|
+
*/
|
|
23
|
+
export async function runPluginLifecycle(input: {
|
|
24
|
+
readonly db: Database
|
|
25
|
+
readonly plugin: PluginDefinition
|
|
26
|
+
readonly phase: PluginLifecyclePhase
|
|
27
|
+
readonly overrides?: ReadonlyMap<string, string>
|
|
28
|
+
}): Promise<{ readonly ran: boolean }> {
|
|
29
|
+
const callback = input.plugin[CALLBACKS[input.phase]]
|
|
30
|
+
if (typeof callback !== 'function') return { ran: false }
|
|
31
|
+
|
|
32
|
+
const context = await pluginRuntimeContext({
|
|
33
|
+
db: input.db,
|
|
34
|
+
plugin: input.plugin,
|
|
35
|
+
component: `plugin-${input.phase}`,
|
|
36
|
+
...(input.overrides === undefined ? {} : { overrides: input.overrides }),
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
await (callback as (ctx: typeof context) => Promise<void> | void)(context)
|
|
40
|
+
return { ran: true }
|
|
41
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { MailDriver, OutgoingMail } from '@meith/core'
|
|
2
|
+
import type { MarkdownPipeline } from '@meith/markdown'
|
|
3
|
+
import type { PluginHost } from '@meith/plugin-kit'
|
|
4
|
+
|
|
5
|
+
export function pluginMarkdownPipeline(host: PluginHost): MarkdownPipeline {
|
|
6
|
+
return {
|
|
7
|
+
text: (text, context) =>
|
|
8
|
+
host.applyFilter('markdown.parse.text', text, {
|
|
9
|
+
...context.viewer,
|
|
10
|
+
source: context.source,
|
|
11
|
+
}),
|
|
12
|
+
|
|
13
|
+
html: (html, context) =>
|
|
14
|
+
host.applyFilter('markdown.render.html', html, {
|
|
15
|
+
...context.viewer,
|
|
16
|
+
source: context.source,
|
|
17
|
+
}),
|
|
18
|
+
|
|
19
|
+
vocabulary: async (source) => ({
|
|
20
|
+
revision: source.revision,
|
|
21
|
+
directives: await host.applyFilter('markdown.directives', source.directives, {}),
|
|
22
|
+
smilies: await host.applyFilter('smilies.list', source.smilies, {}),
|
|
23
|
+
}),
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A message on its way out of the worker, past the plugins first. Returning
|
|
29
|
+
* null from `mail.send.before` drops it; nothing tells the recipient, because
|
|
30
|
+
* nobody was waiting on a page for it.
|
|
31
|
+
*/
|
|
32
|
+
export async function sendAudited(
|
|
33
|
+
host: PluginHost,
|
|
34
|
+
mail: MailDriver,
|
|
35
|
+
template: string,
|
|
36
|
+
outgoing: OutgoingMail,
|
|
37
|
+
): Promise<void> {
|
|
38
|
+
const proposed = await host.applyFilter(
|
|
39
|
+
'mail.send.before',
|
|
40
|
+
{
|
|
41
|
+
to: outgoing.to,
|
|
42
|
+
subject: outgoing.subject,
|
|
43
|
+
textBody: outgoing.text,
|
|
44
|
+
htmlBody: outgoing.html ?? null,
|
|
45
|
+
},
|
|
46
|
+
{ template },
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
if (proposed === null) return
|
|
50
|
+
|
|
51
|
+
await mail.send({
|
|
52
|
+
...outgoing,
|
|
53
|
+
to: proposed.to,
|
|
54
|
+
subject: proposed.subject,
|
|
55
|
+
text: proposed.textBody,
|
|
56
|
+
...(proposed.htmlBody === null ? {} : { html: proposed.htmlBody }),
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
await host.emit('mail.sent', { to: proposed.to, template }, { requestId: null })
|
|
60
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type Database, PostgresSettingsRepository, readPluginHealth } from '@meith/db'
|
|
2
|
+
import { type PluginDefinition, pluginEnabledKey, pluginTaskId } from '@meith/plugin-kit'
|
|
3
|
+
import type { TaskDefinition } from '@meith/tasks'
|
|
4
|
+
|
|
5
|
+
import { pluginRuntimeContext } from './plugin-context'
|
|
6
|
+
|
|
7
|
+
const MAX_DURATION_SECONDS = 60
|
|
8
|
+
|
|
9
|
+
export function pluginTasks(options: {
|
|
10
|
+
readonly db: Database
|
|
11
|
+
readonly plugins: readonly PluginDefinition[]
|
|
12
|
+
}): TaskDefinition[] {
|
|
13
|
+
const definitions: TaskDefinition[] = []
|
|
14
|
+
|
|
15
|
+
for (const plugin of options.plugins) {
|
|
16
|
+
for (const task of plugin.tasks ?? []) {
|
|
17
|
+
const id = pluginTaskId(plugin.key, task.id)
|
|
18
|
+
|
|
19
|
+
definitions.push({
|
|
20
|
+
id,
|
|
21
|
+
title: `${plugin.name}: ${task.id}`,
|
|
22
|
+
description: `Scheduled task contributed by the "${plugin.key}" plugin.`,
|
|
23
|
+
intervalSeconds: task.intervalSeconds,
|
|
24
|
+
maxDurationSeconds: MAX_DURATION_SECONDS,
|
|
25
|
+
async run() {
|
|
26
|
+
const overrides = await new PostgresSettingsRepository(options.db).loadAll()
|
|
27
|
+
if (overrides.get(pluginEnabledKey(plugin.key)) === '0') return {}
|
|
28
|
+
|
|
29
|
+
const health = (await readPluginHealth(options.db)).find(
|
|
30
|
+
(row) => row.pluginKey === plugin.key,
|
|
31
|
+
)
|
|
32
|
+
if (health?.disabledAt != null) {
|
|
33
|
+
return { detail: { skipped: 'disabled after repeated failures' } }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await task.run(
|
|
37
|
+
await pluginRuntimeContext({
|
|
38
|
+
db: options.db,
|
|
39
|
+
plugin,
|
|
40
|
+
component: 'plugin-task',
|
|
41
|
+
overrides,
|
|
42
|
+
}),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
return {}
|
|
46
|
+
},
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return definitions
|
|
52
|
+
}
|