@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
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { DocumentAccess, DocumentAccessSource, DocumentPermission } from '#cloud/contract'
|
|
2
|
+
import {
|
|
3
|
+
documentRoleCan,
|
|
4
|
+
GRANT_DOCUMENT_ROLE,
|
|
5
|
+
workspaceRoleCan,
|
|
6
|
+
type AccessRequest
|
|
7
|
+
} from '#cloud/server/auth/access-control'
|
|
7
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
8
|
-
import { maxBy } from 'es-toolkit'
|
|
9
9
|
import type { Kysely, Transaction } from 'kysely'
|
|
10
10
|
|
|
11
11
|
export type AccessDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return role === 'viewer' ? 'view' : 'edit'
|
|
13
|
+
type DocumentAccessGrant = {
|
|
14
|
+
source: DocumentAccessSource
|
|
15
|
+
can: (request: AccessRequest) => boolean
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Everything that gives a user access to a document, each checked against its role table: a live
|
|
20
|
+
* document, or one in Trash.
|
|
21
|
+
*/
|
|
22
|
+
async function documentAccessGrants(
|
|
20
23
|
database: AccessDatabase,
|
|
21
24
|
userId: string,
|
|
22
|
-
documentId: string
|
|
23
|
-
|
|
25
|
+
documentId: string,
|
|
26
|
+
place: 'live' | 'trash' = 'live'
|
|
27
|
+
): Promise<DocumentAccessGrant[]> {
|
|
24
28
|
const document = await database
|
|
25
29
|
.selectFrom('document')
|
|
26
30
|
.leftJoin('workspaceMember', (join) =>
|
|
@@ -40,33 +44,47 @@ export async function resolveDocumentAccess(
|
|
|
40
44
|
'documentGrant.permission as grantPermission'
|
|
41
45
|
])
|
|
42
46
|
.where('document.id', '=', documentId)
|
|
43
|
-
.where('document.deletedAt', 'is', null)
|
|
47
|
+
.where('document.deletedAt', place === 'live' ? 'is' : 'is not', null)
|
|
48
|
+
// A document an administrator took down is out of reach for everyone, its owner included.
|
|
49
|
+
.where('document.disabledAt', 'is', null)
|
|
44
50
|
.executeTakeFirst()
|
|
45
|
-
if (!document) return
|
|
51
|
+
if (!document) return []
|
|
46
52
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
candidates.push('edit')
|
|
53
|
+
const grants: DocumentAccessGrant[] = []
|
|
54
|
+
// Making a document gives access only while its maker is in the workspace: someone removed
|
|
55
|
+
// from it keeps no way back in.
|
|
56
|
+
if (document.createdBy === userId && document.workspaceRole !== null) {
|
|
57
|
+
grants.push({ source: 'owner', can: (request) => documentRoleCan('owner', request) })
|
|
53
58
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
const { workspaceRole, grantPermission } = document
|
|
60
|
+
if (workspaceRole) {
|
|
61
|
+
grants.push({ source: 'workspace', can: (request) => workspaceRoleCan(workspaceRole, request) })
|
|
57
62
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
if (grantPermission) {
|
|
64
|
+
const role = GRANT_DOCUMENT_ROLE[grantPermission]
|
|
65
|
+
grants.push({ source: 'direct-grant', can: (request) => documentRoleCan(role, request) })
|
|
61
66
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
return grants
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function grantsAllow(grants: DocumentAccessGrant[], request: AccessRequest): boolean {
|
|
71
|
+
return grants.some((grant) => grant.can(request))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function resolveDocumentAccess(
|
|
75
|
+
database: AccessDatabase,
|
|
76
|
+
userId: string,
|
|
77
|
+
documentId: string
|
|
78
|
+
): Promise<DocumentAccess | undefined> {
|
|
79
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
80
|
+
if (!grantsAllow(grants, { document: ['read'] })) return undefined
|
|
81
|
+
const permission: DocumentPermission = grantsAllow(grants, { document: ['edit'] })
|
|
82
|
+
? 'edit'
|
|
83
|
+
: 'view'
|
|
66
84
|
return {
|
|
67
85
|
permission,
|
|
68
|
-
canManageSharing:
|
|
69
|
-
sources
|
|
86
|
+
canManageSharing: grantsAllow(grants, { document: ['share'] }),
|
|
87
|
+
sources: grants.map((grant) => grant.source)
|
|
70
88
|
}
|
|
71
89
|
}
|
|
72
90
|
|
|
@@ -74,30 +92,23 @@ export function canEditDocument(access: DocumentAccess): boolean {
|
|
|
74
92
|
return access.permission === 'edit'
|
|
75
93
|
}
|
|
76
94
|
|
|
77
|
-
/**
|
|
78
|
-
* Removing a document belongs to its owner and its workspace's admins and editors. A direct grant
|
|
79
|
-
* or an accepted invitation lets someone edit, never delete the owner's document.
|
|
80
|
-
*/
|
|
95
|
+
/** Whether the user may delete the document; `documentRoles` and `workspaceRoles` decide who. */
|
|
81
96
|
export async function canDeleteDocument(
|
|
82
97
|
database: AccessDatabase,
|
|
83
98
|
userId: string,
|
|
84
99
|
documentId: string
|
|
85
100
|
): Promise<boolean> {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return (
|
|
99
|
-
document.createdBy === userId ||
|
|
100
|
-
document.workspaceRole === 'admin' ||
|
|
101
|
-
document.workspaceRole === 'editor'
|
|
102
|
-
)
|
|
101
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
102
|
+
return grantsAllow(grants, { document: ['delete'] })
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether the user may restore the document from Trash, or delete it there for good. */
|
|
106
|
+
export async function trashedDocumentAllows(
|
|
107
|
+
database: AccessDatabase,
|
|
108
|
+
userId: string,
|
|
109
|
+
documentId: string,
|
|
110
|
+
action: 'delete' | 'purge'
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
const grants = await documentAccessGrants(database, userId, documentId, 'trash')
|
|
113
|
+
return grantsAllow(grants, { document: [action] })
|
|
103
114
|
}
|
|
@@ -14,3 +14,10 @@ export class DocumentConflictError extends Error {
|
|
|
14
14
|
export class UploadInvalidError extends Error {
|
|
15
15
|
override readonly name = 'UploadInvalidError'
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Destroying a document deleted forever with no grace failed. The failure, its cause, was
|
|
19
|
+
* reported where cleanup reports; the document is out of Trash, and cleanup tries again.
|
|
20
|
+
*/
|
|
21
|
+
export class DocumentPurgeFailedError extends Error {
|
|
22
|
+
override readonly name = 'DocumentPurgeFailedError'
|
|
23
|
+
}
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
RequestImageUploadsInput
|
|
7
7
|
} from '#cloud/contract'
|
|
8
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
9
10
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
10
11
|
import type { StorageQuotaService } from '#cloud/server/quota'
|
|
11
12
|
import { uniq, uniqBy } from 'es-toolkit'
|
|
@@ -53,7 +54,7 @@ export function createDocumentImages(
|
|
|
53
54
|
.execute()
|
|
54
55
|
} catch (error) {
|
|
55
56
|
// Cleanup releases it once it expires.
|
|
56
|
-
|
|
57
|
+
logCloud('warn', 'Giving up an image upload failed', { error: loggedError(error) })
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
}
|
|
@@ -4,11 +4,12 @@ import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/doc
|
|
|
4
4
|
import type { DocumentServiceOptions } from '#cloud/server/documents/service'
|
|
5
5
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
6
6
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
7
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
7
8
|
import type { Kysely } from 'kysely'
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
* The document `userId` may write to, and how large one file and
|
|
11
|
-
*
|
|
11
|
+
* The document `userId` may write to, and how large one file and its owner's storage may be for
|
|
12
|
+
* this save: technical limits, tightened by the owner's plan.
|
|
12
13
|
*/
|
|
13
14
|
export async function writableDocument(
|
|
14
15
|
database: Kysely<CloudDatabase>,
|
|
@@ -23,13 +24,12 @@ export async function writableDocument(
|
|
|
23
24
|
const row = await getDocumentSummaryRow(database, documentId)
|
|
24
25
|
if (!row) throw new DocumentNotFoundError()
|
|
25
26
|
const document = documentSummary(row)
|
|
26
|
-
const policyContext =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
27
|
+
const policyContext = await workspacePolicyContext(
|
|
28
|
+
database,
|
|
29
|
+
{ documentId },
|
|
30
|
+
{ actorId: userId, deploymentMode: 'self-hosted' }
|
|
31
|
+
)
|
|
32
|
+
if (!policyContext) throw new DocumentNotFoundError()
|
|
33
33
|
const technicalMaximum = options.technicalMaximumUploadBytes ?? Number.MAX_SAFE_INTEGER
|
|
34
34
|
const maximumFileBytes = Math.min(
|
|
35
35
|
technicalMaximum,
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { documentObjects } from '#cloud/server/cleanup/documents'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { sumBy } from 'es-toolkit'
|
|
4
|
+
import type { Transaction } from 'kysely'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Deleting forever: deleted documents leave everyone's sight, Trash included, and the files only
|
|
8
|
+
* they hold come off their workspaces' usage at once. Their files and rows stay through the
|
|
9
|
+
* deployment's grace, so a server administrator can still bring them back, then cleanup destroys
|
|
10
|
+
* them and takes off only what this did not. `purgedBy` is empty for an account's deletion.
|
|
11
|
+
*/
|
|
12
|
+
export async function markPurged(
|
|
13
|
+
transaction: Transaction<CloudDatabase>,
|
|
14
|
+
documentIds: readonly string[],
|
|
15
|
+
purgedBy: string | null,
|
|
16
|
+
now: Date
|
|
17
|
+
): Promise<void> {
|
|
18
|
+
for (const documentId of documentIds) {
|
|
19
|
+
const document = await transaction
|
|
20
|
+
.selectFrom('document')
|
|
21
|
+
.select('workspaceId')
|
|
22
|
+
.where('id', '=', documentId)
|
|
23
|
+
.where('purgedAt', 'is', null)
|
|
24
|
+
.executeTakeFirst()
|
|
25
|
+
if (!document) continue
|
|
26
|
+
const bytes = sumBy(await documentObjects(transaction, documentId), (object) => object.byteSize)
|
|
27
|
+
await transaction
|
|
28
|
+
.updateTable('document')
|
|
29
|
+
.set({ purgedAt: now, purgedBy, purgedBytes: bytes })
|
|
30
|
+
.where('id', '=', documentId)
|
|
31
|
+
.execute()
|
|
32
|
+
if (bytes > 0) await changeUsage(transaction, document.workspaceId, -bytes)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Brings a document deleted forever back to life in its workspace: it is live again, and the
|
|
38
|
+
* storage deleting it took off counts again.
|
|
39
|
+
*/
|
|
40
|
+
export async function unmarkPurged(
|
|
41
|
+
transaction: Transaction<CloudDatabase>,
|
|
42
|
+
document: { id: string; workspaceId: string; purgedBytes: number }
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
await transaction
|
|
45
|
+
.updateTable('document')
|
|
46
|
+
// Changed now, so devices that deleted it stop hiding it.
|
|
47
|
+
.set({
|
|
48
|
+
deletedAt: null,
|
|
49
|
+
deletedBy: null,
|
|
50
|
+
purgedAt: null,
|
|
51
|
+
purgedBy: null,
|
|
52
|
+
purgedBytes: 0,
|
|
53
|
+
updatedAt: new Date()
|
|
54
|
+
})
|
|
55
|
+
.where('id', '=', document.id)
|
|
56
|
+
.execute()
|
|
57
|
+
if (document.purgedBytes > 0) {
|
|
58
|
+
await changeUsage(transaction, document.workspaceId, document.purgedBytes)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function changeUsage(
|
|
63
|
+
transaction: Transaction<CloudDatabase>,
|
|
64
|
+
workspaceId: string,
|
|
65
|
+
bytes: number
|
|
66
|
+
): Promise<void> {
|
|
67
|
+
await transaction
|
|
68
|
+
.updateTable('workspaceStorageUsage')
|
|
69
|
+
.set({
|
|
70
|
+
committedBytes: (expression) => expression('committedBytes', '+', bytes),
|
|
71
|
+
updatedAt: new Date()
|
|
72
|
+
})
|
|
73
|
+
.where('workspaceId', '=', workspaceId)
|
|
74
|
+
.execute()
|
|
75
|
+
}
|
|
@@ -51,6 +51,7 @@ export async function listDocuments(
|
|
|
51
51
|
])
|
|
52
52
|
.where('workspaceId', '=', workspaceId)
|
|
53
53
|
.where('deletedAt', 'is', null)
|
|
54
|
+
.where('disabledAt', 'is', null)
|
|
54
55
|
.orderBy('updatedAt', 'desc')
|
|
55
56
|
.execute()
|
|
56
57
|
return rows.map(documentSummary)
|
|
@@ -82,6 +83,7 @@ export async function listSharedDocuments(
|
|
|
82
83
|
.where('documentGrant.userId', '=', userId)
|
|
83
84
|
.where('documentGrant.revokedAt', 'is', null)
|
|
84
85
|
.where('document.deletedAt', 'is', null)
|
|
86
|
+
.where('document.disabledAt', 'is', null)
|
|
85
87
|
.where((expression) =>
|
|
86
88
|
expression.not(
|
|
87
89
|
expression.exists(
|
|
@@ -12,12 +12,22 @@ import {
|
|
|
12
12
|
DocumentConflictError,
|
|
13
13
|
DocumentForbiddenError,
|
|
14
14
|
DocumentNotFoundError,
|
|
15
|
+
DocumentPurgeFailedError,
|
|
15
16
|
UploadInvalidError
|
|
16
17
|
} from '#cloud/server/documents/errors'
|
|
17
18
|
import type { DocumentService } from '#cloud/server/documents/service'
|
|
18
19
|
import { StorageQuotaExceededError } from '#cloud/server/quota'
|
|
19
|
-
import {
|
|
20
|
-
|
|
20
|
+
import {
|
|
21
|
+
CLOUD_RATE_LIMITS,
|
|
22
|
+
routeParamResource,
|
|
23
|
+
createResourceRateLimiter
|
|
24
|
+
} from '#cloud/server/rate-limit'
|
|
25
|
+
import {
|
|
26
|
+
documentParams,
|
|
27
|
+
uploadParams,
|
|
28
|
+
validatedJSON,
|
|
29
|
+
workspaceParams
|
|
30
|
+
} from '#cloud/server/validation'
|
|
21
31
|
import { Hono, type Context } from 'hono'
|
|
22
32
|
import type { Kysely } from 'kysely'
|
|
23
33
|
|
|
@@ -41,6 +51,10 @@ export function documentDomainError(context: Context, error: unknown): Response
|
|
|
41
51
|
if (error instanceof StorageQuotaExceededError) {
|
|
42
52
|
return context.json({ error: { code: 'storage_quota_exceeded' as const } }, 409)
|
|
43
53
|
}
|
|
54
|
+
// Reported where cleanup failed, so the request handler doesn't report it again.
|
|
55
|
+
if (error instanceof DocumentPurgeFailedError) {
|
|
56
|
+
return context.json({ error: { code: 'purge_failed' as const } }, 503)
|
|
57
|
+
}
|
|
44
58
|
if (error instanceof UploadInvalidError) {
|
|
45
59
|
return context.json({ error: { code: 'invalid_upload' as const } }, 422)
|
|
46
60
|
}
|
|
@@ -69,32 +83,32 @@ export function createDocumentRoutes(
|
|
|
69
83
|
if (rateLimit) {
|
|
70
84
|
router.use(
|
|
71
85
|
'/workspaces/:workspaceId/documents',
|
|
72
|
-
|
|
86
|
+
createResourceRateLimiter(
|
|
73
87
|
rateLimit.database,
|
|
74
88
|
rateLimit.secret,
|
|
75
89
|
CLOUD_RATE_LIMITS.documentCreation,
|
|
76
|
-
(
|
|
77
|
-
(context) => context.req.
|
|
90
|
+
routeParamResource('workspaceId'),
|
|
91
|
+
(context) => context.req.method !== 'POST'
|
|
78
92
|
)
|
|
79
93
|
)
|
|
80
94
|
router.use(
|
|
81
95
|
'/documents/:documentId/images',
|
|
82
|
-
|
|
96
|
+
createResourceRateLimiter(
|
|
83
97
|
rateLimit.database,
|
|
84
98
|
rateLimit.secret,
|
|
85
99
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
86
|
-
(
|
|
87
|
-
(context) => context.req.
|
|
100
|
+
routeParamResource('documentId'),
|
|
101
|
+
(context) => context.req.method !== 'POST'
|
|
88
102
|
)
|
|
89
103
|
)
|
|
90
104
|
router.use(
|
|
91
105
|
'/documents/:documentId/uploads',
|
|
92
|
-
|
|
106
|
+
createResourceRateLimiter(
|
|
93
107
|
rateLimit.database,
|
|
94
108
|
rateLimit.secret,
|
|
95
109
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
96
|
-
(
|
|
97
|
-
(context) => context.req.
|
|
110
|
+
routeParamResource('documentId'),
|
|
111
|
+
(context) => context.req.method !== 'POST'
|
|
98
112
|
)
|
|
99
113
|
)
|
|
100
114
|
}
|
|
@@ -102,18 +116,42 @@ export function createDocumentRoutes(
|
|
|
102
116
|
.get('/shared-documents', async (context) =>
|
|
103
117
|
context.json({ documents: await service.listShared(context.get('actor').userId) })
|
|
104
118
|
)
|
|
105
|
-
.get('/
|
|
119
|
+
.get('/trash', async (context) =>
|
|
120
|
+
context.json({ documents: await service.trash.list(context.get('actor').userId) })
|
|
121
|
+
)
|
|
122
|
+
.post('/trash/:documentId/restore', documentParams, (context) =>
|
|
123
|
+
documentResponse(context, async () => {
|
|
124
|
+
await service.trash.restore(
|
|
125
|
+
context.get('actor').userId,
|
|
126
|
+
context.req.valid('param').documentId
|
|
127
|
+
)
|
|
128
|
+
return context.body(null, 204)
|
|
129
|
+
})
|
|
130
|
+
)
|
|
131
|
+
.delete('/trash/:documentId', documentParams, (context) =>
|
|
132
|
+
documentResponse(context, async () => {
|
|
133
|
+
await service.trash.purge(
|
|
134
|
+
context.get('actor').userId,
|
|
135
|
+
context.req.valid('param').documentId
|
|
136
|
+
)
|
|
137
|
+
return context.body(null, 204)
|
|
138
|
+
})
|
|
139
|
+
)
|
|
140
|
+
.get('/workspaces/:workspaceId/documents', workspaceParams, async (context) => {
|
|
106
141
|
const documents = await service.list(
|
|
107
142
|
context.get('actor').userId,
|
|
108
|
-
context.req.
|
|
143
|
+
context.req.valid('param').workspaceId
|
|
109
144
|
)
|
|
110
145
|
if (!documents) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
111
146
|
return context.json({ documents })
|
|
112
147
|
})
|
|
113
|
-
.get('/workspaces/:workspaceId/usage', async (context) => {
|
|
148
|
+
.get('/workspaces/:workspaceId/usage', workspaceParams, async (context) => {
|
|
114
149
|
try {
|
|
115
150
|
return context.json({
|
|
116
|
-
usage: await service.usage(
|
|
151
|
+
usage: await service.usage(
|
|
152
|
+
context.get('actor').userId,
|
|
153
|
+
context.req.valid('param').workspaceId
|
|
154
|
+
)
|
|
117
155
|
})
|
|
118
156
|
} catch (error) {
|
|
119
157
|
const response = documentDomainError(context, error)
|
|
@@ -123,12 +161,13 @@ export function createDocumentRoutes(
|
|
|
123
161
|
})
|
|
124
162
|
.post(
|
|
125
163
|
'/workspaces/:workspaceId/documents',
|
|
164
|
+
workspaceParams,
|
|
126
165
|
validatedJSON(parseCreateDocument),
|
|
127
166
|
async (context) => {
|
|
128
167
|
try {
|
|
129
168
|
const document = await service.create(
|
|
130
169
|
context.get('actor').userId,
|
|
131
|
-
context.req.
|
|
170
|
+
context.req.valid('param').workspaceId,
|
|
132
171
|
context.req.valid('json')
|
|
133
172
|
)
|
|
134
173
|
return context.json({ document }, 201)
|
|
@@ -139,9 +178,9 @@ export function createDocumentRoutes(
|
|
|
139
178
|
}
|
|
140
179
|
}
|
|
141
180
|
)
|
|
142
|
-
.delete('/documents/:documentId', async (context) => {
|
|
181
|
+
.delete('/documents/:documentId', documentParams, async (context) => {
|
|
143
182
|
try {
|
|
144
|
-
await service.remove(context.get('actor').userId, context.req.
|
|
183
|
+
await service.remove(context.get('actor').userId, context.req.valid('param').documentId)
|
|
145
184
|
return context.body(null, 204)
|
|
146
185
|
} catch (error) {
|
|
147
186
|
const response = documentDomainError(context, error)
|
|
@@ -149,91 +188,104 @@ export function createDocumentRoutes(
|
|
|
149
188
|
throw error
|
|
150
189
|
}
|
|
151
190
|
})
|
|
152
|
-
.get('/documents/:documentId/access',
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
access: await service.access(
|
|
156
|
-
})
|
|
157
|
-
} catch (error) {
|
|
158
|
-
const response = documentDomainError(context, error)
|
|
159
|
-
if (response) return response
|
|
160
|
-
throw error
|
|
161
|
-
}
|
|
162
|
-
})
|
|
163
|
-
.get('/documents/:documentId', async (context) => {
|
|
164
|
-
try {
|
|
165
|
-
return context.json({
|
|
166
|
-
document: await service.download(
|
|
191
|
+
.get('/documents/:documentId/access', documentParams, (context) =>
|
|
192
|
+
documentResponse(context, async () =>
|
|
193
|
+
context.json({
|
|
194
|
+
access: await service.access(
|
|
167
195
|
context.get('actor').userId,
|
|
168
|
-
context.req.
|
|
196
|
+
context.req.valid('param').documentId
|
|
169
197
|
)
|
|
170
198
|
})
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
throw error
|
|
175
|
-
}
|
|
176
|
-
})
|
|
177
|
-
.post('/documents/:documentId/uploads', validatedJSON(parseCreateUpload), (context) =>
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
.get('/documents/:documentId', documentParams, (context) =>
|
|
178
202
|
documentResponse(context, async () =>
|
|
179
|
-
context.json(
|
|
180
|
-
await service.
|
|
203
|
+
context.json({
|
|
204
|
+
document: await service.download(
|
|
181
205
|
context.get('actor').userId,
|
|
182
|
-
context.req.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
201
|
|
186
|
-
)
|
|
206
|
+
context.req.valid('param').documentId
|
|
207
|
+
)
|
|
208
|
+
})
|
|
187
209
|
)
|
|
188
210
|
)
|
|
211
|
+
.post(
|
|
212
|
+
'/documents/:documentId/uploads',
|
|
213
|
+
documentParams,
|
|
214
|
+
validatedJSON(parseCreateUpload),
|
|
215
|
+
(context) =>
|
|
216
|
+
documentResponse(context, async () =>
|
|
217
|
+
context.json(
|
|
218
|
+
await service.createUpload(
|
|
219
|
+
context.get('actor').userId,
|
|
220
|
+
context.req.valid('param').documentId,
|
|
221
|
+
context.req.valid('json')
|
|
222
|
+
),
|
|
223
|
+
201
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
)
|
|
189
227
|
.post(
|
|
190
228
|
'/documents/:documentId/images/commit',
|
|
229
|
+
documentParams,
|
|
191
230
|
validatedJSON(parseCommitImageUploads),
|
|
192
231
|
(context) =>
|
|
193
232
|
documentResponse(context, async () => {
|
|
194
233
|
await service.commitImageUploads(
|
|
195
234
|
context.get('actor').userId,
|
|
196
|
-
context.req.
|
|
235
|
+
context.req.valid('param').documentId,
|
|
197
236
|
context.req.valid('json').uploads
|
|
198
237
|
)
|
|
199
238
|
return context.body(null, 204)
|
|
200
239
|
})
|
|
201
240
|
)
|
|
202
|
-
.post(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
241
|
+
.post(
|
|
242
|
+
'/documents/:documentId/images/links',
|
|
243
|
+
documentParams,
|
|
244
|
+
validatedJSON(parseRequestImageLinks),
|
|
245
|
+
(context) =>
|
|
246
|
+
documentResponse(context, async () =>
|
|
247
|
+
context.json(
|
|
248
|
+
await service.imageLinks(
|
|
249
|
+
context.get('actor').userId,
|
|
250
|
+
context.req.valid('param').documentId,
|
|
251
|
+
context.req.valid('json').checksums
|
|
252
|
+
)
|
|
209
253
|
)
|
|
210
254
|
)
|
|
211
|
-
)
|
|
212
255
|
)
|
|
213
|
-
.post(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
256
|
+
.post(
|
|
257
|
+
'/documents/:documentId/images',
|
|
258
|
+
documentParams,
|
|
259
|
+
validatedJSON(parseRequestImageUploads),
|
|
260
|
+
(context) =>
|
|
261
|
+
documentResponse(context, async () =>
|
|
262
|
+
context.json(
|
|
263
|
+
await service.requestImageUploads(
|
|
264
|
+
context.get('actor').userId,
|
|
265
|
+
context.req.valid('param').documentId,
|
|
266
|
+
context.req.valid('json')
|
|
267
|
+
),
|
|
268
|
+
201
|
|
269
|
+
)
|
|
222
270
|
)
|
|
223
|
-
)
|
|
224
271
|
)
|
|
225
|
-
.post(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
272
|
+
.post(
|
|
273
|
+
'/uploads/:uploadId/commit',
|
|
274
|
+
uploadParams,
|
|
275
|
+
validatedJSON(parseCommitUpload),
|
|
276
|
+
async (context) => {
|
|
277
|
+
try {
|
|
278
|
+
const document = await service.commitUpload(
|
|
279
|
+
context.get('actor').userId,
|
|
280
|
+
context.req.valid('param').uploadId,
|
|
281
|
+
context.req.valid('json')
|
|
282
|
+
)
|
|
283
|
+
return context.json({ document })
|
|
284
|
+
} catch (error) {
|
|
285
|
+
const response = documentDomainError(context, error)
|
|
286
|
+
if (response) return response
|
|
287
|
+
throw error
|
|
288
|
+
}
|
|
237
289
|
}
|
|
238
|
-
|
|
290
|
+
)
|
|
239
291
|
}
|