@meith/moderation 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 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,25 @@
1
+ {
2
+ "name": "@meith/moderation",
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/moderation"
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
+ }
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1,101 @@
1
+ export {
2
+ INLINE_CHUNK,
3
+ INLINE_TOOL_ACTIONS,
4
+ InlineModeration,
5
+ type InlineModerationRepository,
6
+ type InlineOutcome,
7
+ type InlineRights,
8
+ type InlineRightsResolver,
9
+ type InlineTarget,
10
+ type InlineTool,
11
+ MAX_INLINE_SELECTION,
12
+ NO_INLINE_RIGHTS,
13
+ parseInlineTool,
14
+ } from './inline'
15
+ export {
16
+ type IpMatch,
17
+ MOD_LOG_ACTIONS,
18
+ MOD_LOG_LABEL_KEYS,
19
+ MODCP_PAGE_SIZE,
20
+ type ModCpRepository,
21
+ type ModeratedForum,
22
+ ModeratorPanel,
23
+ type ModeratorPanelRights,
24
+ type ModLogEntry,
25
+ type ModLogPage,
26
+ } from './modcp'
27
+ export {
28
+ MAX_CHUNK,
29
+ ModerationQueue,
30
+ type ModerationQueueRepository,
31
+ type PendingItem,
32
+ parseSelection,
33
+ QUEUE_PAGE_SIZE,
34
+ type QueueDecision,
35
+ type QueueItem,
36
+ type QueueItemKind,
37
+ type QueueOutcome,
38
+ type QueuePage,
39
+ type QueueSelection,
40
+ } from './queue'
41
+ export {
42
+ inScope,
43
+ type NewReport,
44
+ parseTargetKind,
45
+ REASON_MAX,
46
+ REASON_MIN,
47
+ REPORT_TARGET_KINDS,
48
+ REPORTS_PAGE_SIZE,
49
+ type ReportEvent,
50
+ type ReportNotifierPort,
51
+ type ReportPage,
52
+ type ReportRepository,
53
+ type ReportRow,
54
+ type ReportScope,
55
+ ReportService,
56
+ type ReportStatus,
57
+ type ReportTarget,
58
+ type ReportTargetKind,
59
+ } from './reports'
60
+ export {
61
+ type MergePlan,
62
+ type SplitPlan,
63
+ type SurgeryOutcome,
64
+ type SurgeryRights,
65
+ type SurgeryThread,
66
+ ThreadSurgery,
67
+ type ThreadSurgeryRepository,
68
+ } from './surgery'
69
+ export {
70
+ keepsThreads,
71
+ type MoveDestination,
72
+ parseThreadTool,
73
+ type ThreadTool,
74
+ type ThreadToolOutcome,
75
+ type ThreadToolRights,
76
+ ThreadTools,
77
+ type ThreadToolsRepository,
78
+ type ThreadToolTarget,
79
+ } from './thread-tools'
80
+ export {
81
+ type IssuedWarning,
82
+ NO_RESTRICTION,
83
+ POINTS_MAX,
84
+ type PostingRestriction,
85
+ parseWarningAction,
86
+ REASON_MAX as WARNING_REASON_MAX,
87
+ restrictsPosting,
88
+ TITLE_MAX as WARNING_TITLE_MAX,
89
+ WARNING_ACTIONS,
90
+ WARNINGS_PAGE_SIZE,
91
+ type WarningAction,
92
+ type WarningBanPort,
93
+ type WarningLevel,
94
+ type WarningNotifierPort,
95
+ type WarningPage,
96
+ type WarningRepository,
97
+ type WarningRow,
98
+ WarningService,
99
+ type WarningStanding,
100
+ type WarningType,
101
+ } from './warnings'
package/src/inline.ts ADDED
@@ -0,0 +1,269 @@
1
+ import { ValidationError } from '@meith/core'
2
+ import { msg } from '@meith/i18n'
3
+
4
+ import type { QueueSelection } from './queue'
5
+ import { keepsThreads, type MoveDestination } from './thread-tools'
6
+
7
+ export type InlineTool =
8
+ | 'approve'
9
+ | 'delete'
10
+ | 'restore'
11
+ | 'lock'
12
+ | 'unlock'
13
+ | 'stick'
14
+ | 'unstick'
15
+ | 'move'
16
+
17
+ export interface InlineTarget extends QueueSelection {
18
+ readonly forumId: number
19
+ readonly visibility: 'visible' | 'unapproved' | 'deleted'
20
+ readonly threadVisibility: 'visible' | 'unapproved' | 'deleted'
21
+ readonly isLocked: boolean
22
+ readonly isSticky: boolean
23
+ }
24
+
25
+ export interface InlineRights {
26
+ readonly approve: boolean
27
+ readonly lock: boolean
28
+ readonly stick: boolean
29
+ readonly move: boolean
30
+ readonly deleteThreads: boolean
31
+ readonly deletePosts: boolean
32
+ readonly restoreThreads: boolean
33
+ readonly restorePosts: boolean
34
+ }
35
+
36
+ export const NO_INLINE_RIGHTS: InlineRights = {
37
+ approve: false,
38
+ lock: false,
39
+ stick: false,
40
+ move: false,
41
+ deleteThreads: false,
42
+ deletePosts: false,
43
+ restoreThreads: false,
44
+ restorePosts: false,
45
+ }
46
+
47
+ export interface InlineRightsResolver {
48
+ rightsIn(forumId: number): Promise<InlineRights>
49
+ }
50
+
51
+ export interface InlineOutcome {
52
+ readonly tool: InlineTool
53
+ readonly applied: number
54
+ readonly refused: number
55
+ readonly missing: number
56
+ readonly skipped: number
57
+ }
58
+
59
+ export interface InlineModerationRepository {
60
+ resolve(
61
+ selection: readonly QueueSelection[],
62
+ forumIds: readonly number[],
63
+ ): Promise<readonly InlineTarget[]>
64
+
65
+ findDestination(forumId: number): Promise<MoveDestination | null>
66
+
67
+ apply(input: {
68
+ readonly tool: InlineTool
69
+ readonly threadIds: readonly number[]
70
+ readonly postIds: readonly number[]
71
+ readonly toForumId?: number | undefined
72
+ readonly actorUserId: number
73
+ readonly at: Date
74
+ }): Promise<number>
75
+ }
76
+
77
+ export const INLINE_CHUNK = 25
78
+
79
+ export const MAX_INLINE_SELECTION = 500
80
+
81
+ const THREAD_ONLY: ReadonlySet<InlineTool> = new Set<InlineTool>([
82
+ 'lock',
83
+ 'unlock',
84
+ 'stick',
85
+ 'unstick',
86
+ 'move',
87
+ ])
88
+
89
+ export class InlineModeration {
90
+ private readonly repo: InlineModerationRepository
91
+ private readonly now: () => Date
92
+
93
+ constructor(deps: { inline: InlineModerationRepository; now?: () => Date }) {
94
+ this.repo = deps.inline
95
+ this.now = deps.now ?? (() => new Date())
96
+ }
97
+
98
+ async apply(input: {
99
+ readonly selection: readonly QueueSelection[]
100
+ readonly tool: InlineTool
101
+ readonly toForumId?: number | undefined
102
+ readonly scopeForumIds: readonly number[]
103
+ readonly rights: InlineRightsResolver
104
+ readonly actorUserId: number
105
+ }): Promise<InlineOutcome> {
106
+ if (input.selection.length === 0) {
107
+ throw new ValidationError(msg('error.moderation.select-at-least-one-item'))
108
+ }
109
+ if (input.selection.length > MAX_INLINE_SELECTION) {
110
+ throw new ValidationError(msg('error.moderation.inline-chunk', { max: MAX_INLINE_SELECTION }))
111
+ }
112
+
113
+ const unique = new Map<string, QueueSelection>()
114
+ for (const item of input.selection) unique.set(`${item.kind}:${item.id}`, item)
115
+
116
+ if (input.scopeForumIds.length === 0) {
117
+ return { tool: input.tool, applied: 0, refused: 0, missing: unique.size, skipped: 0 }
118
+ }
119
+
120
+ const targets = await this.repo.resolve([...unique.values()], input.scopeForumIds)
121
+ const missing = unique.size - targets.length
122
+
123
+ if (input.tool === 'move') {
124
+ await this.requireDestination(input.toForumId, input.rights)
125
+ }
126
+
127
+ let refused = 0
128
+ let skipped = 0
129
+ const allowed: InlineTarget[] = []
130
+ const rightsByForum = new Map<number, InlineRights>()
131
+
132
+ for (const target of targets) {
133
+ let rights = rightsByForum.get(target.forumId)
134
+ if (rights === undefined) {
135
+ rights = await input.rights.rightsIn(target.forumId)
136
+ rightsByForum.set(target.forumId, rights)
137
+ }
138
+
139
+ if (!holdsRight(input.tool, target.kind, rights)) {
140
+ refused += 1
141
+ continue
142
+ }
143
+ if (!isApplicable(input.tool, target, input.toForumId)) {
144
+ skipped += 1
145
+ continue
146
+ }
147
+ allowed.push(target)
148
+ }
149
+
150
+ let applied = 0
151
+ for (const chunk of chunked(allowed, INLINE_CHUNK)) {
152
+ applied += await this.repo.apply({
153
+ tool: input.tool,
154
+ threadIds: chunk.filter((i) => i.kind === 'thread').map((i) => i.id),
155
+ postIds: chunk.filter((i) => i.kind === 'post').map((i) => i.id),
156
+ ...(input.toForumId === undefined ? {} : { toForumId: input.toForumId }),
157
+ actorUserId: input.actorUserId,
158
+ at: this.now(),
159
+ })
160
+ }
161
+
162
+ return {
163
+ tool: input.tool,
164
+ applied,
165
+ refused,
166
+ missing,
167
+ skipped: skipped + (allowed.length - applied),
168
+ }
169
+ }
170
+
171
+ private async requireDestination(
172
+ toForumId: number | undefined,
173
+ rights: InlineRightsResolver,
174
+ ): Promise<void> {
175
+ if (toForumId === undefined) {
176
+ throw new ValidationError(msg('error.moderation.choose-forum-move-them'))
177
+ }
178
+ const destinationRights = await rights.rightsIn(toForumId)
179
+ if (!destinationRights.move) {
180
+ throw new ValidationError(msg('error.moderation.move-threads-into-forum'))
181
+ }
182
+ const destination = await this.repo.findDestination(toForumId)
183
+ if (destination === null || !keepsThreads(destination)) {
184
+ throw new ValidationError(msg('error.moderation.forum-threads-live'))
185
+ }
186
+ }
187
+ }
188
+
189
+ function holdsRight(tool: InlineTool, kind: QueueSelection['kind'], rights: InlineRights): boolean {
190
+ if (THREAD_ONLY.has(tool) && kind === 'post') return true
191
+
192
+ switch (tool) {
193
+ case 'approve':
194
+ return rights.approve
195
+ case 'delete':
196
+ return kind === 'thread' ? rights.deleteThreads : rights.deletePosts
197
+ case 'restore':
198
+ return kind === 'thread' ? rights.restoreThreads : rights.restorePosts
199
+ case 'lock':
200
+ case 'unlock':
201
+ return rights.lock
202
+ case 'stick':
203
+ case 'unstick':
204
+ return rights.stick
205
+ case 'move':
206
+ return rights.move
207
+ }
208
+ }
209
+
210
+ function isApplicable(
211
+ tool: InlineTool,
212
+ target: InlineTarget,
213
+ toForumId: number | undefined,
214
+ ): boolean {
215
+ if (THREAD_ONLY.has(tool) && target.kind === 'post') return false
216
+
217
+ switch (tool) {
218
+ case 'approve':
219
+ return (
220
+ target.visibility === 'unapproved' &&
221
+ (target.kind === 'thread' || target.threadVisibility === 'visible')
222
+ )
223
+ case 'delete':
224
+ return target.visibility === 'visible'
225
+ case 'restore':
226
+ return target.visibility === 'deleted'
227
+ case 'lock':
228
+ return target.visibility === 'visible' && !target.isLocked
229
+ case 'unlock':
230
+ return target.visibility === 'visible' && target.isLocked
231
+ case 'stick':
232
+ return target.visibility === 'visible' && !target.isSticky
233
+ case 'unstick':
234
+ return target.visibility === 'visible' && target.isSticky
235
+ case 'move':
236
+ return target.visibility === 'visible' && target.forumId !== toForumId
237
+ }
238
+ }
239
+
240
+ function chunked<T>(items: readonly T[], size: number): T[][] {
241
+ const out: T[][] = []
242
+ for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))
243
+ return out
244
+ }
245
+
246
+ export function parseInlineTool(value: string | undefined): InlineTool | null {
247
+ const tools: readonly InlineTool[] = [
248
+ 'approve',
249
+ 'delete',
250
+ 'restore',
251
+ 'lock',
252
+ 'unlock',
253
+ 'stick',
254
+ 'unstick',
255
+ 'move',
256
+ ]
257
+ return tools.includes(value as InlineTool) ? (value as InlineTool) : null
258
+ }
259
+
260
+ export const INLINE_TOOL_ACTIONS = {
261
+ approve: ['content.approve'],
262
+ lock: ['thread.lock'],
263
+ unlock: ['thread.lock'],
264
+ stick: ['thread.stick'],
265
+ unstick: ['thread.stick'],
266
+ move: ['thread.move'],
267
+ delete: ['thread.delete', 'post.softDelete'],
268
+ restore: ['thread.restore', 'post.restore'],
269
+ } as const satisfies Readonly<Record<InlineTool, readonly string[]>>