@open-pencil/cloud 0.17.0-beta.1 → 0.17.0-beta.4
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/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +468 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +575 -738
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +6564 -2934
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +8931 -4198
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5260 -3859
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5686 -3736
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +9 -6
- package/src/admin/api/routes.ts +103 -33
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +31 -6
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +176 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +89 -26
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +20 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +71 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +32 -0
- package/src/runtime/s3/objects.ts +50 -1
- package/src/runtime/site/server.ts +33 -4
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +12 -7
- package/src/server/api/types.ts +9 -0
- package/src/server/app.ts +93 -15
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +24 -7
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +196 -0
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +279 -195
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +79 -0
- package/src/server/collaboration/relay/persistence.ts +147 -22
- package/src/server/collaboration/relay/resume.ts +28 -0
- package/src/server/collaboration/routes.ts +27 -16
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +35 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +104 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -114
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +70 -7
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +132 -80
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +22 -4
- package/src/server/errors/index.ts +99 -0
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +56 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -15
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +100 -12
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +108 -85
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +59 -2
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +67 -45
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/runtime/cloudflare/relay.ts +0 -136
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
RequestImageUploadsInput,
|
|
10
10
|
SharedDocument
|
|
11
11
|
} from '#cloud/contract'
|
|
12
|
+
import { workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
12
13
|
import { createUploadCleanupService } from '#cloud/server/cleanup'
|
|
13
14
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
14
15
|
import {
|
|
@@ -37,8 +38,14 @@ import {
|
|
|
37
38
|
workspaceRole
|
|
38
39
|
} from '#cloud/server/documents/repository'
|
|
39
40
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
41
|
+
import {
|
|
42
|
+
createDocumentTrash,
|
|
43
|
+
DEFAULT_TRASH_OPTIONS,
|
|
44
|
+
type DocumentTrashOptions
|
|
45
|
+
} from '#cloud/server/documents/trash'
|
|
40
46
|
import type { CreateDocumentUploadResult } from '#cloud/server/documents/types'
|
|
41
47
|
import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
|
|
48
|
+
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
42
49
|
import type { ObjectStore, StoredObject } from '#cloud/server/objects'
|
|
43
50
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
44
51
|
import { createStorageQuotaService } from '#cloud/server/quota'
|
|
@@ -49,13 +56,16 @@ const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
|
|
|
49
56
|
const UPLOAD_LIFETIME_MS = 15 * 60 * 1000
|
|
50
57
|
/** How many of a save's images are checked and landed at once. */
|
|
51
58
|
const IMAGE_COMMIT_CONCURRENCY = 8
|
|
52
|
-
const WRITABLE_ROLES = new Set(['admin', 'editor'])
|
|
53
59
|
|
|
54
60
|
export type DocumentServiceOptions = {
|
|
55
61
|
policy?: CloudPolicy
|
|
56
62
|
technicalMaximumUploadBytes?: number
|
|
57
63
|
/** Called once a save adds a checkpoint to the document's history. */
|
|
58
64
|
onVersionsChanged?: (documentId: string) => void
|
|
65
|
+
/** How long Trash keeps documents, the grace of one deleted forever, and the cleanup lease. */
|
|
66
|
+
trash?: DocumentTrashOptions
|
|
67
|
+
/** Where cleanup the service runs itself, such as of expired uploads, reports a failure. */
|
|
68
|
+
reportError?: CloudErrorReporter
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
/** Whether the stored object is the file the upload declared, which the commit names again. */
|
|
@@ -75,9 +85,10 @@ export function createDocumentService(
|
|
|
75
85
|
objects: ObjectStore,
|
|
76
86
|
options: DocumentServiceOptions = {}
|
|
77
87
|
) {
|
|
78
|
-
const cleanup = createUploadCleanupService(database, objects)
|
|
88
|
+
const cleanup = createUploadCleanupService(database, objects, options.reportError)
|
|
79
89
|
const quota = createStorageQuotaService(database)
|
|
80
90
|
const images = createDocumentImages(database, objects, quota)
|
|
91
|
+
const trash = createDocumentTrash(database, objects, options.trash ?? DEFAULT_TRASH_OPTIONS)
|
|
81
92
|
|
|
82
93
|
/**
|
|
83
94
|
* Makes a committed upload the document's current revision, unless it already was, and records
|
|
@@ -362,6 +373,9 @@ export function createDocumentService(
|
|
|
362
373
|
return listSharedDocuments(database, userId)
|
|
363
374
|
},
|
|
364
375
|
|
|
376
|
+
/** Trash: list what the person may restore, restore it, or delete it for good. */
|
|
377
|
+
trash,
|
|
378
|
+
|
|
365
379
|
async usage(userId: string, workspaceId: string) {
|
|
366
380
|
const role = await workspaceRole(database, userId, workspaceId)
|
|
367
381
|
if (!role) throw new DocumentNotFoundError()
|
|
@@ -423,7 +437,7 @@ export function createDocumentService(
|
|
|
423
437
|
}
|
|
424
438
|
await database
|
|
425
439
|
.updateTable('document')
|
|
426
|
-
.set({ deletedAt: new Date() })
|
|
440
|
+
.set({ deletedAt: new Date(), deletedBy: userId })
|
|
427
441
|
.where('id', '=', documentId)
|
|
428
442
|
.where('deletedAt', 'is', null)
|
|
429
443
|
.execute()
|
|
@@ -436,7 +450,7 @@ export function createDocumentService(
|
|
|
436
450
|
): Promise<DocumentSummary> {
|
|
437
451
|
const role = await workspaceRole(database, userId, workspaceId)
|
|
438
452
|
if (!role) throw new DocumentNotFoundError()
|
|
439
|
-
if (!
|
|
453
|
+
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
440
454
|
const documentId = input.id ?? crypto.randomUUID()
|
|
441
455
|
await insertDocument(database, {
|
|
442
456
|
id: documentId,
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { TrashedDocument, WorkspaceRole } from '#cloud/contract'
|
|
2
|
+
import { documentRoleCan, workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
3
|
+
import { createDocumentCleanupService } from '#cloud/server/cleanup'
|
|
4
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
5
|
+
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
6
|
+
import type { ObjectStore } from '#cloud/server/objects'
|
|
7
|
+
import { compact, uniq } from 'es-toolkit'
|
|
8
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
9
|
+
import * as v from 'valibot'
|
|
10
|
+
|
|
11
|
+
import { trashedDocumentAllows } from './access'
|
|
12
|
+
import { DocumentForbiddenError, DocumentNotFoundError, DocumentPurgeFailedError } from './errors'
|
|
13
|
+
import { markPurged } from './purge'
|
|
14
|
+
|
|
15
|
+
const uuidSchema = v.pipe(v.string(), v.uuid())
|
|
16
|
+
|
|
17
|
+
const WORKSPACE_ROLES: WorkspaceRole[] = ['admin', 'editor', 'viewer']
|
|
18
|
+
|
|
19
|
+
/** The workspace roles whose members may delete any document there, and so restore it. */
|
|
20
|
+
const RESTORING_ROLES = WORKSPACE_ROLES.filter((role) =>
|
|
21
|
+
workspaceRoleCan(role, { document: ['delete'] })
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
export type DocumentTrashOptions = {
|
|
25
|
+
/** How long a deleted document stays in Trash before the cleanup worker deletes it for good. */
|
|
26
|
+
retentionMs: number
|
|
27
|
+
/**
|
|
28
|
+
* How long a document deleted forever keeps its files, out of sight, before cleanup destroys
|
|
29
|
+
* it; 0 destroys it at once.
|
|
30
|
+
*/
|
|
31
|
+
purgeGraceMs: number
|
|
32
|
+
/** How long a cleanup claim holds before another pass may take the document over. */
|
|
33
|
+
leaseDurationMs: number
|
|
34
|
+
/** Where destroying a document deleted forever with no grace reports a failure. */
|
|
35
|
+
reportError?: CloudErrorReporter
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A deployment's defaults: 30 days in Trash, 7 days' grace, and a five-minute cleanup lease. */
|
|
39
|
+
export const DEFAULT_TRASH_OPTIONS: DocumentTrashOptions = {
|
|
40
|
+
retentionMs: 30 * 24 * 60 * 60_000,
|
|
41
|
+
purgeGraceMs: 7 * 24 * 60 * 60_000,
|
|
42
|
+
leaseDurationMs: 5 * 60_000
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The names of the accounts these IDs belong to; IDs that are not accounts are left out. */
|
|
46
|
+
async function accountNames(
|
|
47
|
+
database: Kysely<CloudDatabase>,
|
|
48
|
+
ids: (string | null)[]
|
|
49
|
+
): Promise<Map<string, string>> {
|
|
50
|
+
const accounts = uniq(compact(ids)).filter((id) => v.is(uuidSchema, id))
|
|
51
|
+
if (accounts.length === 0) return new Map()
|
|
52
|
+
const users = await database
|
|
53
|
+
.selectFrom('user')
|
|
54
|
+
.select(['id', 'name'])
|
|
55
|
+
.where('id', 'in', accounts)
|
|
56
|
+
.execute()
|
|
57
|
+
return new Map(users.map((user) => [user.id, user.name]))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toISOString(value: Date | string): string {
|
|
61
|
+
return value instanceof Date ? value.toISOString() : value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Trash: documents deleted in the person's workspaces that cleanup has not removed yet. Whoever
|
|
66
|
+
* may delete a document may restore it; its owner and workspace administrators may also delete
|
|
67
|
+
* it forever, which takes it out of Trash and off their storage at once (see `markPurged`).
|
|
68
|
+
*/
|
|
69
|
+
export function createDocumentTrash(
|
|
70
|
+
database: Kysely<CloudDatabase>,
|
|
71
|
+
objects: ObjectStore,
|
|
72
|
+
options: DocumentTrashOptions
|
|
73
|
+
) {
|
|
74
|
+
const cleanup = createDocumentCleanupService(database, objects, {
|
|
75
|
+
reportError: options.reportError
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
/** Deleted after this moment, a document is still in Trash. */
|
|
79
|
+
function retainedSince(now: Date): Date {
|
|
80
|
+
return new Date(now.getTime() - options.retentionMs)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
async list(userId: string, now = new Date()): Promise<TrashedDocument[]> {
|
|
85
|
+
const rows = await database
|
|
86
|
+
.selectFrom('document')
|
|
87
|
+
.innerJoin('workspaceMember', (join) =>
|
|
88
|
+
join
|
|
89
|
+
.onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
|
|
90
|
+
.on('workspaceMember.userId', '=', userId)
|
|
91
|
+
)
|
|
92
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
93
|
+
.select([
|
|
94
|
+
'document.id',
|
|
95
|
+
'document.workspaceId',
|
|
96
|
+
'workspace.name as workspaceName',
|
|
97
|
+
'document.name',
|
|
98
|
+
'document.kind',
|
|
99
|
+
'document.createdBy',
|
|
100
|
+
'document.deletedAt',
|
|
101
|
+
'document.deletedBy',
|
|
102
|
+
'workspaceMember.role'
|
|
103
|
+
])
|
|
104
|
+
.where('document.deletedAt', '>', retainedSince(now))
|
|
105
|
+
.where('document.purgedAt', 'is', null)
|
|
106
|
+
.where('document.cleanupClaimId', 'is', null)
|
|
107
|
+
.where('document.disabledAt', 'is', null)
|
|
108
|
+
.where((expression) =>
|
|
109
|
+
expression.or([
|
|
110
|
+
expression('workspaceMember.role', 'in', RESTORING_ROLES),
|
|
111
|
+
expression('document.createdBy', '=', userId)
|
|
112
|
+
])
|
|
113
|
+
)
|
|
114
|
+
.orderBy('document.deletedAt', 'desc')
|
|
115
|
+
.execute()
|
|
116
|
+
const names = await accountNames(
|
|
117
|
+
database,
|
|
118
|
+
rows.map((row) => row.deletedBy)
|
|
119
|
+
)
|
|
120
|
+
return rows.flatMap(({ createdBy, deletedAt, deletedBy, role, ...row }) => {
|
|
121
|
+
if (deletedAt === null) return []
|
|
122
|
+
const deleted = new Date(deletedAt)
|
|
123
|
+
const deleterName = deletedBy === null ? undefined : names.get(deletedBy)
|
|
124
|
+
return {
|
|
125
|
+
...row,
|
|
126
|
+
deletedAt: toISOString(deletedAt),
|
|
127
|
+
deletedBy: deleterName === undefined ? null : { name: deleterName },
|
|
128
|
+
purgeAt: new Date(deleted.getTime() + options.retentionMs).toISOString(),
|
|
129
|
+
canPurge:
|
|
130
|
+
workspaceRoleCan(role, { document: ['purge'] }) ||
|
|
131
|
+
(createdBy === userId && documentRoleCan('owner', { document: ['purge'] }))
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/** Puts a document back where it was; it counted toward storage all along, so no quota check. */
|
|
137
|
+
async restore(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
138
|
+
await database.transaction().execute(async (transaction) => {
|
|
139
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
140
|
+
await transaction
|
|
141
|
+
.updateTable('document')
|
|
142
|
+
// Changed now, so devices that deleted it stop hiding it (see `reconcileStorageDocuments`).
|
|
143
|
+
.set({ deletedAt: null, deletedBy: null, updatedAt: now })
|
|
144
|
+
.where('id', '=', documentId)
|
|
145
|
+
.execute()
|
|
146
|
+
})
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Deletes a document in Trash forever, for the person: it leaves Trash and their storage now,
|
|
151
|
+
* and cleanup destroys it once the deployment's grace passes, or at once without one.
|
|
152
|
+
*/
|
|
153
|
+
async purge(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
154
|
+
await database.transaction().execute(async (transaction) => {
|
|
155
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
156
|
+
if (!(await trashedDocumentAllows(transaction, userId, documentId, 'purge'))) {
|
|
157
|
+
throw new DocumentForbiddenError()
|
|
158
|
+
}
|
|
159
|
+
await markPurged(transaction, [documentId], userId, now)
|
|
160
|
+
})
|
|
161
|
+
if (options.purgeGraceMs > 0) return
|
|
162
|
+
try {
|
|
163
|
+
// A save still in flight holds it for the next cleanup pass; it is out of Trash already.
|
|
164
|
+
await cleanup.destroyPurgedDocument(documentId, {
|
|
165
|
+
leaseDurationMs: options.leaseDurationMs,
|
|
166
|
+
now
|
|
167
|
+
})
|
|
168
|
+
} catch (error) {
|
|
169
|
+
throw new DocumentPurgeFailedError(`Document ${documentId} could not be destroyed`, {
|
|
170
|
+
cause: error
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Holds a document in Trash until the transaction ends: deleted, not forever, inside retention,
|
|
178
|
+
* and not being cleaned, by someone who may restore it; anything else is not found.
|
|
179
|
+
*/
|
|
180
|
+
async function lockInTrash(
|
|
181
|
+
transaction: Transaction<CloudDatabase>,
|
|
182
|
+
userId: string,
|
|
183
|
+
documentId: string,
|
|
184
|
+
now: Date
|
|
185
|
+
): Promise<void> {
|
|
186
|
+
const document = await transaction
|
|
187
|
+
.selectFrom('document')
|
|
188
|
+
.select(['deletedAt', 'purgedAt', 'cleanupClaimId'])
|
|
189
|
+
.where('id', '=', documentId)
|
|
190
|
+
.forUpdate()
|
|
191
|
+
.executeTakeFirst()
|
|
192
|
+
const inTrash =
|
|
193
|
+
document?.deletedAt != null &&
|
|
194
|
+
document.purgedAt === null &&
|
|
195
|
+
document.cleanupClaimId === null &&
|
|
196
|
+
new Date(document.deletedAt) > retainedSince(now)
|
|
197
|
+
if (!inTrash || !(await trashedDocumentAllows(transaction, userId, documentId, 'delete'))) {
|
|
198
|
+
throw new DocumentNotFoundError()
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type DocumentTrash = ReturnType<typeof createDocumentTrash>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
2
3
|
import type { Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
/** How long the newest version may be old before a save keeps the state it replaces. */
|
|
@@ -93,6 +94,6 @@ export function announceVersions(
|
|
|
93
94
|
try {
|
|
94
95
|
onVersionsChanged?.(documentId)
|
|
95
96
|
} catch (error) {
|
|
96
|
-
|
|
97
|
+
logCloud('error', 'Announcing a version history change failed', { error: loggedError(error) })
|
|
97
98
|
}
|
|
98
99
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
1
2
|
import { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
2
3
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
4
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
5
|
+
import type { Kysely } from 'kysely'
|
|
3
6
|
|
|
4
7
|
/** How far back a workspace without full revision history keeps its versions. */
|
|
5
8
|
export const LIMITED_HISTORY_DAYS = 30
|
|
@@ -9,17 +12,18 @@ export const LIMITED_HISTORY_DAYS = 30
|
|
|
9
12
|
* `revisionHistory` entitlement off, only the last 30 days, as on a free plan.
|
|
10
13
|
*/
|
|
11
14
|
export async function historyStart(
|
|
15
|
+
database: Kysely<CloudDatabase>,
|
|
12
16
|
policy: CloudPolicy | undefined,
|
|
13
17
|
deploymentMode: CloudPolicyContext['deploymentMode'],
|
|
14
18
|
workspaceId: string,
|
|
15
19
|
now: Date
|
|
16
20
|
): Promise<Date | null> {
|
|
17
|
-
const
|
|
18
|
-
? await
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const context = policy
|
|
22
|
+
? await workspacePolicyContext(database, { workspaceId }, { deploymentMode })
|
|
23
|
+
: null
|
|
24
|
+
const full =
|
|
25
|
+
policy && context
|
|
26
|
+
? await policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, true, context)
|
|
27
|
+
: true
|
|
24
28
|
return full ? null : new Date(now.getTime() - LIMITED_HISTORY_DAYS * 24 * 60 * 60_000)
|
|
25
29
|
}
|
|
@@ -4,7 +4,12 @@ import {
|
|
|
4
4
|
parseUpdateDocumentVersion
|
|
5
5
|
} from '#cloud/contract'
|
|
6
6
|
import { documentDomainError, type DocumentRouteEnvironment } from '#cloud/server/documents/routes'
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
documentParams,
|
|
9
|
+
documentVersionParams,
|
|
10
|
+
validatedJSON,
|
|
11
|
+
validatedQuery
|
|
12
|
+
} from '#cloud/server/validation'
|
|
8
13
|
import { Hono, type Context } from 'hono'
|
|
9
14
|
import * as v from 'valibot'
|
|
10
15
|
|
|
@@ -29,70 +34,76 @@ async function answer(context: Context, run: () => Promise<Response>): Promise<R
|
|
|
29
34
|
/** A document's version history: listing, naming, opening, restoring, and duplicating versions. */
|
|
30
35
|
export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
31
36
|
return new Hono<DocumentRouteEnvironment>()
|
|
32
|
-
.get('/documents/:documentId/versions', validatedQuery(parsePage), (context) =>
|
|
37
|
+
.get('/documents/:documentId/versions', documentParams, validatedQuery(parsePage), (context) =>
|
|
33
38
|
answer(context, async () =>
|
|
34
39
|
context.json(
|
|
35
40
|
await service.list(
|
|
36
41
|
context.get('actor').userId,
|
|
37
|
-
context.req.
|
|
42
|
+
context.req.valid('param').documentId,
|
|
38
43
|
context.req.valid('query')
|
|
39
44
|
)
|
|
40
45
|
)
|
|
41
46
|
)
|
|
42
47
|
)
|
|
43
|
-
.post(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
.post(
|
|
49
|
+
'/documents/:documentId/versions',
|
|
50
|
+
documentParams,
|
|
51
|
+
validatedJSON(parseSaveDocumentVersion),
|
|
52
|
+
(context) =>
|
|
53
|
+
answer(context, async () =>
|
|
54
|
+
context.json(
|
|
55
|
+
{
|
|
56
|
+
version: await service.save(
|
|
57
|
+
context.get('actor').userId,
|
|
58
|
+
context.req.valid('param').documentId,
|
|
59
|
+
context.req.valid('json')
|
|
60
|
+
)
|
|
61
|
+
},
|
|
62
|
+
201
|
|
63
|
+
)
|
|
54
64
|
)
|
|
55
|
-
)
|
|
56
65
|
)
|
|
57
66
|
.patch(
|
|
58
67
|
'/documents/:documentId/versions/:versionId',
|
|
68
|
+
documentVersionParams,
|
|
59
69
|
validatedJSON(parseUpdateDocumentVersion),
|
|
60
70
|
(context) =>
|
|
61
71
|
answer(context, async () =>
|
|
62
72
|
context.json({
|
|
63
73
|
version: await service.update(
|
|
64
74
|
context.get('actor').userId,
|
|
65
|
-
context.req.
|
|
66
|
-
context.req.
|
|
75
|
+
context.req.valid('param').documentId,
|
|
76
|
+
context.req.valid('param').versionId,
|
|
67
77
|
context.req.valid('json')
|
|
68
78
|
)
|
|
69
79
|
})
|
|
70
80
|
)
|
|
71
81
|
)
|
|
72
|
-
.get('/documents/:documentId/versions/:versionId', (context) =>
|
|
82
|
+
.get('/documents/:documentId/versions/:versionId', documentVersionParams, (context) =>
|
|
73
83
|
answer(context, async () =>
|
|
74
84
|
context.json(
|
|
75
85
|
await service.download(
|
|
76
86
|
context.get('actor').userId,
|
|
77
|
-
context.req.
|
|
78
|
-
context.req.
|
|
87
|
+
context.req.valid('param').documentId,
|
|
88
|
+
context.req.valid('param').versionId
|
|
79
89
|
)
|
|
80
90
|
)
|
|
81
91
|
)
|
|
82
92
|
)
|
|
83
|
-
.post('/documents/:documentId/versions/:versionId/restore', (context) =>
|
|
93
|
+
.post('/documents/:documentId/versions/:versionId/restore', documentVersionParams, (context) =>
|
|
84
94
|
answer(context, async () =>
|
|
85
95
|
context.json(
|
|
86
96
|
await service.restore(
|
|
87
97
|
context.get('actor').userId,
|
|
88
|
-
context.req.
|
|
89
|
-
context.req.
|
|
98
|
+
context.req.valid('param').documentId,
|
|
99
|
+
context.req.valid('param').versionId
|
|
90
100
|
)
|
|
91
101
|
)
|
|
92
102
|
)
|
|
93
103
|
)
|
|
94
104
|
.post(
|
|
95
105
|
'/documents/:documentId/versions/:versionId/duplicate',
|
|
106
|
+
documentVersionParams,
|
|
96
107
|
validatedJSON(parseDuplicateDocumentVersion),
|
|
97
108
|
(context) =>
|
|
98
109
|
answer(context, async () =>
|
|
@@ -100,8 +111,8 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
100
111
|
{
|
|
101
112
|
document: await service.duplicate(
|
|
102
113
|
context.get('actor').userId,
|
|
103
|
-
context.req.
|
|
104
|
-
context.req.
|
|
114
|
+
context.req.valid('param').documentId,
|
|
115
|
+
context.req.valid('param').versionId,
|
|
105
116
|
context.req.valid('json')
|
|
106
117
|
)
|
|
107
118
|
},
|
|
@@ -7,12 +7,14 @@ import type {
|
|
|
7
7
|
SaveDocumentVersionInput,
|
|
8
8
|
UpdateDocumentVersionInput
|
|
9
9
|
} from '#cloud/contract'
|
|
10
|
+
import { workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
10
11
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
11
12
|
import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
12
13
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
13
14
|
import { copyRevisionImages, revisionImageDownloads } from '#cloud/server/documents/images'
|
|
14
15
|
import { findDocument, insertDocument, workspaceRole } from '#cloud/server/documents/repository'
|
|
15
16
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
17
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
16
18
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
17
19
|
import type { CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
18
20
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
@@ -24,7 +26,6 @@ import { historyStart } from './history'
|
|
|
24
26
|
const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
|
|
25
27
|
const DEFAULT_PAGE_SIZE = 50
|
|
26
28
|
const MAXIMUM_PAGE_SIZE = 100
|
|
27
|
-
const WRITABLE_ROLES = new Set(['admin', 'editor'])
|
|
28
29
|
|
|
29
30
|
type VersionRow = {
|
|
30
31
|
id: string
|
|
@@ -103,7 +104,13 @@ export function createDocumentVersionService(
|
|
|
103
104
|
if (!document) throw new DocumentNotFoundError()
|
|
104
105
|
return {
|
|
105
106
|
document,
|
|
106
|
-
start: await historyStart(
|
|
107
|
+
start: await historyStart(
|
|
108
|
+
database,
|
|
109
|
+
options.policy,
|
|
110
|
+
deploymentMode,
|
|
111
|
+
document.workspaceId,
|
|
112
|
+
new Date()
|
|
113
|
+
)
|
|
107
114
|
}
|
|
108
115
|
}
|
|
109
116
|
|
|
@@ -309,6 +316,15 @@ export function createDocumentVersionService(
|
|
|
309
316
|
})
|
|
310
317
|
.where('id', '=', documentId)
|
|
311
318
|
.execute()
|
|
319
|
+
// The rooms of the replaced state never open again, so what the relay saved of them goes.
|
|
320
|
+
await transaction
|
|
321
|
+
.deleteFrom('documentRoomUpdate')
|
|
322
|
+
.where('documentId', '=', documentId)
|
|
323
|
+
.execute()
|
|
324
|
+
await transaction
|
|
325
|
+
.deleteFrom('documentRoomSnapshot')
|
|
326
|
+
.where('documentId', '=', documentId)
|
|
327
|
+
.execute()
|
|
312
328
|
// A moment after the state it replaces, so the history lists it first.
|
|
313
329
|
await keepRevision(transaction, {
|
|
314
330
|
documentId,
|
|
@@ -322,7 +338,9 @@ export function createDocumentVersionService(
|
|
|
322
338
|
try {
|
|
323
339
|
options.onRestored?.(documentId)
|
|
324
340
|
} catch (error) {
|
|
325
|
-
|
|
341
|
+
logCloud('error', 'Ending the rooms of a restored document failed', {
|
|
342
|
+
error: loggedError(error)
|
|
343
|
+
})
|
|
326
344
|
}
|
|
327
345
|
const document = await findDocument(database, userId, documentId)
|
|
328
346
|
if (!document) throw new DocumentNotFoundError()
|
|
@@ -342,7 +360,7 @@ export function createDocumentVersionService(
|
|
|
342
360
|
const { document, start } = await openHistory(userId, documentId, false)
|
|
343
361
|
const role = await workspaceRole(database, userId, document.workspaceId)
|
|
344
362
|
if (!role) throw new DocumentNotFoundError()
|
|
345
|
-
if (!
|
|
363
|
+
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
346
364
|
const version = await findVersion(documentId, versionId, start)
|
|
347
365
|
const copyId = crypto.randomUUID()
|
|
348
366
|
const revisionId = crypto.randomUUID()
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { loggedError, writeJSONLine, type CloudLogWriter } from '#cloud/server/logging'
|
|
2
|
+
import type { Context, ErrorHandler } from 'hono'
|
|
3
|
+
import { HTTPException } from 'hono/http-exception'
|
|
4
|
+
import { routePath } from 'hono/route'
|
|
5
|
+
|
|
6
|
+
import type { ErrorReportContext, ErrorReporter } from './posthog'
|
|
7
|
+
|
|
8
|
+
/** Where the server caught an error. */
|
|
9
|
+
export const CLOUD_ERROR_SOURCES = [
|
|
10
|
+
'request',
|
|
11
|
+
'background',
|
|
12
|
+
'cleanup',
|
|
13
|
+
'email',
|
|
14
|
+
'relay',
|
|
15
|
+
'scheduled',
|
|
16
|
+
'setup'
|
|
17
|
+
] as const
|
|
18
|
+
export type CloudErrorSource = (typeof CLOUD_ERROR_SOURCES)[number]
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* What the server knows about a caught error; never who made the request or what it carried. For a
|
|
22
|
+
* request: its method, the route pattern it matched (not the path with its IDs), and its ID.
|
|
23
|
+
*/
|
|
24
|
+
export type CloudErrorContext = ErrorReportContext<CloudErrorSource>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Logs a caught error and, when the deployment configures error tracking, sends it there. Runtimes
|
|
28
|
+
* create one with `createLoggingErrorReporter`; its promise never rejects.
|
|
29
|
+
*/
|
|
30
|
+
export type CloudErrorReporter = ErrorReporter<CloudErrorSource>
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A reporter that writes the error as a structured line with the runtime's writer, then hands it
|
|
34
|
+
* to `send`, such as PostHog's, if the deployment has one.
|
|
35
|
+
*/
|
|
36
|
+
export function createLoggingErrorReporter(
|
|
37
|
+
log: CloudLogWriter,
|
|
38
|
+
send?: CloudErrorReporter
|
|
39
|
+
): CloudErrorReporter {
|
|
40
|
+
return async (error, context) => {
|
|
41
|
+
try {
|
|
42
|
+
log({
|
|
43
|
+
level: 'error',
|
|
44
|
+
message: context.message ?? 'Failed',
|
|
45
|
+
source: context.source,
|
|
46
|
+
method: context.method,
|
|
47
|
+
route: context.route,
|
|
48
|
+
requestId: context.requestId,
|
|
49
|
+
error: loggedError(error)
|
|
50
|
+
})
|
|
51
|
+
} catch (failure) {
|
|
52
|
+
// A writer that fails must not stop the report or reject; the console still gets both.
|
|
53
|
+
console.error('[Cloud] Writing a log line failed:', failure, error)
|
|
54
|
+
}
|
|
55
|
+
await send?.(error, context)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Without a reporter, errors are still logged, as lines of JSON. */
|
|
60
|
+
const logOnly = createLoggingErrorReporter(writeJSONLine)
|
|
61
|
+
|
|
62
|
+
/** Logs a caught error and reports it, through the runtime's reporter. */
|
|
63
|
+
export function reportCloudError(
|
|
64
|
+
reporter: CloudErrorReporter | undefined,
|
|
65
|
+
message: string,
|
|
66
|
+
error: unknown,
|
|
67
|
+
context: CloudErrorContext
|
|
68
|
+
): Promise<void> {
|
|
69
|
+
return (reporter ?? logOnly)(error, { ...context, message })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function requestContext(context: Context): CloudErrorContext {
|
|
73
|
+
const requestId: unknown = context.get('requestId')
|
|
74
|
+
return {
|
|
75
|
+
source: 'request',
|
|
76
|
+
method: context.req.method,
|
|
77
|
+
route: routePath(context),
|
|
78
|
+
...(typeof requestId === 'string' && { requestId })
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The app's last error handler, as Hono's default: an `HTTPException` answers with its response,
|
|
84
|
+
* anything else is logged, reported, and answered with a 500 naming the request's `X-Request-Id`.
|
|
85
|
+
*/
|
|
86
|
+
export function cloudErrorHandler(reporter: CloudErrorReporter | undefined): ErrorHandler {
|
|
87
|
+
return (error, context) => {
|
|
88
|
+
if (error instanceof HTTPException) return error.getResponse()
|
|
89
|
+
void reportCloudError(reporter, 'Request failed', error, requestContext(context))
|
|
90
|
+
return context.text('Internal Server Error', 500)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export {
|
|
95
|
+
createPostHogErrorReporter,
|
|
96
|
+
type ErrorReportContext,
|
|
97
|
+
type ErrorReporter,
|
|
98
|
+
type PostHogErrorReporterOptions
|
|
99
|
+
} from './posthog'
|