@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
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { replaceChecks } from './schema'
|
|
4
|
+
|
|
5
|
+
/** Every kind of transactional email before this migration. */
|
|
6
|
+
const EMAIL_KINDS = [
|
|
7
|
+
'document-invitation',
|
|
8
|
+
'workspace-invitation',
|
|
9
|
+
'enrollment-requested',
|
|
10
|
+
'admin-enrollment-notification',
|
|
11
|
+
'enrollment-approved',
|
|
12
|
+
'enrollment-rejected',
|
|
13
|
+
'enrollment-revoked',
|
|
14
|
+
'email-verification',
|
|
15
|
+
'password-reset',
|
|
16
|
+
'password-changed',
|
|
17
|
+
'email-change-requested',
|
|
18
|
+
'new-sign-in',
|
|
19
|
+
'two-step-changed',
|
|
20
|
+
'document-report',
|
|
21
|
+
'storage-limit-reached'
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
/** Every kind of transactional email once this migration has run. */
|
|
25
|
+
export const DOCUMENT_BLOCK_EMAIL_KINDS = [...EMAIL_KINDS, 'document-blocked', 'document-restored']
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Email gains the kinds that tell a workspace's owner an administrator blocked one of its
|
|
29
|
+
* documents, and that it is back, which the previous release never writes.
|
|
30
|
+
*/
|
|
31
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
32
|
+
await replaceChecks(database, 'widen', [
|
|
33
|
+
['transactional_email', 'kind', DOCUMENT_BLOCK_EMAIL_KINDS]
|
|
34
|
+
])
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Narrowing back checks every row, so a rollback refuses while any email of the new kinds is kept.
|
|
38
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
39
|
+
await replaceChecks(database, 'narrow', [['transactional_email', 'kind', EMAIL_KINDS]])
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { DOCUMENT_BLOCK_EMAIL_KINDS } from './013_document_block_emails'
|
|
4
|
+
import { replaceChecks } from './schema'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whoever reports a document and leaves an address hears that the report arrived and what the
|
|
8
|
+
* administrators decided. A report keeps the language its reporter's browser asked for, since a
|
|
9
|
+
* guest has no account to say it, and email gains the two kinds, which the previous release
|
|
10
|
+
* never writes.
|
|
11
|
+
*/
|
|
12
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
13
|
+
await database.schema.alterTable('document_report').addColumn('reply_locale', 'text').execute()
|
|
14
|
+
await replaceChecks(database, 'widen', [
|
|
15
|
+
[
|
|
16
|
+
'transactional_email',
|
|
17
|
+
'kind',
|
|
18
|
+
[...DOCUMENT_BLOCK_EMAIL_KINDS, 'document-report-received', 'document-report-decided']
|
|
19
|
+
]
|
|
20
|
+
])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
24
|
+
await replaceChecks(database, 'narrow', [
|
|
25
|
+
['transactional_email', 'kind', DOCUMENT_BLOCK_EMAIL_KINDS]
|
|
26
|
+
])
|
|
27
|
+
await database.schema.alterTable('document_report').dropColumn('reply_locale').execute()
|
|
28
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { checkColumnIn, CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
|
+
import { sql, type ColumnDefinitionBuilder, type CreateTableBuilder, type Kysely } from 'kysely'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What Cloud's migrations build tables with. Names follow PostgreSQL's own: `<table>_pkey`,
|
|
6
|
+
* `<table>_<columns>_key` for unique, `<table>_<columns>_fkey`, and `<table>_<column>_check`;
|
|
7
|
+
* indexes are `<table>_<columns>_idx`. Account IDs are text, since Better Auth's tables come after
|
|
8
|
+
* Cloud's (see `migrateCloudDatabase`), and never foreign keys.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const now = (column: ColumnDefinitionBuilder) =>
|
|
12
|
+
column.notNull().defaultTo(CURRENT_TIMESTAMP)
|
|
13
|
+
export const required = (column: ColumnDefinitionBuilder) => column.notNull()
|
|
14
|
+
|
|
15
|
+
export const PERMISSIONS = ['view', 'edit']
|
|
16
|
+
export const ROLES = ['admin', 'editor', 'viewer']
|
|
17
|
+
|
|
18
|
+
/** What every emailed invitation keeps: whom it went to, its access, its secret, and its life. */
|
|
19
|
+
export function withInvitation<Table extends string>(
|
|
20
|
+
table: CreateTableBuilder<Table>,
|
|
21
|
+
access: 'permission' | 'role'
|
|
22
|
+
) {
|
|
23
|
+
return table
|
|
24
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
25
|
+
.addColumn('email_normalized', 'text', required)
|
|
26
|
+
.addColumn(access, 'text', required)
|
|
27
|
+
.addColumn('token_hash', 'text', required)
|
|
28
|
+
.addColumn('invited_by', 'text', required)
|
|
29
|
+
.addColumn('invited_at', 'timestamptz', now)
|
|
30
|
+
.addColumn('expires_at', 'timestamptz', required)
|
|
31
|
+
.addColumn('accepted_at', 'timestamptz')
|
|
32
|
+
.addColumn('revoked_at', 'timestamptz')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Text columns that take one of a few values, as `[table, column, values]`. */
|
|
36
|
+
export async function addChecks(
|
|
37
|
+
database: Kysely<unknown>,
|
|
38
|
+
checks: readonly (readonly [table: string, column: string, values: readonly string[]])[]
|
|
39
|
+
): Promise<void> {
|
|
40
|
+
for (const [table, column, values] of checks) {
|
|
41
|
+
await database.schema
|
|
42
|
+
.alterTable(table)
|
|
43
|
+
.addCheckConstraint(`${table}_${column}_check`, checkColumnIn(column, values))
|
|
44
|
+
.execute()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Replaces the values a column may take, as `addChecks` named them. Widening them, as when a
|
|
50
|
+
* release adds a kind the previous one never writes, adds the check `NOT VALID`: rows already
|
|
51
|
+
* there passed the narrower one, so PostgreSQL skips scanning them while it holds the table, and
|
|
52
|
+
* checks every row written from then on. Narrowing them, as a rollback does, checks every row.
|
|
53
|
+
*/
|
|
54
|
+
export async function replaceChecks(
|
|
55
|
+
database: Kysely<unknown>,
|
|
56
|
+
change: 'widen' | 'narrow',
|
|
57
|
+
checks: readonly (readonly [table: string, column: string, values: readonly string[]])[]
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
for (const [table, column, values] of checks) {
|
|
60
|
+
const name = `${table}_${column}_check`
|
|
61
|
+
await database.schema.alterTable(table).dropConstraint(name).execute()
|
|
62
|
+
if (change === 'narrow') {
|
|
63
|
+
await addChecks(database, [[table, column, values]])
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
// Kysely builds no `NOT VALID`, so this one statement is written out.
|
|
67
|
+
await sql`alter table ${sql.table(table)} add constraint ${sql.id(name)} check (${checkColumnIn(column, values)}) not valid`.execute(
|
|
68
|
+
database
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
74
|
+
export async function addIndexes(
|
|
75
|
+
database: Kysely<unknown>,
|
|
76
|
+
indexes: readonly (readonly [table: string, columns: readonly string[]])[]
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
for (const [table, columns] of indexes) {
|
|
79
|
+
const name = columns.map((column) => column.split(' ')[0]).join('_')
|
|
80
|
+
await database.schema
|
|
81
|
+
.createIndex(`${table}_${name}_idx`)
|
|
82
|
+
.on(table)
|
|
83
|
+
.columns([...columns])
|
|
84
|
+
.execute()
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Drops a migration's tables, last created first. */
|
|
89
|
+
export async function dropTables(
|
|
90
|
+
database: Kysely<unknown>,
|
|
91
|
+
tables: readonly string[]
|
|
92
|
+
): Promise<void> {
|
|
93
|
+
for (const table of tables.toReversed()) {
|
|
94
|
+
await database.schema.dropTable(table).ifExists().cascade().execute()
|
|
95
|
+
}
|
|
96
|
+
}
|
package/src/server/db/tables.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { EnrollmentStatus } from '#cloud/admin/enrollment/service'
|
|
2
2
|
import type { CloudSignInMethod, DocumentPermission, WorkspaceRole } from '#cloud/contract'
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
EmailLocale,
|
|
5
|
+
TransactionalEmailKind,
|
|
6
|
+
TransactionalEmailPayloadByKind
|
|
7
|
+
} from '#cloud/email'
|
|
4
8
|
import type { Account, Session } from 'better-auth'
|
|
5
9
|
import type { ColumnType, Generated, Insertable, Selectable, Updateable } from 'kysely'
|
|
6
10
|
|
|
@@ -51,6 +55,8 @@ export interface WorkspaceTable {
|
|
|
51
55
|
name: string
|
|
52
56
|
slug: string
|
|
53
57
|
createdBy: string
|
|
58
|
+
/** Whose plan the workspace's limits come from; ownership passes to another admin when they go. */
|
|
59
|
+
ownerId: string
|
|
54
60
|
createdAt: TimestampColumn
|
|
55
61
|
updatedAt: TimestampColumn
|
|
56
62
|
}
|
|
@@ -88,8 +94,39 @@ export interface DocumentTable {
|
|
|
88
94
|
createdAt: TimestampColumn
|
|
89
95
|
updatedAt: TimestampColumn
|
|
90
96
|
deletedAt: TimestampColumn | null
|
|
97
|
+
/** Who moved the document to Trash; empty when its account's deletion did. */
|
|
98
|
+
deletedBy: string | null
|
|
99
|
+
/**
|
|
100
|
+
* When it was deleted forever, and by whom (empty for an account's deletion): out of everyone's
|
|
101
|
+
* sight, its files wait out the deployment's grace before cleanup destroys them.
|
|
102
|
+
*/
|
|
103
|
+
purgedAt: TimestampColumn | null
|
|
104
|
+
purgedBy: string | null
|
|
105
|
+
/** Storage taken off its workspace's usage when it was deleted forever; see migration 007. */
|
|
106
|
+
purgedBytes: Generated<number>
|
|
91
107
|
cleanupClaimId: Generated<string | null>
|
|
92
108
|
cleanupClaimedAt: TimestampColumn | null
|
|
109
|
+
/** Set while a deployment administrator has taken the document down; see migration 032. */
|
|
110
|
+
disabledAt: TimestampColumn | null
|
|
111
|
+
disabledReason: Generated<string | null>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A report someone filed about a document shared with them by link; see migration 032. */
|
|
115
|
+
export interface DocumentReportTable {
|
|
116
|
+
id: string
|
|
117
|
+
documentId: string
|
|
118
|
+
shareId: string | null
|
|
119
|
+
reporterUserId: string | null
|
|
120
|
+
reporterGuestId: string | null
|
|
121
|
+
replyEmail: string | null
|
|
122
|
+
/** The language to reply in when the reporter has no account that chose one. */
|
|
123
|
+
replyLocale: Generated<EmailLocale | null>
|
|
124
|
+
reason: 'spam-phishing' | 'illegal' | 'copyright' | 'harassment' | 'other'
|
|
125
|
+
description: string
|
|
126
|
+
status: Generated<'open' | 'dismissed' | 'actioned'>
|
|
127
|
+
createdAt: TimestampColumn
|
|
128
|
+
resolvedAt: TimestampColumn | null
|
|
129
|
+
resolvedBy: string | null
|
|
93
130
|
}
|
|
94
131
|
|
|
95
132
|
export interface StorageObjectTable {
|
|
@@ -175,10 +212,23 @@ export interface AuthUserTable {
|
|
|
175
212
|
banExpires: TimestampColumn | null
|
|
176
213
|
/** Unset on accounts that never turned two-step sign-in on. */
|
|
177
214
|
twoFactorEnabled: Generated<boolean | null>
|
|
215
|
+
/** The language Cloud writes to the account in; unset is English. */
|
|
216
|
+
locale: Generated<EmailLocale | null>
|
|
178
217
|
createdAt: TimestampColumn
|
|
179
218
|
updatedAt: TimestampColumn
|
|
180
219
|
}
|
|
181
220
|
|
|
221
|
+
/** Better Auth's API keys, which are Cloud's personal access tokens; the plugin owns the rest. */
|
|
222
|
+
export interface AccessTokenTable {
|
|
223
|
+
id: string
|
|
224
|
+
referenceId: string
|
|
225
|
+
name: string | null
|
|
226
|
+
start: string | null
|
|
227
|
+
expiresAt: TimestampColumn | null
|
|
228
|
+
lastRequest: TimestampColumn | null
|
|
229
|
+
createdAt: TimestampColumn
|
|
230
|
+
}
|
|
231
|
+
|
|
182
232
|
export interface TwoFactorTable {
|
|
183
233
|
id: string
|
|
184
234
|
secret: string
|
|
@@ -264,8 +314,15 @@ export interface InvitationContinuationTable {
|
|
|
264
314
|
createdAt: TimestampColumn
|
|
265
315
|
}
|
|
266
316
|
|
|
267
|
-
|
|
268
|
-
|
|
317
|
+
/** Which version of the server's terms an account accepted, and when; one row per version. */
|
|
318
|
+
export interface TermsAcceptanceTable {
|
|
319
|
+
userId: string
|
|
320
|
+
version: string
|
|
321
|
+
acceptedAt: TimestampColumn
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export interface AccountEntitlementTable {
|
|
325
|
+
userId: string
|
|
269
326
|
values: unknown
|
|
270
327
|
source: string
|
|
271
328
|
revision: Generated<number>
|
|
@@ -274,6 +331,12 @@ export interface WorkspaceEntitlementTable {
|
|
|
274
331
|
updatedAt: TimestampColumn
|
|
275
332
|
}
|
|
276
333
|
|
|
334
|
+
/** An account told its storage is full; see `createStorageLimitNoticeService`. */
|
|
335
|
+
export interface StorageLimitNoticeTable {
|
|
336
|
+
userId: string
|
|
337
|
+
notifiedAt: TimestampColumn
|
|
338
|
+
}
|
|
339
|
+
|
|
277
340
|
export interface WorkspaceStorageUsageTable {
|
|
278
341
|
workspaceId: string
|
|
279
342
|
committedBytes: number
|
|
@@ -299,6 +362,8 @@ export interface TransactionalEmailTable {
|
|
|
299
362
|
kind: TransactionalEmailKind
|
|
300
363
|
recipientEmailNormalized: string
|
|
301
364
|
payloadEncrypted: string | null
|
|
365
|
+
/** The language the email is written in. */
|
|
366
|
+
locale: Generated<EmailLocale>
|
|
302
367
|
status: TransactionalEmailStatus
|
|
303
368
|
attemptCount: Generated<number>
|
|
304
369
|
nextAttemptAt: TimestampColumn
|
|
@@ -351,6 +416,14 @@ export interface AuthSchemaVersionTable {
|
|
|
351
416
|
updatedAt: TimestampColumn
|
|
352
417
|
}
|
|
353
418
|
|
|
419
|
+
/** One invitation sent, for the daily invitation budget; see `src/server/invitations/budget.ts`. */
|
|
420
|
+
export interface InvitationSendTable {
|
|
421
|
+
id: string
|
|
422
|
+
senderId: string
|
|
423
|
+
emailNormalized: string
|
|
424
|
+
sentAt: TimestampColumn
|
|
425
|
+
}
|
|
426
|
+
|
|
354
427
|
export interface CloudDatabase {
|
|
355
428
|
account: Account
|
|
356
429
|
session: Session
|
|
@@ -360,6 +433,7 @@ export interface CloudDatabase {
|
|
|
360
433
|
signIn: SignInTable
|
|
361
434
|
userPicture: UserPictureTable
|
|
362
435
|
user: AuthUserTable
|
|
436
|
+
apikey: AccessTokenTable
|
|
363
437
|
authSchemaVersion: AuthSchemaVersionTable
|
|
364
438
|
enrollment: EnrollmentTable
|
|
365
439
|
apiRateLimit: APIRateLimitTable
|
|
@@ -379,11 +453,15 @@ export interface CloudDatabase {
|
|
|
379
453
|
documentRoomSnapshot: DocumentRoomSnapshotTable
|
|
380
454
|
documentRoomUpdate: DocumentRoomUpdateTable
|
|
381
455
|
invitationContinuation: InvitationContinuationTable
|
|
382
|
-
|
|
456
|
+
accountEntitlement: AccountEntitlementTable
|
|
457
|
+
termsAcceptance: TermsAcceptanceTable
|
|
383
458
|
workspaceStorageUsage: WorkspaceStorageUsageTable
|
|
459
|
+
storageLimitNotice: StorageLimitNoticeTable
|
|
384
460
|
transactionalEmail: TransactionalEmailTable
|
|
385
461
|
uploadStorageReservation: UploadStorageReservationTable
|
|
386
462
|
upload: UploadTable
|
|
463
|
+
invitationSend: InvitationSendTable
|
|
464
|
+
documentReport: DocumentReportTable
|
|
387
465
|
}
|
|
388
466
|
|
|
389
467
|
export type NewTransactionalEmail = Insertable<TransactionalEmailTable>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ExpressionBuilder } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import type { CloudDatabase } from './tables'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `user.id` as text, the form every other table keeps account IDs in, such as a document's
|
|
7
|
+
* `createdBy`, so the two can be compared.
|
|
8
|
+
*/
|
|
9
|
+
export function userIdText(expression: ExpressionBuilder<CloudDatabase, 'user'>) {
|
|
10
|
+
return expression.cast<string>(expression.ref('user.id'), 'text')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Whether the user row in scope is banned, by a ban that has not run out. */
|
|
14
|
+
export function isBannedNow(expression: ExpressionBuilder<CloudDatabase, 'user'>) {
|
|
15
|
+
return expression.and([
|
|
16
|
+
expression('user.banned', '=', true),
|
|
17
|
+
expression.or([
|
|
18
|
+
expression('user.banExpires', 'is', null),
|
|
19
|
+
expression('user.banExpires', '>', new Date())
|
|
20
|
+
])
|
|
21
|
+
])
|
|
22
|
+
}
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { DocumentAccess, DocumentAccessSource, DocumentPermission } from '#cloud/contract'
|
|
2
|
+
import {
|
|
3
|
+
documentRoleCan,
|
|
4
|
+
GRANT_DOCUMENT_ROLE,
|
|
5
|
+
workspaceRoleCan,
|
|
6
|
+
type AccessRequest
|
|
7
|
+
} from '#cloud/server/auth/access-control'
|
|
7
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
8
|
-
import { maxBy } from 'es-toolkit'
|
|
9
9
|
import type { Kysely, Transaction } from 'kysely'
|
|
10
10
|
|
|
11
11
|
export type AccessDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return role === 'viewer' ? 'view' : 'edit'
|
|
13
|
+
type DocumentAccessGrant = {
|
|
14
|
+
source: DocumentAccessSource
|
|
15
|
+
can: (request: AccessRequest) => boolean
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Everything that gives a user access to a document, each checked against its role table: a live
|
|
20
|
+
* document, or one in Trash.
|
|
21
|
+
*/
|
|
22
|
+
async function documentAccessGrants(
|
|
20
23
|
database: AccessDatabase,
|
|
21
24
|
userId: string,
|
|
22
|
-
documentId: string
|
|
23
|
-
|
|
25
|
+
documentId: string,
|
|
26
|
+
place: 'live' | 'trash' = 'live'
|
|
27
|
+
): Promise<DocumentAccessGrant[]> {
|
|
24
28
|
const document = await database
|
|
25
29
|
.selectFrom('document')
|
|
26
30
|
.leftJoin('workspaceMember', (join) =>
|
|
@@ -40,33 +44,47 @@ export async function resolveDocumentAccess(
|
|
|
40
44
|
'documentGrant.permission as grantPermission'
|
|
41
45
|
])
|
|
42
46
|
.where('document.id', '=', documentId)
|
|
43
|
-
.where('document.deletedAt', 'is', null)
|
|
47
|
+
.where('document.deletedAt', place === 'live' ? 'is' : 'is not', null)
|
|
48
|
+
// A document an administrator took down is out of reach for everyone, its owner included.
|
|
49
|
+
.where('document.disabledAt', 'is', null)
|
|
44
50
|
.executeTakeFirst()
|
|
45
|
-
if (!document) return
|
|
51
|
+
if (!document) return []
|
|
46
52
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
candidates.push('edit')
|
|
53
|
+
const grants: DocumentAccessGrant[] = []
|
|
54
|
+
// Making a document gives access only while its maker is in the workspace: someone removed
|
|
55
|
+
// from it keeps no way back in.
|
|
56
|
+
if (document.createdBy === userId && document.workspaceRole !== null) {
|
|
57
|
+
grants.push({ source: 'owner', can: (request) => documentRoleCan('owner', request) })
|
|
53
58
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
const { workspaceRole, grantPermission } = document
|
|
60
|
+
if (workspaceRole) {
|
|
61
|
+
grants.push({ source: 'workspace', can: (request) => workspaceRoleCan(workspaceRole, request) })
|
|
57
62
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
if (grantPermission) {
|
|
64
|
+
const role = GRANT_DOCUMENT_ROLE[grantPermission]
|
|
65
|
+
grants.push({ source: 'direct-grant', can: (request) => documentRoleCan(role, request) })
|
|
61
66
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
return grants
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function grantsAllow(grants: DocumentAccessGrant[], request: AccessRequest): boolean {
|
|
71
|
+
return grants.some((grant) => grant.can(request))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function resolveDocumentAccess(
|
|
75
|
+
database: AccessDatabase,
|
|
76
|
+
userId: string,
|
|
77
|
+
documentId: string
|
|
78
|
+
): Promise<DocumentAccess | undefined> {
|
|
79
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
80
|
+
if (!grantsAllow(grants, { document: ['read'] })) return undefined
|
|
81
|
+
const permission: DocumentPermission = grantsAllow(grants, { document: ['edit'] })
|
|
82
|
+
? 'edit'
|
|
83
|
+
: 'view'
|
|
66
84
|
return {
|
|
67
85
|
permission,
|
|
68
|
-
canManageSharing:
|
|
69
|
-
sources
|
|
86
|
+
canManageSharing: grantsAllow(grants, { document: ['share'] }),
|
|
87
|
+
sources: grants.map((grant) => grant.source)
|
|
70
88
|
}
|
|
71
89
|
}
|
|
72
90
|
|
|
@@ -74,30 +92,23 @@ export function canEditDocument(access: DocumentAccess): boolean {
|
|
|
74
92
|
return access.permission === 'edit'
|
|
75
93
|
}
|
|
76
94
|
|
|
77
|
-
/**
|
|
78
|
-
* Removing a document belongs to its owner and its workspace's admins and editors. A direct grant
|
|
79
|
-
* or an accepted invitation lets someone edit, never delete the owner's document.
|
|
80
|
-
*/
|
|
95
|
+
/** Whether the user may delete the document; `documentRoles` and `workspaceRoles` decide who. */
|
|
81
96
|
export async function canDeleteDocument(
|
|
82
97
|
database: AccessDatabase,
|
|
83
98
|
userId: string,
|
|
84
99
|
documentId: string
|
|
85
100
|
): Promise<boolean> {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return (
|
|
99
|
-
document.createdBy === userId ||
|
|
100
|
-
document.workspaceRole === 'admin' ||
|
|
101
|
-
document.workspaceRole === 'editor'
|
|
102
|
-
)
|
|
101
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
102
|
+
return grantsAllow(grants, { document: ['delete'] })
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether the user may restore the document from Trash, or delete it there for good. */
|
|
106
|
+
export async function trashedDocumentAllows(
|
|
107
|
+
database: AccessDatabase,
|
|
108
|
+
userId: string,
|
|
109
|
+
documentId: string,
|
|
110
|
+
action: 'delete' | 'purge'
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
const grants = await documentAccessGrants(database, userId, documentId, 'trash')
|
|
113
|
+
return grantsAllow(grants, { document: [action] })
|
|
103
114
|
}
|
|
@@ -14,3 +14,10 @@ export class DocumentConflictError extends Error {
|
|
|
14
14
|
export class UploadInvalidError extends Error {
|
|
15
15
|
override readonly name = 'UploadInvalidError'
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Destroying a document deleted forever with no grace failed. The failure, its cause, was
|
|
19
|
+
* reported where cleanup reports; the document is out of Trash, and cleanup tries again.
|
|
20
|
+
*/
|
|
21
|
+
export class DocumentPurgeFailedError extends Error {
|
|
22
|
+
override readonly name = 'DocumentPurgeFailedError'
|
|
23
|
+
}
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
RequestImageUploadsInput
|
|
7
7
|
} from '#cloud/contract'
|
|
8
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
9
10
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
10
11
|
import type { StorageQuotaService } from '#cloud/server/quota'
|
|
11
12
|
import { uniq, uniqBy } from 'es-toolkit'
|
|
@@ -53,7 +54,7 @@ export function createDocumentImages(
|
|
|
53
54
|
.execute()
|
|
54
55
|
} catch (error) {
|
|
55
56
|
// Cleanup releases it once it expires.
|
|
56
|
-
|
|
57
|
+
logCloud('warn', 'Giving up an image upload failed', { error: loggedError(error) })
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
}
|
|
@@ -4,7 +4,7 @@ export {
|
|
|
4
4
|
resolveDocumentAccess,
|
|
5
5
|
type AccessDatabase
|
|
6
6
|
} from './access'
|
|
7
|
-
export { createDocumentRoutes, type DocumentRouteEnvironment } from './routes'
|
|
7
|
+
export { createDocumentRoutes, documentDomainError, type DocumentRouteEnvironment } from './routes'
|
|
8
8
|
export {
|
|
9
9
|
DocumentConflictError,
|
|
10
10
|
DocumentForbiddenError,
|
|
@@ -4,11 +4,15 @@ 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 { storageLimitReached } from '#cloud/server/policy/storage'
|
|
8
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
9
|
+
import { StorageQuotaExceededError } from '#cloud/server/quota'
|
|
7
10
|
import type { Kysely } from 'kysely'
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
|
-
* The document `userId` may write to, and how large one file and
|
|
11
|
-
*
|
|
13
|
+
* The document `userId` may write to, and how large one file and its owner's storage may be for
|
|
14
|
+
* this save: technical limits, tightened by the owner's plan. An owner who holds their whole
|
|
15
|
+
* storage allowance or more is read-only, so nothing of theirs takes a save, however small.
|
|
12
16
|
*/
|
|
13
17
|
export async function writableDocument(
|
|
14
18
|
database: Kysely<CloudDatabase>,
|
|
@@ -23,12 +27,14 @@ export async function writableDocument(
|
|
|
23
27
|
const row = await getDocumentSummaryRow(database, documentId)
|
|
24
28
|
if (!row) throw new DocumentNotFoundError()
|
|
25
29
|
const document = documentSummary(row)
|
|
26
|
-
const policyContext =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const policyContext = await workspacePolicyContext(
|
|
31
|
+
database,
|
|
32
|
+
{ documentId },
|
|
33
|
+
{ actorId: userId, deploymentMode: 'self-hosted' }
|
|
34
|
+
)
|
|
35
|
+
if (!policyContext) throw new DocumentNotFoundError()
|
|
36
|
+
if (await storageLimitReached(database, options.policy, { documentId })) {
|
|
37
|
+
throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
|
|
32
38
|
}
|
|
33
39
|
const technicalMaximum = options.technicalMaximumUploadBytes ?? Number.MAX_SAFE_INTEGER
|
|
34
40
|
const maximumFileBytes = Math.min(
|
|
@@ -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
|
+
}
|