@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.
Files changed (235) hide show
  1. package/README.md +1 -1
  2. package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
  3. package/dist/bin/services.mjs.map +1 -0
  4. package/dist/client.d.ts +236 -46
  5. package/dist/client.d.ts.map +1 -1
  6. package/dist/client.js +649 -400
  7. package/dist/client.js.map +1 -1
  8. package/dist/contract.d.ts +2 -2
  9. package/dist/contract.js +2 -2
  10. package/dist/contract2.js +680 -443
  11. package/dist/contract2.js.map +1 -1
  12. package/dist/email.d.ts +9 -1
  13. package/dist/email.d.ts.map +1 -1
  14. package/dist/email.js +99 -58
  15. package/dist/email.js.map +1 -1
  16. package/dist/index.d.ts +468 -64
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/listen.mjs +575 -738
  19. package/dist/listen.mjs.map +1 -1
  20. package/dist/runtime-node.d.mts +6564 -2934
  21. package/dist/runtime-node.d.mts.map +1 -1
  22. package/dist/runtime-node.mjs +6 -5
  23. package/dist/runtime-node.mjs.map +1 -1
  24. package/dist/server.d.ts +8931 -4198
  25. package/dist/server.d.ts.map +1 -1
  26. package/dist/server.js +5260 -3859
  27. package/dist/server.js.map +1 -1
  28. package/dist/server.mjs +5686 -3736
  29. package/dist/server.mjs.map +1 -1
  30. package/dist/server2.mjs +101 -79
  31. package/dist/server2.mjs.map +1 -1
  32. package/package.json +9 -6
  33. package/src/admin/api/routes.ts +103 -33
  34. package/src/admin/audit/service.ts +25 -6
  35. package/src/admin/documents/service.ts +361 -0
  36. package/src/admin/errors.ts +9 -1
  37. package/src/admin/index.ts +6 -0
  38. package/src/admin/operations/service.ts +7 -0
  39. package/src/admin/reports/service.ts +147 -0
  40. package/src/admin/schema.ts +6 -0
  41. package/src/client/api.ts +36 -21
  42. package/src/client/auth/navigation.ts +6 -5
  43. package/src/client/index.ts +7 -7
  44. package/src/client/portal/account.ts +25 -0
  45. package/src/client/portal/administration.ts +56 -0
  46. package/src/client/{ai.ts → services.ts} +44 -27
  47. package/src/contract/account.ts +58 -2
  48. package/src/contract/admin/index.ts +67 -3
  49. package/src/contract/collaboration.ts +6 -1
  50. package/src/contract/discovery.ts +22 -4
  51. package/src/contract/documents.ts +27 -0
  52. package/src/contract/entitlements.ts +2 -2
  53. package/src/contract/index.ts +63 -37
  54. package/src/contract/relay.ts +6 -0
  55. package/src/contract/reports.ts +42 -0
  56. package/src/contract/services.ts +233 -0
  57. package/src/contract/sharing.ts +3 -9
  58. package/src/email/render.ts +26 -0
  59. package/src/email/schema.ts +11 -1
  60. package/src/email/templates/brand.ts +1 -1
  61. package/src/runtime/cloudflare/environment.ts +31 -6
  62. package/src/runtime/cloudflare/index.ts +1 -1
  63. package/src/runtime/cloudflare/logging.ts +15 -0
  64. package/src/runtime/cloudflare/maintenance.ts +31 -0
  65. package/src/runtime/cloudflare/relay/index.ts +176 -0
  66. package/src/runtime/cloudflare/relay/sockets.ts +112 -0
  67. package/src/runtime/cloudflare/worker.ts +89 -26
  68. package/src/runtime/node/application.ts +5 -3
  69. package/src/runtime/node/bootstrap.ts +20 -4
  70. package/src/runtime/node/cleanup.ts +16 -3
  71. package/src/runtime/node/entitlements/main.ts +14 -11
  72. package/src/runtime/node/index.ts +1 -1
  73. package/src/runtime/node/migrate.ts +5 -1
  74. package/src/runtime/node/relay.ts +8 -2
  75. package/src/runtime/node/request-id.ts +13 -0
  76. package/src/runtime/node/server.ts +71 -15
  77. package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
  78. package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
  79. package/src/runtime/postgres/database.ts +20 -1
  80. package/src/runtime/posthog/errors.ts +32 -0
  81. package/src/runtime/s3/objects.ts +50 -1
  82. package/src/runtime/site/server.ts +33 -4
  83. package/src/server/account/actions.ts +5 -0
  84. package/src/server/account/deletion.ts +302 -0
  85. package/src/server/account/routes.ts +47 -3
  86. package/src/server/account/terms.ts +42 -0
  87. package/src/server/api/router.ts +12 -7
  88. package/src/server/api/types.ts +9 -0
  89. package/src/server/app.ts +93 -15
  90. package/src/server/auth/access-control.ts +63 -0
  91. package/src/server/auth/adapter.ts +11 -0
  92. package/src/server/auth/endpoints.ts +5 -1
  93. package/src/server/auth/factory.ts +30 -3
  94. package/src/server/auth/index.ts +10 -1
  95. package/src/server/cleanup/configured.ts +12 -5
  96. package/src/server/cleanup/documents.ts +165 -71
  97. package/src/server/cleanup/objects.ts +10 -2
  98. package/src/server/cleanup/revisions.ts +24 -7
  99. package/src/server/cleanup/uploads.ts +8 -2
  100. package/src/server/cleanup/worker.ts +5 -2
  101. package/src/server/collaboration/index.ts +3 -0
  102. package/src/server/collaboration/relay/connection.ts +196 -0
  103. package/src/server/collaboration/relay/documents.ts +13 -10
  104. package/src/server/collaboration/relay/hub.ts +279 -195
  105. package/src/server/collaboration/relay/index.ts +3 -1
  106. package/src/server/collaboration/relay/log.ts +79 -0
  107. package/src/server/collaboration/relay/persistence.ts +147 -22
  108. package/src/server/collaboration/relay/resume.ts +28 -0
  109. package/src/server/collaboration/routes.ts +27 -16
  110. package/src/server/collaboration/service.ts +27 -31
  111. package/src/server/config/deployment.ts +35 -4
  112. package/src/server/config/public.ts +5 -4
  113. package/src/server/config/schema.ts +104 -16
  114. package/src/server/db/expressions.ts +15 -1
  115. package/src/server/db/migrate.ts +34 -114
  116. package/src/server/db/migrations/001_accounts.ts +79 -0
  117. package/src/server/db/migrations/002_workspaces.ts +48 -0
  118. package/src/server/db/migrations/003_storage.ts +44 -0
  119. package/src/server/db/migrations/004_documents.ts +193 -0
  120. package/src/server/db/migrations/005_sharing.ts +95 -0
  121. package/src/server/db/migrations/006_operations.ts +110 -0
  122. package/src/server/db/migrations/007_trash.ts +33 -0
  123. package/src/server/db/migrations/008_terms.ts +18 -0
  124. package/src/server/db/migrations/schema.ts +71 -0
  125. package/src/server/db/tables.ts +70 -7
  126. package/src/server/documents/access.ts +66 -55
  127. package/src/server/documents/errors.ts +7 -0
  128. package/src/server/documents/images.ts +2 -1
  129. package/src/server/documents/limits.ts +9 -9
  130. package/src/server/documents/purge.ts +75 -0
  131. package/src/server/documents/repository.ts +2 -0
  132. package/src/server/documents/routes.ts +132 -80
  133. package/src/server/documents/service.ts +18 -4
  134. package/src/server/documents/summary.ts +1 -0
  135. package/src/server/documents/trash.ts +203 -0
  136. package/src/server/documents/versions/checkpoint.ts +2 -1
  137. package/src/server/documents/versions/history.ts +11 -7
  138. package/src/server/documents/versions/routes.ts +36 -25
  139. package/src/server/documents/versions/service.ts +22 -4
  140. package/src/server/errors/index.ts +99 -0
  141. package/src/server/errors/posthog.ts +80 -0
  142. package/src/server/index.ts +56 -16
  143. package/src/server/internal/index.ts +3 -3
  144. package/src/server/internal/routes.ts +7 -15
  145. package/src/server/internal/schema.ts +1 -1
  146. package/src/server/internal/service.ts +17 -23
  147. package/src/server/invitations/budget.ts +66 -0
  148. package/src/server/invitations/index.ts +1 -0
  149. package/src/server/logging.ts +76 -0
  150. package/src/server/objects/types.ts +12 -0
  151. package/src/server/pictures/routes.ts +4 -2
  152. package/src/server/pictures/service.ts +2 -1
  153. package/src/server/policy/database.ts +39 -25
  154. package/src/server/policy/index.ts +2 -1
  155. package/src/server/policy/keys.ts +5 -2
  156. package/src/server/policy/policy.ts +1 -0
  157. package/src/server/policy/schema.ts +2 -2
  158. package/src/server/policy/service.ts +12 -9
  159. package/src/server/policy/static.ts +5 -5
  160. package/src/server/policy/workspace.ts +41 -0
  161. package/src/server/quota/reconcile.ts +2 -0
  162. package/src/server/quota/storage.ts +69 -31
  163. package/src/server/rate-limit/cleanup.ts +17 -5
  164. package/src/server/rate-limit/index.ts +4 -1
  165. package/src/server/rate-limit/middleware.ts +100 -12
  166. package/src/server/reports/index.ts +8 -0
  167. package/src/server/reports/routes.ts +37 -0
  168. package/src/server/reports/service.ts +123 -0
  169. package/src/server/services/errors.ts +7 -0
  170. package/src/server/services/index.ts +8 -0
  171. package/src/server/{ai → services}/keys.ts +11 -11
  172. package/src/server/services/routes.ts +47 -0
  173. package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
  174. package/src/server/sharing/capabilities/service.ts +68 -22
  175. package/src/server/sharing/document-routes.ts +30 -22
  176. package/src/server/sharing/grants/service.ts +11 -18
  177. package/src/server/sharing/http.ts +4 -0
  178. package/src/server/sharing/invitations/service.ts +21 -11
  179. package/src/server/sharing/policy.ts +9 -14
  180. package/src/server/sharing/routes.ts +108 -85
  181. package/src/server/sharing/service.ts +1 -17
  182. package/src/server/validation.ts +59 -2
  183. package/src/server/workspaces/index.ts +1 -0
  184. package/src/server/workspaces/members.ts +89 -38
  185. package/src/server/workspaces/repository.ts +2 -1
  186. package/src/server/workspaces/routes.ts +67 -45
  187. package/src/{ai-relay → services}/app.ts +48 -44
  188. package/src/{ai-relay → services}/config.ts +25 -19
  189. package/src/services/db/index.ts +10 -0
  190. package/src/services/db/migrate.ts +37 -0
  191. package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
  192. package/src/services/db/schema.ts +22 -0
  193. package/src/services/index.ts +18 -0
  194. package/src/{ai-relay → services}/metering.ts +2 -2
  195. package/src/{ai-relay → services}/usage.ts +43 -19
  196. package/dist/bin/ai-relay.mjs.map +0 -1
  197. package/src/ai-relay/db/index.ts +0 -10
  198. package/src/ai-relay/db/migrate.ts +0 -32
  199. package/src/ai-relay/db/schema.ts +0 -20
  200. package/src/ai-relay/index.ts +0 -18
  201. package/src/contract/ai.ts +0 -150
  202. package/src/runtime/cloudflare/relay.ts +0 -136
  203. package/src/server/ai/errors.ts +0 -7
  204. package/src/server/ai/index.ts +0 -4
  205. package/src/server/ai/routes.ts +0 -43
  206. package/src/server/auth/workspace-access.ts +0 -23
  207. package/src/server/db/migrations/001_foundation.ts +0 -164
  208. package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
  209. package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
  210. package/src/server/db/migrations/004_document_sharing.ts +0 -82
  211. package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
  212. package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
  213. package/src/server/db/migrations/007_collaboration_state.ts +0 -25
  214. package/src/server/db/migrations/008_storage_reservations.ts +0 -37
  215. package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
  216. package/src/server/db/migrations/010_upload_finalization.ts +0 -22
  217. package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
  218. package/src/server/db/migrations/012_transactional_email.ts +0 -48
  219. package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
  220. package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
  221. package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
  222. package/src/server/db/migrations/016_auth_schema.ts +0 -16
  223. package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
  224. package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
  225. package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
  226. package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
  227. package/src/server/db/migrations/021_document_versions.ts +0 -66
  228. package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
  229. package/src/server/db/migrations/023_document_images.ts +0 -61
  230. package/src/server/db/migrations/024_account_activity.ts +0 -66
  231. package/src/server/db/migrations/025_table_names.ts +0 -58
  232. package/src/server/db/migrations/026_user_picture.ts +0 -30
  233. package/src/server/db/migrations/027_document_kind.ts +0 -19
  234. package/src/server/db/migrations/helpers.ts +0 -78
  235. /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
@@ -1,26 +1,30 @@
1
- import type {
2
- DocumentAccess,
3
- DocumentAccessSource,
4
- DocumentPermission,
5
- WorkspaceRole
6
- } from '#cloud/contract'
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
- const PERMISSION_WEIGHT: Record<DocumentPermission, number> = { view: 1, edit: 2 }
14
-
15
- function workspacePermission(role: WorkspaceRole): DocumentPermission {
16
- return role === 'viewer' ? 'view' : 'edit'
13
+ type DocumentAccessGrant = {
14
+ source: DocumentAccessSource
15
+ can: (request: AccessRequest) => boolean
17
16
  }
18
17
 
19
- export async function resolveDocumentAccess(
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
- ): Promise<DocumentAccess | undefined> {
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 undefined
51
+ if (!document) return []
46
52
 
47
- const sources: DocumentAccessSource[] = []
48
- const candidates: DocumentPermission[] = []
49
- const owner = document.createdBy === userId
50
- if (owner) {
51
- sources.push('owner')
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
- if (document.workspaceRole) {
55
- sources.push('workspace')
56
- candidates.push(workspacePermission(document.workspaceRole))
59
+ const { workspaceRole, grantPermission } = document
60
+ if (workspaceRole) {
61
+ grants.push({ source: 'workspace', can: (request) => workspaceRoleCan(workspaceRole, request) })
57
62
  }
58
- if (document.grantPermission) {
59
- sources.push('direct-grant')
60
- candidates.push(document.grantPermission)
63
+ if (grantPermission) {
64
+ const role = GRANT_DOCUMENT_ROLE[grantPermission]
65
+ grants.push({ source: 'direct-grant', can: (request) => documentRoleCan(role, request) })
61
66
  }
62
- const permission = maxBy(candidates, (candidate) => PERMISSION_WEIGHT[candidate])
63
- if (!permission) return undefined
64
- const workspaceCanManage =
65
- document.workspaceRole === 'admin' || document.workspaceRole === 'editor'
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: owner || workspaceCanManage || document.grantPermission === 'edit',
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 document = await database
87
- .selectFrom('document')
88
- .leftJoin('workspaceMember', (join) =>
89
- join
90
- .onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
91
- .on('workspaceMember.userId', '=', userId)
92
- )
93
- .select(['document.createdBy', 'workspaceMember.role as workspaceRole'])
94
- .where('document.id', '=', documentId)
95
- .where('document.deletedAt', 'is', null)
96
- .executeTakeFirst()
97
- if (!document) return false
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
- console.warn('Giving up an image upload failed', error)
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 the workspace's storage may
11
- * be for this save: technical limits, tightened by policy.
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
- targetingKey: document.workspaceId,
28
- actorId: userId,
29
- workspaceId: document.workspaceId,
30
- documentId,
31
- deploymentMode: 'self-hosted' as const
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 { CLOUD_RATE_LIMITS, createRouteRateLimiter } from '#cloud/server/rate-limit'
20
- import { validatedJSON } from '#cloud/server/validation'
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
- createRouteRateLimiter(
86
+ createResourceRateLimiter(
73
87
  rateLimit.database,
74
88
  rateLimit.secret,
75
89
  CLOUD_RATE_LIMITS.documentCreation,
76
- (context) => context.req.method !== 'POST',
77
- (context) => context.req.param('workspaceId') ?? 'unknown'
90
+ routeParamResource('workspaceId'),
91
+ (context) => context.req.method !== 'POST'
78
92
  )
79
93
  )
80
94
  router.use(
81
95
  '/documents/:documentId/images',
82
- createRouteRateLimiter(
96
+ createResourceRateLimiter(
83
97
  rateLimit.database,
84
98
  rateLimit.secret,
85
99
  CLOUD_RATE_LIMITS.uploadCreation,
86
- (context) => context.req.method !== 'POST',
87
- (context) => context.req.param('documentId') ?? 'unknown'
100
+ routeParamResource('documentId'),
101
+ (context) => context.req.method !== 'POST'
88
102
  )
89
103
  )
90
104
  router.use(
91
105
  '/documents/:documentId/uploads',
92
- createRouteRateLimiter(
106
+ createResourceRateLimiter(
93
107
  rateLimit.database,
94
108
  rateLimit.secret,
95
109
  CLOUD_RATE_LIMITS.uploadCreation,
96
- (context) => context.req.method !== 'POST',
97
- (context) => context.req.param('documentId') ?? 'unknown'
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('/workspaces/:workspaceId/documents', async (context) => {
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.param('workspaceId')
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(context.get('actor').userId, context.req.param('workspaceId'))
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.param('workspaceId'),
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.param('documentId'))
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', async (context) => {
153
- try {
154
- return context.json({
155
- access: await service.access(context.get('actor').userId, context.req.param('documentId'))
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.param('documentId')
196
+ context.req.valid('param').documentId
169
197
  )
170
198
  })
171
- } catch (error) {
172
- const response = documentDomainError(context, error)
173
- if (response) return response
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.createUpload(
203
+ context.json({
204
+ document: await service.download(
181
205
  context.get('actor').userId,
182
- context.req.param('documentId'),
183
- context.req.valid('json')
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.param('documentId'),
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('/documents/:documentId/images/links', validatedJSON(parseRequestImageLinks), (context) =>
203
- documentResponse(context, async () =>
204
- context.json(
205
- await service.imageLinks(
206
- context.get('actor').userId,
207
- context.req.param('documentId'),
208
- context.req.valid('json').checksums
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('/documents/:documentId/images', validatedJSON(parseRequestImageUploads), (context) =>
214
- documentResponse(context, async () =>
215
- context.json(
216
- await service.requestImageUploads(
217
- context.get('actor').userId,
218
- context.req.param('documentId'),
219
- context.req.valid('json')
220
- ),
221
- 201
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('/uploads/:uploadId/commit', validatedJSON(parseCommitUpload), async (context) => {
226
- try {
227
- const document = await service.commitUpload(
228
- context.get('actor').userId,
229
- context.req.param('uploadId'),
230
- context.req.valid('json')
231
- )
232
- return context.json({ document })
233
- } catch (error) {
234
- const response = documentDomainError(context, error)
235
- if (response) return response
236
- throw error
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
  }