@pairdraw/cloud 0.0.0-stage → 0.16.0
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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1578 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +2008 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1673 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +155 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +700 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1945 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1905 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10715 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +13772 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8856 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8836 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +120 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +26 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +530 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/profiles.ts +79 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/comments.ts +149 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +263 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +210 -0
- package/src/contract/links.ts +108 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/profiles.ts +88 -0
- package/src/contract/relay.ts +370 -0
- package/src/contract/sharing.ts +215 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +13 -0
- package/src/email/render.ts +304 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +125 -0
- package/src/email/templates/document-invitation.ts +39 -0
- package/src/email/templates/enrollment.ts +30 -0
- package/src/email/templates/layout.ts +20 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +161 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +44 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +119 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +129 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +385 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +246 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +54 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/comments.ts +86 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +567 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +190 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_sharing_comments_snapshots_profiles.ts +74 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +399 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +405 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +131 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +607 -0
- package/src/server/documents/summary.ts +36 -0
- package/src/server/documents/types.ts +13 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +396 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +247 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +288 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/profiles/index.ts +12 -0
- package/src/server/profiles/routes.ts +103 -0
- package/src/server/profiles/service.ts +266 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +318 -0
- package/src/server/sharing/continuation.ts +83 -0
- package/src/server/sharing/contracts.ts +82 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +20 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +45 -0
- package/src/server/sharing/index.ts +40 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +42 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { checkColumnIn, CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The picture each account shows, kept by the server so pages never load one from another
|
|
6
|
+
* service: one uploaded by its owner, or the one a linked Google account gave it, copied at sign-in.
|
|
7
|
+
*/
|
|
8
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
9
|
+
await database.schema
|
|
10
|
+
.createTable('user_picture')
|
|
11
|
+
// Better Auth's tables may come after this migration, so the account is not a foreign key.
|
|
12
|
+
.addColumn('user_id', 'uuid', (column) => column.primaryKey())
|
|
13
|
+
.addColumn('bytes', 'bytea', (column) => column.notNull())
|
|
14
|
+
.addColumn('format', 'text', (column) => column.notNull())
|
|
15
|
+
.addColumn('checksum', 'text', (column) => column.notNull())
|
|
16
|
+
.addColumn('source', 'text', (column) => column.notNull())
|
|
17
|
+
.addColumn('created_at', 'timestamptz', (column) =>
|
|
18
|
+
column.notNull().defaultTo(CURRENT_TIMESTAMP)
|
|
19
|
+
)
|
|
20
|
+
.addCheckConstraint('user_picture_format_check', checkColumnIn('format', ['png', 'jpeg']))
|
|
21
|
+
.addCheckConstraint(
|
|
22
|
+
'user_picture_source_check',
|
|
23
|
+
checkColumnIn('source', ['upload', 'provider'])
|
|
24
|
+
)
|
|
25
|
+
.execute()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
29
|
+
await database.schema.dropTable('user_picture').execute()
|
|
30
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addCurrentTimestampColumns, replaceCheckConstraint } from './helpers'
|
|
4
|
+
|
|
5
|
+
const PERMISSION_TABLES = ['document_share', 'document_grant', 'document_invitation'] as const
|
|
6
|
+
type PermissionTables = Record<(typeof PERMISSION_TABLES)[number], { permission: string }>
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Three ways to share more precisely: a "can comment" permission for links, grants, and
|
|
10
|
+
* invitations; snapshot links that show one revision as it was shared; and public profiles that
|
|
11
|
+
* list the workspaces their owner made public.
|
|
12
|
+
*/
|
|
13
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
14
|
+
for (const table of PERMISSION_TABLES) {
|
|
15
|
+
await replaceCheckConstraint(database, table, `${table}_permission_check`, 'permission', [
|
|
16
|
+
'view',
|
|
17
|
+
'comment',
|
|
18
|
+
'edit'
|
|
19
|
+
])
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A snapshot link keeps its revision; the revision going takes the link with it.
|
|
23
|
+
await database.schema
|
|
24
|
+
.alterTable('document_share')
|
|
25
|
+
.addColumn('revision_id', 'uuid', (column) =>
|
|
26
|
+
column.references('document_revision.id').onDelete('cascade')
|
|
27
|
+
)
|
|
28
|
+
.execute()
|
|
29
|
+
await database.schema
|
|
30
|
+
.createIndex('document_share_revision_index')
|
|
31
|
+
.on('document_share')
|
|
32
|
+
.columns(['revision_id', 'revoked_at'])
|
|
33
|
+
.execute()
|
|
34
|
+
|
|
35
|
+
// When the workspace went public, and the administrator whose profile lists it; both null while
|
|
36
|
+
// it is private. Better Auth's tables may come after this migration, so neither is a foreign key.
|
|
37
|
+
await database.schema
|
|
38
|
+
.alterTable('workspace')
|
|
39
|
+
.addColumn('public_at', 'timestamptz')
|
|
40
|
+
.addColumn('public_by', 'uuid')
|
|
41
|
+
.execute()
|
|
42
|
+
|
|
43
|
+
await database.schema
|
|
44
|
+
.createTable('public_profile')
|
|
45
|
+
// Better Auth's tables may come after this migration, so the account is not a foreign key.
|
|
46
|
+
.addColumn('user_id', 'uuid', (column) => column.primaryKey())
|
|
47
|
+
.addColumn('handle', 'text', (column) => column.notNull().unique())
|
|
48
|
+
.addColumn('bio', 'text')
|
|
49
|
+
.execute()
|
|
50
|
+
await addCurrentTimestampColumns(database, 'public_profile', ['created_at', 'updated_at'])
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
54
|
+
await database.schema.dropTable('public_profile').ifExists().execute()
|
|
55
|
+
await database.schema
|
|
56
|
+
.alterTable('workspace')
|
|
57
|
+
.dropColumn('public_by')
|
|
58
|
+
.dropColumn('public_at')
|
|
59
|
+
.execute()
|
|
60
|
+
await database.schema.dropIndex('document_share_revision_index').ifExists().execute()
|
|
61
|
+
await database.schema.alterTable('document_share').dropColumn('revision_id').execute()
|
|
62
|
+
for (const table of PERMISSION_TABLES) {
|
|
63
|
+
// Commenters keep looking; they lose only the comments.
|
|
64
|
+
await (database as Kysely<PermissionTables>)
|
|
65
|
+
.updateTable(table)
|
|
66
|
+
.set({ permission: 'view' })
|
|
67
|
+
.where('permission', '=', 'comment')
|
|
68
|
+
.execute()
|
|
69
|
+
await replaceCheckConstraint(database, table, `${table}_permission_check`, 'permission', [
|
|
70
|
+
'view',
|
|
71
|
+
'edit'
|
|
72
|
+
])
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { checkColumnIn, CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
|
+
import type { AuthSchemaVersionTable } from '#cloud/server/db/tables'
|
|
3
|
+
import type { CreateTableBuilder, Kysely } from 'kysely'
|
|
4
|
+
|
|
5
|
+
export async function addCheckConstraint(
|
|
6
|
+
database: Kysely<unknown>,
|
|
7
|
+
table: string,
|
|
8
|
+
constraint: string,
|
|
9
|
+
column: string,
|
|
10
|
+
values: readonly string[]
|
|
11
|
+
): Promise<void> {
|
|
12
|
+
await database.schema
|
|
13
|
+
.alterTable(table)
|
|
14
|
+
.addCheckConstraint(constraint, checkColumnIn(column, values))
|
|
15
|
+
.execute()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function replaceCheckConstraint(
|
|
19
|
+
database: Kysely<unknown>,
|
|
20
|
+
table: string,
|
|
21
|
+
constraint: string,
|
|
22
|
+
column: string,
|
|
23
|
+
values: readonly string[]
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
await database.schema.alterTable(table).dropConstraint(constraint).execute()
|
|
26
|
+
await addCheckConstraint(database, table, constraint, column, values)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function addCurrentTimestampColumns(
|
|
30
|
+
database: Kysely<unknown>,
|
|
31
|
+
table: string,
|
|
32
|
+
columns: readonly string[]
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
for (const column of columns) {
|
|
35
|
+
await database.schema
|
|
36
|
+
.alterTable(table)
|
|
37
|
+
.addColumn(column, 'timestamptz', (definition) =>
|
|
38
|
+
definition.notNull().defaultTo(CURRENT_TIMESTAMP)
|
|
39
|
+
)
|
|
40
|
+
.execute()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type InvitationColumn =
|
|
45
|
+
| 'email_normalized'
|
|
46
|
+
| 'token_hash'
|
|
47
|
+
| 'invited_by'
|
|
48
|
+
| 'expires_at'
|
|
49
|
+
| 'accepted_at'
|
|
50
|
+
| 'revoked_at'
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* What every emailed invitation keeps: whom it went to, the access it grants (`permission` on a
|
|
54
|
+
* document, `role` in a workspace), its hashed secret, and its lifecycle.
|
|
55
|
+
*/
|
|
56
|
+
export function addInvitationColumns<
|
|
57
|
+
Table extends string,
|
|
58
|
+
Column extends string,
|
|
59
|
+
Access extends 'permission' | 'role'
|
|
60
|
+
>(
|
|
61
|
+
table: CreateTableBuilder<Table, Column>,
|
|
62
|
+
access: Access
|
|
63
|
+
): CreateTableBuilder<Table, Column | InvitationColumn | Access> {
|
|
64
|
+
return table
|
|
65
|
+
.addColumn('email_normalized', 'text', (column) => column.notNull())
|
|
66
|
+
.addColumn(access, 'text', (column) => column.notNull())
|
|
67
|
+
.addColumn('token_hash', 'text', (column) => column.notNull())
|
|
68
|
+
.addColumn('invited_by', 'text', (column) => column.notNull())
|
|
69
|
+
.addColumn('expires_at', 'timestamptz', (column) => column.notNull())
|
|
70
|
+
.addColumn('accepted_at', 'timestamptz')
|
|
71
|
+
.addColumn('revoked_at', 'timestamptz')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The Better Auth schema record under the name it had before `025_table_names`, for the
|
|
76
|
+
* migrations that ran while it was `cloud_auth_schema`; they must keep emitting the same SQL.
|
|
77
|
+
*/
|
|
78
|
+
export type LegacyAuthSchemaDatabase = { cloudAuthSchema: AuthSchemaVersionTable }
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import type { EnrollmentStatus } from '#cloud/admin/enrollment/service'
|
|
2
|
+
import type { CloudSignInMethod, DocumentPermission, WorkspaceRole } from '#cloud/contract'
|
|
3
|
+
import type { TransactionalEmailKind } from '#cloud/email'
|
|
4
|
+
import type { Account, Session } from 'better-auth'
|
|
5
|
+
import type { ColumnType, Generated, Insertable, Selectable, Updateable } from 'kysely'
|
|
6
|
+
|
|
7
|
+
export type TimestampColumn = ColumnType<Date, Date | string | undefined, Date | string | null>
|
|
8
|
+
|
|
9
|
+
export type UploadStatus = 'pending' | 'finalizing' | 'cleaning' | 'committed' | 'abandoned'
|
|
10
|
+
|
|
11
|
+
interface DocumentShareTable {
|
|
12
|
+
id: string
|
|
13
|
+
documentId: string
|
|
14
|
+
permission: DocumentPermission
|
|
15
|
+
secretHash: string
|
|
16
|
+
roomEpoch: Generated<number>
|
|
17
|
+
createdBy: string
|
|
18
|
+
createdAt: TimestampColumn
|
|
19
|
+
updatedAt: TimestampColumn
|
|
20
|
+
expiresAt: TimestampColumn | null
|
|
21
|
+
revokedAt: TimestampColumn | null
|
|
22
|
+
lastUsedAt: TimestampColumn | null
|
|
23
|
+
/** The revision a snapshot link shows; null for a live link (migration 027). */
|
|
24
|
+
revisionId: ColumnType<string | null, string | null | undefined, string | null>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface DocumentGrantTable {
|
|
28
|
+
id: string
|
|
29
|
+
documentId: string
|
|
30
|
+
userId: string
|
|
31
|
+
permission: DocumentPermission
|
|
32
|
+
createdBy: string
|
|
33
|
+
createdAt: TimestampColumn
|
|
34
|
+
updatedAt: TimestampColumn
|
|
35
|
+
revokedAt: TimestampColumn | null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface DocumentInvitationTable {
|
|
39
|
+
id: string
|
|
40
|
+
documentId: string
|
|
41
|
+
emailNormalized: string
|
|
42
|
+
permission: DocumentPermission
|
|
43
|
+
tokenHash: string
|
|
44
|
+
invitedBy: string
|
|
45
|
+
invitedAt: TimestampColumn
|
|
46
|
+
expiresAt: TimestampColumn
|
|
47
|
+
acceptedAt: TimestampColumn | null
|
|
48
|
+
revokedAt: TimestampColumn | null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface WorkspaceTable {
|
|
52
|
+
id: string
|
|
53
|
+
name: string
|
|
54
|
+
slug: string
|
|
55
|
+
createdBy: string
|
|
56
|
+
createdAt: TimestampColumn
|
|
57
|
+
updatedAt: TimestampColumn
|
|
58
|
+
/** When the workspace went public (migration 027). */
|
|
59
|
+
publicAt: ColumnType<Date | null, Date | string | null | undefined, Date | string | null>
|
|
60
|
+
/** The administrator who made it public, whose profile alone lists it (migration 027). */
|
|
61
|
+
publicBy: ColumnType<string | null, string | null | undefined, string | null>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A person's public profile, `/u/<handle>` (migration 027). */
|
|
65
|
+
interface PublicProfileTable {
|
|
66
|
+
userId: string
|
|
67
|
+
handle: string
|
|
68
|
+
bio: string | null
|
|
69
|
+
createdAt: TimestampColumn
|
|
70
|
+
updatedAt: TimestampColumn
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface WorkspaceMemberTable {
|
|
74
|
+
workspaceId: string
|
|
75
|
+
userId: string
|
|
76
|
+
role: WorkspaceRole
|
|
77
|
+
createdAt: TimestampColumn
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface WorkspaceInvitationTable {
|
|
81
|
+
id: string
|
|
82
|
+
workspaceId: string
|
|
83
|
+
emailNormalized: string
|
|
84
|
+
role: WorkspaceRole
|
|
85
|
+
tokenHash: string
|
|
86
|
+
invitedBy: string
|
|
87
|
+
invitedAt: TimestampColumn
|
|
88
|
+
expiresAt: TimestampColumn
|
|
89
|
+
acceptedAt: TimestampColumn | null
|
|
90
|
+
revokedAt: TimestampColumn | null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface DocumentTable {
|
|
94
|
+
id: string
|
|
95
|
+
workspaceId: string
|
|
96
|
+
name: string
|
|
97
|
+
currentRevisionId: string | null
|
|
98
|
+
collaborationEpoch: Generated<number>
|
|
99
|
+
version: Generated<number>
|
|
100
|
+
createdBy: string
|
|
101
|
+
createdAt: TimestampColumn
|
|
102
|
+
updatedAt: TimestampColumn
|
|
103
|
+
deletedAt: TimestampColumn | null
|
|
104
|
+
cleanupClaimId: Generated<string | null>
|
|
105
|
+
cleanupClaimedAt: TimestampColumn | null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface StorageObjectTable {
|
|
109
|
+
id: string
|
|
110
|
+
objectKey: string
|
|
111
|
+
checksum: string
|
|
112
|
+
byteSize: number
|
|
113
|
+
contentType: string
|
|
114
|
+
createdAt: TimestampColumn
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface DocumentRevisionTable {
|
|
118
|
+
id: string
|
|
119
|
+
documentId: string
|
|
120
|
+
parentRevisionId: string | null
|
|
121
|
+
storageObjectId: string
|
|
122
|
+
createdBy: string
|
|
123
|
+
createdAt: TimestampColumn
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** An image a workspace's documents use, stored once; see migration 023. */
|
|
127
|
+
interface WorkspaceImageTable {
|
|
128
|
+
workspaceId: string
|
|
129
|
+
/** The SHA-256 of the bytes, which the object store verified. */
|
|
130
|
+
checksum: string
|
|
131
|
+
storageObjectId: string
|
|
132
|
+
/** When a save last asked for it; an image no revision uses is released a while after. */
|
|
133
|
+
touchedAt: TimestampColumn
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** An image a stored revision uses, under the name its file gives it. */
|
|
137
|
+
interface RevisionImageTable {
|
|
138
|
+
revisionId: string
|
|
139
|
+
name: string
|
|
140
|
+
workspaceId: string
|
|
141
|
+
checksum: string
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** A revision kept in the document's history; see migration 021. */
|
|
145
|
+
interface DocumentVersionTable {
|
|
146
|
+
id: string
|
|
147
|
+
documentId: string
|
|
148
|
+
revisionId: string
|
|
149
|
+
kind: 'checkpoint' | 'named'
|
|
150
|
+
name: string | null
|
|
151
|
+
description: string | null
|
|
152
|
+
createdBy: string
|
|
153
|
+
restoredFromVersionId: string | null
|
|
154
|
+
createdAt: TimestampColumn
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
type UploadKind = 'revision' | 'image'
|
|
158
|
+
|
|
159
|
+
interface UploadTable {
|
|
160
|
+
id: string
|
|
161
|
+
/** An image upload names the document it was asked for, which decides who may make it. */
|
|
162
|
+
documentId: string
|
|
163
|
+
kind: Generated<UploadKind>
|
|
164
|
+
baseRevisionId: string | null
|
|
165
|
+
objectKey: string
|
|
166
|
+
checksum: string
|
|
167
|
+
byteSize: number
|
|
168
|
+
contentType: string
|
|
169
|
+
multipartUploadId: string | null
|
|
170
|
+
status: UploadStatus
|
|
171
|
+
finalizationStartedAt: TimestampColumn | null
|
|
172
|
+
cleanupClaimId: Generated<string | null>
|
|
173
|
+
cleanupClaimedAt: TimestampColumn | null
|
|
174
|
+
createdBy: string
|
|
175
|
+
createdAt: TimestampColumn
|
|
176
|
+
expiresAt: TimestampColumn
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
interface AuthUserTable {
|
|
180
|
+
id: string
|
|
181
|
+
name: string
|
|
182
|
+
email: string
|
|
183
|
+
emailVerified: boolean
|
|
184
|
+
image: string | null
|
|
185
|
+
role: Generated<string | null>
|
|
186
|
+
banned: Generated<boolean | null>
|
|
187
|
+
banReason: Generated<string | null>
|
|
188
|
+
banExpires: TimestampColumn | null
|
|
189
|
+
/** Unset on accounts that never turned two-step sign-in on. */
|
|
190
|
+
twoFactorEnabled: Generated<boolean | null>
|
|
191
|
+
createdAt: TimestampColumn
|
|
192
|
+
updatedAt: TimestampColumn
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
interface TwoFactorTable {
|
|
196
|
+
id: string
|
|
197
|
+
secret: string
|
|
198
|
+
backupCodes: string
|
|
199
|
+
userId: string
|
|
200
|
+
verified: Generated<boolean>
|
|
201
|
+
failedVerificationCount: Generated<number>
|
|
202
|
+
lockedUntil: TimestampColumn | null
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
interface PasskeyTable {
|
|
206
|
+
id: string
|
|
207
|
+
name: string | null
|
|
208
|
+
publicKey: string
|
|
209
|
+
userId: string
|
|
210
|
+
credentialID: string
|
|
211
|
+
counter: number
|
|
212
|
+
deviceType: string
|
|
213
|
+
backedUp: boolean
|
|
214
|
+
transports: string | null
|
|
215
|
+
createdAt: TimestampColumn
|
|
216
|
+
aaguid: string | null
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
interface MFAAssuranceTable {
|
|
220
|
+
sessionId: string
|
|
221
|
+
userId: string
|
|
222
|
+
method: 'totp' | 'recovery-code' | 'passkey'
|
|
223
|
+
verifiedAt: TimestampColumn
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
interface SignInTable {
|
|
227
|
+
id: string
|
|
228
|
+
userId: string
|
|
229
|
+
sessionId: string | null
|
|
230
|
+
method: CloudSignInMethod
|
|
231
|
+
ipAddress: string | null
|
|
232
|
+
userAgent: string | null
|
|
233
|
+
createdAt: TimestampColumn
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export type UserPictureFormat = 'png' | 'jpeg'
|
|
237
|
+
|
|
238
|
+
/** The picture an account shows: uploaded by its owner, or copied from a linked account. */
|
|
239
|
+
interface UserPictureTable {
|
|
240
|
+
userId: string
|
|
241
|
+
bytes: Uint8Array
|
|
242
|
+
format: UserPictureFormat
|
|
243
|
+
/** The SHA-256 of the bytes, hex, which names this picture in its address. */
|
|
244
|
+
checksum: string
|
|
245
|
+
source: 'upload' | 'provider'
|
|
246
|
+
createdAt: TimestampColumn
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
interface DocumentCollaborationStateTable {
|
|
250
|
+
documentId: string
|
|
251
|
+
roomEpoch: number
|
|
252
|
+
state: Uint8Array
|
|
253
|
+
version: Generated<number>
|
|
254
|
+
createdAt: TimestampColumn
|
|
255
|
+
updatedAt: TimestampColumn
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface InvitationContinuationTable {
|
|
259
|
+
id: string
|
|
260
|
+
invitationId: string
|
|
261
|
+
tokenEncrypted: string
|
|
262
|
+
expiresAt: TimestampColumn
|
|
263
|
+
consumedAt: TimestampColumn | null
|
|
264
|
+
createdAt: TimestampColumn
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface WorkspaceEntitlementTable {
|
|
268
|
+
workspaceId: string
|
|
269
|
+
values: unknown
|
|
270
|
+
source: string
|
|
271
|
+
revision: Generated<number>
|
|
272
|
+
/** Set only by versioned writes; see `setVersioned`. */
|
|
273
|
+
externalVersion: number | null
|
|
274
|
+
updatedAt: TimestampColumn
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
interface WorkspaceStorageUsageTable {
|
|
278
|
+
workspaceId: string
|
|
279
|
+
committedBytes: number
|
|
280
|
+
updatedAt: TimestampColumn
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
interface UploadStorageReservationTable {
|
|
284
|
+
id: string
|
|
285
|
+
workspaceId: string
|
|
286
|
+
uploadId: string
|
|
287
|
+
bytes: number
|
|
288
|
+
expiresAt: TimestampColumn
|
|
289
|
+
committedAt: TimestampColumn | null
|
|
290
|
+
releasedAt: TimestampColumn | null
|
|
291
|
+
createdAt: TimestampColumn
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export type TransactionalEmailStatus = 'pending' | 'sending' | 'accepted' | 'failed' | 'suppressed'
|
|
295
|
+
|
|
296
|
+
interface TransactionalEmailTable {
|
|
297
|
+
id: string
|
|
298
|
+
idempotencyKey: string
|
|
299
|
+
kind: TransactionalEmailKind
|
|
300
|
+
recipientEmailNormalized: string
|
|
301
|
+
payloadEncrypted: string | null
|
|
302
|
+
status: TransactionalEmailStatus
|
|
303
|
+
attemptCount: Generated<number>
|
|
304
|
+
nextAttemptAt: TimestampColumn
|
|
305
|
+
claimId: string | null
|
|
306
|
+
claimedAt: TimestampColumn | null
|
|
307
|
+
transport: string | null
|
|
308
|
+
transportMessageId: string | null
|
|
309
|
+
lastErrorCode: string | null
|
|
310
|
+
createdAt: TimestampColumn
|
|
311
|
+
updatedAt: TimestampColumn
|
|
312
|
+
acceptedAt: TimestampColumn | null
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
interface EnrollmentTable {
|
|
316
|
+
id: string
|
|
317
|
+
emailNormalized: string
|
|
318
|
+
name: string | null
|
|
319
|
+
reason: string | null
|
|
320
|
+
status: EnrollmentStatus
|
|
321
|
+
requestedAt: TimestampColumn
|
|
322
|
+
reviewedAt: TimestampColumn | null
|
|
323
|
+
reviewedBy: string | null
|
|
324
|
+
reviewNote: string | null
|
|
325
|
+
requestRevision: Generated<number>
|
|
326
|
+
approvedUserId: string | null
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
interface APIRateLimitTable {
|
|
330
|
+
keyHash: string
|
|
331
|
+
windowStartedAt: TimestampColumn
|
|
332
|
+
requestCount: Generated<number>
|
|
333
|
+
updatedAt: TimestampColumn
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
interface AdminAuditEventTable {
|
|
337
|
+
id: string
|
|
338
|
+
actorUserId: string
|
|
339
|
+
action: string
|
|
340
|
+
subjectType: string
|
|
341
|
+
subjectId: string
|
|
342
|
+
metadata: unknown
|
|
343
|
+
createdAt: TimestampColumn
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export interface AuthSchemaVersionTable {
|
|
347
|
+
id: string
|
|
348
|
+
version: string
|
|
349
|
+
updatedAt: TimestampColumn
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export interface CloudDatabase {
|
|
353
|
+
account: Account
|
|
354
|
+
session: Session
|
|
355
|
+
twoFactor: TwoFactorTable
|
|
356
|
+
passkey: PasskeyTable
|
|
357
|
+
mfaAssurance: MFAAssuranceTable
|
|
358
|
+
signIn: SignInTable
|
|
359
|
+
userPicture: UserPictureTable
|
|
360
|
+
user: AuthUserTable
|
|
361
|
+
authSchemaVersion: AuthSchemaVersionTable
|
|
362
|
+
enrollment: EnrollmentTable
|
|
363
|
+
apiRateLimit: APIRateLimitTable
|
|
364
|
+
adminAuditEvent: AdminAuditEventTable
|
|
365
|
+
workspace: WorkspaceTable
|
|
366
|
+
workspaceMember: WorkspaceMemberTable
|
|
367
|
+
workspaceInvitation: WorkspaceInvitationTable
|
|
368
|
+
document: DocumentTable
|
|
369
|
+
storageObject: StorageObjectTable
|
|
370
|
+
documentRevision: DocumentRevisionTable
|
|
371
|
+
revisionImage: RevisionImageTable
|
|
372
|
+
workspaceImage: WorkspaceImageTable
|
|
373
|
+
documentVersion: DocumentVersionTable
|
|
374
|
+
documentShare: DocumentShareTable
|
|
375
|
+
publicProfile: PublicProfileTable
|
|
376
|
+
documentGrant: DocumentGrantTable
|
|
377
|
+
documentInvitation: DocumentInvitationTable
|
|
378
|
+
documentCollaborationState: DocumentCollaborationStateTable
|
|
379
|
+
invitationContinuation: InvitationContinuationTable
|
|
380
|
+
workspaceEntitlement: WorkspaceEntitlementTable
|
|
381
|
+
workspaceStorageUsage: WorkspaceStorageUsageTable
|
|
382
|
+
transactionalEmail: TransactionalEmailTable
|
|
383
|
+
uploadStorageReservation: UploadStorageReservationTable
|
|
384
|
+
upload: UploadTable
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export type NewTransactionalEmail = Insertable<TransactionalEmailTable>
|
|
388
|
+
export type TransactionalEmail = Selectable<TransactionalEmailTable>
|
|
389
|
+
export type Workspace = Selectable<WorkspaceTable>
|
|
390
|
+
export type NewWorkspace = Insertable<WorkspaceTable>
|
|
391
|
+
export type WorkspaceUpdate = Updateable<WorkspaceTable>
|
|
392
|
+
export type Document = Selectable<DocumentTable>
|
|
393
|
+
export type NewDocument = Insertable<DocumentTable>
|
|
394
|
+
export type DocumentRevision = Selectable<DocumentRevisionTable>
|
|
395
|
+
export type NewDocumentRevision = Insertable<DocumentRevisionTable>
|
|
396
|
+
export type StorageObject = Selectable<StorageObjectTable>
|
|
397
|
+
export type NewStorageObject = Insertable<StorageObjectTable>
|
|
398
|
+
export type Upload = Selectable<UploadTable>
|
|
399
|
+
export type NewUpload = Insertable<UploadTable>
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DocumentAccess,
|
|
3
|
+
DocumentAccessSource,
|
|
4
|
+
DocumentPermission,
|
|
5
|
+
WorkspaceRole
|
|
6
|
+
} from '#cloud/contract'
|
|
7
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
8
|
+
import { maxBy } from 'es-toolkit'
|
|
9
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
10
|
+
|
|
11
|
+
export type AccessDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
12
|
+
|
|
13
|
+
const PERMISSION_WEIGHT: Record<DocumentPermission, number> = { view: 1, comment: 2, edit: 3 }
|
|
14
|
+
|
|
15
|
+
function workspacePermission(role: WorkspaceRole): DocumentPermission {
|
|
16
|
+
return role === 'viewer' ? 'view' : 'edit'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function resolveDocumentAccess(
|
|
20
|
+
database: AccessDatabase,
|
|
21
|
+
userId: string,
|
|
22
|
+
documentId: string
|
|
23
|
+
): Promise<DocumentAccess | undefined> {
|
|
24
|
+
const document = await database
|
|
25
|
+
.selectFrom('document')
|
|
26
|
+
.leftJoin('workspaceMember', (join) =>
|
|
27
|
+
join
|
|
28
|
+
.onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
|
|
29
|
+
.on('workspaceMember.userId', '=', userId)
|
|
30
|
+
)
|
|
31
|
+
.leftJoin('documentGrant', (join) =>
|
|
32
|
+
join
|
|
33
|
+
.onRef('documentGrant.documentId', '=', 'document.id')
|
|
34
|
+
.on('documentGrant.userId', '=', userId)
|
|
35
|
+
.on('documentGrant.revokedAt', 'is', null)
|
|
36
|
+
)
|
|
37
|
+
.select([
|
|
38
|
+
'document.createdBy',
|
|
39
|
+
'workspaceMember.role as workspaceRole',
|
|
40
|
+
'documentGrant.permission as grantPermission'
|
|
41
|
+
])
|
|
42
|
+
.where('document.id', '=', documentId)
|
|
43
|
+
.where('document.deletedAt', 'is', null)
|
|
44
|
+
.executeTakeFirst()
|
|
45
|
+
if (!document) return undefined
|
|
46
|
+
|
|
47
|
+
const sources: DocumentAccessSource[] = []
|
|
48
|
+
const candidates: DocumentPermission[] = []
|
|
49
|
+
const owner = document.createdBy === userId
|
|
50
|
+
if (owner) {
|
|
51
|
+
sources.push('owner')
|
|
52
|
+
candidates.push('edit')
|
|
53
|
+
}
|
|
54
|
+
if (document.workspaceRole) {
|
|
55
|
+
sources.push('workspace')
|
|
56
|
+
candidates.push(workspacePermission(document.workspaceRole))
|
|
57
|
+
}
|
|
58
|
+
if (document.grantPermission) {
|
|
59
|
+
sources.push('direct-grant')
|
|
60
|
+
candidates.push(document.grantPermission)
|
|
61
|
+
}
|
|
62
|
+
const permission = maxBy(candidates, (candidate) => PERMISSION_WEIGHT[candidate])
|
|
63
|
+
if (!permission) return undefined
|
|
64
|
+
const workspaceCanManage =
|
|
65
|
+
document.workspaceRole === 'admin' || document.workspaceRole === 'editor'
|
|
66
|
+
return {
|
|
67
|
+
permission,
|
|
68
|
+
canManageSharing: owner || workspaceCanManage || document.grantPermission === 'edit',
|
|
69
|
+
sources
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function canEditDocument(access: DocumentAccess): boolean {
|
|
74
|
+
return access.permission === 'edit'
|
|
75
|
+
}
|
|
76
|
+
|
|
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
|
+
*/
|
|
81
|
+
export async function canDeleteDocument(
|
|
82
|
+
database: AccessDatabase,
|
|
83
|
+
userId: string,
|
|
84
|
+
documentId: string
|
|
85
|
+
): Promise<boolean> {
|
|
86
|
+
const document = await database
|
|
87
|
+
.selectFrom('document')
|
|
88
|
+
.leftJoin('workspaceMember', (join) =>
|
|
89
|
+
join
|
|
90
|
+
.onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
|
|
91
|
+
.on('workspaceMember.userId', '=', userId)
|
|
92
|
+
)
|
|
93
|
+
.select(['document.createdBy', 'workspaceMember.role as workspaceRole'])
|
|
94
|
+
.where('document.id', '=', documentId)
|
|
95
|
+
.where('document.deletedAt', 'is', null)
|
|
96
|
+
.executeTakeFirst()
|
|
97
|
+
if (!document) return false
|
|
98
|
+
return (
|
|
99
|
+
document.createdBy === userId ||
|
|
100
|
+
document.workspaceRole === 'admin' ||
|
|
101
|
+
document.workspaceRole === 'editor'
|
|
102
|
+
)
|
|
103
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The document does not exist, or the person may not know it does. */
|
|
2
|
+
export class DocumentNotFoundError extends Error {
|
|
3
|
+
override readonly name = 'DocumentNotFoundError'
|
|
4
|
+
}
|
|
5
|
+
/** The person may see the document but not do this to it. */
|
|
6
|
+
export class DocumentForbiddenError extends Error {
|
|
7
|
+
override readonly name = 'DocumentForbiddenError'
|
|
8
|
+
}
|
|
9
|
+
/** A save named a base revision the document has moved on from. */
|
|
10
|
+
export class DocumentConflictError extends Error {
|
|
11
|
+
override readonly name = 'DocumentConflictError'
|
|
12
|
+
}
|
|
13
|
+
/** An upload is not the file it declared, or no longer one that can land. */
|
|
14
|
+
export class UploadInvalidError extends Error {
|
|
15
|
+
override readonly name = 'UploadInvalidError'
|
|
16
|
+
}
|