@open-pencil/cloud 0.0.0-stage → 0.17.0-beta.1
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 +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -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 +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -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 +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -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 +28 -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 +528 -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/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/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -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 +160 -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 +43 -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 +121 -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 +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -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 +232 -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 +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -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 +188 -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_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -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 +397 -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 +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -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/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 +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -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,19 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addCheckConstraint } from './helpers'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whether each document is a Figma design or a FigJam board, so lists can tell them apart
|
|
7
|
+
* without opening the file. Documents stored before this are designs; a board's next save says so.
|
|
8
|
+
*/
|
|
9
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
10
|
+
await database.schema
|
|
11
|
+
.alterTable('document')
|
|
12
|
+
.addColumn('kind', 'text', (column) => column.notNull().defaultTo('design'))
|
|
13
|
+
.execute()
|
|
14
|
+
await addCheckConstraint(database, 'document', 'document_kind_check', 'kind', ['design', 'board'])
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
18
|
+
await database.schema.alterTable('document').dropColumn('kind').execute()
|
|
19
|
+
}
|
|
@@ -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,394 @@
|
|
|
1
|
+
import type { EnrollmentStatus } from '#cloud/admin/enrollment/service'
|
|
2
|
+
import type { CloudSignInMethod, DocumentPermission, WorkspaceRole } from '#cloud/contract'
|
|
3
|
+
import type { TransactionalEmailKind, TransactionalEmailPayloadByKind } 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
|
+
export 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
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DocumentGrantTable {
|
|
26
|
+
id: string
|
|
27
|
+
documentId: string
|
|
28
|
+
userId: string
|
|
29
|
+
permission: DocumentPermission
|
|
30
|
+
createdBy: string
|
|
31
|
+
createdAt: TimestampColumn
|
|
32
|
+
updatedAt: TimestampColumn
|
|
33
|
+
revokedAt: TimestampColumn | null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface DocumentInvitationTable {
|
|
37
|
+
id: string
|
|
38
|
+
documentId: string
|
|
39
|
+
emailNormalized: string
|
|
40
|
+
permission: DocumentPermission
|
|
41
|
+
tokenHash: string
|
|
42
|
+
invitedBy: string
|
|
43
|
+
invitedAt: TimestampColumn
|
|
44
|
+
expiresAt: TimestampColumn
|
|
45
|
+
acceptedAt: TimestampColumn | null
|
|
46
|
+
revokedAt: TimestampColumn | null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WorkspaceTable {
|
|
50
|
+
id: string
|
|
51
|
+
name: string
|
|
52
|
+
slug: string
|
|
53
|
+
createdBy: string
|
|
54
|
+
createdAt: TimestampColumn
|
|
55
|
+
updatedAt: TimestampColumn
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface WorkspaceMemberTable {
|
|
59
|
+
workspaceId: string
|
|
60
|
+
userId: string
|
|
61
|
+
role: WorkspaceRole
|
|
62
|
+
createdAt: TimestampColumn
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface WorkspaceInvitationTable {
|
|
66
|
+
id: string
|
|
67
|
+
workspaceId: string
|
|
68
|
+
emailNormalized: string
|
|
69
|
+
role: WorkspaceRole
|
|
70
|
+
tokenHash: string
|
|
71
|
+
invitedBy: string
|
|
72
|
+
invitedAt: TimestampColumn
|
|
73
|
+
expiresAt: TimestampColumn
|
|
74
|
+
acceptedAt: TimestampColumn | null
|
|
75
|
+
revokedAt: TimestampColumn | null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface DocumentTable {
|
|
79
|
+
id: string
|
|
80
|
+
workspaceId: string
|
|
81
|
+
name: string
|
|
82
|
+
/** A design or a board; see migration 027. */
|
|
83
|
+
kind: Generated<'design' | 'board'>
|
|
84
|
+
currentRevisionId: string | null
|
|
85
|
+
collaborationEpoch: Generated<number>
|
|
86
|
+
version: Generated<number>
|
|
87
|
+
createdBy: string
|
|
88
|
+
createdAt: TimestampColumn
|
|
89
|
+
updatedAt: TimestampColumn
|
|
90
|
+
deletedAt: TimestampColumn | null
|
|
91
|
+
cleanupClaimId: Generated<string | null>
|
|
92
|
+
cleanupClaimedAt: TimestampColumn | null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface StorageObjectTable {
|
|
96
|
+
id: string
|
|
97
|
+
objectKey: string
|
|
98
|
+
checksum: string
|
|
99
|
+
byteSize: number
|
|
100
|
+
contentType: string
|
|
101
|
+
createdAt: TimestampColumn
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface DocumentRevisionTable {
|
|
105
|
+
id: string
|
|
106
|
+
documentId: string
|
|
107
|
+
parentRevisionId: string | null
|
|
108
|
+
storageObjectId: string
|
|
109
|
+
createdBy: string
|
|
110
|
+
createdAt: TimestampColumn
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** An image a workspace's documents use, stored once; see migration 023. */
|
|
114
|
+
export interface WorkspaceImageTable {
|
|
115
|
+
workspaceId: string
|
|
116
|
+
/** The SHA-256 of the bytes, which the object store verified. */
|
|
117
|
+
checksum: string
|
|
118
|
+
storageObjectId: string
|
|
119
|
+
/** When a save last asked for it; an image no revision uses is released a while after. */
|
|
120
|
+
touchedAt: TimestampColumn
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** An image a stored revision uses, under the name its file gives it. */
|
|
124
|
+
export interface RevisionImageTable {
|
|
125
|
+
revisionId: string
|
|
126
|
+
name: string
|
|
127
|
+
workspaceId: string
|
|
128
|
+
checksum: string
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** A revision kept in the document's history; see migration 021. */
|
|
132
|
+
export interface DocumentVersionTable {
|
|
133
|
+
id: string
|
|
134
|
+
documentId: string
|
|
135
|
+
revisionId: string
|
|
136
|
+
kind: 'checkpoint' | 'named'
|
|
137
|
+
name: string | null
|
|
138
|
+
description: string | null
|
|
139
|
+
createdBy: string
|
|
140
|
+
restoredFromVersionId: string | null
|
|
141
|
+
createdAt: TimestampColumn
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type UploadKind = 'revision' | 'image'
|
|
145
|
+
|
|
146
|
+
export interface UploadTable {
|
|
147
|
+
id: string
|
|
148
|
+
/** An image upload names the document it was asked for, which decides who may make it. */
|
|
149
|
+
documentId: string
|
|
150
|
+
kind: Generated<UploadKind>
|
|
151
|
+
baseRevisionId: string | null
|
|
152
|
+
objectKey: string
|
|
153
|
+
checksum: string
|
|
154
|
+
byteSize: number
|
|
155
|
+
contentType: string
|
|
156
|
+
multipartUploadId: string | null
|
|
157
|
+
status: UploadStatus
|
|
158
|
+
finalizationStartedAt: TimestampColumn | null
|
|
159
|
+
cleanupClaimId: Generated<string | null>
|
|
160
|
+
cleanupClaimedAt: TimestampColumn | null
|
|
161
|
+
createdBy: string
|
|
162
|
+
createdAt: TimestampColumn
|
|
163
|
+
expiresAt: TimestampColumn
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface AuthUserTable {
|
|
167
|
+
id: string
|
|
168
|
+
name: string
|
|
169
|
+
email: string
|
|
170
|
+
emailVerified: boolean
|
|
171
|
+
image: string | null
|
|
172
|
+
role: Generated<string | null>
|
|
173
|
+
banned: Generated<boolean | null>
|
|
174
|
+
banReason: Generated<string | null>
|
|
175
|
+
banExpires: TimestampColumn | null
|
|
176
|
+
/** Unset on accounts that never turned two-step sign-in on. */
|
|
177
|
+
twoFactorEnabled: Generated<boolean | null>
|
|
178
|
+
createdAt: TimestampColumn
|
|
179
|
+
updatedAt: TimestampColumn
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface TwoFactorTable {
|
|
183
|
+
id: string
|
|
184
|
+
secret: string
|
|
185
|
+
backupCodes: string
|
|
186
|
+
userId: string
|
|
187
|
+
verified: Generated<boolean>
|
|
188
|
+
failedVerificationCount: Generated<number>
|
|
189
|
+
lockedUntil: TimestampColumn | null
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface PasskeyTable {
|
|
193
|
+
id: string
|
|
194
|
+
name: string | null
|
|
195
|
+
publicKey: string
|
|
196
|
+
userId: string
|
|
197
|
+
credentialID: string
|
|
198
|
+
counter: number
|
|
199
|
+
deviceType: string
|
|
200
|
+
backedUp: boolean
|
|
201
|
+
transports: string | null
|
|
202
|
+
createdAt: TimestampColumn
|
|
203
|
+
aaguid: string | null
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface MFAAssuranceTable {
|
|
207
|
+
sessionId: string
|
|
208
|
+
userId: string
|
|
209
|
+
method: 'totp' | 'recovery-code' | 'passkey'
|
|
210
|
+
verifiedAt: TimestampColumn
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface SignInTable {
|
|
214
|
+
id: string
|
|
215
|
+
userId: string
|
|
216
|
+
sessionId: string | null
|
|
217
|
+
method: CloudSignInMethod
|
|
218
|
+
ipAddress: string | null
|
|
219
|
+
userAgent: string | null
|
|
220
|
+
createdAt: TimestampColumn
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export type UserPictureFormat = 'png' | 'jpeg'
|
|
224
|
+
|
|
225
|
+
/** The picture an account shows: uploaded by its owner, or copied from a linked account. */
|
|
226
|
+
export interface UserPictureTable {
|
|
227
|
+
userId: string
|
|
228
|
+
bytes: Uint8Array
|
|
229
|
+
format: UserPictureFormat
|
|
230
|
+
/** The SHA-256 of the bytes, hex, which names this picture in its address. */
|
|
231
|
+
checksum: string
|
|
232
|
+
source: 'upload' | 'provider'
|
|
233
|
+
createdAt: TimestampColumn
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface DocumentCollaborationStateTable {
|
|
237
|
+
documentId: string
|
|
238
|
+
roomEpoch: number
|
|
239
|
+
state: Uint8Array
|
|
240
|
+
version: Generated<number>
|
|
241
|
+
createdAt: TimestampColumn
|
|
242
|
+
updatedAt: TimestampColumn
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export interface InvitationContinuationTable {
|
|
246
|
+
id: string
|
|
247
|
+
invitationId: string
|
|
248
|
+
tokenEncrypted: string
|
|
249
|
+
expiresAt: TimestampColumn
|
|
250
|
+
consumedAt: TimestampColumn | null
|
|
251
|
+
createdAt: TimestampColumn
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface WorkspaceEntitlementTable {
|
|
255
|
+
workspaceId: string
|
|
256
|
+
values: unknown
|
|
257
|
+
source: string
|
|
258
|
+
revision: Generated<number>
|
|
259
|
+
/** Set only by versioned writes; see `setVersioned`. */
|
|
260
|
+
externalVersion: number | null
|
|
261
|
+
updatedAt: TimestampColumn
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface WorkspaceStorageUsageTable {
|
|
265
|
+
workspaceId: string
|
|
266
|
+
committedBytes: number
|
|
267
|
+
updatedAt: TimestampColumn
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export interface UploadStorageReservationTable {
|
|
271
|
+
id: string
|
|
272
|
+
workspaceId: string
|
|
273
|
+
uploadId: string
|
|
274
|
+
bytes: number
|
|
275
|
+
expiresAt: TimestampColumn
|
|
276
|
+
committedAt: TimestampColumn | null
|
|
277
|
+
releasedAt: TimestampColumn | null
|
|
278
|
+
createdAt: TimestampColumn
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export type TransactionalEmailStatus = 'pending' | 'sending' | 'accepted' | 'failed' | 'suppressed'
|
|
282
|
+
|
|
283
|
+
export interface TransactionalEmailTable {
|
|
284
|
+
id: string
|
|
285
|
+
idempotencyKey: string
|
|
286
|
+
kind: TransactionalEmailKind
|
|
287
|
+
recipientEmailNormalized: string
|
|
288
|
+
payloadEncrypted: string | null
|
|
289
|
+
status: TransactionalEmailStatus
|
|
290
|
+
attemptCount: Generated<number>
|
|
291
|
+
nextAttemptAt: TimestampColumn
|
|
292
|
+
claimId: string | null
|
|
293
|
+
claimedAt: TimestampColumn | null
|
|
294
|
+
transport: string | null
|
|
295
|
+
transportMessageId: string | null
|
|
296
|
+
lastErrorCode: string | null
|
|
297
|
+
createdAt: TimestampColumn
|
|
298
|
+
updatedAt: TimestampColumn
|
|
299
|
+
acceptedAt: TimestampColumn | null
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export type TransactionalEmailPayload = TransactionalEmailPayloadByKind[TransactionalEmailKind]
|
|
303
|
+
|
|
304
|
+
export interface EnrollmentTable {
|
|
305
|
+
id: string
|
|
306
|
+
emailNormalized: string
|
|
307
|
+
name: string | null
|
|
308
|
+
reason: string | null
|
|
309
|
+
status: EnrollmentStatus
|
|
310
|
+
requestedAt: TimestampColumn
|
|
311
|
+
reviewedAt: TimestampColumn | null
|
|
312
|
+
reviewedBy: string | null
|
|
313
|
+
reviewNote: string | null
|
|
314
|
+
requestRevision: Generated<number>
|
|
315
|
+
approvedUserId: string | null
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export interface APIRateLimitTable {
|
|
319
|
+
keyHash: string
|
|
320
|
+
windowStartedAt: TimestampColumn
|
|
321
|
+
requestCount: Generated<number>
|
|
322
|
+
updatedAt: TimestampColumn
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export interface AdminAuditEventTable {
|
|
326
|
+
id: string
|
|
327
|
+
actorUserId: string
|
|
328
|
+
action: string
|
|
329
|
+
subjectType: string
|
|
330
|
+
subjectId: string
|
|
331
|
+
metadata: unknown
|
|
332
|
+
createdAt: TimestampColumn
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface AuthSchemaVersionTable {
|
|
336
|
+
id: string
|
|
337
|
+
version: string
|
|
338
|
+
updatedAt: TimestampColumn
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export interface CloudDatabase {
|
|
342
|
+
account: Account
|
|
343
|
+
session: Session
|
|
344
|
+
twoFactor: TwoFactorTable
|
|
345
|
+
passkey: PasskeyTable
|
|
346
|
+
mfaAssurance: MFAAssuranceTable
|
|
347
|
+
signIn: SignInTable
|
|
348
|
+
userPicture: UserPictureTable
|
|
349
|
+
user: AuthUserTable
|
|
350
|
+
authSchemaVersion: AuthSchemaVersionTable
|
|
351
|
+
enrollment: EnrollmentTable
|
|
352
|
+
apiRateLimit: APIRateLimitTable
|
|
353
|
+
adminAuditEvent: AdminAuditEventTable
|
|
354
|
+
workspace: WorkspaceTable
|
|
355
|
+
workspaceMember: WorkspaceMemberTable
|
|
356
|
+
workspaceInvitation: WorkspaceInvitationTable
|
|
357
|
+
document: DocumentTable
|
|
358
|
+
storageObject: StorageObjectTable
|
|
359
|
+
documentRevision: DocumentRevisionTable
|
|
360
|
+
revisionImage: RevisionImageTable
|
|
361
|
+
workspaceImage: WorkspaceImageTable
|
|
362
|
+
documentVersion: DocumentVersionTable
|
|
363
|
+
documentShare: DocumentShareTable
|
|
364
|
+
documentGrant: DocumentGrantTable
|
|
365
|
+
documentInvitation: DocumentInvitationTable
|
|
366
|
+
documentCollaborationState: DocumentCollaborationStateTable
|
|
367
|
+
invitationContinuation: InvitationContinuationTable
|
|
368
|
+
workspaceEntitlement: WorkspaceEntitlementTable
|
|
369
|
+
workspaceStorageUsage: WorkspaceStorageUsageTable
|
|
370
|
+
transactionalEmail: TransactionalEmailTable
|
|
371
|
+
uploadStorageReservation: UploadStorageReservationTable
|
|
372
|
+
upload: UploadTable
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export type NewTransactionalEmail = Insertable<TransactionalEmailTable>
|
|
376
|
+
export type TransactionalEmail = Selectable<TransactionalEmailTable>
|
|
377
|
+
export type Workspace = Selectable<WorkspaceTable>
|
|
378
|
+
export type NewWorkspace = Insertable<WorkspaceTable>
|
|
379
|
+
export type WorkspaceUpdate = Updateable<WorkspaceTable>
|
|
380
|
+
export type Document = Selectable<DocumentTable>
|
|
381
|
+
export type NewDocument = Insertable<DocumentTable>
|
|
382
|
+
export type DocumentRevision = Selectable<DocumentRevisionTable>
|
|
383
|
+
export type DocumentVersion = Selectable<DocumentVersionTable>
|
|
384
|
+
export type NewDocumentRevision = Insertable<DocumentRevisionTable>
|
|
385
|
+
export type DocumentShare = Selectable<DocumentShareTable>
|
|
386
|
+
export type NewDocumentShare = Insertable<DocumentShareTable>
|
|
387
|
+
export type DocumentGrant = Selectable<DocumentGrantTable>
|
|
388
|
+
export type NewDocumentGrant = Insertable<DocumentGrantTable>
|
|
389
|
+
export type DocumentInvitation = Selectable<DocumentInvitationTable>
|
|
390
|
+
export type NewDocumentInvitation = Insertable<DocumentInvitationTable>
|
|
391
|
+
export type StorageObject = Selectable<StorageObjectTable>
|
|
392
|
+
export type NewStorageObject = Insertable<StorageObjectTable>
|
|
393
|
+
export type Upload = Selectable<UploadTable>
|
|
394
|
+
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, edit: 2 }
|
|
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
|
+
}
|