@open-pencil/cloud 0.17.0-beta.2 → 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.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- 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 +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -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 +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- 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 +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.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 +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- 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 +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- 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/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- 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/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- 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/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -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 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -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 +35 -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/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- 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/app.ts +0 -379
- 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/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -3,6 +3,7 @@ import type { ObjectStore } from '#cloud/server/objects'
|
|
|
3
3
|
import {
|
|
4
4
|
AbortMultipartUploadCommand,
|
|
5
5
|
CompleteMultipartUploadCommand,
|
|
6
|
+
CopyObjectCommand,
|
|
6
7
|
CreateMultipartUploadCommand,
|
|
7
8
|
DeleteObjectCommand,
|
|
8
9
|
GetObjectCommand,
|
|
@@ -11,7 +12,7 @@ import {
|
|
|
11
12
|
S3Client,
|
|
12
13
|
UploadPartCommand
|
|
13
14
|
} from '@aws-sdk/client-s3'
|
|
14
|
-
// eslint-disable-next-line open-pencil/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
15
|
+
// eslint-disable-next-line @open-pencil/structure/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
15
16
|
import { getSignedUrl as getSignedURL } from '@aws-sdk/s3-request-presigner'
|
|
16
17
|
|
|
17
18
|
const CHECKSUM_METADATA_KEY = 'openpencil-sha256'
|
|
@@ -70,6 +71,27 @@ export function createS3ObjectStore(config: CloudServerConfig): ObjectStore {
|
|
|
70
71
|
)
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Copies a file under the deleted prefix before it goes, so it outlives the delete until the
|
|
76
|
+
* bucket's lifecycle rule expires it. A file already gone, as when a cleanup pass retries, has
|
|
77
|
+
* nothing to keep.
|
|
78
|
+
*/
|
|
79
|
+
async function keepDeleted(key: string, prefix: string): Promise<void> {
|
|
80
|
+
try {
|
|
81
|
+
await client.send(
|
|
82
|
+
new CopyObjectCommand({
|
|
83
|
+
Bucket: config.s3Bucket,
|
|
84
|
+
Key: `${prefix}${key}`,
|
|
85
|
+
CopySource: `${config.s3Bucket}/${key.split('/').map(encodeURIComponent).join('/')}`,
|
|
86
|
+
MetadataDirective: 'COPY',
|
|
87
|
+
...encryption(config)
|
|
88
|
+
})
|
|
89
|
+
)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (!isNotFoundError(error)) throw error
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
73
95
|
return {
|
|
74
96
|
capabilities: {
|
|
75
97
|
nativeSHA256,
|
|
@@ -238,6 +260,7 @@ export function createS3ObjectStore(config: CloudServerConfig): ObjectStore {
|
|
|
238
260
|
},
|
|
239
261
|
|
|
240
262
|
async delete(key) {
|
|
263
|
+
if (config.s3DeletedPrefix) await keepDeleted(key, config.s3DeletedPrefix)
|
|
241
264
|
await client.send(new DeleteObjectCommand({ Bucket: config.s3Bucket, Key: key }))
|
|
242
265
|
},
|
|
243
266
|
|
|
@@ -265,6 +288,23 @@ export function createS3ObjectStore(config: CloudServerConfig): ObjectStore {
|
|
|
265
288
|
if (isNotFoundError(error)) return null
|
|
266
289
|
throw error
|
|
267
290
|
}
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
async readRange(key, start, endExclusive) {
|
|
294
|
+
if (endExclusive <= start) return new Uint8Array()
|
|
295
|
+
try {
|
|
296
|
+
const object = await client.send(
|
|
297
|
+
new GetObjectCommand({
|
|
298
|
+
Bucket: config.s3Bucket,
|
|
299
|
+
Key: key,
|
|
300
|
+
Range: `bytes=${start}-${endExclusive - 1}`
|
|
301
|
+
})
|
|
302
|
+
)
|
|
303
|
+
return object.Body ? await object.Body.transformToByteArray() : null
|
|
304
|
+
} catch (error) {
|
|
305
|
+
if (isNotFoundError(error)) return null
|
|
306
|
+
throw error
|
|
307
|
+
}
|
|
268
308
|
}
|
|
269
309
|
}
|
|
270
310
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CLOUD_COMMUNITY_PATH,
|
|
3
|
+
cloudCommunityPagePath,
|
|
4
|
+
communityDocumentsSchema,
|
|
5
|
+
type CommunityDocument
|
|
6
|
+
} from '#cloud/contract'
|
|
7
|
+
// eslint-disable-next-line @open-pencil/structure/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
8
|
+
import { createHead, transformHtmlTemplate as transformHTMLTemplate } from 'unhead/server'
|
|
9
|
+
import * as v from 'valibot'
|
|
10
|
+
|
|
11
|
+
/** What a community page says about itself to link previews and search engines. */
|
|
12
|
+
export type PageMeta = {
|
|
13
|
+
title: string
|
|
14
|
+
description: string
|
|
15
|
+
url: string
|
|
16
|
+
image?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type API = { fetch(request: Request): Response | Promise<Response> }
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Why a community page shows no tags: there is no gallery or no such document (404), or the API
|
|
23
|
+
* could not answer, such as when it limited the visitor, whose status and retry advice the page
|
|
24
|
+
* keeps.
|
|
25
|
+
*/
|
|
26
|
+
export type CommunityPageFailure = { status: number; retryAfter: string | null }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The gallery's documents, read through the server's own API as the visitor asked for the page,
|
|
30
|
+
* so the request counts against the visitor's limits, or why they could not be read.
|
|
31
|
+
*/
|
|
32
|
+
async function communityDocuments(
|
|
33
|
+
api: API,
|
|
34
|
+
page: Request
|
|
35
|
+
): Promise<CommunityDocument[] | CommunityPageFailure> {
|
|
36
|
+
const url = new URL(`/api${CLOUD_COMMUNITY_PATH}`, page.url)
|
|
37
|
+
const response = await api.fetch(new Request(url, { headers: page.headers }))
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
return { status: response.status, retryAfter: response.headers.get('Retry-After') }
|
|
40
|
+
}
|
|
41
|
+
const body = v.safeParse(communityDocumentsSchema, await response.json())
|
|
42
|
+
return body.success ? body.output.documents : { status: 502, retryAfter: null }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const NOT_FOUND: CommunityPageFailure = { status: 404, retryAfter: null }
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The tags for the gallery, or for one of its documents when `documentId` names one, or why the
|
|
49
|
+
* page has none: no gallery or no such document, or an API that could not answer.
|
|
50
|
+
*/
|
|
51
|
+
export async function communityPageMeta(
|
|
52
|
+
api: API,
|
|
53
|
+
page: Request,
|
|
54
|
+
documentId: string | undefined
|
|
55
|
+
): Promise<PageMeta | CommunityPageFailure> {
|
|
56
|
+
const documents = await communityDocuments(api, page)
|
|
57
|
+
if (!Array.isArray(documents)) return documents
|
|
58
|
+
const { origin, host } = new URL(page.url)
|
|
59
|
+
if (documentId === undefined) {
|
|
60
|
+
const first = documents.at(0)
|
|
61
|
+
return {
|
|
62
|
+
title: `Community · ${host}`,
|
|
63
|
+
description: 'Files to open, explore, and copy into your own workspace.',
|
|
64
|
+
url: new URL(CLOUD_COMMUNITY_PATH, origin).href,
|
|
65
|
+
image: first ? new URL(first.thumbnailPath, origin).href : undefined
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const document = documents.find((entry) => entry.id === documentId)
|
|
69
|
+
if (!document) return NOT_FOUND
|
|
70
|
+
return {
|
|
71
|
+
title: `${document.name} · Community · ${host}`,
|
|
72
|
+
description:
|
|
73
|
+
document.versions.at(0)?.description ??
|
|
74
|
+
`${document.name}, a file to open in OpenPencil and copy into your own workspace.`,
|
|
75
|
+
url: new URL(cloudCommunityPagePath(document.id), origin).href,
|
|
76
|
+
image: new URL(document.thumbnailPath, origin).href
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The page with the gallery's title, description, preview, and canonical address in its head. */
|
|
81
|
+
export async function withPageMeta(html: string, meta: PageMeta): Promise<string> {
|
|
82
|
+
const head = createHead()
|
|
83
|
+
head.push({
|
|
84
|
+
title: meta.title,
|
|
85
|
+
meta: [
|
|
86
|
+
{ name: 'description', content: meta.description },
|
|
87
|
+
{ property: 'og:type', content: 'website' },
|
|
88
|
+
{ property: 'og:title', content: meta.title },
|
|
89
|
+
{ property: 'og:description', content: meta.description },
|
|
90
|
+
{ property: 'og:url', content: meta.url },
|
|
91
|
+
...(meta.image
|
|
92
|
+
? [
|
|
93
|
+
{ property: 'og:image', content: meta.image },
|
|
94
|
+
{ name: 'twitter:card', content: 'summary_large_image' }
|
|
95
|
+
]
|
|
96
|
+
: [])
|
|
97
|
+
],
|
|
98
|
+
link: [{ rel: 'canonical', href: meta.url }]
|
|
99
|
+
})
|
|
100
|
+
return transformHTMLTemplate(head, html)
|
|
101
|
+
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { CLOUD_PORTAL_PAGE_ROOTS } from '#cloud/contract'
|
|
2
|
-
import {
|
|
1
|
+
import { CLOUD_PORTAL_COMMUNITY_PAGES, CLOUD_PORTAL_PAGE_ROOTS } from '#cloud/contract'
|
|
2
|
+
import {
|
|
3
|
+
cloudErrorHandler,
|
|
4
|
+
cloudRequestId,
|
|
5
|
+
withIndexingPolicy,
|
|
6
|
+
type CloudErrorReporter,
|
|
7
|
+
type ForwardedRequestId
|
|
8
|
+
} from '#cloud/server'
|
|
3
9
|
import { Hono } from 'hono'
|
|
4
10
|
|
|
11
|
+
import { communityPageMeta, withPageMeta } from './community'
|
|
12
|
+
|
|
5
13
|
/** A file of the Cloud site (`bun run build:cloud`): the editor and the portal. */
|
|
6
14
|
export type SiteFile = {
|
|
7
15
|
body: ReadableStream | Uint8Array<ArrayBuffer>
|
|
@@ -16,11 +24,28 @@ export type SiteFiles = (path: string, request: Request) => Promise<SiteFile | n
|
|
|
16
24
|
const PORTAL_PAGE = '/cloud.html'
|
|
17
25
|
const EDITOR_PAGE = '/index.html'
|
|
18
26
|
|
|
27
|
+
/**
|
|
28
|
+
* What the site's pages may run: only the site's own scripts and Turnstile's on the portal, so an
|
|
29
|
+
* injected script tag or one from another host never runs. The pages carry no inline script (the
|
|
30
|
+
* boot splash loads its own file). The editor compiles design JSX and code-tool snippets with
|
|
31
|
+
* `Function` and runs WebAssembly and workers from blobs, which the policy keeps; plugins and AI
|
|
32
|
+
* providers reach other hosts through `fetch`, which it leaves be. Desktop sign-in approval and
|
|
33
|
+
* the editor must never be framed by another site.
|
|
34
|
+
*/
|
|
35
|
+
export const SITE_CONTENT_SECURITY_POLICY = [
|
|
36
|
+
"script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval' blob: https://challenges.cloudflare.com",
|
|
37
|
+
"worker-src 'self' blob:",
|
|
38
|
+
"object-src 'none'",
|
|
39
|
+
"base-uri 'self'",
|
|
40
|
+
"frame-ancestors 'none'"
|
|
41
|
+
].join('; ')
|
|
42
|
+
|
|
19
43
|
const PAGE_HEADERS = {
|
|
20
44
|
'Content-Type': 'text/html; charset=utf-8',
|
|
21
45
|
'Cache-Control': 'no-cache',
|
|
22
|
-
|
|
23
|
-
|
|
46
|
+
'Content-Security-Policy': SITE_CONTENT_SECURITY_POLICY,
|
|
47
|
+
// Browsers take this only over HTTPS; a deployment on plain HTTP is unaffected.
|
|
48
|
+
'Strict-Transport-Security': 'max-age=31536000',
|
|
24
49
|
'Referrer-Policy': 'no-referrer',
|
|
25
50
|
'X-Content-Type-Options': 'nosniff'
|
|
26
51
|
}
|
|
@@ -31,6 +56,8 @@ export type CloudServerOptions = {
|
|
|
31
56
|
indexingPolicy: 'allow' | 'deny'
|
|
32
57
|
/** Reports errors reading the site's files, when the deployment configures error tracking. */
|
|
33
58
|
reportError?: CloudErrorReporter
|
|
59
|
+
/** The request ID the runtime trusts from what stands in front of it; see `cloudRequestId`. */
|
|
60
|
+
forwardedRequestId?: ForwardedRequestId
|
|
34
61
|
}
|
|
35
62
|
|
|
36
63
|
function cacheControl(path: string): string {
|
|
@@ -45,9 +72,11 @@ function cacheControl(path: string): string {
|
|
|
45
72
|
*/
|
|
46
73
|
export function createCloudServer(
|
|
47
74
|
api: { fetch(request: Request): Response | Promise<Response> },
|
|
48
|
-
{ site, indexingPolicy, reportError }: CloudServerOptions
|
|
75
|
+
{ site, indexingPolicy, reportError, forwardedRequestId }: CloudServerOptions
|
|
49
76
|
) {
|
|
50
|
-
const server = new Hono()
|
|
77
|
+
const server = new Hono()
|
|
78
|
+
.onError(cloudErrorHandler(reportError))
|
|
79
|
+
.use(cloudRequestId(forwardedRequestId))
|
|
51
80
|
server.use(async (c, next) => {
|
|
52
81
|
await next()
|
|
53
82
|
c.res = withIndexingPolicy(c.res, c.req.path, indexingPolicy)
|
|
@@ -58,6 +87,20 @@ export function createCloudServer(
|
|
|
58
87
|
const page = await site(PORTAL_PAGE, c.req.raw)
|
|
59
88
|
return page ? c.body(page.body, 200, PAGE_HEADERS) : c.notFound()
|
|
60
89
|
})
|
|
90
|
+
// The gallery's pages say what they show to link previews and search engines, which read the
|
|
91
|
+
// page as served rather than as its script draws it.
|
|
92
|
+
server.on(['GET', 'HEAD'], [...CLOUD_PORTAL_COMMUNITY_PAGES], async (c) => {
|
|
93
|
+
const page = await site(PORTAL_PAGE, c.req.raw)
|
|
94
|
+
if (!page) return c.notFound()
|
|
95
|
+
const meta = await communityPageMeta(api, c.req.raw, c.req.param('documentId'))
|
|
96
|
+
const html = await new Response(page.body).text()
|
|
97
|
+
if ('title' in meta) return c.body(await withPageMeta(html, meta), 200, PAGE_HEADERS)
|
|
98
|
+
// The page still loads and says what went wrong; the status and retry advice stay the API's.
|
|
99
|
+
const headers = meta.retryAfter
|
|
100
|
+
? { ...PAGE_HEADERS, 'Retry-After': meta.retryAfter }
|
|
101
|
+
: PAGE_HEADERS
|
|
102
|
+
return new Response(html, { status: meta.status, headers })
|
|
103
|
+
})
|
|
61
104
|
server.on(['GET', 'HEAD'], '*', async (c, next) => {
|
|
62
105
|
await next()
|
|
63
106
|
if (c.res.status !== 404) return
|
|
@@ -9,6 +9,11 @@ const ACCOUNT_SECURITY_ERROR_CODES: Record<string, AccountSecurityErrorCode> = {
|
|
|
9
9
|
PASSWORD_TOO_LONG: 'password_too_long',
|
|
10
10
|
FAILED_TO_UNLINK_LAST_ACCOUNT: 'last_authentication_method',
|
|
11
11
|
SESSION_NOT_FRESH: 'session_not_fresh',
|
|
12
|
+
// Better Auth's own word for a session too old to delete the account without its password.
|
|
13
|
+
SESSION_EXPIRED: 'session_not_fresh',
|
|
14
|
+
CREDENTIAL_ACCOUNT_NOT_FOUND: 'session_not_fresh',
|
|
15
|
+
ACCOUNT_SOLE_ADMIN: 'account_sole_admin',
|
|
16
|
+
LAST_ADMIN_REQUIRED: 'last_admin_required',
|
|
12
17
|
CHANGE_EMAIL_DISABLED: 'email_change_unavailable',
|
|
13
18
|
CURRENT_SESSION: 'current_session',
|
|
14
19
|
EMAIL_UNCHANGED: 'email_unchanged'
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { normalizeEnrollmentEmail } from '#cloud/admin/enrollment/service'
|
|
2
|
+
import type { CloudAccountDeletion } from '#cloud/contract'
|
|
3
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
+
import { markPurged } from '#cloud/server/documents/purge'
|
|
5
|
+
import { handOverOwnership } from '#cloud/server/workspaces'
|
|
6
|
+
import { APIError } from 'better-auth'
|
|
7
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
8
|
+
|
|
9
|
+
/** Workspaces the account alone administers while others belong to them, which it must hand on. */
|
|
10
|
+
export class AccountDeletionBlockedError extends Error {
|
|
11
|
+
constructor(readonly workspaces: CloudAccountDeletion['blockedBy']) {
|
|
12
|
+
super('The account is the only administrator of workspaces with other members')
|
|
13
|
+
this.name = 'AccountDeletionBlockedError'
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The account is the deployment's last administrator, so deleting it would lock the console. */
|
|
18
|
+
export class AccountLastAdministratorError extends Error {
|
|
19
|
+
constructor() {
|
|
20
|
+
super('Cannot delete the last deployment administrator')
|
|
21
|
+
this.name = 'AccountLastAdministratorError'
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether the account is an administrator of the deployment with no other active one, the
|
|
27
|
+
* check the console makes before banning or demoting one (`src/admin/users/service.ts`).
|
|
28
|
+
* Inside a transaction it locks the administrators' rows, so two of them can't both go at once.
|
|
29
|
+
*/
|
|
30
|
+
async function lastAdministrator(
|
|
31
|
+
database: Kysely<CloudDatabase> | Transaction<CloudDatabase>,
|
|
32
|
+
userId: string,
|
|
33
|
+
lock: boolean
|
|
34
|
+
): Promise<boolean> {
|
|
35
|
+
let query = database
|
|
36
|
+
.selectFrom('user')
|
|
37
|
+
.select(['id', 'banned'])
|
|
38
|
+
.where('role', '=', 'admin')
|
|
39
|
+
.orderBy('id')
|
|
40
|
+
if (lock) query = query.forUpdate()
|
|
41
|
+
const administrators = await query.execute()
|
|
42
|
+
if (!administrators.some((row) => row.id === userId)) return false
|
|
43
|
+
return !administrators.some((row) => row.id !== userId && row.banned !== true)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type Membership = {
|
|
47
|
+
workspaceId: string
|
|
48
|
+
name: string
|
|
49
|
+
role: string
|
|
50
|
+
members: number
|
|
51
|
+
admins: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What happens to each workspace the account belongs to when it goes. */
|
|
55
|
+
function classify(memberships: Membership[]) {
|
|
56
|
+
const deletes: Membership[] = []
|
|
57
|
+
const blockedBy: Membership[] = []
|
|
58
|
+
const leaves: Membership[] = []
|
|
59
|
+
for (const membership of memberships) {
|
|
60
|
+
if (membership.members === 1) deletes.push(membership)
|
|
61
|
+
else if (membership.role === 'admin' && membership.admins === 1) blockedBy.push(membership)
|
|
62
|
+
else leaves.push(membership)
|
|
63
|
+
}
|
|
64
|
+
return { deletes, blockedBy, leaves }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function memberships(
|
|
68
|
+
database: Kysely<CloudDatabase> | Transaction<CloudDatabase>,
|
|
69
|
+
userId: string
|
|
70
|
+
): Promise<Membership[]> {
|
|
71
|
+
return database
|
|
72
|
+
.selectFrom('workspaceMember as own')
|
|
73
|
+
.innerJoin('workspace', 'workspace.id', 'own.workspaceId')
|
|
74
|
+
.innerJoin('workspaceMember as everyone', 'everyone.workspaceId', 'own.workspaceId')
|
|
75
|
+
.select((expression) => [
|
|
76
|
+
'own.workspaceId',
|
|
77
|
+
'workspace.name',
|
|
78
|
+
'own.role',
|
|
79
|
+
expression.fn.count<number>('everyone.userId').as('members'),
|
|
80
|
+
expression.fn
|
|
81
|
+
.count<number>('everyone.userId')
|
|
82
|
+
.filterWhere('everyone.role', '=', 'admin')
|
|
83
|
+
.as('admins')
|
|
84
|
+
])
|
|
85
|
+
.where('own.userId', '=', userId)
|
|
86
|
+
.groupBy(['own.workspaceId', 'workspace.name', 'own.role'])
|
|
87
|
+
.orderBy('workspace.name')
|
|
88
|
+
.orderBy('own.workspaceId')
|
|
89
|
+
.execute()
|
|
90
|
+
.then((rows) =>
|
|
91
|
+
rows.map((row) => ({ ...row, members: Number(row.members), admins: Number(row.admins) }))
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const workspaceSummary = (membership: Membership) => ({
|
|
96
|
+
id: membership.workspaceId,
|
|
97
|
+
name: membership.name
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Deleting an account: what it takes, and the Cloud side of taking it. Better Auth removes the
|
|
102
|
+
* user with their sessions, sign-in methods, passkeys, and two-step secrets; this removes what
|
|
103
|
+
* Cloud keeps about them. Workspaces only they belong to go with their documents, deleted
|
|
104
|
+
* forever, which cleanup destroys once the deployment's grace passes; in workspaces others keep
|
|
105
|
+
* they drop out, handing ownership, and so the plan the workspace draws on, to the
|
|
106
|
+
* longest-standing other admin.
|
|
107
|
+
*/
|
|
108
|
+
export function createAccountDeletionService(database: Kysely<CloudDatabase>) {
|
|
109
|
+
return {
|
|
110
|
+
async plan(userId: string): Promise<CloudAccountDeletion> {
|
|
111
|
+
const { deletes, blockedBy, leaves } = classify(await memberships(database, userId))
|
|
112
|
+
const deleted = deletes.map((membership) => membership.workspaceId)
|
|
113
|
+
const left = leaves.map((membership) => membership.workspaceId)
|
|
114
|
+
const blocked = blockedBy.map((membership) => membership.workspaceId)
|
|
115
|
+
// What the account can still download: everything in workspaces that go with it, and what
|
|
116
|
+
// it created in workspaces it leaves, keeps an admin role in, or reaches only by a grant.
|
|
117
|
+
const documents = await database
|
|
118
|
+
.selectFrom('document')
|
|
119
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
120
|
+
.select(['document.id', 'document.name', 'workspace.name as workspaceName'])
|
|
121
|
+
.where('document.deletedAt', 'is', null)
|
|
122
|
+
.where('document.disabledAt', 'is', null)
|
|
123
|
+
.where((expression) =>
|
|
124
|
+
expression.or([
|
|
125
|
+
...(deleted.length > 0 ? [expression('document.workspaceId', 'in', deleted)] : []),
|
|
126
|
+
expression.and([
|
|
127
|
+
expression('document.createdBy', '=', userId),
|
|
128
|
+
expression.or([
|
|
129
|
+
...(left.length > 0 ? [expression('document.workspaceId', 'in', left)] : []),
|
|
130
|
+
...(blocked.length > 0 ? [expression('document.workspaceId', 'in', blocked)] : []),
|
|
131
|
+
expression(
|
|
132
|
+
'document.id',
|
|
133
|
+
'in',
|
|
134
|
+
expression
|
|
135
|
+
.selectFrom('documentGrant')
|
|
136
|
+
.select('documentGrant.documentId')
|
|
137
|
+
.where('documentGrant.userId', '=', userId)
|
|
138
|
+
.where('documentGrant.revokedAt', 'is', null)
|
|
139
|
+
)
|
|
140
|
+
])
|
|
141
|
+
])
|
|
142
|
+
])
|
|
143
|
+
)
|
|
144
|
+
.orderBy('workspace.name')
|
|
145
|
+
.orderBy('document.name')
|
|
146
|
+
.orderBy('document.id')
|
|
147
|
+
.execute()
|
|
148
|
+
return {
|
|
149
|
+
lastAdministrator: await lastAdministrator(database, userId, false),
|
|
150
|
+
blockedBy: blockedBy.map(workspaceSummary),
|
|
151
|
+
deletes: deletes.map(workspaceSummary),
|
|
152
|
+
leaves: leaves.map(workspaceSummary),
|
|
153
|
+
documents
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Removes everything Cloud keeps about the account, in one transaction, or refuses with
|
|
159
|
+
* `AccountDeletionBlockedError` and changes nothing. Its workspaces are locked first, so nobody
|
|
160
|
+
* joins one, changes a role in one, or adds a document to one while it decides.
|
|
161
|
+
*/
|
|
162
|
+
async remove(user: { id: string; email: string }): Promise<void> {
|
|
163
|
+
await database.transaction().execute(async (transaction) => {
|
|
164
|
+
if (await lastAdministrator(transaction, user.id, true)) {
|
|
165
|
+
throw new AccountLastAdministratorError()
|
|
166
|
+
}
|
|
167
|
+
const workspaceIds = await transaction
|
|
168
|
+
.selectFrom('workspaceMember')
|
|
169
|
+
.select('workspaceId')
|
|
170
|
+
.where('userId', '=', user.id)
|
|
171
|
+
.orderBy('workspaceId')
|
|
172
|
+
.execute()
|
|
173
|
+
for (const { workspaceId } of workspaceIds) {
|
|
174
|
+
await transaction
|
|
175
|
+
.selectFrom('workspace')
|
|
176
|
+
.select('id')
|
|
177
|
+
.where('id', '=', workspaceId)
|
|
178
|
+
.forUpdate()
|
|
179
|
+
.execute()
|
|
180
|
+
}
|
|
181
|
+
const { deletes, blockedBy, leaves } = classify(await memberships(transaction, user.id))
|
|
182
|
+
if (blockedBy.length > 0) {
|
|
183
|
+
throw new AccountDeletionBlockedError(blockedBy.map(workspaceSummary))
|
|
184
|
+
}
|
|
185
|
+
const now = new Date()
|
|
186
|
+
for (const { workspaceId } of deletes) {
|
|
187
|
+
await transaction
|
|
188
|
+
.updateTable('document')
|
|
189
|
+
.set({ deletedAt: now })
|
|
190
|
+
.where('workspaceId', '=', workspaceId)
|
|
191
|
+
.where('deletedAt', 'is', null)
|
|
192
|
+
.execute()
|
|
193
|
+
// Nobody is left to restore them, so they are deleted forever, Trash included: out of
|
|
194
|
+
// sight now, and destroyed by cleanup once the deployment's grace passes.
|
|
195
|
+
const documents = await transaction
|
|
196
|
+
.selectFrom('document')
|
|
197
|
+
.select('id')
|
|
198
|
+
.where('workspaceId', '=', workspaceId)
|
|
199
|
+
.where('purgedAt', 'is', null)
|
|
200
|
+
.execute()
|
|
201
|
+
await markPurged(
|
|
202
|
+
transaction,
|
|
203
|
+
documents.map((document) => document.id),
|
|
204
|
+
null,
|
|
205
|
+
now
|
|
206
|
+
)
|
|
207
|
+
await transaction
|
|
208
|
+
.updateTable('workspaceInvitation')
|
|
209
|
+
.set({ revokedAt: now })
|
|
210
|
+
.where('workspaceId', '=', workspaceId)
|
|
211
|
+
.where('acceptedAt', 'is', null)
|
|
212
|
+
.where('revokedAt', 'is', null)
|
|
213
|
+
.execute()
|
|
214
|
+
await transaction
|
|
215
|
+
.deleteFrom('workspaceMember')
|
|
216
|
+
.where('workspaceId', '=', workspaceId)
|
|
217
|
+
.where('userId', '=', user.id)
|
|
218
|
+
.execute()
|
|
219
|
+
// One with documents stays, member-less, until cleanup purges the last of them.
|
|
220
|
+
await transaction
|
|
221
|
+
.deleteFrom('workspace')
|
|
222
|
+
.where('id', '=', workspaceId)
|
|
223
|
+
.where((expression) =>
|
|
224
|
+
expression.not(
|
|
225
|
+
expression.exists(
|
|
226
|
+
expression
|
|
227
|
+
.selectFrom('document')
|
|
228
|
+
.select('document.id')
|
|
229
|
+
.whereRef('document.workspaceId', '=', 'workspace.id')
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
.execute()
|
|
234
|
+
}
|
|
235
|
+
for (const { workspaceId } of leaves) {
|
|
236
|
+
await transaction
|
|
237
|
+
.deleteFrom('workspaceMember')
|
|
238
|
+
.where('workspaceId', '=', workspaceId)
|
|
239
|
+
.where('userId', '=', user.id)
|
|
240
|
+
.execute()
|
|
241
|
+
await handOverOwnership(transaction, workspaceId, user.id)
|
|
242
|
+
}
|
|
243
|
+
await transaction.deleteFrom('documentGrant').where('userId', '=', user.id).execute()
|
|
244
|
+
for (const table of ['documentInvitation', 'workspaceInvitation'] as const) {
|
|
245
|
+
await transaction
|
|
246
|
+
.updateTable(table)
|
|
247
|
+
.set({ revokedAt: now })
|
|
248
|
+
.where('invitedBy', '=', user.id)
|
|
249
|
+
.where('acceptedAt', 'is', null)
|
|
250
|
+
.where('revokedAt', 'is', null)
|
|
251
|
+
.execute()
|
|
252
|
+
}
|
|
253
|
+
await transaction.deleteFrom('invitationSend').where('senderId', '=', user.id).execute()
|
|
254
|
+
for (const table of [
|
|
255
|
+
'accountEntitlement',
|
|
256
|
+
'termsAcceptance',
|
|
257
|
+
'userPicture',
|
|
258
|
+
'signIn',
|
|
259
|
+
'mfaAssurance'
|
|
260
|
+
] as const) {
|
|
261
|
+
await transaction.deleteFrom(table).where('userId', '=', user.id).execute()
|
|
262
|
+
}
|
|
263
|
+
// Personal access tokens have no key to cascade from, since Cloud builds their table.
|
|
264
|
+
await transaction.deleteFrom('apikey').where('referenceId', '=', user.id).execute()
|
|
265
|
+
await transaction
|
|
266
|
+
.deleteFrom('enrollment')
|
|
267
|
+
.where('emailNormalized', '=', normalizeEnrollmentEmail(user.email))
|
|
268
|
+
.execute()
|
|
269
|
+
// The user row goes in the same transaction, with the sessions, sign-in methods, passkeys
|
|
270
|
+
// and two-step secrets its keys cascade to, so a failure leaves the account whole and
|
|
271
|
+
// signed in to try again. Better Auth's own deletes that follow then find nothing.
|
|
272
|
+
await transaction.deleteFrom('user').where('id', '=', user.id).execute()
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export type AccountDeletionService = ReturnType<typeof createAccountDeletionService>
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Better Auth's `user.deleteUser` options: it checks the password, or that the session is fresh,
|
|
282
|
+
* then runs `beforeDelete` outside any transaction of its own, so Cloud's may query. That
|
|
283
|
+
* transaction deletes the user too; a refusal reaches the client as `account_sole_admin` or
|
|
284
|
+
* `last_admin_required`.
|
|
285
|
+
*/
|
|
286
|
+
export function accountDeletionOptions(database: Kysely<CloudDatabase>) {
|
|
287
|
+
const deletion = createAccountDeletionService(database)
|
|
288
|
+
return {
|
|
289
|
+
enabled: true,
|
|
290
|
+
async beforeDelete(user: { id: string; email: string }) {
|
|
291
|
+
try {
|
|
292
|
+
await deletion.remove(user)
|
|
293
|
+
} catch (error) {
|
|
294
|
+
if (error instanceof AccountDeletionBlockedError) {
|
|
295
|
+
throw APIError.from('CONFLICT', { code: 'ACCOUNT_SOLE_ADMIN', message: error.message })
|
|
296
|
+
}
|
|
297
|
+
if (error instanceof AccountLastAdministratorError) {
|
|
298
|
+
throw APIError.from('CONFLICT', { code: 'LAST_ADMIN_REQUIRED', message: error.message })
|
|
299
|
+
}
|
|
300
|
+
throw error
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|