@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- 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 +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +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 +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +39 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- 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 +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -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/schema.ts +71 -0
- package/src/server/db/tables.ts +52 -3
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- 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/logging.ts +76 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +2 -2
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +1 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/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,110 @@
|
|
|
1
|
+
import { EMPTY_JSON_OBJECT } from '#cloud/server/db/expressions'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
|
|
4
|
+
import { addChecks, addIndexes, dropTables, now, required } from './schema'
|
|
5
|
+
|
|
6
|
+
/** Running the deployment: the email outbox, enrollment, reports, and the administrators' log. */
|
|
7
|
+
const TABLES = ['transactional_email', 'enrollment', 'document_report', 'admin_audit_event']
|
|
8
|
+
|
|
9
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
10
|
+
// The email outbox; a payload is encrypted until it is sent.
|
|
11
|
+
await database.schema
|
|
12
|
+
.createTable('transactional_email')
|
|
13
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
14
|
+
.addColumn('idempotency_key', 'text', (column) => column.notNull().unique())
|
|
15
|
+
.addColumn('kind', 'text', required)
|
|
16
|
+
.addColumn('recipient_email_normalized', 'text', required)
|
|
17
|
+
.addColumn('payload_encrypted', 'text')
|
|
18
|
+
.addColumn('status', 'text', (column) => column.notNull().defaultTo('pending'))
|
|
19
|
+
.addColumn('attempt_count', 'integer', (column) => column.notNull().defaultTo(0))
|
|
20
|
+
.addColumn('next_attempt_at', 'timestamptz', now)
|
|
21
|
+
.addColumn('claim_id', 'uuid')
|
|
22
|
+
.addColumn('claimed_at', 'timestamptz')
|
|
23
|
+
.addColumn('transport', 'text')
|
|
24
|
+
.addColumn('transport_message_id', 'text')
|
|
25
|
+
.addColumn('last_error_code', 'text')
|
|
26
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
27
|
+
.addColumn('updated_at', 'timestamptz', now)
|
|
28
|
+
.addColumn('accepted_at', 'timestamptz')
|
|
29
|
+
.execute()
|
|
30
|
+
await database.schema
|
|
31
|
+
.createTable('enrollment')
|
|
32
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
33
|
+
.addColumn('email_normalized', 'text', (column) => column.notNull().unique())
|
|
34
|
+
.addColumn('name', 'text')
|
|
35
|
+
.addColumn('reason', 'text')
|
|
36
|
+
.addColumn('status', 'text', (column) => column.notNull().defaultTo('pending'))
|
|
37
|
+
.addColumn('request_revision', 'integer', (column) => column.notNull().defaultTo(1))
|
|
38
|
+
.addColumn('requested_at', 'timestamptz', now)
|
|
39
|
+
.addColumn('reviewed_at', 'timestamptz')
|
|
40
|
+
.addColumn('reviewed_by', 'text')
|
|
41
|
+
.addColumn('review_note', 'text')
|
|
42
|
+
.addColumn('approved_user_id', 'text')
|
|
43
|
+
.execute()
|
|
44
|
+
// What someone reported about a shared document, and what an administrator did about it.
|
|
45
|
+
await database.schema
|
|
46
|
+
.createTable('document_report')
|
|
47
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
48
|
+
.addColumn('document_id', 'uuid', (column) =>
|
|
49
|
+
column.notNull().references('document.id').onDelete('cascade')
|
|
50
|
+
)
|
|
51
|
+
.addColumn('share_id', 'uuid')
|
|
52
|
+
.addColumn('reporter_user_id', 'text')
|
|
53
|
+
.addColumn('reporter_guest_id', 'text')
|
|
54
|
+
.addColumn('reply_email', 'text')
|
|
55
|
+
.addColumn('reason', 'text', required)
|
|
56
|
+
.addColumn('description', 'text', required)
|
|
57
|
+
.addColumn('status', 'text', (column) => column.notNull().defaultTo('open'))
|
|
58
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
59
|
+
.addColumn('resolved_at', 'timestamptz')
|
|
60
|
+
.addColumn('resolved_by', 'text')
|
|
61
|
+
.execute()
|
|
62
|
+
await database.schema
|
|
63
|
+
.createTable('admin_audit_event')
|
|
64
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
65
|
+
.addColumn('actor_user_id', 'text', required)
|
|
66
|
+
.addColumn('action', 'text', required)
|
|
67
|
+
.addColumn('subject_type', 'text', required)
|
|
68
|
+
.addColumn('subject_id', 'text', required)
|
|
69
|
+
.addColumn('metadata', 'jsonb', (column) => column.notNull().defaultTo(EMPTY_JSON_OBJECT))
|
|
70
|
+
.addColumn('created_at', 'timestamptz', now)
|
|
71
|
+
.execute()
|
|
72
|
+
await addChecks(database, [
|
|
73
|
+
[
|
|
74
|
+
'transactional_email',
|
|
75
|
+
'kind',
|
|
76
|
+
[
|
|
77
|
+
'document-invitation',
|
|
78
|
+
'workspace-invitation',
|
|
79
|
+
'enrollment-requested',
|
|
80
|
+
'admin-enrollment-notification',
|
|
81
|
+
'enrollment-approved',
|
|
82
|
+
'enrollment-rejected',
|
|
83
|
+
'enrollment-revoked',
|
|
84
|
+
'email-verification',
|
|
85
|
+
'password-reset',
|
|
86
|
+
'password-changed',
|
|
87
|
+
'email-change-requested',
|
|
88
|
+
'new-sign-in',
|
|
89
|
+
'two-step-changed',
|
|
90
|
+
'document-report'
|
|
91
|
+
]
|
|
92
|
+
],
|
|
93
|
+
['transactional_email', 'status', ['pending', 'sending', 'accepted', 'failed', 'suppressed']],
|
|
94
|
+
['enrollment', 'status', ['pending', 'approved', 'rejected', 'revoked']],
|
|
95
|
+
['document_report', 'reason', ['spam-phishing', 'illegal', 'copyright', 'harassment', 'other']],
|
|
96
|
+
['document_report', 'status', ['open', 'dismissed', 'actioned']]
|
|
97
|
+
])
|
|
98
|
+
await addIndexes(database, [
|
|
99
|
+
['transactional_email', ['status', 'next_attempt_at']],
|
|
100
|
+
['enrollment', ['status', 'requested_at']],
|
|
101
|
+
['document_report', ['document_id']],
|
|
102
|
+
['document_report', ['share_id', 'created_at']],
|
|
103
|
+
['document_report', ['status', 'created_at']],
|
|
104
|
+
['admin_audit_event', ['created_at']]
|
|
105
|
+
])
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
109
|
+
await dropTables(database, TABLES)
|
|
110
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addIndexes } from './schema'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Trash: who deleted a document, and who deleted it forever, when, and the storage that took off
|
|
7
|
+
* its workspace's usage while its files wait out the grace before cleanup destroys them.
|
|
8
|
+
*/
|
|
9
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
10
|
+
await database.schema
|
|
11
|
+
.alterTable('document')
|
|
12
|
+
.addColumn('deleted_by', 'text')
|
|
13
|
+
.addColumn('purged_at', 'timestamptz')
|
|
14
|
+
.addColumn('purged_by', 'text')
|
|
15
|
+
.addColumn('purged_bytes', 'bigint', (column) => column.notNull().defaultTo(0))
|
|
16
|
+
.execute()
|
|
17
|
+
await addIndexes(database, [
|
|
18
|
+
['document', ['workspace_id', 'deleted_at desc']],
|
|
19
|
+
['document', ['purged_at']]
|
|
20
|
+
])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
24
|
+
await database.schema.dropIndex('document_purged_at_idx').execute()
|
|
25
|
+
await database.schema.dropIndex('document_workspace_id_deleted_at_idx').execute()
|
|
26
|
+
await database.schema
|
|
27
|
+
.alterTable('document')
|
|
28
|
+
.dropColumn('purged_bytes')
|
|
29
|
+
.dropColumn('purged_by')
|
|
30
|
+
.dropColumn('purged_at')
|
|
31
|
+
.dropColumn('deleted_by')
|
|
32
|
+
.execute()
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { now, required } from './schema'
|
|
4
|
+
|
|
5
|
+
/** Which version of the deployment's terms each account accepted, and when. */
|
|
6
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
7
|
+
await database.schema
|
|
8
|
+
.createTable('terms_acceptance')
|
|
9
|
+
.addColumn('user_id', 'text', required)
|
|
10
|
+
.addColumn('version', 'text', required)
|
|
11
|
+
.addColumn('accepted_at', 'timestamptz', now)
|
|
12
|
+
.addPrimaryKeyConstraint('terms_acceptance_pkey', ['user_id', 'version'])
|
|
13
|
+
.execute()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
17
|
+
await database.schema.dropTable('terms_acceptance').execute()
|
|
18
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { checkColumnIn, CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
|
+
import type { ColumnDefinitionBuilder, CreateTableBuilder, 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
|
+
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
49
|
+
export async function addIndexes(
|
|
50
|
+
database: Kysely<unknown>,
|
|
51
|
+
indexes: readonly (readonly [table: string, columns: readonly string[]])[]
|
|
52
|
+
): Promise<void> {
|
|
53
|
+
for (const [table, columns] of indexes) {
|
|
54
|
+
const name = columns.map((column) => column.split(' ')[0]).join('_')
|
|
55
|
+
await database.schema
|
|
56
|
+
.createIndex(`${table}_${name}_idx`)
|
|
57
|
+
.on(table)
|
|
58
|
+
.columns([...columns])
|
|
59
|
+
.execute()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Drops a migration's tables, last created first. */
|
|
64
|
+
export async function dropTables(
|
|
65
|
+
database: Kysely<unknown>,
|
|
66
|
+
tables: readonly string[]
|
|
67
|
+
): Promise<void> {
|
|
68
|
+
for (const table of tables.toReversed()) {
|
|
69
|
+
await database.schema.dropTable(table).ifExists().cascade().execute()
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/server/db/tables.ts
CHANGED
|
@@ -51,6 +51,8 @@ export interface WorkspaceTable {
|
|
|
51
51
|
name: string
|
|
52
52
|
slug: string
|
|
53
53
|
createdBy: string
|
|
54
|
+
/** Whose plan the workspace's limits come from; ownership passes to another admin when they go. */
|
|
55
|
+
ownerId: string
|
|
54
56
|
createdAt: TimestampColumn
|
|
55
57
|
updatedAt: TimestampColumn
|
|
56
58
|
}
|
|
@@ -88,8 +90,37 @@ export interface DocumentTable {
|
|
|
88
90
|
createdAt: TimestampColumn
|
|
89
91
|
updatedAt: TimestampColumn
|
|
90
92
|
deletedAt: TimestampColumn | null
|
|
93
|
+
/** Who moved the document to Trash; empty when its account's deletion did. */
|
|
94
|
+
deletedBy: string | null
|
|
95
|
+
/**
|
|
96
|
+
* When it was deleted forever, and by whom (empty for an account's deletion): out of everyone's
|
|
97
|
+
* sight, its files wait out the deployment's grace before cleanup destroys them.
|
|
98
|
+
*/
|
|
99
|
+
purgedAt: TimestampColumn | null
|
|
100
|
+
purgedBy: string | null
|
|
101
|
+
/** Storage taken off its workspace's usage when it was deleted forever; see migration 007. */
|
|
102
|
+
purgedBytes: Generated<number>
|
|
91
103
|
cleanupClaimId: Generated<string | null>
|
|
92
104
|
cleanupClaimedAt: TimestampColumn | null
|
|
105
|
+
/** Set while a deployment administrator has taken the document down; see migration 032. */
|
|
106
|
+
disabledAt: TimestampColumn | null
|
|
107
|
+
disabledReason: Generated<string | null>
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A report someone filed about a document shared with them by link; see migration 032. */
|
|
111
|
+
export interface DocumentReportTable {
|
|
112
|
+
id: string
|
|
113
|
+
documentId: string
|
|
114
|
+
shareId: string | null
|
|
115
|
+
reporterUserId: string | null
|
|
116
|
+
reporterGuestId: string | null
|
|
117
|
+
replyEmail: string | null
|
|
118
|
+
reason: 'spam-phishing' | 'illegal' | 'copyright' | 'harassment' | 'other'
|
|
119
|
+
description: string
|
|
120
|
+
status: Generated<'open' | 'dismissed' | 'actioned'>
|
|
121
|
+
createdAt: TimestampColumn
|
|
122
|
+
resolvedAt: TimestampColumn | null
|
|
123
|
+
resolvedBy: string | null
|
|
93
124
|
}
|
|
94
125
|
|
|
95
126
|
export interface StorageObjectTable {
|
|
@@ -264,8 +295,15 @@ export interface InvitationContinuationTable {
|
|
|
264
295
|
createdAt: TimestampColumn
|
|
265
296
|
}
|
|
266
297
|
|
|
267
|
-
|
|
268
|
-
|
|
298
|
+
/** Which version of the server's terms an account accepted, and when; one row per version. */
|
|
299
|
+
export interface TermsAcceptanceTable {
|
|
300
|
+
userId: string
|
|
301
|
+
version: string
|
|
302
|
+
acceptedAt: TimestampColumn
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export interface AccountEntitlementTable {
|
|
306
|
+
userId: string
|
|
269
307
|
values: unknown
|
|
270
308
|
source: string
|
|
271
309
|
revision: Generated<number>
|
|
@@ -351,6 +389,14 @@ export interface AuthSchemaVersionTable {
|
|
|
351
389
|
updatedAt: TimestampColumn
|
|
352
390
|
}
|
|
353
391
|
|
|
392
|
+
/** One invitation sent, for the daily invitation budget; see `src/server/invitations/budget.ts`. */
|
|
393
|
+
export interface InvitationSendTable {
|
|
394
|
+
id: string
|
|
395
|
+
senderId: string
|
|
396
|
+
emailNormalized: string
|
|
397
|
+
sentAt: TimestampColumn
|
|
398
|
+
}
|
|
399
|
+
|
|
354
400
|
export interface CloudDatabase {
|
|
355
401
|
account: Account
|
|
356
402
|
session: Session
|
|
@@ -379,11 +425,14 @@ export interface CloudDatabase {
|
|
|
379
425
|
documentRoomSnapshot: DocumentRoomSnapshotTable
|
|
380
426
|
documentRoomUpdate: DocumentRoomUpdateTable
|
|
381
427
|
invitationContinuation: InvitationContinuationTable
|
|
382
|
-
|
|
428
|
+
accountEntitlement: AccountEntitlementTable
|
|
429
|
+
termsAcceptance: TermsAcceptanceTable
|
|
383
430
|
workspaceStorageUsage: WorkspaceStorageUsageTable
|
|
384
431
|
transactionalEmail: TransactionalEmailTable
|
|
385
432
|
uploadStorageReservation: UploadStorageReservationTable
|
|
386
433
|
upload: UploadTable
|
|
434
|
+
invitationSend: InvitationSendTable
|
|
435
|
+
documentReport: DocumentReportTable
|
|
387
436
|
}
|
|
388
437
|
|
|
389
438
|
export type NewTransactionalEmail = Insertable<TransactionalEmailTable>
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { DocumentAccess, DocumentAccessSource, DocumentPermission } from '#cloud/contract'
|
|
2
|
+
import {
|
|
3
|
+
documentRoleCan,
|
|
4
|
+
GRANT_DOCUMENT_ROLE,
|
|
5
|
+
workspaceRoleCan,
|
|
6
|
+
type AccessRequest
|
|
7
|
+
} from '#cloud/server/auth/access-control'
|
|
7
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
8
|
-
import { maxBy } from 'es-toolkit'
|
|
9
9
|
import type { Kysely, Transaction } from 'kysely'
|
|
10
10
|
|
|
11
11
|
export type AccessDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return role === 'viewer' ? 'view' : 'edit'
|
|
13
|
+
type DocumentAccessGrant = {
|
|
14
|
+
source: DocumentAccessSource
|
|
15
|
+
can: (request: AccessRequest) => boolean
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Everything that gives a user access to a document, each checked against its role table: a live
|
|
20
|
+
* document, or one in Trash.
|
|
21
|
+
*/
|
|
22
|
+
async function documentAccessGrants(
|
|
20
23
|
database: AccessDatabase,
|
|
21
24
|
userId: string,
|
|
22
|
-
documentId: string
|
|
23
|
-
|
|
25
|
+
documentId: string,
|
|
26
|
+
place: 'live' | 'trash' = 'live'
|
|
27
|
+
): Promise<DocumentAccessGrant[]> {
|
|
24
28
|
const document = await database
|
|
25
29
|
.selectFrom('document')
|
|
26
30
|
.leftJoin('workspaceMember', (join) =>
|
|
@@ -40,33 +44,47 @@ export async function resolveDocumentAccess(
|
|
|
40
44
|
'documentGrant.permission as grantPermission'
|
|
41
45
|
])
|
|
42
46
|
.where('document.id', '=', documentId)
|
|
43
|
-
.where('document.deletedAt', 'is', null)
|
|
47
|
+
.where('document.deletedAt', place === 'live' ? 'is' : 'is not', null)
|
|
48
|
+
// A document an administrator took down is out of reach for everyone, its owner included.
|
|
49
|
+
.where('document.disabledAt', 'is', null)
|
|
44
50
|
.executeTakeFirst()
|
|
45
|
-
if (!document) return
|
|
51
|
+
if (!document) return []
|
|
46
52
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
candidates.push('edit')
|
|
53
|
+
const grants: DocumentAccessGrant[] = []
|
|
54
|
+
// Making a document gives access only while its maker is in the workspace: someone removed
|
|
55
|
+
// from it keeps no way back in.
|
|
56
|
+
if (document.createdBy === userId && document.workspaceRole !== null) {
|
|
57
|
+
grants.push({ source: 'owner', can: (request) => documentRoleCan('owner', request) })
|
|
53
58
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
const { workspaceRole, grantPermission } = document
|
|
60
|
+
if (workspaceRole) {
|
|
61
|
+
grants.push({ source: 'workspace', can: (request) => workspaceRoleCan(workspaceRole, request) })
|
|
57
62
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
if (grantPermission) {
|
|
64
|
+
const role = GRANT_DOCUMENT_ROLE[grantPermission]
|
|
65
|
+
grants.push({ source: 'direct-grant', can: (request) => documentRoleCan(role, request) })
|
|
61
66
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
return grants
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function grantsAllow(grants: DocumentAccessGrant[], request: AccessRequest): boolean {
|
|
71
|
+
return grants.some((grant) => grant.can(request))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function resolveDocumentAccess(
|
|
75
|
+
database: AccessDatabase,
|
|
76
|
+
userId: string,
|
|
77
|
+
documentId: string
|
|
78
|
+
): Promise<DocumentAccess | undefined> {
|
|
79
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
80
|
+
if (!grantsAllow(grants, { document: ['read'] })) return undefined
|
|
81
|
+
const permission: DocumentPermission = grantsAllow(grants, { document: ['edit'] })
|
|
82
|
+
? 'edit'
|
|
83
|
+
: 'view'
|
|
66
84
|
return {
|
|
67
85
|
permission,
|
|
68
|
-
canManageSharing:
|
|
69
|
-
sources
|
|
86
|
+
canManageSharing: grantsAllow(grants, { document: ['share'] }),
|
|
87
|
+
sources: grants.map((grant) => grant.source)
|
|
70
88
|
}
|
|
71
89
|
}
|
|
72
90
|
|
|
@@ -74,30 +92,23 @@ export function canEditDocument(access: DocumentAccess): boolean {
|
|
|
74
92
|
return access.permission === 'edit'
|
|
75
93
|
}
|
|
76
94
|
|
|
77
|
-
/**
|
|
78
|
-
* Removing a document belongs to its owner and its workspace's admins and editors. A direct grant
|
|
79
|
-
* or an accepted invitation lets someone edit, never delete the owner's document.
|
|
80
|
-
*/
|
|
95
|
+
/** Whether the user may delete the document; `documentRoles` and `workspaceRoles` decide who. */
|
|
81
96
|
export async function canDeleteDocument(
|
|
82
97
|
database: AccessDatabase,
|
|
83
98
|
userId: string,
|
|
84
99
|
documentId: string
|
|
85
100
|
): Promise<boolean> {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return (
|
|
99
|
-
document.createdBy === userId ||
|
|
100
|
-
document.workspaceRole === 'admin' ||
|
|
101
|
-
document.workspaceRole === 'editor'
|
|
102
|
-
)
|
|
101
|
+
const grants = await documentAccessGrants(database, userId, documentId)
|
|
102
|
+
return grantsAllow(grants, { document: ['delete'] })
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether the user may restore the document from Trash, or delete it there for good. */
|
|
106
|
+
export async function trashedDocumentAllows(
|
|
107
|
+
database: AccessDatabase,
|
|
108
|
+
userId: string,
|
|
109
|
+
documentId: string,
|
|
110
|
+
action: 'delete' | 'purge'
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
const grants = await documentAccessGrants(database, userId, documentId, 'trash')
|
|
113
|
+
return grantsAllow(grants, { document: [action] })
|
|
103
114
|
}
|
|
@@ -14,3 +14,10 @@ export class DocumentConflictError extends Error {
|
|
|
14
14
|
export class UploadInvalidError extends Error {
|
|
15
15
|
override readonly name = 'UploadInvalidError'
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Destroying a document deleted forever with no grace failed. The failure, its cause, was
|
|
19
|
+
* reported where cleanup reports; the document is out of Trash, and cleanup tries again.
|
|
20
|
+
*/
|
|
21
|
+
export class DocumentPurgeFailedError extends Error {
|
|
22
|
+
override readonly name = 'DocumentPurgeFailedError'
|
|
23
|
+
}
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
RequestImageUploadsInput
|
|
7
7
|
} from '#cloud/contract'
|
|
8
8
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
9
10
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
10
11
|
import type { StorageQuotaService } from '#cloud/server/quota'
|
|
11
12
|
import { uniq, uniqBy } from 'es-toolkit'
|
|
@@ -53,7 +54,7 @@ export function createDocumentImages(
|
|
|
53
54
|
.execute()
|
|
54
55
|
} catch (error) {
|
|
55
56
|
// Cleanup releases it once it expires.
|
|
56
|
-
|
|
57
|
+
logCloud('warn', 'Giving up an image upload failed', { error: loggedError(error) })
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
}
|
|
@@ -4,11 +4,12 @@ import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/doc
|
|
|
4
4
|
import type { DocumentServiceOptions } from '#cloud/server/documents/service'
|
|
5
5
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
6
6
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
7
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
7
8
|
import type { Kysely } from 'kysely'
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
* The document `userId` may write to, and how large one file and
|
|
11
|
-
*
|
|
11
|
+
* The document `userId` may write to, and how large one file and its owner's storage may be for
|
|
12
|
+
* this save: technical limits, tightened by the owner's plan.
|
|
12
13
|
*/
|
|
13
14
|
export async function writableDocument(
|
|
14
15
|
database: Kysely<CloudDatabase>,
|
|
@@ -23,13 +24,12 @@ export async function writableDocument(
|
|
|
23
24
|
const row = await getDocumentSummaryRow(database, documentId)
|
|
24
25
|
if (!row) throw new DocumentNotFoundError()
|
|
25
26
|
const document = documentSummary(row)
|
|
26
|
-
const policyContext =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
27
|
+
const policyContext = await workspacePolicyContext(
|
|
28
|
+
database,
|
|
29
|
+
{ documentId },
|
|
30
|
+
{ actorId: userId, deploymentMode: 'self-hosted' }
|
|
31
|
+
)
|
|
32
|
+
if (!policyContext) throw new DocumentNotFoundError()
|
|
33
33
|
const technicalMaximum = options.technicalMaximumUploadBytes ?? Number.MAX_SAFE_INTEGER
|
|
34
34
|
const maximumFileBytes = Math.min(
|
|
35
35
|
technicalMaximum,
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { documentObjects } from '#cloud/server/cleanup/documents'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { sumBy } from 'es-toolkit'
|
|
4
|
+
import type { Transaction } from 'kysely'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Deleting forever: deleted documents leave everyone's sight, Trash included, and the files only
|
|
8
|
+
* they hold come off their workspaces' usage at once. Their files and rows stay through the
|
|
9
|
+
* deployment's grace, so a server administrator can still bring them back, then cleanup destroys
|
|
10
|
+
* them and takes off only what this did not. `purgedBy` is empty for an account's deletion.
|
|
11
|
+
*/
|
|
12
|
+
export async function markPurged(
|
|
13
|
+
transaction: Transaction<CloudDatabase>,
|
|
14
|
+
documentIds: readonly string[],
|
|
15
|
+
purgedBy: string | null,
|
|
16
|
+
now: Date
|
|
17
|
+
): Promise<void> {
|
|
18
|
+
for (const documentId of documentIds) {
|
|
19
|
+
const document = await transaction
|
|
20
|
+
.selectFrom('document')
|
|
21
|
+
.select('workspaceId')
|
|
22
|
+
.where('id', '=', documentId)
|
|
23
|
+
.where('purgedAt', 'is', null)
|
|
24
|
+
.executeTakeFirst()
|
|
25
|
+
if (!document) continue
|
|
26
|
+
const bytes = sumBy(await documentObjects(transaction, documentId), (object) => object.byteSize)
|
|
27
|
+
await transaction
|
|
28
|
+
.updateTable('document')
|
|
29
|
+
.set({ purgedAt: now, purgedBy, purgedBytes: bytes })
|
|
30
|
+
.where('id', '=', documentId)
|
|
31
|
+
.execute()
|
|
32
|
+
if (bytes > 0) await changeUsage(transaction, document.workspaceId, -bytes)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Brings a document deleted forever back to life in its workspace: it is live again, and the
|
|
38
|
+
* storage deleting it took off counts again.
|
|
39
|
+
*/
|
|
40
|
+
export async function unmarkPurged(
|
|
41
|
+
transaction: Transaction<CloudDatabase>,
|
|
42
|
+
document: { id: string; workspaceId: string; purgedBytes: number }
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
await transaction
|
|
45
|
+
.updateTable('document')
|
|
46
|
+
// Changed now, so devices that deleted it stop hiding it.
|
|
47
|
+
.set({
|
|
48
|
+
deletedAt: null,
|
|
49
|
+
deletedBy: null,
|
|
50
|
+
purgedAt: null,
|
|
51
|
+
purgedBy: null,
|
|
52
|
+
purgedBytes: 0,
|
|
53
|
+
updatedAt: new Date()
|
|
54
|
+
})
|
|
55
|
+
.where('id', '=', document.id)
|
|
56
|
+
.execute()
|
|
57
|
+
if (document.purgedBytes > 0) {
|
|
58
|
+
await changeUsage(transaction, document.workspaceId, document.purgedBytes)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function changeUsage(
|
|
63
|
+
transaction: Transaction<CloudDatabase>,
|
|
64
|
+
workspaceId: string,
|
|
65
|
+
bytes: number
|
|
66
|
+
): Promise<void> {
|
|
67
|
+
await transaction
|
|
68
|
+
.updateTable('workspaceStorageUsage')
|
|
69
|
+
.set({
|
|
70
|
+
committedBytes: (expression) => expression('committedBytes', '+', bytes),
|
|
71
|
+
updatedAt: new Date()
|
|
72
|
+
})
|
|
73
|
+
.where('workspaceId', '=', workspaceId)
|
|
74
|
+
.execute()
|
|
75
|
+
}
|
|
@@ -51,6 +51,7 @@ export async function listDocuments(
|
|
|
51
51
|
])
|
|
52
52
|
.where('workspaceId', '=', workspaceId)
|
|
53
53
|
.where('deletedAt', 'is', null)
|
|
54
|
+
.where('disabledAt', 'is', null)
|
|
54
55
|
.orderBy('updatedAt', 'desc')
|
|
55
56
|
.execute()
|
|
56
57
|
return rows.map(documentSummary)
|
|
@@ -82,6 +83,7 @@ export async function listSharedDocuments(
|
|
|
82
83
|
.where('documentGrant.userId', '=', userId)
|
|
83
84
|
.where('documentGrant.revokedAt', 'is', null)
|
|
84
85
|
.where('document.deletedAt', 'is', null)
|
|
86
|
+
.where('document.disabledAt', 'is', null)
|
|
85
87
|
.where((expression) =>
|
|
86
88
|
expression.not(
|
|
87
89
|
expression.exists(
|