@meith/subscriptions 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/subscriptions",
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/subscriptions"
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,38 @@
1
+ export {
2
+ CADENCE_INTERVAL_MS,
3
+ DIGEST_CADENCES,
4
+ type DigestCadence,
5
+ isDigestCadence,
6
+ MODE_LABELS,
7
+ parseSubscriptionMode,
8
+ parseSubscriptionTarget,
9
+ SUBSCRIPTION_MODES,
10
+ type SubscriptionMode,
11
+ type SubscriptionTarget,
12
+ } from './modes'
13
+ export {
14
+ type DigestThread,
15
+ groupByThread,
16
+ MAX_POSTS_PER_USER,
17
+ MAX_THREADS_IN_DIGEST,
18
+ MAX_USERS_PER_RUN,
19
+ type RunOutcome,
20
+ SubscriptionNotifier,
21
+ } from './notifier'
22
+ export {
23
+ SUBSCRIPTIONS_PAGE_SIZE,
24
+ SubscriptionService,
25
+ } from './service'
26
+ export {
27
+ mintUnsubscribeToken,
28
+ readUnsubscribeToken,
29
+ type UnsubscribeClaim,
30
+ } from './tokens'
31
+ export type {
32
+ PendingForUser,
33
+ PendingPost,
34
+ SubscriptionNotifierPort,
35
+ SubscriptionRepository,
36
+ SubscriptionRow,
37
+ VisibleForumSource,
38
+ } from './types'
package/src/modes.ts ADDED
@@ -0,0 +1,33 @@
1
+ export const SUBSCRIPTION_MODES = ['instant', 'daily', 'weekly', 'none'] as const
2
+
3
+ export type SubscriptionMode = (typeof SUBSCRIPTION_MODES)[number]
4
+
5
+ export const DIGEST_CADENCES = ['daily', 'weekly'] as const
6
+
7
+ export type DigestCadence = (typeof DIGEST_CADENCES)[number]
8
+
9
+ export const CADENCE_INTERVAL_MS: Readonly<Record<DigestCadence, number>> = {
10
+ daily: 24 * 60 * 60 * 1000,
11
+ weekly: 7 * 24 * 60 * 60 * 1000,
12
+ }
13
+
14
+ export const MODE_LABELS: Readonly<Record<SubscriptionMode, string>> = {
15
+ instant: 'As it happens',
16
+ daily: 'Daily digest',
17
+ weekly: 'Weekly digest',
18
+ none: 'Do not notify me',
19
+ }
20
+
21
+ export function parseSubscriptionMode(value: string): SubscriptionMode | null {
22
+ return SUBSCRIPTION_MODES.includes(value as SubscriptionMode) ? (value as SubscriptionMode) : null
23
+ }
24
+
25
+ export function isDigestCadence(value: string): value is DigestCadence {
26
+ return DIGEST_CADENCES.includes(value as DigestCadence)
27
+ }
28
+
29
+ export type SubscriptionTarget = 'thread' | 'forum'
30
+
31
+ export function parseSubscriptionTarget(value: string): SubscriptionTarget | null {
32
+ return value === 'thread' || value === 'forum' ? value : null
33
+ }
@@ -0,0 +1,184 @@
1
+ import { CADENCE_INTERVAL_MS, type DigestCadence, type SubscriptionMode } from './modes'
2
+ import { mintUnsubscribeToken } from './tokens'
3
+ import type {
4
+ PendingPost,
5
+ SubscriptionNotifierPort,
6
+ SubscriptionRepository,
7
+ VisibleForumSource,
8
+ } from './types'
9
+
10
+ export const MAX_USERS_PER_RUN = 50
11
+
12
+ export const MAX_POSTS_PER_USER = 200
13
+
14
+ export const MAX_THREADS_IN_DIGEST = 10
15
+
16
+ export interface DigestThread {
17
+ readonly threadId: number
18
+ readonly title: string
19
+ readonly href: string
20
+ readonly posts: number
21
+ readonly lastAuthor: string | null
22
+ }
23
+
24
+ export function groupByThread(posts: readonly PendingPost[]): readonly DigestThread[] {
25
+ const byThread = new Map<number, { thread: PendingPost; count: number; last: PendingPost }>()
26
+
27
+ for (const post of posts) {
28
+ const entry = byThread.get(post.threadId)
29
+ if (entry === undefined) {
30
+ byThread.set(post.threadId, { thread: post, count: 1, last: post })
31
+ continue
32
+ }
33
+ entry.count += 1
34
+ if (post.postId > entry.last.postId) entry.last = post
35
+ }
36
+
37
+ return [...byThread.values()]
38
+ .sort((a, b) => b.count - a.count || a.thread.threadId - b.thread.threadId)
39
+ .map(({ thread, count, last }) => ({
40
+ threadId: thread.threadId,
41
+ title: thread.threadTitle,
42
+ href: `/thread/${thread.threadId}-${thread.threadSlug}?after=${thread.postId - 1}`,
43
+ posts: count,
44
+ lastAuthor: last.authorUsername,
45
+ }))
46
+ }
47
+
48
+ export interface RunOutcome {
49
+ readonly notified: number
50
+ readonly considered: number
51
+ }
52
+
53
+ export class SubscriptionNotifier {
54
+ private readonly subscriptions: SubscriptionRepository
55
+ private readonly notifications: SubscriptionNotifierPort
56
+ private readonly forums: VisibleForumSource
57
+ private readonly now: () => Date
58
+
59
+ private readonly secret: string | null
60
+
61
+ constructor(deps: {
62
+ subscriptions: SubscriptionRepository
63
+ notifications: SubscriptionNotifierPort
64
+ forums: VisibleForumSource
65
+ unsubscribeSecret?: string | null
66
+ now?: () => Date
67
+ }) {
68
+ this.subscriptions = deps.subscriptions
69
+ this.notifications = deps.notifications
70
+ this.forums = deps.forums
71
+ this.secret = deps.unsubscribeSecret ?? null
72
+ this.now = deps.now ?? (() => new Date())
73
+ }
74
+
75
+ private token(
76
+ userId: number,
77
+ scope: 'thread' | 'forum' | 'email',
78
+ targetId: number,
79
+ ): string | null {
80
+ return this.secret === null
81
+ ? null
82
+ : mintUnsubscribeToken({ userId, scope, targetId }, this.secret)
83
+ }
84
+
85
+ async runInstant(limit = MAX_USERS_PER_RUN, signal?: AbortSignal): Promise<RunOutcome> {
86
+ return this.run('instant', null, limit, signal, (userId, threads) =>
87
+ Promise.all(
88
+ threads.map((thread) =>
89
+ this.notifications.raise({
90
+ userId,
91
+ kind: 'subscription.reply',
92
+ data: {
93
+ threadTitle: thread.title,
94
+ posts: thread.posts,
95
+ lastAuthor: thread.lastAuthor,
96
+ unsubscribe: this.token(userId, 'thread', thread.threadId),
97
+ },
98
+ href: thread.href,
99
+ dedupeKey: `subscription.reply:${thread.threadId}`,
100
+ }),
101
+ ),
102
+ ).then(() => undefined),
103
+ )
104
+ }
105
+
106
+ async runDigest(
107
+ cadence: DigestCadence,
108
+ limit = MAX_USERS_PER_RUN,
109
+ signal?: AbortSignal,
110
+ ): Promise<RunOutcome> {
111
+ const at = this.now()
112
+ const dueBefore = new Date(at.getTime() - CADENCE_INTERVAL_MS[cadence])
113
+
114
+ return this.run(cadence, dueBefore, limit, signal, async (userId, threads) => {
115
+ const totalPosts = threads.reduce((sum, thread) => sum + thread.posts, 0)
116
+
117
+ await this.notifications.raise({
118
+ userId,
119
+ kind: 'subscription.digest',
120
+ data: {
121
+ cadence,
122
+ threadCount: threads.length,
123
+ postCount: totalPosts,
124
+ threads: threads.slice(0, MAX_THREADS_IN_DIGEST).map((thread) => ({
125
+ title: thread.title,
126
+ href: thread.href,
127
+ posts: thread.posts,
128
+ lastAuthor: thread.lastAuthor,
129
+ })),
130
+ more: Math.max(0, threads.length - MAX_THREADS_IN_DIGEST),
131
+ unsubscribe: this.token(userId, 'email', 0),
132
+ },
133
+ href: '/subscriptions',
134
+ dedupeKey: null,
135
+ })
136
+
137
+ await this.subscriptions.recordDigestRun({ userId, cadence, at })
138
+ })
139
+ }
140
+
141
+ private async run(
142
+ mode: SubscriptionMode,
143
+ dueBefore: Date | null,
144
+ limit: number,
145
+ signal: AbortSignal | undefined,
146
+ tell: (userId: number, threads: readonly DigestThread[]) => Promise<void>,
147
+ ): Promise<RunOutcome> {
148
+ const users = await this.subscriptions.usersWithPending({ mode, dueBefore, limit })
149
+
150
+ let notified = 0
151
+ let considered = 0
152
+ for (const userId of users) {
153
+ if (signal?.aborted === true) break
154
+ considered += 1
155
+
156
+ try {
157
+ const visibleForumIds = await this.forums.visibleForumIdsFor(userId)
158
+ const pending = await this.subscriptions.pendingFor({
159
+ userId,
160
+ mode,
161
+ visibleForumIds,
162
+ limit: MAX_POSTS_PER_USER,
163
+ })
164
+
165
+ const threads = groupByThread(pending.posts)
166
+ if (threads.length > 0) {
167
+ await tell(userId, threads)
168
+ notified += 1
169
+ }
170
+
171
+ if (pending.watermarks.length > 0) {
172
+ await this.subscriptions.advanceWatermarks({
173
+ userId,
174
+ watermarks: pending.watermarks,
175
+ })
176
+ }
177
+ } catch {
178
+ /* ignore */
179
+ }
180
+ }
181
+
182
+ return { notified, considered }
183
+ }
184
+ }
package/src/service.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { ValidationError } from '@meith/core'
2
+ import { msg } from '@meith/i18n'
3
+
4
+ import { parseSubscriptionMode, type SubscriptionMode, type SubscriptionTarget } from './modes'
5
+ import type { SubscriptionRepository, SubscriptionRow } from './types'
6
+
7
+ export const SUBSCRIPTIONS_PAGE_SIZE = 200
8
+
9
+ export class SubscriptionService {
10
+ private readonly repository: SubscriptionRepository
11
+ private readonly now: () => Date
12
+
13
+ constructor(deps: { subscriptions: SubscriptionRepository; now?: () => Date }) {
14
+ this.repository = deps.subscriptions
15
+ this.now = deps.now ?? (() => new Date())
16
+ }
17
+
18
+ async subscribe(input: {
19
+ readonly userId: number
20
+ readonly target: SubscriptionTarget
21
+ readonly targetId: number
22
+ readonly mode: string
23
+ readonly mayView: boolean
24
+ }): Promise<SubscriptionMode> {
25
+ if (!input.mayView) throw new ValidationError(msg('error.subscriptions.exist'))
26
+
27
+ const mode = parseSubscriptionMode(input.mode)
28
+ if (mode === null) throw new ValidationError(msg('error.subscriptions.notification-setting'))
29
+
30
+ const written = await this.repository.subscribe({
31
+ userId: input.userId,
32
+ target: input.target,
33
+ targetId: input.targetId,
34
+ mode,
35
+ at: this.now(),
36
+ })
37
+ if (!written) throw new ValidationError(msg('error.subscriptions.exist'))
38
+
39
+ return mode
40
+ }
41
+
42
+ async unsubscribe(input: {
43
+ readonly userId: number
44
+ readonly target: SubscriptionTarget
45
+ readonly targetId: number
46
+ }): Promise<boolean> {
47
+ return this.repository.unsubscribe(input)
48
+ }
49
+
50
+ async modeFor(
51
+ userId: number,
52
+ target: SubscriptionTarget,
53
+ targetId: number,
54
+ ): Promise<SubscriptionMode | null> {
55
+ return this.repository.modeFor(userId, target, targetId)
56
+ }
57
+
58
+ async list(
59
+ userId: number,
60
+ visibleForumIds: readonly number[],
61
+ ): Promise<readonly SubscriptionRow[]> {
62
+ return this.repository.listFor(userId, {
63
+ visibleForumIds,
64
+ limit: SUBSCRIPTIONS_PAGE_SIZE,
65
+ })
66
+ }
67
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,61 @@
1
+ import { createHmac } from 'node:crypto'
2
+
3
+ import { timingSafeEqualString } from '@meith/core'
4
+
5
+ import type { SubscriptionTarget } from './modes'
6
+
7
+ export type UnsubscribeScope = SubscriptionTarget | 'email'
8
+
9
+ export function parseUnsubscribeScope(value: string): UnsubscribeScope | null {
10
+ return value === 'thread' || value === 'forum' || value === 'email' ? value : null
11
+ }
12
+
13
+ export interface UnsubscribeClaim {
14
+ readonly userId: number
15
+ readonly scope: UnsubscribeScope
16
+ readonly targetId: number
17
+ }
18
+
19
+ const VERSION = 'v1'
20
+
21
+ const DIGEST_LENGTH = 32
22
+
23
+ function digest(payload: string, secret: string): string {
24
+ return createHmac('sha256', secret).update(payload).digest('base64url').slice(0, DIGEST_LENGTH)
25
+ }
26
+
27
+ function payloadOf(claim: UnsubscribeClaim): string {
28
+ return `${VERSION}.${claim.userId}.${claim.scope}.${claim.targetId}`
29
+ }
30
+
31
+ export function mintUnsubscribeToken(claim: UnsubscribeClaim, secret: string): string {
32
+ return `${payloadOf(claim)}.${digest(payloadOf(claim), secret)}`
33
+ }
34
+
35
+ export function readUnsubscribeToken(token: string, secret: string): UnsubscribeClaim | null {
36
+ const parts = token.split('.')
37
+ if (parts.length !== 5) return null
38
+
39
+ const [version, rawUserId, rawScope, rawTargetId, signature] = parts as [
40
+ string,
41
+ string,
42
+ string,
43
+ string,
44
+ string,
45
+ ]
46
+ if (version !== VERSION) return null
47
+
48
+ const scope = parseUnsubscribeScope(rawScope)
49
+ if (scope === null) return null
50
+
51
+ const targetPattern = scope === 'email' ? /^0$/ : /^[1-9]\d*$/
52
+ if (!/^[1-9]\d*$/.test(rawUserId) || !targetPattern.test(rawTargetId)) return null
53
+ const userId = Number(rawUserId)
54
+ const targetId = Number(rawTargetId)
55
+ if (!Number.isSafeInteger(userId) || !Number.isSafeInteger(targetId)) return null
56
+
57
+ const claim: UnsubscribeClaim = { userId, scope, targetId }
58
+ if (!timingSafeEqualString(signature, digest(payloadOf(claim), secret))) return null
59
+
60
+ return claim
61
+ }
package/src/types.ts ADDED
@@ -0,0 +1,96 @@
1
+ import type { DigestCadence, SubscriptionMode, SubscriptionTarget } from './modes'
2
+
3
+ export interface SubscriptionRow {
4
+ readonly target: SubscriptionTarget
5
+ readonly targetId: number
6
+ readonly title: string
7
+ readonly href: string
8
+ readonly mode: SubscriptionMode
9
+ readonly createdAt: Date
10
+ readonly pending: number
11
+ }
12
+
13
+ export interface PendingPost {
14
+ readonly postId: number
15
+ readonly threadId: number
16
+ readonly threadTitle: string
17
+ readonly threadSlug: string
18
+ readonly forumId: number
19
+ readonly authorUsername: string | null
20
+ readonly createdAt: Date
21
+ }
22
+
23
+ export interface PendingForUser {
24
+ readonly userId: number
25
+ readonly posts: readonly PendingPost[]
26
+ readonly watermarks: readonly {
27
+ readonly target: SubscriptionTarget
28
+ readonly targetId: number
29
+ readonly lastPostId: number
30
+ }[]
31
+ }
32
+
33
+ export interface SubscriptionRepository {
34
+ subscribe(input: {
35
+ readonly userId: number
36
+ readonly target: SubscriptionTarget
37
+ readonly targetId: number
38
+ readonly mode: SubscriptionMode
39
+ readonly at: Date
40
+ }): Promise<boolean>
41
+
42
+ unsubscribe(input: {
43
+ readonly userId: number
44
+ readonly target: SubscriptionTarget
45
+ readonly targetId: number
46
+ }): Promise<boolean>
47
+
48
+ modeFor(
49
+ userId: number,
50
+ target: SubscriptionTarget,
51
+ targetId: number,
52
+ ): Promise<SubscriptionMode | null>
53
+
54
+ listFor(
55
+ userId: number,
56
+ options: { readonly visibleForumIds: readonly number[]; readonly limit: number },
57
+ ): Promise<readonly SubscriptionRow[]>
58
+
59
+ usersWithPending(input: {
60
+ readonly mode: SubscriptionMode
61
+ readonly dueBefore: Date | null
62
+ readonly limit: number
63
+ }): Promise<readonly number[]>
64
+
65
+ pendingFor(input: {
66
+ readonly userId: number
67
+ readonly mode: SubscriptionMode
68
+ readonly visibleForumIds: readonly number[]
69
+ readonly limit: number
70
+ }): Promise<PendingForUser>
71
+
72
+ advanceWatermarks(input: {
73
+ readonly userId: number
74
+ readonly watermarks: PendingForUser['watermarks']
75
+ }): Promise<void>
76
+
77
+ recordDigestRun(input: {
78
+ readonly userId: number
79
+ readonly cadence: DigestCadence
80
+ readonly at: Date
81
+ }): Promise<void>
82
+ }
83
+
84
+ export interface SubscriptionNotifierPort {
85
+ raise(input: {
86
+ readonly userId: number
87
+ readonly kind: 'subscription.reply' | 'subscription.digest'
88
+ readonly data: Record<string, unknown>
89
+ readonly href: string | null
90
+ readonly dedupeKey: string | null
91
+ }): Promise<void>
92
+ }
93
+
94
+ export interface VisibleForumSource {
95
+ visibleForumIdsFor(userId: number): Promise<readonly number[]>
96
+ }