@open-pencil/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 +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,502 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreateWorkspaceInvitationInput,
|
|
3
|
+
CreatedWorkspaceInvitation,
|
|
4
|
+
InvitationPreview,
|
|
5
|
+
WorkspaceInvitation,
|
|
6
|
+
WorkspaceMembers,
|
|
7
|
+
WorkspaceRole,
|
|
8
|
+
WorkspaceSummary
|
|
9
|
+
} from '#cloud/contract'
|
|
10
|
+
import { cloudInvitationURL } from '#cloud/contract'
|
|
11
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
12
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
|
+
import type { TransactionalEmailService } from '#cloud/server/email/types'
|
|
14
|
+
import {
|
|
15
|
+
invitationMatchesActor,
|
|
16
|
+
invitationTokenIsActive,
|
|
17
|
+
recipientHint
|
|
18
|
+
} from '#cloud/server/sharing/continuation'
|
|
19
|
+
import { dateString } from '#cloud/server/sharing/contracts'
|
|
20
|
+
import { hashCapability } from '#cloud/server/sharing/crypto'
|
|
21
|
+
import { findWorkspace, type WorkspaceDatabase } from '#cloud/server/workspaces/repository'
|
|
22
|
+
import type { Kysely } from 'kysely'
|
|
23
|
+
|
|
24
|
+
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
25
|
+
|
|
26
|
+
const INVITATION_SECRET_BYTES = 24
|
|
27
|
+
const INVITATION_LIFETIME_MS = 7 * 24 * 60 * 60_000
|
|
28
|
+
|
|
29
|
+
/** The workspace does not exist, or the person is not a member of it. */
|
|
30
|
+
export class WorkspaceNotFoundError extends Error {
|
|
31
|
+
constructor() {
|
|
32
|
+
super('Workspace not found')
|
|
33
|
+
this.name = 'WorkspaceNotFoundError'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Only a workspace's administrators may do this. */
|
|
38
|
+
export class WorkspaceForbiddenError extends Error {
|
|
39
|
+
constructor() {
|
|
40
|
+
super('Workspace administrators only')
|
|
41
|
+
this.name = 'WorkspaceForbiddenError'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The change would leave a workspace with members but no administrator. */
|
|
46
|
+
export class WorkspaceLastAdminError extends Error {
|
|
47
|
+
constructor() {
|
|
48
|
+
super('A workspace needs an administrator')
|
|
49
|
+
this.name = 'WorkspaceLastAdminError'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A workspace still holds documents: live ones, or deleted ones kept until cleanup removes their
|
|
55
|
+
* files, which deleting the workspace now would leave behind in object storage.
|
|
56
|
+
*/
|
|
57
|
+
export class WorkspaceNotEmptyError extends Error {
|
|
58
|
+
constructor(readonly reason: 'documents' | 'pending-cleanup') {
|
|
59
|
+
super('Workspace still holds documents')
|
|
60
|
+
this.name = 'WorkspaceNotEmptyError'
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The person invited already belongs to the workspace. */
|
|
65
|
+
export class WorkspaceAlreadyMemberError extends Error {
|
|
66
|
+
constructor() {
|
|
67
|
+
super('Already a member of this workspace')
|
|
68
|
+
this.name = 'WorkspaceAlreadyMemberError'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The invitation is unknown, used, withdrawn, expired, or meant for someone else. */
|
|
73
|
+
export class WorkspaceInvitationInvalidError extends Error {
|
|
74
|
+
constructor() {
|
|
75
|
+
super('Workspace invitation is not valid')
|
|
76
|
+
this.name = 'WorkspaceInvitationInvalidError'
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type WorkspaceMemberServiceOptions = {
|
|
81
|
+
email?: TransactionalEmailService
|
|
82
|
+
publicURL: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function invitationContract(row: {
|
|
86
|
+
id: string
|
|
87
|
+
emailNormalized: string
|
|
88
|
+
role: WorkspaceRole
|
|
89
|
+
invitedBy: string
|
|
90
|
+
inviterName: string | null
|
|
91
|
+
invitedAt: Date | string
|
|
92
|
+
expiresAt: Date | string
|
|
93
|
+
}): WorkspaceInvitation {
|
|
94
|
+
return {
|
|
95
|
+
id: row.id,
|
|
96
|
+
email: row.emailNormalized,
|
|
97
|
+
role: row.role,
|
|
98
|
+
invitedBy: { id: row.invitedBy, name: row.inviterName },
|
|
99
|
+
invitedAt: dateString(row.invitedAt) ?? '',
|
|
100
|
+
expiresAt: dateString(row.expiresAt) ?? ''
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function adminCount(database: WorkspaceDatabase, workspaceId: string): Promise<number> {
|
|
105
|
+
const rows = await database
|
|
106
|
+
.selectFrom('workspaceMember')
|
|
107
|
+
.select('userId')
|
|
108
|
+
.where('workspaceId', '=', workspaceId)
|
|
109
|
+
.where('role', '=', 'admin')
|
|
110
|
+
.forUpdate()
|
|
111
|
+
.execute()
|
|
112
|
+
return rows.length
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** A member's row, locked until the transaction ends so roles change one at a time. */
|
|
116
|
+
async function lockedMember(
|
|
117
|
+
database: WorkspaceDatabase,
|
|
118
|
+
workspaceId: string,
|
|
119
|
+
memberId: string
|
|
120
|
+
): Promise<{ role: WorkspaceRole }> {
|
|
121
|
+
const member = await database
|
|
122
|
+
.selectFrom('workspaceMember')
|
|
123
|
+
.select('role')
|
|
124
|
+
.where('workspaceId', '=', workspaceId)
|
|
125
|
+
.where('userId', '=', memberId)
|
|
126
|
+
.forUpdate()
|
|
127
|
+
.executeTakeFirst()
|
|
128
|
+
if (!member) throw new WorkspaceNotFoundError()
|
|
129
|
+
return member
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Who belongs to a workspace and who is invited to it. Administrators invite, change roles, and
|
|
134
|
+
* remove people; anyone may leave. A workspace with members always keeps an administrator.
|
|
135
|
+
*/
|
|
136
|
+
export function createWorkspaceMemberService(
|
|
137
|
+
database: Kysely<CloudDatabase>,
|
|
138
|
+
options: WorkspaceMemberServiceOptions
|
|
139
|
+
) {
|
|
140
|
+
async function membership(userId: string, workspaceId: string): Promise<WorkspaceSummary> {
|
|
141
|
+
const workspace = await findWorkspace(database, userId, workspaceId)
|
|
142
|
+
if (!workspace) throw new WorkspaceNotFoundError()
|
|
143
|
+
return workspace
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function requireAdmin(userId: string, workspaceId: string): Promise<WorkspaceSummary> {
|
|
147
|
+
const workspace = await membership(userId, workspaceId)
|
|
148
|
+
if (workspace.role !== 'admin') throw new WorkspaceForbiddenError()
|
|
149
|
+
return workspace
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A workspace's members with their accounts; user ids are text in membership rows. */
|
|
153
|
+
function memberUsers(workspaceId: string) {
|
|
154
|
+
return database
|
|
155
|
+
.selectFrom('workspaceMember')
|
|
156
|
+
.innerJoin('user', (join) =>
|
|
157
|
+
join.on((expression) =>
|
|
158
|
+
expression(
|
|
159
|
+
expression.cast(expression.ref('user.id'), 'text'),
|
|
160
|
+
'=',
|
|
161
|
+
expression.ref('workspaceMember.userId')
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
.where('workspaceMember.workspaceId', '=', workspaceId)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function acceptanceURL(invitationId: string, token: string): string {
|
|
169
|
+
return cloudInvitationURL(options.publicURL, invitationId, token)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Invites someone by email; a newer invitation to the same address replaces the older. */
|
|
173
|
+
async function invite(
|
|
174
|
+
userId: string,
|
|
175
|
+
workspaceId: string,
|
|
176
|
+
input: CreateWorkspaceInvitationInput
|
|
177
|
+
): Promise<CreatedWorkspaceInvitation> {
|
|
178
|
+
const workspace = await requireAdmin(userId, workspaceId)
|
|
179
|
+
const existing = await memberUsers(workspaceId)
|
|
180
|
+
.select('workspaceMember.userId')
|
|
181
|
+
.where((expression) =>
|
|
182
|
+
expression(expression.fn('lower', [expression.ref('user.email')]), '=', input.email)
|
|
183
|
+
)
|
|
184
|
+
.executeTakeFirst()
|
|
185
|
+
if (existing) throw new WorkspaceAlreadyMemberError()
|
|
186
|
+
const inviter = await database
|
|
187
|
+
.selectFrom('user')
|
|
188
|
+
.select('name')
|
|
189
|
+
.where('id', '=', userId)
|
|
190
|
+
.executeTakeFirst()
|
|
191
|
+
const id = crypto.randomUUID()
|
|
192
|
+
const token = randomHex(INVITATION_SECRET_BYTES)
|
|
193
|
+
const expiresAt = new Date(Date.now() + INVITATION_LIFETIME_MS)
|
|
194
|
+
const url = acceptanceURL(id, token)
|
|
195
|
+
const row = await database.transaction().execute(async (transaction) => {
|
|
196
|
+
await transaction
|
|
197
|
+
.updateTable('workspaceInvitation')
|
|
198
|
+
.set({ revokedAt: new Date() })
|
|
199
|
+
.where('workspaceId', '=', workspaceId)
|
|
200
|
+
.where('emailNormalized', '=', input.email)
|
|
201
|
+
.where('acceptedAt', 'is', null)
|
|
202
|
+
.where('revokedAt', 'is', null)
|
|
203
|
+
.execute()
|
|
204
|
+
const inserted = await transaction
|
|
205
|
+
.insertInto('workspaceInvitation')
|
|
206
|
+
.values({
|
|
207
|
+
id,
|
|
208
|
+
workspaceId,
|
|
209
|
+
emailNormalized: input.email,
|
|
210
|
+
role: input.role,
|
|
211
|
+
tokenHash: hashCapability(token),
|
|
212
|
+
invitedBy: userId,
|
|
213
|
+
expiresAt,
|
|
214
|
+
acceptedAt: null,
|
|
215
|
+
revokedAt: null
|
|
216
|
+
})
|
|
217
|
+
.returning(['id', 'emailNormalized', 'role', 'invitedBy', 'invitedAt', 'expiresAt'])
|
|
218
|
+
.executeTakeFirstOrThrow()
|
|
219
|
+
await options.email?.enqueue(
|
|
220
|
+
{
|
|
221
|
+
idempotencyKey: `workspace-invitation/${id}`,
|
|
222
|
+
kind: 'workspace-invitation',
|
|
223
|
+
recipientEmail: input.email,
|
|
224
|
+
payload: {
|
|
225
|
+
inviterName: inviter?.name ?? 'An OpenPencil user',
|
|
226
|
+
workspaceName: workspace.name,
|
|
227
|
+
role: input.role,
|
|
228
|
+
expiresAt: expiresAt.toISOString(),
|
|
229
|
+
acceptanceURL: url
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
transaction
|
|
233
|
+
)
|
|
234
|
+
return inserted
|
|
235
|
+
})
|
|
236
|
+
return {
|
|
237
|
+
invitation: invitationContract({ ...row, inviterName: inviter?.name ?? null }),
|
|
238
|
+
acceptanceURL: url
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
async rename(userId: string, workspaceId: string, name: string): Promise<WorkspaceSummary> {
|
|
244
|
+
await requireAdmin(userId, workspaceId)
|
|
245
|
+
await database
|
|
246
|
+
.updateTable('workspace')
|
|
247
|
+
.set({ name, updatedAt: new Date() })
|
|
248
|
+
.where('id', '=', workspaceId)
|
|
249
|
+
.execute()
|
|
250
|
+
return membership(userId, workspaceId)
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
async list(userId: string, workspaceId: string): Promise<WorkspaceMembers> {
|
|
254
|
+
const workspace = await membership(userId, workspaceId)
|
|
255
|
+
const members = await memberUsers(workspaceId)
|
|
256
|
+
.select([
|
|
257
|
+
'workspaceMember.userId',
|
|
258
|
+
'workspaceMember.role',
|
|
259
|
+
'workspaceMember.createdAt',
|
|
260
|
+
'user.name',
|
|
261
|
+
'user.email',
|
|
262
|
+
'user.image'
|
|
263
|
+
])
|
|
264
|
+
.orderBy('workspaceMember.createdAt')
|
|
265
|
+
.execute()
|
|
266
|
+
const invitations =
|
|
267
|
+
workspace.role === 'admin'
|
|
268
|
+
? await database
|
|
269
|
+
.selectFrom('workspaceInvitation')
|
|
270
|
+
.leftJoin('user', (join) =>
|
|
271
|
+
join.on((expression) =>
|
|
272
|
+
expression(
|
|
273
|
+
expression.cast(expression.ref('user.id'), 'text'),
|
|
274
|
+
'=',
|
|
275
|
+
expression.ref('workspaceInvitation.invitedBy')
|
|
276
|
+
)
|
|
277
|
+
)
|
|
278
|
+
)
|
|
279
|
+
.select([
|
|
280
|
+
'workspaceInvitation.id',
|
|
281
|
+
'workspaceInvitation.emailNormalized',
|
|
282
|
+
'workspaceInvitation.role',
|
|
283
|
+
'workspaceInvitation.invitedAt',
|
|
284
|
+
'workspaceInvitation.expiresAt',
|
|
285
|
+
'workspaceInvitation.invitedBy',
|
|
286
|
+
'user.name as inviterName'
|
|
287
|
+
])
|
|
288
|
+
.where('workspaceInvitation.workspaceId', '=', workspaceId)
|
|
289
|
+
.where('workspaceInvitation.acceptedAt', 'is', null)
|
|
290
|
+
.where('workspaceInvitation.revokedAt', 'is', null)
|
|
291
|
+
.where('workspaceInvitation.expiresAt', '>', new Date())
|
|
292
|
+
.orderBy('workspaceInvitation.invitedAt')
|
|
293
|
+
.execute()
|
|
294
|
+
: []
|
|
295
|
+
return {
|
|
296
|
+
members: members.map((member) => ({
|
|
297
|
+
userId: member.userId,
|
|
298
|
+
name: member.name,
|
|
299
|
+
email: member.email,
|
|
300
|
+
image: member.image,
|
|
301
|
+
role: member.role,
|
|
302
|
+
joinedAt: dateString(member.createdAt) ?? ''
|
|
303
|
+
})),
|
|
304
|
+
invitations: invitations.map(invitationContract)
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
invite,
|
|
309
|
+
|
|
310
|
+
/** Sends a waiting invitation again with a new link; the old link stops working. */
|
|
311
|
+
async resendInvitation(
|
|
312
|
+
userId: string,
|
|
313
|
+
workspaceId: string,
|
|
314
|
+
invitationId: string
|
|
315
|
+
): Promise<CreatedWorkspaceInvitation> {
|
|
316
|
+
await requireAdmin(userId, workspaceId)
|
|
317
|
+
const invitation = await database
|
|
318
|
+
.selectFrom('workspaceInvitation')
|
|
319
|
+
.select(['emailNormalized', 'role'])
|
|
320
|
+
.where('id', '=', invitationId)
|
|
321
|
+
.where('workspaceId', '=', workspaceId)
|
|
322
|
+
.where('acceptedAt', 'is', null)
|
|
323
|
+
.where('revokedAt', 'is', null)
|
|
324
|
+
.executeTakeFirst()
|
|
325
|
+
if (!invitation) throw new WorkspaceNotFoundError()
|
|
326
|
+
return invite(userId, workspaceId, {
|
|
327
|
+
email: invitation.emailNormalized,
|
|
328
|
+
role: invitation.role
|
|
329
|
+
})
|
|
330
|
+
},
|
|
331
|
+
|
|
332
|
+
async revokeInvitation(
|
|
333
|
+
userId: string,
|
|
334
|
+
workspaceId: string,
|
|
335
|
+
invitationId: string
|
|
336
|
+
): Promise<void> {
|
|
337
|
+
await requireAdmin(userId, workspaceId)
|
|
338
|
+
const result = await database
|
|
339
|
+
.updateTable('workspaceInvitation')
|
|
340
|
+
.set({ revokedAt: new Date() })
|
|
341
|
+
.where('id', '=', invitationId)
|
|
342
|
+
.where('workspaceId', '=', workspaceId)
|
|
343
|
+
.where('acceptedAt', 'is', null)
|
|
344
|
+
.where('revokedAt', 'is', null)
|
|
345
|
+
.executeTakeFirst()
|
|
346
|
+
if (Number(result.numUpdatedRows) === 0) throw new WorkspaceNotFoundError()
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
async updateRole(
|
|
350
|
+
userId: string,
|
|
351
|
+
workspaceId: string,
|
|
352
|
+
memberId: string,
|
|
353
|
+
role: WorkspaceRole
|
|
354
|
+
): Promise<void> {
|
|
355
|
+
await requireAdmin(userId, workspaceId)
|
|
356
|
+
await database.transaction().execute(async (transaction) => {
|
|
357
|
+
const member = await lockedMember(transaction, workspaceId, memberId)
|
|
358
|
+
if (member.role === 'admin' && role !== 'admin') {
|
|
359
|
+
if ((await adminCount(transaction, workspaceId)) <= 1) throw new WorkspaceLastAdminError()
|
|
360
|
+
}
|
|
361
|
+
await transaction
|
|
362
|
+
.updateTable('workspaceMember')
|
|
363
|
+
.set({ role })
|
|
364
|
+
.where('workspaceId', '=', workspaceId)
|
|
365
|
+
.where('userId', '=', memberId)
|
|
366
|
+
.execute()
|
|
367
|
+
})
|
|
368
|
+
},
|
|
369
|
+
|
|
370
|
+
/** Deletes an empty workspace, with its members and invitations. */
|
|
371
|
+
async delete(userId: string, workspaceId: string): Promise<void> {
|
|
372
|
+
await requireAdmin(userId, workspaceId)
|
|
373
|
+
await database.transaction().execute(async (transaction) => {
|
|
374
|
+
// Locked first, so a document created meanwhile waits and then finds no workspace.
|
|
375
|
+
await transaction
|
|
376
|
+
.selectFrom('workspace')
|
|
377
|
+
.select('id')
|
|
378
|
+
.where('id', '=', workspaceId)
|
|
379
|
+
.forUpdate()
|
|
380
|
+
.executeTakeFirstOrThrow()
|
|
381
|
+
const documents = await transaction
|
|
382
|
+
.selectFrom('document')
|
|
383
|
+
.select(['deletedAt'])
|
|
384
|
+
.where('workspaceId', '=', workspaceId)
|
|
385
|
+
.execute()
|
|
386
|
+
if (documents.some((document) => document.deletedAt === null)) {
|
|
387
|
+
throw new WorkspaceNotEmptyError('documents')
|
|
388
|
+
}
|
|
389
|
+
if (documents.length > 0) throw new WorkspaceNotEmptyError('pending-cleanup')
|
|
390
|
+
await transaction.deleteFrom('workspace').where('id', '=', workspaceId).execute()
|
|
391
|
+
})
|
|
392
|
+
},
|
|
393
|
+
|
|
394
|
+
/** Removes a member, or lets someone leave; the last administrator cannot go. */
|
|
395
|
+
async remove(userId: string, workspaceId: string, memberId: string): Promise<void> {
|
|
396
|
+
if (memberId === userId) await membership(userId, workspaceId)
|
|
397
|
+
else await requireAdmin(userId, workspaceId)
|
|
398
|
+
await database.transaction().execute(async (transaction) => {
|
|
399
|
+
const member = await lockedMember(transaction, workspaceId, memberId)
|
|
400
|
+
if (member.role === 'admin' && (await adminCount(transaction, workspaceId)) <= 1) {
|
|
401
|
+
throw new WorkspaceLastAdminError()
|
|
402
|
+
}
|
|
403
|
+
await transaction
|
|
404
|
+
.deleteFrom('workspaceMember')
|
|
405
|
+
.where('workspaceId', '=', workspaceId)
|
|
406
|
+
.where('userId', '=', memberId)
|
|
407
|
+
.execute()
|
|
408
|
+
})
|
|
409
|
+
},
|
|
410
|
+
|
|
411
|
+
/** What a workspace invitation link offers, or null when the id is not one. */
|
|
412
|
+
async preview(invitationId: string, token: string): Promise<InvitationPreview | null> {
|
|
413
|
+
const invitation = await database
|
|
414
|
+
.selectFrom('workspaceInvitation')
|
|
415
|
+
.innerJoin('workspace', 'workspace.id', 'workspaceInvitation.workspaceId')
|
|
416
|
+
.select([
|
|
417
|
+
'workspaceInvitation.emailNormalized',
|
|
418
|
+
'workspaceInvitation.role',
|
|
419
|
+
'workspaceInvitation.tokenHash',
|
|
420
|
+
'workspaceInvitation.expiresAt',
|
|
421
|
+
'workspaceInvitation.acceptedAt',
|
|
422
|
+
'workspaceInvitation.revokedAt',
|
|
423
|
+
'workspaceInvitation.invitedBy',
|
|
424
|
+
'workspace.name as workspaceName'
|
|
425
|
+
])
|
|
426
|
+
.where('workspaceInvitation.id', '=', invitationId)
|
|
427
|
+
.executeTakeFirst()
|
|
428
|
+
if (!invitation) return null
|
|
429
|
+
if (!invitationTokenIsActive(invitation, token)) throw new WorkspaceInvitationInvalidError()
|
|
430
|
+
const inviter = await database
|
|
431
|
+
.selectFrom('user')
|
|
432
|
+
.select('name')
|
|
433
|
+
.where('id', '=', invitation.invitedBy)
|
|
434
|
+
.executeTakeFirst()
|
|
435
|
+
return {
|
|
436
|
+
kind: 'workspace',
|
|
437
|
+
workspaceName: invitation.workspaceName,
|
|
438
|
+
role: invitation.role,
|
|
439
|
+
inviterName: inviter?.name ?? 'An OpenPencil user',
|
|
440
|
+
expiresAt: dateString(invitation.expiresAt) ?? '',
|
|
441
|
+
recipientHint: recipientHint(invitation.emailNormalized)
|
|
442
|
+
}
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Joins the workspace an invitation names, or returns null when the id is not a workspace
|
|
447
|
+
* invitation. Someone already in it keeps the role they have.
|
|
448
|
+
*/
|
|
449
|
+
async accept(
|
|
450
|
+
actor: CloudActor,
|
|
451
|
+
invitationId: string,
|
|
452
|
+
token: string
|
|
453
|
+
): Promise<{ workspaceId: string; role: WorkspaceRole } | null> {
|
|
454
|
+
const invitation = await database
|
|
455
|
+
.selectFrom('workspaceInvitation')
|
|
456
|
+
.select([
|
|
457
|
+
'workspaceId',
|
|
458
|
+
'emailNormalized',
|
|
459
|
+
'role',
|
|
460
|
+
'tokenHash',
|
|
461
|
+
'expiresAt',
|
|
462
|
+
'acceptedAt',
|
|
463
|
+
'revokedAt'
|
|
464
|
+
])
|
|
465
|
+
.where('id', '=', invitationId)
|
|
466
|
+
.executeTakeFirst()
|
|
467
|
+
if (!invitation) return null
|
|
468
|
+
if (
|
|
469
|
+
!invitationMatchesActor(actor, invitation.emailNormalized) ||
|
|
470
|
+
!invitationTokenIsActive(invitation, token)
|
|
471
|
+
) {
|
|
472
|
+
throw new WorkspaceInvitationInvalidError()
|
|
473
|
+
}
|
|
474
|
+
return database.transaction().execute(async (transaction) => {
|
|
475
|
+
await transaction
|
|
476
|
+
.insertInto('workspaceMember')
|
|
477
|
+
.values({
|
|
478
|
+
workspaceId: invitation.workspaceId,
|
|
479
|
+
userId: actor.userId,
|
|
480
|
+
role: invitation.role
|
|
481
|
+
})
|
|
482
|
+
.onConflict((conflict) => conflict.columns(['workspaceId', 'userId']).doNothing())
|
|
483
|
+
.execute()
|
|
484
|
+
await transaction
|
|
485
|
+
.updateTable('workspaceInvitation')
|
|
486
|
+
.set({ acceptedAt: new Date() })
|
|
487
|
+
.where('id', '=', invitationId)
|
|
488
|
+
.where('acceptedAt', 'is', null)
|
|
489
|
+
.execute()
|
|
490
|
+
const member = await transaction
|
|
491
|
+
.selectFrom('workspaceMember')
|
|
492
|
+
.select('role')
|
|
493
|
+
.where('workspaceId', '=', invitation.workspaceId)
|
|
494
|
+
.where('userId', '=', actor.userId)
|
|
495
|
+
.executeTakeFirstOrThrow()
|
|
496
|
+
return { workspaceId: invitation.workspaceId, role: member.role }
|
|
497
|
+
})
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export type WorkspaceMemberService = ReturnType<typeof createWorkspaceMemberService>
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { CreateWorkspaceInput, WorkspaceRole, WorkspaceSummary } from '#cloud/contract'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
4
|
+
|
|
5
|
+
export type WorkspaceDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
6
|
+
|
|
7
|
+
function toISOString(value: Date | string): string {
|
|
8
|
+
return value instanceof Date ? value.toISOString() : value
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function listWorkspaces(
|
|
12
|
+
database: WorkspaceDatabase,
|
|
13
|
+
userId: string
|
|
14
|
+
): Promise<WorkspaceSummary[]> {
|
|
15
|
+
const rows = await database
|
|
16
|
+
.selectFrom('workspace')
|
|
17
|
+
.innerJoin('workspaceMember', 'workspaceMember.workspaceId', 'workspace.id')
|
|
18
|
+
.select([
|
|
19
|
+
'workspace.id',
|
|
20
|
+
'workspace.name',
|
|
21
|
+
'workspace.slug',
|
|
22
|
+
'workspace.createdAt',
|
|
23
|
+
'workspace.updatedAt',
|
|
24
|
+
'workspaceMember.role'
|
|
25
|
+
])
|
|
26
|
+
.where('workspaceMember.userId', '=', userId)
|
|
27
|
+
.orderBy('workspace.updatedAt', 'desc')
|
|
28
|
+
.execute()
|
|
29
|
+
|
|
30
|
+
return rows.map((row) => ({
|
|
31
|
+
...row,
|
|
32
|
+
createdAt: toISOString(row.createdAt),
|
|
33
|
+
updatedAt: toISOString(row.updatedAt)
|
|
34
|
+
}))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function findWorkspace(
|
|
38
|
+
database: WorkspaceDatabase,
|
|
39
|
+
userId: string,
|
|
40
|
+
workspaceId: string
|
|
41
|
+
): Promise<WorkspaceSummary | undefined> {
|
|
42
|
+
const row = await database
|
|
43
|
+
.selectFrom('workspace')
|
|
44
|
+
.innerJoin('workspaceMember', 'workspaceMember.workspaceId', 'workspace.id')
|
|
45
|
+
.select([
|
|
46
|
+
'workspace.id',
|
|
47
|
+
'workspace.name',
|
|
48
|
+
'workspace.slug',
|
|
49
|
+
'workspace.createdAt',
|
|
50
|
+
'workspace.updatedAt',
|
|
51
|
+
'workspaceMember.role'
|
|
52
|
+
])
|
|
53
|
+
.where('workspace.id', '=', workspaceId)
|
|
54
|
+
.where('workspaceMember.userId', '=', userId)
|
|
55
|
+
.executeTakeFirst()
|
|
56
|
+
return row
|
|
57
|
+
? {
|
|
58
|
+
...row,
|
|
59
|
+
createdAt: toISOString(row.createdAt),
|
|
60
|
+
updatedAt: toISOString(row.updatedAt)
|
|
61
|
+
}
|
|
62
|
+
: undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function insertWorkspace(
|
|
66
|
+
database: WorkspaceDatabase,
|
|
67
|
+
actorId: string,
|
|
68
|
+
input: CreateWorkspaceInput & { id: string; slug: string }
|
|
69
|
+
): Promise<boolean> {
|
|
70
|
+
const inserted = await database
|
|
71
|
+
.insertInto('workspace')
|
|
72
|
+
.values({
|
|
73
|
+
id: input.id,
|
|
74
|
+
name: input.name,
|
|
75
|
+
slug: input.slug,
|
|
76
|
+
createdBy: actorId
|
|
77
|
+
})
|
|
78
|
+
.onConflict((conflict) => conflict.column('slug').doNothing())
|
|
79
|
+
.returning('id')
|
|
80
|
+
.executeTakeFirst()
|
|
81
|
+
return inserted !== undefined
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function insertWorkspaceMember(
|
|
85
|
+
database: WorkspaceDatabase,
|
|
86
|
+
workspaceId: string,
|
|
87
|
+
userId: string,
|
|
88
|
+
role: WorkspaceRole
|
|
89
|
+
): Promise<void> {
|
|
90
|
+
await database.insertInto('workspaceMember').values({ workspaceId, userId, role }).execute()
|
|
91
|
+
}
|