@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5

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 (206) hide show
  1. package/README.md +2 -1
  2. package/dist/bin/server.mjs +1 -1
  3. package/dist/bin/server.mjs.map +1 -1
  4. package/dist/bin/services.mjs +1 -1
  5. package/dist/bin/services.mjs.map +1 -1
  6. package/dist/client.d.ts +296 -27
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +649 -295
  9. package/dist/client.js.map +1 -1
  10. package/dist/contract.d.ts +2 -2
  11. package/dist/contract.js +2 -2
  12. package/dist/contract2.js +331 -21
  13. package/dist/contract2.js.map +1 -1
  14. package/dist/email.d.ts +1773 -32
  15. package/dist/email.d.ts.map +1 -1
  16. package/dist/email.js +3478 -288
  17. package/dist/email.js.map +1 -1
  18. package/dist/errors.d.ts +2 -0
  19. package/dist/errors.js +48 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +466 -13
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index2.d.ts +35 -0
  24. package/dist/index2.d.ts.map +1 -0
  25. package/dist/listen.mjs +447 -290
  26. package/dist/listen.mjs.map +1 -1
  27. package/dist/runtime-cloudflare.d.ts +89 -0
  28. package/dist/runtime-cloudflare.d.ts.map +1 -0
  29. package/dist/runtime-cloudflare.js +15667 -0
  30. package/dist/runtime-cloudflare.js.map +1 -0
  31. package/dist/runtime-node.d.mts +2963 -294
  32. package/dist/runtime-node.d.mts.map +1 -1
  33. package/dist/server.d.ts +3473 -842
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +12187 -8244
  36. package/dist/server.js.map +1 -1
  37. package/dist/server.mjs +7739 -3548
  38. package/dist/server.mjs.map +1 -1
  39. package/dist/server2.mjs +5 -5
  40. package/dist/server2.mjs.map +1 -1
  41. package/package.json +22 -5
  42. package/src/admin/audit/service.ts +2 -2
  43. package/src/admin/documents/service.ts +76 -6
  44. package/src/admin/reports/service.ts +7 -1
  45. package/src/client/api.ts +23 -471
  46. package/src/client/auth/client.ts +22 -3
  47. package/src/client/billing.ts +80 -0
  48. package/src/client/community.ts +40 -0
  49. package/src/client/discovery.ts +35 -9
  50. package/src/client/documents.ts +160 -0
  51. package/src/client/index.ts +7 -0
  52. package/src/client/portal/account.ts +54 -2
  53. package/src/client/portal/request.ts +2 -0
  54. package/src/client/response.ts +21 -0
  55. package/src/client/schema.ts +8 -0
  56. package/src/client/services.ts +23 -18
  57. package/src/client/sharing.ts +262 -0
  58. package/src/client/versions.ts +104 -0
  59. package/src/contract/account.ts +50 -0
  60. package/src/contract/billing.ts +166 -0
  61. package/src/contract/collaboration.ts +5 -0
  62. package/src/contract/community.ts +60 -0
  63. package/src/contract/discovery.ts +63 -2
  64. package/src/contract/entitlements.ts +10 -0
  65. package/src/contract/index.ts +54 -5
  66. package/src/contract/links.ts +57 -4
  67. package/src/contract/locale.ts +27 -0
  68. package/src/contract/portal.ts +12 -1
  69. package/src/contract/routes.ts +10 -0
  70. package/src/contract/services.ts +18 -16
  71. package/src/email/i18n.ts +174 -0
  72. package/src/email/index.ts +13 -2
  73. package/src/email/locales/ar.json +141 -0
  74. package/src/email/locales/de.json +141 -0
  75. package/src/email/locales/es.json +141 -0
  76. package/src/email/locales/fr.json +141 -0
  77. package/src/email/locales/it.json +141 -0
  78. package/src/email/locales/ja.json +141 -0
  79. package/src/email/locales/pl.json +141 -0
  80. package/src/email/locales/pt-br.json +141 -0
  81. package/src/email/locales/ru.json +141 -0
  82. package/src/email/locales/zh-cn.json +141 -0
  83. package/src/email/messages.ts +200 -0
  84. package/src/email/render.ts +142 -276
  85. package/src/email/samples.ts +115 -0
  86. package/src/email/schema.ts +70 -6
  87. package/src/email/templates/BrandEmail.vue +54 -0
  88. package/src/email/templates/DocumentBlocked.vue +45 -0
  89. package/src/email/templates/DocumentInvitation.vue +24 -0
  90. package/src/email/templates/DocumentReport.vue +32 -0
  91. package/src/email/templates/DocumentReportDecided.vue +36 -0
  92. package/src/email/templates/DocumentReportReceived.vue +18 -0
  93. package/src/email/templates/DocumentRestored.vue +24 -0
  94. package/src/email/templates/EmailChangeRequested.vue +24 -0
  95. package/src/email/templates/EmailRoot.vue +19 -0
  96. package/src/email/templates/EmailText.vue +9 -0
  97. package/src/email/templates/EmailVerification.vue +38 -0
  98. package/src/email/templates/EnrollmentNotification.vue +24 -0
  99. package/src/email/templates/EnrollmentUpdate.vue +52 -0
  100. package/src/email/templates/NewSignIn.vue +37 -0
  101. package/src/email/templates/PasswordChanged.vue +22 -0
  102. package/src/email/templates/PasswordReset.vue +23 -0
  103. package/src/email/templates/StorageLimitReached.vue +28 -0
  104. package/src/email/templates/TwoStepChanged.vue +32 -0
  105. package/src/email/templates/WaitlistConfirmation.vue +29 -0
  106. package/src/email/templates/WorkspaceInvitation.vue +28 -0
  107. package/src/email/templates/brand.ts +9 -61
  108. package/src/email/templates/localization.ts +17 -0
  109. package/src/errors/index.ts +6 -0
  110. package/src/global.d.ts +7 -0
  111. package/src/runtime/cloudflare/relay/index.ts +7 -1
  112. package/src/runtime/cloudflare/tsconfig.json +2 -1
  113. package/src/runtime/cloudflare/worker.ts +8 -1
  114. package/src/runtime/node/cleanup.ts +5 -1
  115. package/src/runtime/node/email/transport.ts +13 -10
  116. package/src/runtime/node/main.ts +1 -1
  117. package/src/runtime/node/server.ts +12 -2
  118. package/src/runtime/node/services/main.ts +1 -1
  119. package/src/runtime/posthog/errors.ts +2 -1
  120. package/src/runtime/s3/objects.ts +17 -0
  121. package/src/runtime/site/community.ts +101 -0
  122. package/src/runtime/site/server.ts +17 -1
  123. package/src/server/account/deletion.ts +2 -0
  124. package/src/server/account/routes.ts +271 -240
  125. package/src/server/account/tokens.ts +57 -0
  126. package/src/server/api/router.ts +23 -0
  127. package/src/server/app/checks.ts +84 -0
  128. package/src/server/app/editors.ts +20 -0
  129. package/src/server/app/index.ts +128 -0
  130. package/src/server/app/indexing.ts +40 -0
  131. package/src/server/app/limiters.ts +53 -0
  132. package/src/server/app/routes.ts +114 -0
  133. package/src/server/app/services.ts +185 -0
  134. package/src/server/auth/access-tokens.ts +62 -0
  135. package/src/server/auth/adapter.ts +21 -1
  136. package/src/server/auth/email.ts +4 -1
  137. package/src/server/auth/enrollment.ts +116 -0
  138. package/src/server/auth/factory.ts +88 -131
  139. package/src/server/auth/logger.ts +23 -0
  140. package/src/server/auth/mfa-assurance.ts +51 -0
  141. package/src/server/auth/passwords.ts +43 -0
  142. package/src/server/auth/session.ts +2 -0
  143. package/src/server/billing/index.ts +6 -0
  144. package/src/server/billing/routes.ts +27 -0
  145. package/src/server/billing/tokens.ts +37 -0
  146. package/src/server/cleanup/configured.ts +16 -3
  147. package/src/server/cleanup/worker.ts +10 -1
  148. package/src/server/collaboration/relay/hub.ts +25 -142
  149. package/src/server/collaboration/relay/room.ts +154 -0
  150. package/src/server/collaboration/service.ts +21 -2
  151. package/src/server/community/index.ts +2 -0
  152. package/src/server/community/routes.ts +93 -0
  153. package/src/server/community/service.ts +134 -0
  154. package/src/server/config/deployment.ts +17 -5
  155. package/src/server/config/index.ts +1 -1
  156. package/src/server/config/public.ts +7 -1
  157. package/src/server/config/schema.ts +40 -292
  158. package/src/server/config/toml/schema.ts +307 -0
  159. package/src/server/db/index.ts +1 -0
  160. package/src/server/db/migrate.ts +13 -1
  161. package/src/server/db/migrations/009_access_tokens.ts +47 -0
  162. package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
  163. package/src/server/db/migrations/011_account_ids.ts +30 -0
  164. package/src/server/db/migrations/012_email_locales.ts +34 -0
  165. package/src/server/db/migrations/013_document_block_emails.ts +40 -0
  166. package/src/server/db/migrations/014_report_replies.ts +28 -0
  167. package/src/server/db/migrations/schema.ts +26 -1
  168. package/src/server/db/tables.ts +30 -1
  169. package/src/server/db/users.ts +22 -0
  170. package/src/server/documents/index.ts +1 -1
  171. package/src/server/documents/limits.ts +7 -1
  172. package/src/server/documents/service.ts +12 -402
  173. package/src/server/documents/uploads.ts +450 -0
  174. package/src/server/documents/versions/service.ts +81 -24
  175. package/src/server/email/invitations.ts +1 -0
  176. package/src/server/email/service.ts +31 -4
  177. package/src/server/email/types.ts +12 -0
  178. package/src/server/errors/index.ts +1 -9
  179. package/src/server/index.ts +12 -5
  180. package/src/server/invitations/types.ts +5 -1
  181. package/src/server/objects/types.ts +5 -0
  182. package/src/server/policy/configured.ts +9 -3
  183. package/src/server/policy/database.ts +13 -2
  184. package/src/server/policy/policy.ts +39 -12
  185. package/src/server/policy/service.ts +71 -52
  186. package/src/server/policy/storage.ts +33 -0
  187. package/src/server/quota/index.ts +6 -0
  188. package/src/server/quota/notices.ts +121 -0
  189. package/src/server/quota/storage.ts +5 -0
  190. package/src/server/rate-limit/middleware.ts +4 -0
  191. package/src/server/reports/index.ts +1 -0
  192. package/src/server/reports/replies.ts +55 -0
  193. package/src/server/reports/routes.ts +3 -1
  194. package/src/server/reports/service.ts +24 -2
  195. package/src/server/sharing/capabilities/service.ts +2 -3
  196. package/src/server/sharing/invitations/service.ts +14 -4
  197. package/src/server/validation.ts +3 -0
  198. package/src/server/workspaces/members.ts +7 -19
  199. package/src/services/app.ts +1 -3
  200. package/src/services/config.ts +1 -0
  201. package/src/services/usage.ts +6 -14
  202. package/src/email/templates/document-invitation.ts +0 -35
  203. package/src/email/templates/enrollment.ts +0 -27
  204. package/src/email/templates/layout.ts +0 -18
  205. package/src/server/app.ts +0 -453
  206. /package/src/{server/errors → errors}/posthog.ts +0 -0
@@ -0,0 +1,450 @@
1
+ import type {
2
+ CommitImageUploadsInput,
3
+ CommitUploadInput,
4
+ CreateUploadInput,
5
+ DocumentSummary,
6
+ ImageUploadOffer,
7
+ RequestImageUploadsInput
8
+ } from '#cloud/contract'
9
+ import type { CloudDatabase } from '#cloud/server/db'
10
+ import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
11
+ import {
12
+ DocumentConflictError,
13
+ DocumentForbiddenError,
14
+ DocumentNotFoundError,
15
+ UploadInvalidError
16
+ } from '#cloud/server/documents/errors'
17
+ import type { DocumentImages } from '#cloud/server/documents/images'
18
+ import { writableDocument } from '#cloud/server/documents/limits'
19
+ import { findDocument } from '#cloud/server/documents/repository'
20
+ import type { DocumentServiceOptions } from '#cloud/server/documents/service'
21
+ import type { CreateDocumentUploadResult } from '#cloud/server/documents/types'
22
+ import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
23
+ import type { ObjectStore, StoredObject } from '#cloud/server/objects'
24
+ import { storageLimitReached } from '#cloud/server/policy/storage'
25
+ import { StorageQuotaExceededError, type StorageQuotaService } from '#cloud/server/quota'
26
+ import { isUndefined, limitAsync, omitBy, uniq, uniqBy } from 'es-toolkit'
27
+ import type { Kysely, Transaction } from 'kysely'
28
+
29
+ const UPLOAD_LIFETIME_MS = 15 * 60 * 1000
30
+ /** How many of a save's images are checked and landed at once. */
31
+ const IMAGE_COMMIT_CONCURRENCY = 8
32
+
33
+ /** Whether the stored object is the file the upload declared, which the commit names again. */
34
+ function matchesUpload(
35
+ stored: { checksum: string; byteSize: number; contentType: string },
36
+ upload: { checksum: string; byteSize: number | string | bigint; contentType: string }
37
+ ): boolean {
38
+ return (
39
+ stored.checksum === upload.checksum &&
40
+ stored.byteSize === Number(upload.byteSize) &&
41
+ stored.contentType === upload.contentType
42
+ )
43
+ }
44
+
45
+ /**
46
+ * Saving a document: uploads of a new revision and of the images it names, reserved against the
47
+ * workspace's storage, then committed once the store holds what each declared, landing the
48
+ * revision as the document's current one.
49
+ */
50
+ export function createDocumentUploads(
51
+ database: Kysely<CloudDatabase>,
52
+ objects: ObjectStore,
53
+ {
54
+ quota,
55
+ images,
56
+ options
57
+ }: {
58
+ quota: StorageQuotaService
59
+ images: DocumentImages
60
+ options: Pick<
61
+ DocumentServiceOptions,
62
+ 'policy' | 'technicalMaximumUploadBytes' | 'onVersionsChanged'
63
+ >
64
+ }
65
+ ) {
66
+ /**
67
+ * Refuses to land an upload once its document's owner holds their whole storage allowance, as
68
+ * when their plan shrank after the upload was offered; an upload already landed is not landed
69
+ * again, so retrying one stays as it was.
70
+ */
71
+ async function refuseAtStorageLimit(documentId: string): Promise<void> {
72
+ if (await storageLimitReached(database, options.policy, { documentId })) {
73
+ throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Makes a committed upload the document's current revision, unless it already was, and records
79
+ * the checkpoint the save adds, if any. Whether the history gained one.
80
+ */
81
+ async function landRevision(
82
+ transaction: Transaction<CloudDatabase>,
83
+ {
84
+ userId,
85
+ uploadId,
86
+ upload,
87
+ stored,
88
+ revisionId,
89
+ storageObjectId,
90
+ images: revisionImages,
91
+ name,
92
+ kind
93
+ }: {
94
+ userId: string
95
+ uploadId: string
96
+ upload: {
97
+ documentId: string
98
+ workspaceId: string
99
+ baseRevisionId: string | null
100
+ objectKey: string
101
+ }
102
+ stored: { checksum: string; byteSize: number; contentType: string }
103
+ revisionId: string
104
+ storageObjectId: string
105
+ images: CommitUploadInput['images']
106
+ name: CommitUploadInput['name']
107
+ kind: CommitUploadInput['kind']
108
+ }
109
+ ): Promise<boolean> {
110
+ const lockedUpload = await transaction
111
+ .selectFrom('upload')
112
+ .select('status')
113
+ .where('id', '=', uploadId)
114
+ .forUpdate()
115
+ .executeTakeFirstOrThrow()
116
+ if (lockedUpload.status === 'committed') return false
117
+ if (lockedUpload.status !== 'finalizing') throw new UploadInvalidError()
118
+ const document = await transaction
119
+ .selectFrom('document')
120
+ .select(['currentRevisionId'])
121
+ .where('id', '=', upload.documentId)
122
+ .forUpdate()
123
+ .executeTakeFirstOrThrow()
124
+ if (document.currentRevisionId !== upload.baseRevisionId) {
125
+ throw new DocumentConflictError()
126
+ }
127
+ await transaction
128
+ .insertInto('storageObject')
129
+ .values({
130
+ id: storageObjectId,
131
+ objectKey: upload.objectKey,
132
+ checksum: stored.checksum,
133
+ byteSize: stored.byteSize,
134
+ contentType: stored.contentType
135
+ })
136
+ .execute()
137
+ await transaction
138
+ .insertInto('documentRevision')
139
+ .values({
140
+ id: revisionId,
141
+ documentId: upload.documentId,
142
+ parentRevisionId: upload.baseRevisionId,
143
+ storageObjectId,
144
+ createdBy: userId
145
+ })
146
+ .execute()
147
+ const attached = await images.attachInTransaction(transaction, {
148
+ workspaceId: upload.workspaceId,
149
+ revisionId,
150
+ images: revisionImages
151
+ })
152
+ if (!attached) throw new UploadInvalidError()
153
+ await transaction
154
+ .updateTable('document')
155
+ .set({
156
+ // A revision may also retitle its document, or say the file it holds is a board.
157
+ ...omitBy({ name, kind }, isUndefined),
158
+ currentRevisionId: revisionId,
159
+ version: (expression) => expression('version', '+', 1)
160
+ })
161
+ .where('id', '=', upload.documentId)
162
+ .execute()
163
+ const checkpointed = await recordCheckpoint(transaction, {
164
+ documentId: upload.documentId,
165
+ previousRevisionId: document.currentRevisionId,
166
+ revisionId,
167
+ userId,
168
+ now: new Date()
169
+ })
170
+ await transaction
171
+ .updateTable('upload')
172
+ .set({ status: 'committed', finalizationStartedAt: null })
173
+ .where('id', '=', uploadId)
174
+ .execute()
175
+ await quota.commitInTransaction(transaction, uploadId)
176
+ return checkpointed
177
+ }
178
+
179
+ /** What committing an upload reads of it and its document. */
180
+ function selectUploads() {
181
+ return database
182
+ .selectFrom('upload')
183
+ .innerJoin('document', 'document.id', 'upload.documentId')
184
+ .select([
185
+ 'upload.id',
186
+ 'upload.documentId',
187
+ 'upload.kind',
188
+ 'document.workspaceId',
189
+ 'upload.baseRevisionId',
190
+ 'upload.objectKey',
191
+ 'upload.checksum',
192
+ 'upload.byteSize',
193
+ 'upload.contentType',
194
+ 'upload.multipartUploadId',
195
+ 'upload.status',
196
+ 'upload.expiresAt'
197
+ ])
198
+ }
199
+ type UploadRow = Awaited<ReturnType<ReturnType<typeof selectUploads>['executeTakeFirstOrThrow']>>
200
+
201
+ /**
202
+ * Claims a pending upload, completes it when it came in parts, checks the store holds the file
203
+ * it declared, and lands it with `land`. An upload that fails goes back to pending, so its
204
+ * commit can be tried again.
205
+ */
206
+ async function finalizeUpload<T>(
207
+ upload: UploadRow,
208
+ multipart: CommitUploadInput['multipart'],
209
+ land: (stored: StoredObject) => Promise<T>
210
+ ): Promise<T> {
211
+ if (upload.status !== 'pending' || new Date(upload.expiresAt).getTime() <= Date.now()) {
212
+ throw new UploadInvalidError()
213
+ }
214
+ const claimed = await database
215
+ .updateTable('upload')
216
+ .set({ status: 'finalizing', finalizationStartedAt: new Date() })
217
+ .where('id', '=', upload.id)
218
+ .where('status', '=', 'pending')
219
+ .returning('id')
220
+ .executeTakeFirst()
221
+ if (!claimed) throw new UploadInvalidError()
222
+ try {
223
+ if (multipart) {
224
+ if (
225
+ !objects.capabilities.multipartUpload ||
226
+ multipart.uploadId !== upload.multipartUploadId
227
+ ) {
228
+ throw new UploadInvalidError()
229
+ }
230
+ await objects.completeUpload({
231
+ key: upload.objectKey,
232
+ uploadId: multipart.uploadId,
233
+ parts: multipart.parts
234
+ })
235
+ }
236
+ const stored = await objects.head(upload.objectKey)
237
+ if (!stored || !matchesUpload(stored, upload)) throw new UploadInvalidError()
238
+ return await land(stored)
239
+ } catch (error) {
240
+ await database
241
+ .updateTable('upload')
242
+ .set({ status: 'pending', finalizationStartedAt: null })
243
+ .where('id', '=', upload.id)
244
+ .where('status', '=', 'finalizing')
245
+ .execute()
246
+ throw error
247
+ }
248
+ }
249
+
250
+ function landImage(upload: UploadRow, stored: StoredObject): Promise<void> {
251
+ return database.transaction().execute((transaction) =>
252
+ images.landInTransaction(transaction, {
253
+ uploadId: upload.id,
254
+ workspaceId: upload.workspaceId,
255
+ objectKey: upload.objectKey,
256
+ stored
257
+ })
258
+ )
259
+ }
260
+
261
+ async function commitUpload(
262
+ userId: string,
263
+ uploadId: string,
264
+ input: CommitUploadInput
265
+ ): Promise<DocumentSummary> {
266
+ const upload = await selectUploads()
267
+ .where('upload.id', '=', uploadId)
268
+ .where('upload.createdBy', '=', userId)
269
+ .executeTakeFirst()
270
+ if (!upload) throw new DocumentNotFoundError()
271
+ const access = await resolveDocumentAccess(database, userId, upload.documentId)
272
+ if (!access) throw new DocumentNotFoundError()
273
+ if (!canEditDocument(access)) throw new DocumentForbiddenError()
274
+ if (input.checksum !== upload.checksum) throw new UploadInvalidError()
275
+ if (upload.status !== 'committed') {
276
+ await refuseAtStorageLimit(upload.documentId)
277
+ const checkpointed =
278
+ upload.kind === 'image'
279
+ ? await finalizeUpload(upload, input.multipart, (stored) =>
280
+ landImage(upload, stored).then(() => false)
281
+ )
282
+ : await finalizeUpload(upload, input.multipart, (stored) =>
283
+ database.transaction().execute((transaction) =>
284
+ landRevision(transaction, {
285
+ userId,
286
+ uploadId,
287
+ upload,
288
+ stored,
289
+ revisionId: crypto.randomUUID(),
290
+ storageObjectId: crypto.randomUUID(),
291
+ images: input.images,
292
+ name: input.name,
293
+ kind: input.kind
294
+ })
295
+ )
296
+ )
297
+ if (checkpointed) announceVersions(options.onVersionsChanged, upload.documentId)
298
+ }
299
+ const document = await findDocument(database, userId, upload.documentId)
300
+ if (!document) throw new DocumentNotFoundError()
301
+ return document
302
+ }
303
+
304
+ return {
305
+ async createUpload(
306
+ userId: string,
307
+ documentId: string,
308
+ input: CreateUploadInput
309
+ ): Promise<CreateDocumentUploadResult> {
310
+ const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
311
+ database,
312
+ options,
313
+ userId,
314
+ documentId
315
+ )
316
+ if (document.currentRevisionId !== input.baseRevisionId) throw new DocumentConflictError()
317
+ if (input.byteSize > maximumFileBytes) throw new UploadInvalidError()
318
+ const uploadId = crypto.randomUUID()
319
+ const objectKey = `documents/${document.workspaceId}/${documentId}/uploads/${uploadId}.fig`
320
+ const expiresAt = new Date(Date.now() + UPLOAD_LIFETIME_MS)
321
+ await database.transaction().execute(async (transaction) => {
322
+ await transaction
323
+ .insertInto('upload')
324
+ .values({
325
+ id: uploadId,
326
+ documentId,
327
+ baseRevisionId: input.baseRevisionId,
328
+ objectKey,
329
+ checksum: input.checksum,
330
+ byteSize: input.byteSize,
331
+ contentType: input.contentType,
332
+ multipartUploadId: null,
333
+ status: 'pending',
334
+ finalizationStartedAt: null,
335
+ createdBy: userId,
336
+ expiresAt
337
+ })
338
+ .execute()
339
+ await quota.reserveInTransaction(transaction, {
340
+ workspaceId: document.workspaceId,
341
+ uploadId,
342
+ bytes: input.byteSize,
343
+ expiresAt,
344
+ maximumBytes: maximumStorageBytes
345
+ })
346
+ })
347
+ let objectUpload
348
+ try {
349
+ objectUpload = await objects.createUpload({
350
+ key: objectKey,
351
+ byteSize: input.byteSize,
352
+ checksum: input.checksum,
353
+ contentType: input.contentType,
354
+ expiresAt
355
+ })
356
+ } catch (error) {
357
+ await quota.release(uploadId)
358
+ await database
359
+ .updateTable('upload')
360
+ .set({ status: 'abandoned' })
361
+ .where('id', '=', uploadId)
362
+ .execute()
363
+ throw error
364
+ }
365
+ if (objectUpload.kind === 'multipart') {
366
+ await database
367
+ .updateTable('upload')
368
+ .set({ multipartUploadId: objectUpload.uploadId })
369
+ .where('id', '=', uploadId)
370
+ .execute()
371
+ }
372
+ return { id: uploadId, upload: objectUpload }
373
+ },
374
+
375
+ /** Uploads for the images a save of the document will name that its workspace lacks. */
376
+ async requestImageUploads(
377
+ userId: string,
378
+ documentId: string,
379
+ input: RequestImageUploadsInput
380
+ ): Promise<{ uploads: ImageUploadOffer[] }> {
381
+ const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
382
+ database,
383
+ options,
384
+ userId,
385
+ documentId
386
+ )
387
+ if (input.images.some((image) => image.byteSize > maximumFileBytes)) {
388
+ throw new UploadInvalidError()
389
+ }
390
+ return {
391
+ uploads: await images.requestUploads({
392
+ userId,
393
+ documentId,
394
+ workspaceId: document.workspaceId,
395
+ images: input.images,
396
+ expiresAt: new Date(Date.now() + UPLOAD_LIFETIME_MS),
397
+ maximumBytes: maximumStorageBytes
398
+ })
399
+ }
400
+ },
401
+ commitUpload,
402
+
403
+ /**
404
+ * Commits the image uploads a save asked for in one request, however many there are: each
405
+ * must be an image upload this person made for the document.
406
+ */
407
+ async commitImageUploads(
408
+ userId: string,
409
+ documentId: string,
410
+ uploads: CommitImageUploadsInput['uploads']
411
+ ): Promise<void> {
412
+ const access = await resolveDocumentAccess(database, userId, documentId)
413
+ if (!access) throw new DocumentNotFoundError()
414
+ if (!canEditDocument(access)) throw new DocumentForbiddenError()
415
+ const ids = uniq(uploads.map((upload) => upload.uploadId))
416
+ const rows = new Map(
417
+ (
418
+ await selectUploads()
419
+ .where('upload.id', 'in', ids)
420
+ .where('upload.documentId', '=', documentId)
421
+ .where('upload.createdBy', '=', userId)
422
+ .execute()
423
+ ).map((row) => [row.id, row])
424
+ )
425
+ if (rows.size !== ids.length || [...rows.values()].some((row) => row.kind !== 'image')) {
426
+ throw new UploadInvalidError()
427
+ }
428
+ for (const commit of uploads) {
429
+ if (rows.get(commit.uploadId)?.checksum !== commit.checksum) throw new UploadInvalidError()
430
+ }
431
+ if ([...rows.values()].some((row) => row.status !== 'committed')) {
432
+ await refuseAtStorageLimit(documentId)
433
+ }
434
+ // Each image is its own object, so a few are checked and landed at once, each once however
435
+ // often the request names it.
436
+ const land = limitAsync(
437
+ (upload: UploadRow, multipart: CommitUploadInput['multipart']) =>
438
+ finalizeUpload(upload, multipart, (stored) => landImage(upload, stored)),
439
+ IMAGE_COMMIT_CONCURRENCY
440
+ )
441
+ await Promise.all(
442
+ uniqBy(uploads, (commit) => commit.uploadId).map(async (commit) => {
443
+ const upload = rows.get(commit.uploadId)
444
+ if (!upload) throw new UploadInvalidError()
445
+ if (upload.status !== 'committed') await land(upload, commit.multipart)
446
+ })
447
+ )
448
+ }
449
+ }
450
+ }
@@ -8,7 +8,7 @@ import type {
8
8
  UpdateDocumentVersionInput
9
9
  } from '#cloud/contract'
10
10
  import { workspaceRoleCan } from '#cloud/server/auth/access-control'
11
- import type { CloudDatabase } from '#cloud/server/db'
11
+ import { userIdText, type CloudDatabase } from '#cloud/server/db'
12
12
  import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
13
13
  import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
14
14
  import { copyRevisionImages, revisionImageDownloads } from '#cloud/server/documents/images'
@@ -73,15 +73,7 @@ export function createDocumentVersionService(
73
73
  function versions() {
74
74
  return database
75
75
  .selectFrom('documentVersion')
76
- .leftJoin('user', (join) =>
77
- join.on((expression) =>
78
- expression(
79
- expression.cast(expression.ref('user.id'), 'text'),
80
- '=',
81
- expression.ref('documentVersion.createdBy')
82
- )
83
- )
84
- )
76
+ .leftJoin('user', (join) => join.onRef(userIdText, '=', 'documentVersion.createdBy'))
85
77
  .select([
86
78
  'documentVersion.id',
87
79
  'documentVersion.revisionId',
@@ -145,6 +137,41 @@ export function createDocumentVersionService(
145
137
  return stored
146
138
  }
147
139
 
140
+ /** A document's history for a caller that already let the reader in, and where it starts. */
141
+ async function sharedHistory(documentId: string) {
142
+ const document = await getDocumentSummaryRow(database, documentId)
143
+ if (!document) throw new DocumentNotFoundError()
144
+ const start = await historyStart(
145
+ database,
146
+ options.policy,
147
+ deploymentMode,
148
+ document.workspaceId,
149
+ new Date()
150
+ )
151
+ return { document, start }
152
+ }
153
+
154
+ /** A version's stored file, with where to download it and the images it names. */
155
+ async function downloadVersion(
156
+ document: Awaited<ReturnType<typeof sharedHistory>>['document'],
157
+ version: VersionRow
158
+ ): Promise<DocumentVersionDownload> {
159
+ const stored = await storedRevision(database, version.revisionId)
160
+ const expiresAt = new Date(Date.now() + DOWNLOAD_LIFETIME_MS)
161
+ return {
162
+ document: {
163
+ document: documentSummary(document),
164
+ revisionId: version.revisionId,
165
+ byteSize: Number(stored.byteSize),
166
+ checksum: stored.checksum,
167
+ contentType: stored.contentType,
168
+ download: await objects.createDownload({ key: stored.objectKey, expiresAt }),
169
+ images: await revisionImageDownloads(database, objects, version.revisionId, expiresAt)
170
+ },
171
+ version: versionOf(version, document.currentRevisionId)
172
+ }
173
+ }
174
+
148
175
  return {
149
176
  async list(
150
177
  userId: string,
@@ -242,21 +269,51 @@ export function createDocumentVersionService(
242
269
  versionId: string
243
270
  ): Promise<DocumentVersionDownload> {
244
271
  const { document, start } = await openHistory(userId, documentId, false)
245
- const version = await findVersion(documentId, versionId, start)
246
- const stored = await storedRevision(database, version.revisionId)
247
- const expiresAt = new Date(Date.now() + DOWNLOAD_LIFETIME_MS)
248
- return {
249
- document: {
250
- document: documentSummary(document),
251
- revisionId: version.revisionId,
252
- byteSize: Number(stored.byteSize),
253
- checksum: stored.checksum,
254
- contentType: stored.contentType,
255
- download: await objects.createDownload({ key: stored.objectKey, expiresAt }),
256
- images: await revisionImageDownloads(database, objects, version.revisionId, expiresAt)
257
- },
258
- version: versionOf(version, document.currentRevisionId)
272
+ return downloadVersion(document, await findVersion(documentId, versionId, start))
273
+ },
274
+
275
+ /**
276
+ * The named versions of several documents of one workspace, newest first, each list keyed by
277
+ * its document, for a caller that already let the reader in, such as the community gallery;
278
+ * the workspace's history limit still applies, and one query reads them all.
279
+ */
280
+ async namedSharedIn(
281
+ workspaceId: string,
282
+ documents: { id: string; currentRevisionId: string | null }[]
283
+ ): Promise<Map<string, DocumentVersion[]>> {
284
+ const named = new Map(documents.map((document) => [document.id, [] as DocumentVersion[]]))
285
+ if (documents.length === 0) return named
286
+ const start = await historyStart(
287
+ database,
288
+ options.policy,
289
+ deploymentMode,
290
+ workspaceId,
291
+ new Date()
292
+ )
293
+ let query = versions()
294
+ .select('documentVersion.documentId')
295
+ .where('documentVersion.documentId', 'in', [...named.keys()])
296
+ .where('documentVersion.kind', '=', 'named')
297
+ if (start) query = query.where('documentVersion.createdAt', '>=', start)
298
+ const rows = await query.orderBy('documentVersion.createdAt', 'desc').execute()
299
+ const current = new Map(
300
+ documents.map((document) => [document.id, document.currentRevisionId])
301
+ )
302
+ for (const row of rows) {
303
+ named.get(row.documentId)?.push(versionOf(row, current.get(row.documentId) ?? null))
259
304
  }
305
+ return named
306
+ },
307
+
308
+ /** A named version with where to download it, for a caller that already let the reader in. */
309
+ async downloadNamedShared(
310
+ documentId: string,
311
+ versionId: string
312
+ ): Promise<DocumentVersionDownload> {
313
+ const { document, start } = await sharedHistory(documentId)
314
+ const version = await findVersion(documentId, versionId, start)
315
+ if (version.kind !== 'named') throw new DocumentNotFoundError()
316
+ return downloadVersion(document, version)
260
317
  },
261
318
 
262
319
  /**
@@ -15,6 +15,7 @@ export function createInvitationOutbox(email: TransactionalEmailService): Invita
15
15
  idempotencyKey: `document-invitation/${message.deliveryId}`,
16
16
  kind: 'document-invitation',
17
17
  recipientEmail: message.recipientEmail,
18
+ fallbackLocale: message.locale,
18
19
  payload: {
19
20
  inviterName: message.inviterName,
20
21
  documentName: message.documentName,
@@ -1,5 +1,5 @@
1
1
  import { renderTransactionalEmail } from '#cloud/email'
2
- import type { TransactionalEmailKind } from '#cloud/email'
2
+ import type { EmailLocale, TransactionalEmailKind } from '#cloud/email'
3
3
  import type { CloudDatabase, TransactionalEmailStatus } from '#cloud/server/db'
4
4
  import type { Kysely, Transaction } from 'kysely'
5
5
 
@@ -40,6 +40,7 @@ type ClaimedTransactionalEmail = {
40
40
  kind: TransactionalEmailKind
41
41
  recipientEmailNormalized: string
42
42
  payloadEncrypted: string
43
+ locale: EmailLocale
43
44
  attemptCount: number
44
45
  }
45
46
 
@@ -93,14 +94,33 @@ export function createTransactionalEmailService(
93
94
  input.payload
94
95
  )
95
96
  const executor = transaction ?? database
97
+ const recipientEmailNormalized = normalizedEmail(input.recipientEmail)
98
+ // An account is written to in the language it chose, whoever sends to it.
99
+ const recipientLocale =
100
+ input.recipientLocale !== undefined
101
+ ? input.recipientLocale
102
+ : (
103
+ await executor
104
+ .selectFrom('user')
105
+ .select('locale')
106
+ .where((expression) =>
107
+ expression(
108
+ expression.fn('lower', [expression.ref('email')]),
109
+ '=',
110
+ recipientEmailNormalized
111
+ )
112
+ )
113
+ .executeTakeFirst()
114
+ )?.locale
96
115
  const row = await executor
97
116
  .insertInto('transactionalEmail')
98
117
  .values({
99
118
  id,
100
119
  idempotencyKey: input.idempotencyKey,
101
120
  kind: input.kind,
102
- recipientEmailNormalized: normalizedEmail(input.recipientEmail),
121
+ recipientEmailNormalized,
103
122
  payloadEncrypted,
123
+ locale: recipientLocale ?? input.fallbackLocale ?? 'en',
104
124
  status: 'pending',
105
125
  nextAttemptAt: new Date(),
106
126
  claimId: null,
@@ -136,7 +156,14 @@ export function createTransactionalEmailService(
136
156
  const claimed = await database.transaction().execute(async (transaction) => {
137
157
  const candidates = await transaction
138
158
  .selectFrom('transactionalEmail')
139
- .select(['id', 'kind', 'recipientEmailNormalized', 'payloadEncrypted', 'attemptCount'])
159
+ .select([
160
+ 'id',
161
+ 'kind',
162
+ 'recipientEmailNormalized',
163
+ 'payloadEncrypted',
164
+ 'locale',
165
+ 'attemptCount'
166
+ ])
140
167
  .where('payloadEncrypted', 'is not', null)
141
168
  .where((expression) =>
142
169
  expression.or([
@@ -186,7 +213,7 @@ export function createTransactionalEmailService(
186
213
  message.kind,
187
214
  message.payloadEncrypted
188
215
  )
189
- const rendered = await renderTransactionalEmail(content)
216
+ const rendered = await renderTransactionalEmail(content, message.locale)
190
217
  const sent = await options.transport.send({
191
218
  deliveryId: message.id,
192
219
  from: options.from,
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ EmailLocale,
2
3
  RenderedTransactionalEmail,
3
4
  TransactionalEmailKind,
4
5
  TransactionalEmailPayloadByKind
@@ -28,6 +29,17 @@ export type EnqueueTransactionalEmailInput<Kind extends TransactionalEmailKind>
28
29
  kind: Kind
29
30
  recipientEmail: string
30
31
  payload: TransactionalEmailPayloadByKind[Kind]
32
+ /**
33
+ * The recipient account's own language, when the sender has the account: the email is then
34
+ * written in it, or in English when it has none, without looking the account up. Signing up
35
+ * needs this, since its callbacks run inside its transaction and must not query the database.
36
+ */
37
+ recipientLocale?: EmailLocale | null
38
+ /**
39
+ * The language to write in when the recipient has no account that chose one: the inviter's for
40
+ * an invitation. English otherwise.
41
+ */
42
+ fallbackLocale?: EmailLocale | null
31
43
  }
32
44
 
33
45
  export type TransactionalEmailDeliveryOptions = {