@open-pencil/cloud 0.17.0-beta.5 → 0.17.0-beta.7
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/dist/client.d.ts +560 -93
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1101 -186
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +390 -44
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +366 -3
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +1403 -556
- package/dist/email.js.map +1 -1
- package/dist/errors.js +16 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +878 -37
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +12 -2
- package/dist/index2.d.ts.map +1 -1
- package/dist/listen.mjs +589 -128
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +4 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -1
- package/dist/runtime-cloudflare.js +8690 -6325
- package/dist/runtime-cloudflare.js.map +1 -1
- package/dist/runtime-node.d.mts +10098 -5401
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +13807 -7638
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +11723 -9412
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +10659 -8650
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +79 -57
- package/dist/server2.mjs.map +1 -1
- package/package.json +13 -4
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +9 -1
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +119 -4
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +9 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +29 -2
- package/src/client/comments.ts +205 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +4 -2
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +3 -6
- package/src/client/portal/account.ts +20 -2
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +4 -2
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +7 -1
- package/src/contract/account.ts +61 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +73 -4
- package/src/contract/comments.ts +223 -0
- package/src/contract/discovery.ts +47 -19
- package/src/contract/index.ts +10 -0
- package/src/contract/links.ts +5 -0
- package/src/contract/relay.ts +3 -1
- package/src/contract/services.ts +44 -4
- package/src/contract/workspaces.ts +5 -0
- package/src/email/locales/ar.json +28 -0
- package/src/email/locales/de.json +28 -0
- package/src/email/locales/es.json +28 -0
- package/src/email/locales/fr.json +28 -0
- package/src/email/locales/it.json +28 -0
- package/src/email/locales/ja.json +28 -0
- package/src/email/locales/pl.json +28 -0
- package/src/email/locales/pt-br.json +28 -0
- package/src/email/locales/ru.json +28 -0
- package/src/email/locales/zh-cn.json +28 -0
- package/src/email/messages.ts +35 -0
- package/src/email/render.ts +18 -0
- package/src/email/samples.ts +28 -1
- package/src/email/schema.ts +29 -3
- package/src/email/templates/BrandEmail.vue +3 -1
- package/src/email/templates/CommentNotification.vue +39 -0
- package/src/email/templates/EnrollmentInvitation.vue +53 -0
- package/src/email/templates/TwoStepChanged.vue +17 -2
- package/src/email/templates/brand.ts +6 -0
- package/src/errors/posthog.ts +28 -4
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +21 -6
- package/src/runtime/cloudflare/worker.ts +6 -0
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +1 -0
- package/src/runtime/node/server.ts +8 -2
- package/src/runtime/posthog/errors.ts +3 -1
- package/src/runtime/site/server.ts +4 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +38 -4
- package/src/server/account/routes.ts +67 -46
- package/src/server/account/tokens.ts +3 -3
- package/src/server/api/router.ts +22 -7
- package/src/server/app/checks.ts +6 -1
- package/src/server/app/cors.ts +44 -0
- package/src/server/app/index.ts +76 -76
- package/src/server/app/routes.ts +14 -5
- package/src/server/app/services.ts +49 -15
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +16 -3
- package/src/server/auth/email.ts +29 -17
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/factory.ts +83 -64
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +147 -10
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +2 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +1 -1
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +55 -24
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- package/src/server/community/routes.ts +6 -6
- package/src/server/config/deployment.ts +19 -6
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +19 -6
- package/src/server/config/schema.ts +14 -2
- package/src/server/config/toml/schema.ts +27 -4
- package/src/server/db/index.ts +2 -1
- package/src/server/db/migrate.ts +21 -1
- package/src/server/db/migrations/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/tables.ts +92 -2
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +19 -5
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +5 -2
- package/src/server/email/service.ts +8 -2
- package/src/server/email/types.ts +1 -0
- package/src/server/errors/index.ts +48 -3
- package/src/server/index.ts +15 -1
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/pictures/service.ts +3 -1
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/reports/routes.ts +2 -2
- package/src/server/reports/service.ts +3 -1
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +1 -1
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +3 -4
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +33 -25
- package/src/server/workspaces/members.ts +50 -12
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +30 -19
- package/src/services/config.ts +47 -8
- package/src/services/metering.ts +15 -27
- package/src/client/billing.ts +0 -80
|
@@ -56,8 +56,8 @@ export function createRevisionCompactionService(
|
|
|
56
56
|
} = {}
|
|
57
57
|
): RevisionCompactionService {
|
|
58
58
|
const deploymentMode = options.deploymentMode ?? 'self-hosted'
|
|
59
|
-
const report = (message: string, error: unknown) =>
|
|
60
|
-
reportCloudError(options.reportError, message, error, { source: 'cleanup' })
|
|
59
|
+
const report = (message: string, error: unknown, resources?: Record<string, string>) =>
|
|
60
|
+
reportCloudError(options.reportError, message, error, { source: 'cleanup', resources })
|
|
61
61
|
async function pruneVersions(now: Date, batchSize: number): Promise<number> {
|
|
62
62
|
if (!options.policy) return 0
|
|
63
63
|
const limitedBefore = new Date(now.getTime() - LIMITED_HISTORY_DAYS * 24 * 60 * 60_000)
|
|
@@ -225,7 +225,10 @@ export function createRevisionCompactionService(
|
|
|
225
225
|
result.bytesReclaimed += compacted.bytes
|
|
226
226
|
} catch (error) {
|
|
227
227
|
result.failed++
|
|
228
|
-
await report(`Compacting document ${documentId} failed`, error
|
|
228
|
+
await report(`Compacting document ${documentId} failed`, error, {
|
|
229
|
+
documentId,
|
|
230
|
+
workspaceId
|
|
231
|
+
})
|
|
229
232
|
}
|
|
230
233
|
}
|
|
231
234
|
// Compaction just freed the images of the saves it deleted; the release lets their grace pass.
|
|
@@ -102,7 +102,8 @@ export function createUploadCleanupService(
|
|
|
102
102
|
} catch (error) {
|
|
103
103
|
failed++
|
|
104
104
|
await reportCloudError(reportError, `Cleaning up upload ${upload.id} failed`, error, {
|
|
105
|
-
source: 'cleanup'
|
|
105
|
+
source: 'cleanup',
|
|
106
|
+
resources: { uploadId: upload.id }
|
|
106
107
|
})
|
|
107
108
|
await database
|
|
108
109
|
.updateTable('upload')
|
|
@@ -52,7 +52,8 @@ export type ConnectionRelay = {
|
|
|
52
52
|
/** Handles a peer's room message; a promise while it waits for the room's document. */
|
|
53
53
|
deliver(peer: RelayPeer, frame: RelayRoomFrame): Promise<void> | undefined
|
|
54
54
|
depart(peer: RelayPeer): void
|
|
55
|
-
|
|
55
|
+
/** Reports a failure, naming the ticket's document and person once the ticket verified. */
|
|
56
|
+
report(message: string, error: unknown, authorization?: RelayAuthorization): void
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
/**
|
|
@@ -66,6 +67,8 @@ export function openRelayConnection(
|
|
|
66
67
|
relay: ConnectionRelay
|
|
67
68
|
): RelayConnection {
|
|
68
69
|
let peer: RelayPeer | null = resumed
|
|
70
|
+
/** The last ticket that passed every check, which a failure handling it reports. */
|
|
71
|
+
let verified: RelayAuthorization | undefined
|
|
69
72
|
/** Read through an object, since it changes across awaits and between messages. */
|
|
70
73
|
const state = { closed: false, busy: false }
|
|
71
74
|
const isClosed = () => state.closed
|
|
@@ -107,11 +110,13 @@ export function openRelayConnection(
|
|
|
107
110
|
socket.close(RELAY_CLOSE.unauthorized, 'Ticket is for a different room or person')
|
|
108
111
|
return
|
|
109
112
|
}
|
|
113
|
+
verified = authorization
|
|
110
114
|
peer.authorization = authorization
|
|
111
115
|
relay.refreshed(peer)
|
|
112
116
|
return
|
|
113
117
|
}
|
|
114
118
|
clearTimeout(authTimer)
|
|
119
|
+
verified = authorization
|
|
115
120
|
const admitted = await relay.admit(socket, authorization)
|
|
116
121
|
// The socket may have closed while the room loaded.
|
|
117
122
|
if (admitted && isClosed()) relay.depart(admitted)
|
|
@@ -138,7 +143,7 @@ export function openRelayConnection(
|
|
|
138
143
|
state.busy = true
|
|
139
144
|
void authenticate(frame.token)
|
|
140
145
|
.catch((error: unknown) => {
|
|
141
|
-
relay.report('
|
|
146
|
+
relay.report('Checking a collaboration ticket failed', error, verified)
|
|
142
147
|
socket.close(RELAY_CLOSE.protocol, 'Could not open the collaboration room')
|
|
143
148
|
})
|
|
144
149
|
.finally(() => {
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
9
9
|
import * as Y from 'yjs'
|
|
10
10
|
|
|
11
|
+
import { DEADLINE_MS, SECOND_MS } from '@open-pencil/std/timing'
|
|
12
|
+
|
|
11
13
|
import type { RelayAuthorization } from './authorize'
|
|
12
14
|
import { stampAwareness } from './awareness'
|
|
13
15
|
import {
|
|
@@ -33,8 +35,26 @@ import {
|
|
|
33
35
|
type RoomContent
|
|
34
36
|
} from './room'
|
|
35
37
|
|
|
38
|
+
/** How long a room waits after an edit before saving, so a burst of edits saves once. */
|
|
39
|
+
const PERSIST_DELAY_MS = 2 * SECOND_MS
|
|
40
|
+
/** How long a room waits to save again after a save failed. */
|
|
41
|
+
const PERSIST_RETRY_MS = 30 * SECOND_MS
|
|
42
|
+
/** How long a new socket has to present its ticket before the room closes it. */
|
|
43
|
+
const AUTH_TIMEOUT_MS = DEADLINE_MS.request
|
|
44
|
+
|
|
36
45
|
export type { RelayConnection, RelaySocket } from './connection'
|
|
37
46
|
|
|
47
|
+
/**
|
|
48
|
+
* What a relay failure names: the room's document, and the user whose message met it. A peer
|
|
49
|
+
* that failed to be admitted names only the document a single-document relay serves.
|
|
50
|
+
*/
|
|
51
|
+
export type RelayErrorIds = { documentId?: string; userId?: string }
|
|
52
|
+
|
|
53
|
+
/** A verified ticket's document, and its person when they signed in rather than came as a guest. */
|
|
54
|
+
function ticketIds({ documentId, principal }: RelayAuthorization): RelayErrorIds {
|
|
55
|
+
return principal.kind === 'user' ? { documentId, userId: principal.userId } : { documentId }
|
|
56
|
+
}
|
|
57
|
+
|
|
38
58
|
export type CollaborationRelayOptions = {
|
|
39
59
|
authSecret: string
|
|
40
60
|
store: CollaborationStateStore
|
|
@@ -73,7 +93,8 @@ export type CollaborationRelayOptions = {
|
|
|
73
93
|
persistRetryMs?: number
|
|
74
94
|
/** How long a socket may stay open without presenting a ticket. */
|
|
75
95
|
authTimeoutMs?: number
|
|
76
|
-
|
|
96
|
+
/** Told of each failure, with the document and, when one asked, the signed-in user. */
|
|
97
|
+
onError?: (message: string, error: unknown, ids?: RelayErrorIds) => void
|
|
77
98
|
}
|
|
78
99
|
|
|
79
100
|
/** Messages a viewer may send; their document writes never leave the relay. */
|
|
@@ -93,8 +114,8 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
93
114
|
const closing = new Map<string, Promise<void>>()
|
|
94
115
|
/** Set by `close()`: a failed save then drops the room instead of retrying. */
|
|
95
116
|
let shuttingDown = false
|
|
96
|
-
const persistDelayMs = options.persistDelayMs ??
|
|
97
|
-
const persistRetryMs = options.persistRetryMs ??
|
|
117
|
+
const persistDelayMs = options.persistDelayMs ?? PERSIST_DELAY_MS
|
|
118
|
+
const persistRetryMs = options.persistRetryMs ?? PERSIST_RETRY_MS
|
|
98
119
|
const logLimits = {
|
|
99
120
|
updates: options.compactAfterUpdates ?? 500,
|
|
100
121
|
bytes: options.compactAfterBytes ?? 4 * 1024 * 1024
|
|
@@ -103,10 +124,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
103
124
|
options.maximumRoomBytes ?? MAX_COLLAB_MESSAGE_BYTES,
|
|
104
125
|
MAX_COLLAB_MESSAGE_BYTES
|
|
105
126
|
)
|
|
106
|
-
const authTimeoutMs = options.authTimeoutMs ??
|
|
127
|
+
const authTimeoutMs = options.authTimeoutMs ?? AUTH_TIMEOUT_MS
|
|
107
128
|
const report =
|
|
108
129
|
options.onError ??
|
|
109
|
-
((message, error
|
|
130
|
+
((message, error, ids) =>
|
|
131
|
+
logCloud('error', message, { source: 'relay', ...ids, error: loggedError(error) }))
|
|
110
132
|
|
|
111
133
|
async function openRoom(authorization: RelayAuthorization): Promise<Room> {
|
|
112
134
|
const existing = rooms.get(authorization.roomId)
|
|
@@ -168,7 +190,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
168
190
|
await log.save({ closing })
|
|
169
191
|
return true
|
|
170
192
|
} catch (error) {
|
|
171
|
-
report('Saving a collaboration room failed', error)
|
|
193
|
+
report('Saving a collaboration room failed', error, { documentId: room.identity.documentId })
|
|
172
194
|
// The room holds edits the store doesn't have; save again without waiting for another edit.
|
|
173
195
|
// A closing room retries through `saveAndClose`.
|
|
174
196
|
if (!closing && rooms.get(room.id) === room) {
|
|
@@ -206,8 +228,8 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
206
228
|
) {
|
|
207
229
|
const readOnly = peer.authorization.permission === 'view'
|
|
208
230
|
if (readOnly && !VIEWER_ACTIONS.has(namespace)) return
|
|
209
|
-
// Only the server says the history changed.
|
|
210
|
-
if (namespace === COLLAB_ACTIONS.versions) return
|
|
231
|
+
// Only the server says the history or the comments changed.
|
|
232
|
+
if (namespace === COLLAB_ACTIONS.versions || namespace === COLLAB_ACTIONS.comments) return
|
|
211
233
|
|
|
212
234
|
let forwarded = payload
|
|
213
235
|
if (namespace === COLLAB_ACTIONS.awareness) {
|
|
@@ -252,6 +274,19 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
252
274
|
return [...peers].filter(current).map((peer) => peer.socket)
|
|
253
275
|
}
|
|
254
276
|
|
|
277
|
+
/** Sends a server message to every peer in the document's rooms. */
|
|
278
|
+
function announce(documentId: string, namespace: string, payload: Uint8Array) {
|
|
279
|
+
const frame: RelayServerFrame = { type: 'message', namespace, from: RELAY_SERVER_PEER, payload }
|
|
280
|
+
sendAll(
|
|
281
|
+
deliverable(
|
|
282
|
+
[...rooms.values()]
|
|
283
|
+
.filter((room) => room.identity.documentId === documentId)
|
|
284
|
+
.flatMap((room) => [...room.peers.values()])
|
|
285
|
+
),
|
|
286
|
+
frame
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
|
|
255
290
|
function remember(peer: RelayPeer) {
|
|
256
291
|
peer.socket.remember?.({
|
|
257
292
|
peerId: peer.id,
|
|
@@ -281,7 +316,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
281
316
|
return undefined
|
|
282
317
|
},
|
|
283
318
|
(error: unknown) => {
|
|
284
|
-
report('Loading a collaboration room failed', error)
|
|
319
|
+
report('Loading a collaboration room failed', error, ticketIds(peer.authorization))
|
|
285
320
|
peer.socket.close(RELAY_CLOSE.protocol, 'Could not open the collaboration room')
|
|
286
321
|
}
|
|
287
322
|
)
|
|
@@ -386,7 +421,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
386
421
|
refreshed: remember,
|
|
387
422
|
deliver,
|
|
388
423
|
depart,
|
|
389
|
-
report
|
|
424
|
+
report: (message, error, authorization) => {
|
|
425
|
+
// Before a ticket verified, only the document a per-document relay serves is known.
|
|
426
|
+
const configured = options.documentId === undefined ? {} : { documentId: options.documentId }
|
|
427
|
+
report(message, error, authorization ? ticketIds(authorization) : configured)
|
|
428
|
+
}
|
|
390
429
|
}
|
|
391
430
|
|
|
392
431
|
return {
|
|
@@ -456,20 +495,12 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
456
495
|
|
|
457
496
|
/** Tells everyone in a document's rooms that its version history changed. */
|
|
458
497
|
announceVersions(documentId: string) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
sendAll(
|
|
466
|
-
deliverable(
|
|
467
|
-
[...rooms.values()]
|
|
468
|
-
.filter((room) => room.identity.documentId === documentId)
|
|
469
|
-
.flatMap((room) => [...room.peers.values()])
|
|
470
|
-
),
|
|
471
|
-
frame
|
|
472
|
-
)
|
|
498
|
+
announce(documentId, COLLAB_ACTIONS.versions, new Uint8Array())
|
|
499
|
+
},
|
|
500
|
+
|
|
501
|
+
/** Tells everyone in a document's rooms that one of its comment threads changed. */
|
|
502
|
+
announceComments(documentId: string, threadId: string) {
|
|
503
|
+
announce(documentId, COLLAB_ACTIONS.comments, new TextEncoder().encode(threadId))
|
|
473
504
|
},
|
|
474
505
|
|
|
475
506
|
/** Saves every open room and closes its sockets, as the runtime shuts down. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveDocumentShareSchema } from '#cloud/contract'
|
|
2
2
|
import type { CloudActor, CloudSessionResolver } from '#cloud/server/auth'
|
|
3
3
|
import type { CollaborationTicketService } from '#cloud/server/collaboration/service'
|
|
4
4
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
@@ -62,20 +62,23 @@ export function createCollaborationRoutes(
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
export function createPublicCollaborationRoutes(
|
|
65
|
-
service: CollaborationTicketService,
|
|
65
|
+
service: Pick<CollaborationTicketService, 'issueShareTicket'>,
|
|
66
66
|
resolveSession?: CloudSessionResolver
|
|
67
67
|
) {
|
|
68
|
-
return new Hono().post(
|
|
68
|
+
return new Hono<CollaborationRouteEnvironment>().post(
|
|
69
69
|
'/shares/:shareId/collaboration-ticket',
|
|
70
70
|
shareParams,
|
|
71
|
-
validatedJSON(
|
|
71
|
+
validatedJSON(resolveDocumentShareSchema),
|
|
72
72
|
async (context) => {
|
|
73
73
|
try {
|
|
74
|
+
const actor = resolveSession ? await resolveSession(context.req.raw) : null
|
|
75
|
+
// A signed-in caller is named in error reports, as on the routes that require one.
|
|
76
|
+
if (actor) context.set('actor', actor)
|
|
74
77
|
return context.json({
|
|
75
78
|
ticket: await service.issueShareTicket(
|
|
76
79
|
context.req.valid('param').shareId,
|
|
77
80
|
context.req.valid('json'),
|
|
78
|
-
|
|
81
|
+
actor ?? undefined
|
|
79
82
|
)
|
|
80
83
|
})
|
|
81
84
|
} catch (error) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { createCommentRoutes, createSharedCommentRoutes } from './routes'
|
|
2
|
+
export {
|
|
3
|
+
CommentConflictError,
|
|
4
|
+
CommentLimitError,
|
|
5
|
+
createCommentService,
|
|
6
|
+
type CommentActor,
|
|
7
|
+
type CommentService
|
|
8
|
+
} from './service'
|
|
9
|
+
export { COMMENT_EMAIL_BUDGET, DEFAULT_COMMENT_EMAILS } from './notify'
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { cloudCommentURL, type CommentEmailSetting, type CommentPerson } from '#cloud/contract'
|
|
2
|
+
import { userIdText, type CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
4
|
+
import { resolveDocumentCommentAccess } from '#cloud/server/documents/access'
|
|
5
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
6
|
+
import { uniq } from 'es-toolkit'
|
|
7
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
8
|
+
import { fromMarkdown } from 'mdast-util-from-markdown'
|
|
9
|
+
import { toString } from 'mdast-util-to-string'
|
|
10
|
+
|
|
11
|
+
import { DAY_MS } from '@open-pencil/std/timing'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* How many comment emails one person's comments may send in a day, and how many one account may
|
|
15
|
+
* receive. A comment's words are the author's, so without a budget mentions are a way to send spam.
|
|
16
|
+
*/
|
|
17
|
+
export const COMMENT_EMAIL_BUDGET = {
|
|
18
|
+
windowMs: DAY_MS,
|
|
19
|
+
perSender: 200,
|
|
20
|
+
perRecipient: 50
|
|
21
|
+
} as const
|
|
22
|
+
|
|
23
|
+
/** What an account hears about when it chose nothing: comments that name it. */
|
|
24
|
+
export const DEFAULT_COMMENT_EMAILS: CommentEmailSetting = 'mentions'
|
|
25
|
+
|
|
26
|
+
const EXCERPT_LENGTH = 280
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The start of what a comment says, as plain text on one line: its Markdown read, so a mention
|
|
30
|
+
* reads as `@Ana`, a link as its text, and each block apart from the next.
|
|
31
|
+
*/
|
|
32
|
+
export function commentExcerpt(body: string): string {
|
|
33
|
+
const line = fromMarkdown(body)
|
|
34
|
+
.children.map((block) => toString(block))
|
|
35
|
+
.join(' ')
|
|
36
|
+
.replace(/\s+/g, ' ')
|
|
37
|
+
.trim()
|
|
38
|
+
return line.length > EXCERPT_LENGTH ? `${line.slice(0, EXCERPT_LENGTH - 1).trimEnd()}…` : line
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type Source = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Everyone a document's comments may name: its workspace's members, the people it was shared with
|
|
45
|
+
* directly, and its maker while they are in the workspace. Names only, never addresses.
|
|
46
|
+
*/
|
|
47
|
+
export async function commentPeople(source: Source, documentId: string): Promise<CommentPerson[]> {
|
|
48
|
+
const document = await source
|
|
49
|
+
.selectFrom('document')
|
|
50
|
+
.select(['workspaceId'])
|
|
51
|
+
.where('id', '=', documentId)
|
|
52
|
+
.executeTakeFirst()
|
|
53
|
+
if (!document) return []
|
|
54
|
+
const members = source
|
|
55
|
+
.selectFrom('workspaceMember')
|
|
56
|
+
.select('workspaceMember.userId as userId')
|
|
57
|
+
.where('workspaceMember.workspaceId', '=', document.workspaceId)
|
|
58
|
+
const grantees = source
|
|
59
|
+
.selectFrom('documentGrant')
|
|
60
|
+
.select('documentGrant.userId as userId')
|
|
61
|
+
.where('documentGrant.documentId', '=', documentId)
|
|
62
|
+
.where('documentGrant.revokedAt', 'is', null)
|
|
63
|
+
const ids = uniq(
|
|
64
|
+
[...(await members.execute()), ...(await grantees.execute())].map((row) => row.userId)
|
|
65
|
+
)
|
|
66
|
+
if (ids.length === 0) return []
|
|
67
|
+
const rows = await source
|
|
68
|
+
.selectFrom('user')
|
|
69
|
+
.select((expression) => [userIdText(expression).as('id'), 'user.name', 'user.image'])
|
|
70
|
+
.where(userIdText, 'in', ids)
|
|
71
|
+
.orderBy('user.name')
|
|
72
|
+
.execute()
|
|
73
|
+
return rows.map((row) => ({ id: row.id, name: row.name, image: row.image ?? null }))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The accounts of `ids` a comment by `authorId` may name: others who can read the document. */
|
|
77
|
+
async function readers(
|
|
78
|
+
transaction: Transaction<CloudDatabase>,
|
|
79
|
+
documentId: string,
|
|
80
|
+
ids: readonly string[],
|
|
81
|
+
authorId: string
|
|
82
|
+
): Promise<string[]> {
|
|
83
|
+
const kept: string[] = []
|
|
84
|
+
for (const id of uniq(ids)) {
|
|
85
|
+
if (id === authorId) continue
|
|
86
|
+
if (await resolveDocumentCommentAccess(transaction, id, documentId)) kept.push(id)
|
|
87
|
+
}
|
|
88
|
+
return kept
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Records whom a comment names, among those who may read it, and answers who is named now that
|
|
93
|
+
* was not before, so an edit tells only the people it adds.
|
|
94
|
+
*/
|
|
95
|
+
export async function recordMentions(
|
|
96
|
+
transaction: Transaction<CloudDatabase>,
|
|
97
|
+
input: { documentId: string; commentId: string; authorId: string; mentions: readonly string[] }
|
|
98
|
+
): Promise<string[]> {
|
|
99
|
+
const named = await readers(transaction, input.documentId, input.mentions, input.authorId)
|
|
100
|
+
if (named.length === 0) return []
|
|
101
|
+
const added = await transaction
|
|
102
|
+
.insertInto('commentMention')
|
|
103
|
+
.values(named.map((userId) => ({ commentId: input.commentId, userId })))
|
|
104
|
+
.onConflict((conflict) => conflict.columns(['commentId', 'userId']).doNothing())
|
|
105
|
+
.returning('userId')
|
|
106
|
+
.execute()
|
|
107
|
+
return added.map((row) => row.userId)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Counts an email against both daily budgets, or answers false once either is used. */
|
|
111
|
+
async function spendCommentEmail(
|
|
112
|
+
transaction: Transaction<CloudDatabase>,
|
|
113
|
+
senderId: string,
|
|
114
|
+
recipientId: string,
|
|
115
|
+
now: Date
|
|
116
|
+
): Promise<boolean> {
|
|
117
|
+
// Always sender first, then recipient, so two transactions never wait on each other in turn.
|
|
118
|
+
await lockForTransaction(transaction, `comment-email-sender:${senderId}`)
|
|
119
|
+
await lockForTransaction(transaction, `comment-email-recipient:${recipientId}`)
|
|
120
|
+
const since = new Date(now.getTime() - COMMENT_EMAIL_BUDGET.windowMs)
|
|
121
|
+
// Both budgets in one read: what the sender's comments sent, and what the recipient received.
|
|
122
|
+
const spent = await transaction
|
|
123
|
+
.selectFrom('commentEmail')
|
|
124
|
+
.select((expression) => [
|
|
125
|
+
expression.fn.countAll<string>().filterWhere('senderId', '=', senderId).as('sent'),
|
|
126
|
+
expression.fn.countAll<string>().filterWhere('recipientId', '=', recipientId).as('received')
|
|
127
|
+
])
|
|
128
|
+
.where('sentAt', '>', since)
|
|
129
|
+
.where((expression) =>
|
|
130
|
+
expression.or([
|
|
131
|
+
expression('senderId', '=', senderId),
|
|
132
|
+
expression('recipientId', '=', recipientId)
|
|
133
|
+
])
|
|
134
|
+
)
|
|
135
|
+
.executeTakeFirstOrThrow()
|
|
136
|
+
if (
|
|
137
|
+
Number(spent.sent) >= COMMENT_EMAIL_BUDGET.perSender ||
|
|
138
|
+
Number(spent.received) >= COMMENT_EMAIL_BUDGET.perRecipient
|
|
139
|
+
) {
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
await transaction
|
|
143
|
+
.insertInto('commentEmail')
|
|
144
|
+
.values({ id: crypto.randomUUID(), senderId, recipientId, sentAt: now })
|
|
145
|
+
.execute()
|
|
146
|
+
return true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Who else wrote in a thread and may still read it: whom a reply tells, as they chose. */
|
|
150
|
+
export async function threadParticipants(
|
|
151
|
+
transaction: Transaction<CloudDatabase>,
|
|
152
|
+
documentId: string,
|
|
153
|
+
threadId: string,
|
|
154
|
+
authorId: string
|
|
155
|
+
): Promise<string[]> {
|
|
156
|
+
const rows = await transaction
|
|
157
|
+
.selectFrom('comment')
|
|
158
|
+
.select('authorId')
|
|
159
|
+
.where('threadId', '=', threadId)
|
|
160
|
+
.where('authorId', 'is not', null)
|
|
161
|
+
.execute()
|
|
162
|
+
return readers(
|
|
163
|
+
transaction,
|
|
164
|
+
documentId,
|
|
165
|
+
rows.flatMap((row) => (row.authorId ? [row.authorId] : [])),
|
|
166
|
+
authorId
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export type CommentNotifier = {
|
|
171
|
+
email: TransactionalEmailService
|
|
172
|
+
publicURL: string
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Emails the people a new comment concerns, in the transaction that writes it: those it names,
|
|
177
|
+
* and, for a reply, the thread's other writers who asked to hear about every comment. Each hears
|
|
178
|
+
* once, as their setting allows and while the day's budgets last.
|
|
179
|
+
*/
|
|
180
|
+
export async function notifyComment(
|
|
181
|
+
transaction: Transaction<CloudDatabase>,
|
|
182
|
+
notifier: CommentNotifier,
|
|
183
|
+
comment: {
|
|
184
|
+
id: string
|
|
185
|
+
documentId: string
|
|
186
|
+
threadId: string
|
|
187
|
+
authorId: string
|
|
188
|
+
body: string
|
|
189
|
+
mentioned: readonly string[]
|
|
190
|
+
participants: readonly string[]
|
|
191
|
+
}
|
|
192
|
+
): Promise<void> {
|
|
193
|
+
// In account order, so two comments naming the same people take their budget locks in turn.
|
|
194
|
+
const recipients = [
|
|
195
|
+
...comment.mentioned.map((userId) => ({ userId, reason: 'mention' as const })),
|
|
196
|
+
...comment.participants
|
|
197
|
+
.filter((userId) => !comment.mentioned.includes(userId))
|
|
198
|
+
.map((userId) => ({ userId, reason: 'reply' as const }))
|
|
199
|
+
].toSorted((a, b) => a.userId.localeCompare(b.userId))
|
|
200
|
+
if (recipients.length === 0) return
|
|
201
|
+
const ids = recipients.map((recipient) => recipient.userId)
|
|
202
|
+
const author = comment.authorId
|
|
203
|
+
const [users, settings, document] = await Promise.all([
|
|
204
|
+
transaction
|
|
205
|
+
.selectFrom('user')
|
|
206
|
+
.select((expression) => [userIdText(expression).as('id'), 'user.name', 'user.email'])
|
|
207
|
+
.where(userIdText, 'in', [...ids, comment.authorId])
|
|
208
|
+
.execute(),
|
|
209
|
+
transaction
|
|
210
|
+
.selectFrom('notificationSetting')
|
|
211
|
+
.select(['userId', 'commentEmails'])
|
|
212
|
+
.where('userId', 'in', ids)
|
|
213
|
+
.execute(),
|
|
214
|
+
transaction
|
|
215
|
+
.selectFrom('document')
|
|
216
|
+
.select('name')
|
|
217
|
+
.where('id', '=', comment.documentId)
|
|
218
|
+
.executeTakeFirstOrThrow()
|
|
219
|
+
])
|
|
220
|
+
const byId = new Map(users.map((user) => [user.id, user]))
|
|
221
|
+
const chosen = new Map(settings.map((setting) => [setting.userId, setting.commentEmails]))
|
|
222
|
+
const now = new Date()
|
|
223
|
+
for (const recipient of recipients) {
|
|
224
|
+
const user = byId.get(recipient.userId)
|
|
225
|
+
const setting = chosen.get(recipient.userId) ?? DEFAULT_COMMENT_EMAILS
|
|
226
|
+
const wanted = setting === 'all' || (setting === 'mentions' && recipient.reason === 'mention')
|
|
227
|
+
if (!user || !wanted) continue
|
|
228
|
+
if (!(await spendCommentEmail(transaction, author, recipient.userId, now))) continue
|
|
229
|
+
await notifier.email.enqueue(
|
|
230
|
+
{
|
|
231
|
+
idempotencyKey: `comment-notification/${comment.id}/${recipient.userId}`,
|
|
232
|
+
kind: 'comment-notification',
|
|
233
|
+
recipientEmail: user.email,
|
|
234
|
+
payload: {
|
|
235
|
+
name: user.name || null,
|
|
236
|
+
authorName: byId.get(author)?.name || null,
|
|
237
|
+
documentName: document.name,
|
|
238
|
+
reason: recipient.reason,
|
|
239
|
+
excerpt: commentExcerpt(comment.body),
|
|
240
|
+
actionURL: cloudCommentURL(notifier.publicURL, comment.documentId, comment.threadId),
|
|
241
|
+
settingsURL: new URL('/account/notifications', notifier.publicURL).href
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
transaction
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Which comments an account hears about by email. */
|
|
250
|
+
export async function readNotificationSettings(
|
|
251
|
+
database: Kysely<CloudDatabase>,
|
|
252
|
+
userId: string
|
|
253
|
+
): Promise<{ commentEmails: CommentEmailSetting }> {
|
|
254
|
+
const row = await database
|
|
255
|
+
.selectFrom('notificationSetting')
|
|
256
|
+
.select('commentEmails')
|
|
257
|
+
.where('userId', '=', userId)
|
|
258
|
+
.executeTakeFirst()
|
|
259
|
+
return { commentEmails: row?.commentEmails ?? DEFAULT_COMMENT_EMAILS }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export async function writeNotificationSettings(
|
|
263
|
+
database: Kysely<CloudDatabase>,
|
|
264
|
+
userId: string,
|
|
265
|
+
settings: { commentEmails: CommentEmailSetting }
|
|
266
|
+
): Promise<{ commentEmails: CommentEmailSetting }> {
|
|
267
|
+
await database
|
|
268
|
+
.insertInto('notificationSetting')
|
|
269
|
+
.values({ userId, commentEmails: settings.commentEmails, updatedAt: new Date() })
|
|
270
|
+
.onConflict((conflict) =>
|
|
271
|
+
conflict
|
|
272
|
+
.column('userId')
|
|
273
|
+
.doUpdateSet({ commentEmails: settings.commentEmails, updatedAt: new Date() })
|
|
274
|
+
)
|
|
275
|
+
.execute()
|
|
276
|
+
return settings
|
|
277
|
+
}
|