@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,82 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DocumentGrant,
|
|
3
|
+
DocumentInvitation,
|
|
4
|
+
DocumentPermission,
|
|
5
|
+
DocumentShare
|
|
6
|
+
} from '#cloud/contract'
|
|
7
|
+
|
|
8
|
+
export function dateString(value: Date | string | null): string | null {
|
|
9
|
+
if (value === null) return null
|
|
10
|
+
return value instanceof Date ? value.toISOString() : value
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function shareContract(row: {
|
|
14
|
+
id: string
|
|
15
|
+
documentId: string
|
|
16
|
+
permission: DocumentPermission
|
|
17
|
+
roomEpoch: number
|
|
18
|
+
createdBy: string
|
|
19
|
+
createdAt: Date | string
|
|
20
|
+
updatedAt: Date | string
|
|
21
|
+
expiresAt: Date | string | null
|
|
22
|
+
revokedAt: Date | string | null
|
|
23
|
+
lastUsedAt: Date | string | null
|
|
24
|
+
revisionId: string | null
|
|
25
|
+
}): DocumentShare {
|
|
26
|
+
// Rows carry the secret's hash; responses name each field so it never goes out.
|
|
27
|
+
return {
|
|
28
|
+
id: row.id,
|
|
29
|
+
documentId: row.documentId,
|
|
30
|
+
permission: row.permission,
|
|
31
|
+
roomEpoch: row.roomEpoch,
|
|
32
|
+
createdBy: row.createdBy,
|
|
33
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
34
|
+
updatedAt: dateString(row.updatedAt) ?? '',
|
|
35
|
+
expiresAt: dateString(row.expiresAt),
|
|
36
|
+
revokedAt: dateString(row.revokedAt),
|
|
37
|
+
lastUsedAt: dateString(row.lastUsedAt),
|
|
38
|
+
revisionId: row.revisionId
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function grantContract(row: {
|
|
43
|
+
id: string
|
|
44
|
+
documentId: string
|
|
45
|
+
userId: string
|
|
46
|
+
permission: DocumentPermission
|
|
47
|
+
createdBy: string
|
|
48
|
+
createdAt: Date | string
|
|
49
|
+
updatedAt: Date | string
|
|
50
|
+
}): DocumentGrant {
|
|
51
|
+
return {
|
|
52
|
+
id: row.id,
|
|
53
|
+
documentId: row.documentId,
|
|
54
|
+
userId: row.userId,
|
|
55
|
+
permission: row.permission,
|
|
56
|
+
createdBy: row.createdBy,
|
|
57
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
58
|
+
updatedAt: dateString(row.updatedAt) ?? ''
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function invitationContract(row: {
|
|
63
|
+
id: string
|
|
64
|
+
documentId: string
|
|
65
|
+
emailNormalized: string
|
|
66
|
+
permission: DocumentPermission
|
|
67
|
+
invitedBy: string
|
|
68
|
+
invitedAt: Date | string
|
|
69
|
+
expiresAt: Date | string
|
|
70
|
+
acceptedAt: Date | string | null
|
|
71
|
+
}): DocumentInvitation {
|
|
72
|
+
return {
|
|
73
|
+
id: row.id,
|
|
74
|
+
documentId: row.documentId,
|
|
75
|
+
email: row.emailNormalized,
|
|
76
|
+
permission: row.permission,
|
|
77
|
+
invitedBy: row.invitedBy,
|
|
78
|
+
invitedAt: dateString(row.invitedAt) ?? '',
|
|
79
|
+
expiresAt: dateString(row.expiresAt) ?? '',
|
|
80
|
+
acceptedAt: dateString(row.acceptedAt)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { hkdf } from '@noble/hashes/hkdf.js'
|
|
2
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
3
|
+
import { bytesToHex, hexToBytes, utf8ToBytes } from '@noble/hashes/utils.js'
|
|
4
|
+
import { compactDecrypt, CompactEncrypt } from 'jose'
|
|
5
|
+
|
|
6
|
+
const CONTINUATION_SALT = utf8ToBytes('pairdraw-cloud-invitation-continuation')
|
|
7
|
+
const CONTINUATION_HEADER = { alg: 'dir', enc: 'A256GCM' } as const
|
|
8
|
+
|
|
9
|
+
export function hashCapability(value: string): string {
|
|
10
|
+
return bytesToHex(sha256(utf8ToBytes(value)))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function capabilityHashMatches(stored: string, value: string): boolean {
|
|
14
|
+
let expected: Uint8Array
|
|
15
|
+
try {
|
|
16
|
+
expected = hexToBytes(stored)
|
|
17
|
+
} catch {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
const actual = sha256(utf8ToBytes(value))
|
|
21
|
+
if (expected.length !== actual.length) return false
|
|
22
|
+
let difference = 0
|
|
23
|
+
for (const [index, expectedByte] of expected.entries()) {
|
|
24
|
+
difference |= expectedByte ^ (actual[index] ?? 0)
|
|
25
|
+
}
|
|
26
|
+
return difference === 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function continuationKey(secret: string): Uint8Array {
|
|
30
|
+
return hkdf(sha256, utf8ToBytes(secret), CONTINUATION_SALT, new Uint8Array(), 32)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function encryptContinuationToken(secret: string, token: string): Promise<string> {
|
|
34
|
+
return new CompactEncrypt(utf8ToBytes(token))
|
|
35
|
+
.setProtectedHeader(CONTINUATION_HEADER)
|
|
36
|
+
.encrypt(continuationKey(secret))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function decryptContinuationToken(secret: string, value: string): Promise<string> {
|
|
40
|
+
const { plaintext, protectedHeader } = await compactDecrypt(value, continuationKey(secret))
|
|
41
|
+
if (
|
|
42
|
+
protectedHeader.alg !== CONTINUATION_HEADER.alg ||
|
|
43
|
+
protectedHeader.enc !== CONTINUATION_HEADER.enc
|
|
44
|
+
) {
|
|
45
|
+
throw new Error('Unsupported invitation continuation encryption')
|
|
46
|
+
}
|
|
47
|
+
return new TextDecoder().decode(plaintext)
|
|
48
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { parseRequestSharedImageLinks, parseResolveDocumentShare } from '#cloud/contract'
|
|
2
|
+
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
3
|
+
import type { DocumentService } from '#cloud/server/documents'
|
|
4
|
+
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
5
|
+
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
6
|
+
import { validatedJSON } from '#cloud/server/validation'
|
|
7
|
+
import { Hono } from 'hono'
|
|
8
|
+
|
|
9
|
+
import { shareActor } from './actor'
|
|
10
|
+
|
|
11
|
+
export function createPublicDocumentRoutes(
|
|
12
|
+
sharing: DocumentSharingService,
|
|
13
|
+
documents: DocumentService,
|
|
14
|
+
resolveSession?: CloudSessionResolver
|
|
15
|
+
) {
|
|
16
|
+
return new Hono()
|
|
17
|
+
.post('/shares/:shareId/document', validatedJSON(parseResolveDocumentShare), (context) =>
|
|
18
|
+
sharingRoute(context, async () => {
|
|
19
|
+
const resolution = await sharing.resolveShare(
|
|
20
|
+
context.req.param('shareId'),
|
|
21
|
+
context.req.valid('json'),
|
|
22
|
+
await shareActor(context.req.raw, resolveSession)
|
|
23
|
+
)
|
|
24
|
+
return context.json({
|
|
25
|
+
resolution,
|
|
26
|
+
document: await documents.downloadShared(resolution.documentId, resolution.revisionId)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
)
|
|
30
|
+
.post('/shares/:shareId/images', validatedJSON(parseRequestSharedImageLinks), (context) =>
|
|
31
|
+
sharingRoute(context, async () => {
|
|
32
|
+
const { checksums, ...share } = context.req.valid('json')
|
|
33
|
+
const resolution = await sharing.resolveShare(
|
|
34
|
+
context.req.param('shareId'),
|
|
35
|
+
share,
|
|
36
|
+
await shareActor(context.req.raw, resolveSession)
|
|
37
|
+
)
|
|
38
|
+
return context.json(await documents.imageLinksShared(resolution.documentId, checksums))
|
|
39
|
+
})
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class InvitationDeliveryError extends Error {
|
|
2
|
+
override readonly name = 'InvitationDeliveryError'
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export class DocumentShareInvalidError extends Error {
|
|
6
|
+
override readonly name = 'DocumentShareInvalidError'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A valid link this server opens only for people signed in to it. */
|
|
10
|
+
export class DocumentShareSignInRequiredError extends Error {
|
|
11
|
+
override readonly name = 'DocumentShareSignInRequiredError'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A change a link's kind does not allow: a snapshot link stays view-only, and a live link has no
|
|
16
|
+
* revision to pin. Also a snapshot of a revision the document does not have, or of no revision.
|
|
17
|
+
*/
|
|
18
|
+
export class DocumentShareKindError extends Error {
|
|
19
|
+
override readonly name = 'DocumentShareKindError'
|
|
20
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { DocumentGrant, PutDocumentGrantInput } from '#cloud/contract'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
4
|
+
import type { Kysely } from 'kysely'
|
|
5
|
+
|
|
6
|
+
import { advanceCollaborationEpoch, requireSharingAccess } from '../access'
|
|
7
|
+
import { grantContract } from '../contracts'
|
|
8
|
+
|
|
9
|
+
export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
10
|
+
return {
|
|
11
|
+
async listGrants(userId: string, documentId: string): Promise<DocumentGrant[]> {
|
|
12
|
+
await requireSharingAccess(database, userId, documentId)
|
|
13
|
+
const rows = await database
|
|
14
|
+
.selectFrom('documentGrant')
|
|
15
|
+
.select(['id', 'documentId', 'userId', 'permission', 'createdBy', 'createdAt', 'updatedAt'])
|
|
16
|
+
.where('documentId', '=', documentId)
|
|
17
|
+
.where('revokedAt', 'is', null)
|
|
18
|
+
.orderBy('createdAt')
|
|
19
|
+
.execute()
|
|
20
|
+
return rows.map(grantContract)
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
async putGrant(
|
|
24
|
+
userId: string,
|
|
25
|
+
documentId: string,
|
|
26
|
+
targetUserId: string,
|
|
27
|
+
input: PutDocumentGrantInput
|
|
28
|
+
): Promise<DocumentGrant> {
|
|
29
|
+
await requireSharingAccess(database, userId, documentId)
|
|
30
|
+
const row = await database
|
|
31
|
+
.insertInto('documentGrant')
|
|
32
|
+
.values({
|
|
33
|
+
id: crypto.randomUUID(),
|
|
34
|
+
documentId,
|
|
35
|
+
userId: targetUserId,
|
|
36
|
+
permission: input.permission,
|
|
37
|
+
createdBy: userId,
|
|
38
|
+
revokedAt: null
|
|
39
|
+
})
|
|
40
|
+
.onConflict((conflict) =>
|
|
41
|
+
conflict.columns(['documentId', 'userId']).doUpdateSet({
|
|
42
|
+
permission: input.permission,
|
|
43
|
+
createdBy: userId,
|
|
44
|
+
revokedAt: null,
|
|
45
|
+
updatedAt: new Date()
|
|
46
|
+
})
|
|
47
|
+
)
|
|
48
|
+
.returning([
|
|
49
|
+
'id',
|
|
50
|
+
'documentId',
|
|
51
|
+
'userId',
|
|
52
|
+
'permission',
|
|
53
|
+
'createdBy',
|
|
54
|
+
'createdAt',
|
|
55
|
+
'updatedAt'
|
|
56
|
+
])
|
|
57
|
+
.executeTakeFirstOrThrow()
|
|
58
|
+
return grantContract(row)
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/** Revoking access advances the collaboration epoch, so tickets and room keys issued before stop working. */
|
|
62
|
+
async revokeGrant(userId: string, documentId: string, targetUserId: string): Promise<void> {
|
|
63
|
+
await database.transaction().execute(async (transaction) => {
|
|
64
|
+
await requireSharingAccess(transaction, userId, documentId)
|
|
65
|
+
const result = await transaction
|
|
66
|
+
.updateTable('documentGrant')
|
|
67
|
+
.set({ revokedAt: new Date(), updatedAt: new Date() })
|
|
68
|
+
.where('documentId', '=', documentId)
|
|
69
|
+
.where('userId', '=', targetUserId)
|
|
70
|
+
.where('revokedAt', 'is', null)
|
|
71
|
+
.executeTakeFirst()
|
|
72
|
+
if (Number(result.numUpdatedRows) === 0) throw new DocumentNotFoundError()
|
|
73
|
+
await advanceCollaborationEpoch(transaction, documentId)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type GrantService = ReturnType<typeof createGrantService>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents'
|
|
2
|
+
import {
|
|
3
|
+
DocumentShareInvalidError,
|
|
4
|
+
DocumentShareKindError,
|
|
5
|
+
DocumentShareSignInRequiredError,
|
|
6
|
+
InvitationDeliveryError
|
|
7
|
+
} from '#cloud/server/sharing/errors'
|
|
8
|
+
import { WorkspaceInvitationInvalidError } from '#cloud/server/workspaces/members'
|
|
9
|
+
import type { Context } from 'hono'
|
|
10
|
+
|
|
11
|
+
function sharingDomainError(context: Context, error: unknown): Response | null {
|
|
12
|
+
if (
|
|
13
|
+
error instanceof DocumentNotFoundError ||
|
|
14
|
+
error instanceof DocumentShareInvalidError ||
|
|
15
|
+
error instanceof WorkspaceInvitationInvalidError
|
|
16
|
+
) {
|
|
17
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
18
|
+
}
|
|
19
|
+
if (error instanceof DocumentShareKindError) {
|
|
20
|
+
return context.json({ error: { code: 'invalid_request' as const } }, 400)
|
|
21
|
+
}
|
|
22
|
+
if (error instanceof DocumentShareSignInRequiredError) {
|
|
23
|
+
return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
24
|
+
}
|
|
25
|
+
if (error instanceof InvitationDeliveryError) {
|
|
26
|
+
return context.json({ error: { code: 'invitation_delivery_failed' as const } }, 502)
|
|
27
|
+
}
|
|
28
|
+
if (error instanceof DocumentForbiddenError) {
|
|
29
|
+
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
30
|
+
}
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function sharingRoute<T>(
|
|
35
|
+
context: Context,
|
|
36
|
+
action: () => Promise<T>
|
|
37
|
+
): Promise<T | Response> {
|
|
38
|
+
try {
|
|
39
|
+
return await action()
|
|
40
|
+
} catch (error) {
|
|
41
|
+
const response = sharingDomainError(context, error)
|
|
42
|
+
if (response) return response
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createCapabilityService,
|
|
3
|
+
type CapabilityService,
|
|
4
|
+
type CapabilityServiceOptions
|
|
5
|
+
} from './capabilities/service'
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
capabilityHashMatches,
|
|
9
|
+
decryptContinuationToken,
|
|
10
|
+
encryptContinuationToken,
|
|
11
|
+
hashCapability
|
|
12
|
+
} from './crypto'
|
|
13
|
+
export {
|
|
14
|
+
DocumentShareInvalidError,
|
|
15
|
+
DocumentShareKindError,
|
|
16
|
+
DocumentShareSignInRequiredError,
|
|
17
|
+
InvitationDeliveryError
|
|
18
|
+
} from './errors'
|
|
19
|
+
export { createGrantService, type GrantService } from './grants/service'
|
|
20
|
+
|
|
21
|
+
export { createPublicDocumentRoutes } from './document-routes'
|
|
22
|
+
export {
|
|
23
|
+
createInvitationService,
|
|
24
|
+
type InvitationService,
|
|
25
|
+
type InvitationServiceOptions
|
|
26
|
+
} from './invitations/service'
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
createDocumentSharingRoutes,
|
|
30
|
+
createPublicSharingRoutes,
|
|
31
|
+
type SharingRouteEnvironment
|
|
32
|
+
} from './routes'
|
|
33
|
+
export {
|
|
34
|
+
createDocumentSharingService,
|
|
35
|
+
type DocumentShareCapability,
|
|
36
|
+
type DocumentSharingServiceOptions,
|
|
37
|
+
type DocumentSharingService,
|
|
38
|
+
type ResolvedDocumentShare,
|
|
39
|
+
type ResolvedSharePrincipal
|
|
40
|
+
} from './service'
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AcceptDocumentInvitationInput,
|
|
3
|
+
AcceptedInvitation,
|
|
4
|
+
CreateDocumentInvitationInput,
|
|
5
|
+
CreateInvitationContinuationInput,
|
|
6
|
+
DocumentInvitation,
|
|
7
|
+
InvitationPreview,
|
|
8
|
+
WorkspaceRole
|
|
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 { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
14
|
+
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
15
|
+
import type { Kysely } from 'kysely'
|
|
16
|
+
|
|
17
|
+
import { randomHex } from '@pairdraw/scene-graph/random'
|
|
18
|
+
|
|
19
|
+
import { requireSharingAccess } from '../access'
|
|
20
|
+
import {
|
|
21
|
+
createInvitationContinuationService,
|
|
22
|
+
invitationMatchesActor,
|
|
23
|
+
invitationTokenIsActive,
|
|
24
|
+
recipientHint
|
|
25
|
+
} from '../continuation'
|
|
26
|
+
import { dateString, grantContract, invitationContract } from '../contracts'
|
|
27
|
+
import { hashCapability } from '../crypto'
|
|
28
|
+
import { DocumentShareInvalidError, InvitationDeliveryError } from '../errors'
|
|
29
|
+
|
|
30
|
+
const INVITATION_SECRET_BYTES = 24
|
|
31
|
+
const INVITATION_LIFETIME_MS = 7 * 24 * 60 * 60_000
|
|
32
|
+
|
|
33
|
+
/** Workspace invitations share the invitation link; their ids are not document invitations. */
|
|
34
|
+
export type WorkspaceInvitationResolver = {
|
|
35
|
+
preview(invitationId: string, token: string): Promise<InvitationPreview | null>
|
|
36
|
+
accept(
|
|
37
|
+
actor: CloudActor,
|
|
38
|
+
invitationId: string,
|
|
39
|
+
token: string
|
|
40
|
+
): Promise<{ workspaceId: string; role: WorkspaceRole } | null>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type InvitationServiceOptions = {
|
|
44
|
+
workspaceInvitations?: WorkspaceInvitationResolver
|
|
45
|
+
continuationSecret?: string
|
|
46
|
+
delivery?: InvitationDelivery
|
|
47
|
+
outbox?: InvitationOutbox
|
|
48
|
+
publicURL?: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createInvitationService(
|
|
52
|
+
database: Kysely<CloudDatabase>,
|
|
53
|
+
options: InvitationServiceOptions
|
|
54
|
+
) {
|
|
55
|
+
async function previewDocumentInvitation(
|
|
56
|
+
invitationId: string,
|
|
57
|
+
input: AcceptDocumentInvitationInput
|
|
58
|
+
): Promise<InvitationPreview> {
|
|
59
|
+
const invitation = await database
|
|
60
|
+
.selectFrom('documentInvitation')
|
|
61
|
+
.innerJoin('document', 'document.id', 'documentInvitation.documentId')
|
|
62
|
+
.select([
|
|
63
|
+
'documentInvitation.emailNormalized',
|
|
64
|
+
'documentInvitation.permission',
|
|
65
|
+
'documentInvitation.tokenHash',
|
|
66
|
+
'documentInvitation.expiresAt',
|
|
67
|
+
'documentInvitation.acceptedAt',
|
|
68
|
+
'documentInvitation.revokedAt',
|
|
69
|
+
'documentInvitation.invitedBy',
|
|
70
|
+
'document.name as documentName'
|
|
71
|
+
])
|
|
72
|
+
.where('documentInvitation.id', '=', invitationId)
|
|
73
|
+
.executeTakeFirst()
|
|
74
|
+
if (!invitation || !invitationTokenIsActive(invitation, input.token)) {
|
|
75
|
+
throw new DocumentShareInvalidError()
|
|
76
|
+
}
|
|
77
|
+
const inviter = await database
|
|
78
|
+
.selectFrom('user')
|
|
79
|
+
.select('name')
|
|
80
|
+
.where('id', '=', invitation.invitedBy)
|
|
81
|
+
.executeTakeFirst()
|
|
82
|
+
return {
|
|
83
|
+
kind: 'document',
|
|
84
|
+
documentName: invitation.documentName,
|
|
85
|
+
inviterName: inviter?.name ?? 'A Pairdraw user',
|
|
86
|
+
permission: invitation.permission,
|
|
87
|
+
expiresAt: dateString(invitation.expiresAt) ?? '',
|
|
88
|
+
recipientHint: recipientHint(invitation.emailNormalized)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const continuation = options.continuationSecret
|
|
93
|
+
? createInvitationContinuationService(
|
|
94
|
+
database,
|
|
95
|
+
options.continuationSecret,
|
|
96
|
+
// Continuations are kept for document invitations only.
|
|
97
|
+
async (invitationId, token) => previewDocumentInvitation(invitationId, { token })
|
|
98
|
+
)
|
|
99
|
+
: null
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
async listInvitations(userId: string, documentId: string): Promise<DocumentInvitation[]> {
|
|
103
|
+
await requireSharingAccess(database, userId, documentId)
|
|
104
|
+
const rows = await database
|
|
105
|
+
.selectFrom('documentInvitation')
|
|
106
|
+
.select([
|
|
107
|
+
'id',
|
|
108
|
+
'documentId',
|
|
109
|
+
'emailNormalized',
|
|
110
|
+
'permission',
|
|
111
|
+
'invitedBy',
|
|
112
|
+
'invitedAt',
|
|
113
|
+
'expiresAt',
|
|
114
|
+
'acceptedAt'
|
|
115
|
+
])
|
|
116
|
+
.where('documentId', '=', documentId)
|
|
117
|
+
.where('revokedAt', 'is', null)
|
|
118
|
+
.orderBy('invitedAt')
|
|
119
|
+
.execute()
|
|
120
|
+
return rows.map(invitationContract)
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
async createInvitation(
|
|
124
|
+
userId: string,
|
|
125
|
+
documentId: string,
|
|
126
|
+
input: CreateDocumentInvitationInput
|
|
127
|
+
): Promise<{ invitation: DocumentInvitation; token: string }> {
|
|
128
|
+
await requireSharingAccess(database, userId, documentId)
|
|
129
|
+
const id = crypto.randomUUID()
|
|
130
|
+
const token = randomHex(INVITATION_SECRET_BYTES)
|
|
131
|
+
const expiresAt = new Date(Date.now() + INVITATION_LIFETIME_MS)
|
|
132
|
+
const deliveryContext =
|
|
133
|
+
(options.delivery || options.outbox) && options.publicURL
|
|
134
|
+
? await Promise.all([
|
|
135
|
+
database
|
|
136
|
+
.selectFrom('user')
|
|
137
|
+
.select('name')
|
|
138
|
+
.where('id', '=', userId)
|
|
139
|
+
.executeTakeFirst(),
|
|
140
|
+
database
|
|
141
|
+
.selectFrom('document')
|
|
142
|
+
.select('name')
|
|
143
|
+
.where('id', '=', documentId)
|
|
144
|
+
.executeTakeFirstOrThrow()
|
|
145
|
+
])
|
|
146
|
+
: null
|
|
147
|
+
const acceptanceURL =
|
|
148
|
+
deliveryContext && options.publicURL
|
|
149
|
+
? cloudInvitationURL(options.publicURL, id, token)
|
|
150
|
+
: null
|
|
151
|
+
const message =
|
|
152
|
+
deliveryContext && acceptanceURL
|
|
153
|
+
? {
|
|
154
|
+
deliveryId: id,
|
|
155
|
+
recipientEmail: input.email,
|
|
156
|
+
inviterName: deliveryContext[0]?.name ?? 'A Pairdraw user',
|
|
157
|
+
documentName: deliveryContext[1].name,
|
|
158
|
+
permission: input.permission,
|
|
159
|
+
expiresAt: expiresAt.toISOString(),
|
|
160
|
+
acceptanceURL
|
|
161
|
+
}
|
|
162
|
+
: null
|
|
163
|
+
const row = await database.transaction().execute(async (transaction) => {
|
|
164
|
+
const inserted = await transaction
|
|
165
|
+
.insertInto('documentInvitation')
|
|
166
|
+
.values({
|
|
167
|
+
id,
|
|
168
|
+
documentId,
|
|
169
|
+
emailNormalized: input.email,
|
|
170
|
+
permission: input.permission,
|
|
171
|
+
tokenHash: hashCapability(token),
|
|
172
|
+
invitedBy: userId,
|
|
173
|
+
expiresAt,
|
|
174
|
+
acceptedAt: null,
|
|
175
|
+
revokedAt: null
|
|
176
|
+
})
|
|
177
|
+
.returning([
|
|
178
|
+
'id',
|
|
179
|
+
'documentId',
|
|
180
|
+
'emailNormalized',
|
|
181
|
+
'permission',
|
|
182
|
+
'invitedBy',
|
|
183
|
+
'invitedAt',
|
|
184
|
+
'expiresAt',
|
|
185
|
+
'acceptedAt'
|
|
186
|
+
])
|
|
187
|
+
.executeTakeFirstOrThrow()
|
|
188
|
+
if (options.outbox && message) {
|
|
189
|
+
await options.outbox.enqueueDocumentInvitation(message, transaction)
|
|
190
|
+
}
|
|
191
|
+
return inserted
|
|
192
|
+
})
|
|
193
|
+
const invitation = invitationContract(row)
|
|
194
|
+
if (options.delivery && message && !options.outbox) {
|
|
195
|
+
try {
|
|
196
|
+
await options.delivery.sendDocumentInvitation(message)
|
|
197
|
+
} catch (error) {
|
|
198
|
+
await database
|
|
199
|
+
.updateTable('documentInvitation')
|
|
200
|
+
.set({ revokedAt: new Date() })
|
|
201
|
+
.where('id', '=', id)
|
|
202
|
+
.execute()
|
|
203
|
+
throw new InvitationDeliveryError('Invitation delivery failed', { cause: error })
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { invitation, token }
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
async createInvitationContinuation(input: CreateInvitationContinuationInput) {
|
|
210
|
+
if (!continuation) throw new DocumentShareInvalidError()
|
|
211
|
+
return continuation.create(input)
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
async consumeInvitationContinuation(id: string) {
|
|
215
|
+
if (!continuation) throw new DocumentShareInvalidError()
|
|
216
|
+
return continuation.consume(id)
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
/** What an invitation link offers: a document, or a workspace when the id is one of those. */
|
|
220
|
+
async previewInvitation(
|
|
221
|
+
invitationId: string,
|
|
222
|
+
input: AcceptDocumentInvitationInput
|
|
223
|
+
): Promise<InvitationPreview> {
|
|
224
|
+
const workspace = await options.workspaceInvitations?.preview(invitationId, input.token)
|
|
225
|
+
return workspace ?? previewDocumentInvitation(invitationId, input)
|
|
226
|
+
},
|
|
227
|
+
|
|
228
|
+
async acceptInvitation(
|
|
229
|
+
actor: CloudActor,
|
|
230
|
+
invitationId: string,
|
|
231
|
+
input: AcceptDocumentInvitationInput
|
|
232
|
+
): Promise<AcceptedInvitation> {
|
|
233
|
+
const workspace = await options.workspaceInvitations?.accept(actor, invitationId, input.token)
|
|
234
|
+
if (workspace) return { kind: 'workspace', ...workspace }
|
|
235
|
+
const invitation = await database
|
|
236
|
+
.selectFrom('documentInvitation')
|
|
237
|
+
.select([
|
|
238
|
+
'id',
|
|
239
|
+
'documentId',
|
|
240
|
+
'emailNormalized',
|
|
241
|
+
'permission',
|
|
242
|
+
'invitedBy',
|
|
243
|
+
'tokenHash',
|
|
244
|
+
'expiresAt',
|
|
245
|
+
'acceptedAt',
|
|
246
|
+
'revokedAt'
|
|
247
|
+
])
|
|
248
|
+
.where('id', '=', invitationId)
|
|
249
|
+
.executeTakeFirst()
|
|
250
|
+
if (
|
|
251
|
+
!invitation ||
|
|
252
|
+
!invitationMatchesActor(actor, invitation.emailNormalized) ||
|
|
253
|
+
!invitationTokenIsActive(invitation, input.token)
|
|
254
|
+
) {
|
|
255
|
+
throw new DocumentShareInvalidError()
|
|
256
|
+
}
|
|
257
|
+
return database.transaction().execute(async (transaction) => {
|
|
258
|
+
const row = await transaction
|
|
259
|
+
.insertInto('documentGrant')
|
|
260
|
+
.values({
|
|
261
|
+
id: crypto.randomUUID(),
|
|
262
|
+
documentId: invitation.documentId,
|
|
263
|
+
userId: actor.userId,
|
|
264
|
+
permission: invitation.permission,
|
|
265
|
+
// The grant is the inviter's, so "shared by" names them, not the recipient.
|
|
266
|
+
createdBy: invitation.invitedBy,
|
|
267
|
+
revokedAt: null
|
|
268
|
+
})
|
|
269
|
+
.onConflict((conflict) =>
|
|
270
|
+
conflict.columns(['documentId', 'userId']).doUpdateSet({
|
|
271
|
+
permission: invitation.permission,
|
|
272
|
+
createdBy: invitation.invitedBy,
|
|
273
|
+
revokedAt: null,
|
|
274
|
+
updatedAt: new Date()
|
|
275
|
+
})
|
|
276
|
+
)
|
|
277
|
+
.returning([
|
|
278
|
+
'id',
|
|
279
|
+
'documentId',
|
|
280
|
+
'userId',
|
|
281
|
+
'permission',
|
|
282
|
+
'createdBy',
|
|
283
|
+
'createdAt',
|
|
284
|
+
'updatedAt'
|
|
285
|
+
])
|
|
286
|
+
.executeTakeFirstOrThrow()
|
|
287
|
+
await transaction
|
|
288
|
+
.updateTable('documentInvitation')
|
|
289
|
+
.set({ acceptedAt: new Date() })
|
|
290
|
+
.where('id', '=', invitationId)
|
|
291
|
+
.where('acceptedAt', 'is', null)
|
|
292
|
+
.execute()
|
|
293
|
+
return { kind: 'document' as const, grant: grantContract(row) }
|
|
294
|
+
})
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
async revokeInvitation(
|
|
298
|
+
userId: string,
|
|
299
|
+
documentId: string,
|
|
300
|
+
invitationId: string
|
|
301
|
+
): Promise<void> {
|
|
302
|
+
await requireSharingAccess(database, userId, documentId)
|
|
303
|
+
const result = await database
|
|
304
|
+
.updateTable('documentInvitation')
|
|
305
|
+
.set({ revokedAt: new Date() })
|
|
306
|
+
.where('id', '=', invitationId)
|
|
307
|
+
.where('documentId', '=', documentId)
|
|
308
|
+
.where('revokedAt', 'is', null)
|
|
309
|
+
.executeTakeFirst()
|
|
310
|
+
if (Number(result.numUpdatedRows) === 0) throw new DocumentNotFoundError()
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export type InvitationService = ReturnType<typeof createInvitationService>
|