@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { FileDocumentReportInput } from '#cloud/contract'
|
|
2
|
+
import type { EmailLocale } from '#cloud/email'
|
|
3
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
4
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
5
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
6
|
+
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
7
|
+
import type { Kysely } from 'kysely'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* How many reports one person, or guest, may file about one link in a day, and how many a link
|
|
11
|
+
* may gather from everyone, since a guest names themselves and could otherwise flood the queue.
|
|
12
|
+
*/
|
|
13
|
+
export const DOCUMENT_REPORT_BUDGET = {
|
|
14
|
+
windowMs: 24 * 60 * 60 * 1000,
|
|
15
|
+
perReporter: 3,
|
|
16
|
+
perShare: 50
|
|
17
|
+
} as const
|
|
18
|
+
|
|
19
|
+
/** How much of a description an administrator's email quotes; the console shows all of it. */
|
|
20
|
+
const EMAIL_DESCRIPTION_CHARACTERS = 500
|
|
21
|
+
|
|
22
|
+
/** A report past the reporter's or the link's daily budget. */
|
|
23
|
+
export class DocumentReportLimitError extends Error {
|
|
24
|
+
override readonly name = 'DocumentReportLimitError'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type DocumentReportServiceOptions = {
|
|
28
|
+
sharing: Pick<DocumentSharingService, 'resolveShare'>
|
|
29
|
+
email?: TransactionalEmailService
|
|
30
|
+
/** Who hears of a new report: the deployment's administrators. */
|
|
31
|
+
adminRecipients: readonly string[]
|
|
32
|
+
publicURL: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createDocumentReportService(
|
|
36
|
+
database: Kysely<CloudDatabase>,
|
|
37
|
+
options: DocumentReportServiceOptions
|
|
38
|
+
) {
|
|
39
|
+
return {
|
|
40
|
+
/**
|
|
41
|
+
* Files a report about the document a share link opens, for someone the link lets in, and
|
|
42
|
+
* tells the deployment's administrators. It answers nothing about the document, so a report
|
|
43
|
+
* never tells its reporter more than the link already did.
|
|
44
|
+
*/
|
|
45
|
+
async file(
|
|
46
|
+
shareId: string,
|
|
47
|
+
input: FileDocumentReportInput,
|
|
48
|
+
actor?: CloudActor,
|
|
49
|
+
/** The language the reporter's browser asked for, to reply in without an account's own. */
|
|
50
|
+
replyLocale: EmailLocale | null = null
|
|
51
|
+
): Promise<void> {
|
|
52
|
+
const resolved = await options.sharing.resolveShare(
|
|
53
|
+
shareId,
|
|
54
|
+
{ secret: input.secret, guestId: input.guestId },
|
|
55
|
+
actor
|
|
56
|
+
)
|
|
57
|
+
const reporterUserId = actor?.userId ?? null
|
|
58
|
+
const reporterGuestId = actor ? null : (input.guestId ?? null)
|
|
59
|
+
const since = new Date(Date.now() - DOCUMENT_REPORT_BUDGET.windowMs)
|
|
60
|
+
await database.transaction().execute(async (transaction) => {
|
|
61
|
+
const [byReporter, byShare] = await Promise.all([
|
|
62
|
+
transaction
|
|
63
|
+
.selectFrom('documentReport')
|
|
64
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
65
|
+
.where('shareId', '=', shareId)
|
|
66
|
+
.where('createdAt', '>', since)
|
|
67
|
+
.where((expression) => {
|
|
68
|
+
if (reporterUserId) return expression('reporterUserId', '=', reporterUserId)
|
|
69
|
+
if (reporterGuestId) return expression('reporterGuestId', '=', reporterGuestId)
|
|
70
|
+
return expression.and([
|
|
71
|
+
expression('reporterUserId', 'is', null),
|
|
72
|
+
expression('reporterGuestId', 'is', null)
|
|
73
|
+
])
|
|
74
|
+
})
|
|
75
|
+
.executeTakeFirstOrThrow(),
|
|
76
|
+
transaction
|
|
77
|
+
.selectFrom('documentReport')
|
|
78
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
79
|
+
.where('shareId', '=', shareId)
|
|
80
|
+
.where('createdAt', '>', since)
|
|
81
|
+
.executeTakeFirstOrThrow()
|
|
82
|
+
])
|
|
83
|
+
if (
|
|
84
|
+
Number(byReporter.count) >= DOCUMENT_REPORT_BUDGET.perReporter ||
|
|
85
|
+
Number(byShare.count) >= DOCUMENT_REPORT_BUDGET.perShare
|
|
86
|
+
) {
|
|
87
|
+
throw new DocumentReportLimitError()
|
|
88
|
+
}
|
|
89
|
+
const id = crypto.randomUUID()
|
|
90
|
+
const replyEmail = input.replyEmail ?? actor?.email ?? null
|
|
91
|
+
await transaction
|
|
92
|
+
.insertInto('documentReport')
|
|
93
|
+
.values({
|
|
94
|
+
id,
|
|
95
|
+
documentId: resolved.documentId,
|
|
96
|
+
shareId,
|
|
97
|
+
reporterUserId,
|
|
98
|
+
reporterGuestId,
|
|
99
|
+
replyEmail,
|
|
100
|
+
replyLocale,
|
|
101
|
+
reason: input.reason,
|
|
102
|
+
description: input.description
|
|
103
|
+
})
|
|
104
|
+
.execute()
|
|
105
|
+
if (!options.email) return
|
|
106
|
+
const document = await transaction
|
|
107
|
+
.selectFrom('document')
|
|
108
|
+
.select('name')
|
|
109
|
+
.where('id', '=', resolved.documentId)
|
|
110
|
+
.executeTakeFirstOrThrow()
|
|
111
|
+
// Whoever left an address hears that the report arrived, and later what was decided.
|
|
112
|
+
if (replyEmail) {
|
|
113
|
+
await options.email.enqueue(
|
|
114
|
+
{
|
|
115
|
+
idempotencyKey: `document-report-received/${id}`,
|
|
116
|
+
kind: 'document-report-received',
|
|
117
|
+
recipientEmail: replyEmail,
|
|
118
|
+
fallbackLocale: replyLocale,
|
|
119
|
+
payload: { documentName: document.name }
|
|
120
|
+
},
|
|
121
|
+
transaction
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
for (const recipientEmail of options.adminRecipients) {
|
|
125
|
+
await options.email.enqueue(
|
|
126
|
+
{
|
|
127
|
+
idempotencyKey: `document-report/${id}/${recipientEmail}`,
|
|
128
|
+
kind: 'document-report',
|
|
129
|
+
recipientEmail,
|
|
130
|
+
payload: {
|
|
131
|
+
documentName: document.name,
|
|
132
|
+
reason: input.reason,
|
|
133
|
+
description: input.description.slice(0, EMAIL_DESCRIPTION_CHARACTERS),
|
|
134
|
+
actionURL: new URL('/admin/reports', options.publicURL).href
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
transaction
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export type DocumentReportService = ReturnType<typeof createDocumentReportService>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { ServicesNotEntitledError } from './errors'
|
|
2
|
+
export { importServicesSigningKey, loadServicesSigningKey, type ServicesSigningKey } from './keys'
|
|
3
|
+
export { createServicesRoutes, type ServicesRouteEnvironment } from './routes'
|
|
4
|
+
export {
|
|
5
|
+
createServicesTokenService,
|
|
6
|
+
type ServicesTokenService,
|
|
7
|
+
type ServicesTokenServiceOptions
|
|
8
|
+
} from './tokens'
|
|
@@ -3,18 +3,18 @@ import { calculateJwkThumbprint, exportJWK, importPKCS8, type JWK } from 'jose'
|
|
|
3
3
|
|
|
4
4
|
const ALGORITHM = 'ES256'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type ServicesSigningKey = {
|
|
7
7
|
privateKey: CryptoKey
|
|
8
8
|
/** The public half, as published in the JWKS. */
|
|
9
9
|
publicJWK: JWK & { kid: string }
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Imports the P-256 key that signs
|
|
13
|
+
* Imports the P-256 key that signs service tokens. Its `kid` is the public key's thumbprint,
|
|
14
14
|
* so replacing the key changes the `kid`, and a service that caches the JWKS refetches it on the
|
|
15
15
|
* first token it cannot match. Tokens the old key signed fail once and are re-requested.
|
|
16
16
|
*/
|
|
17
|
-
export async function
|
|
17
|
+
export async function importServicesSigningKey(pem: string): Promise<ServicesSigningKey> {
|
|
18
18
|
const privateKey = await importPKCS8(pem, ALGORITHM, { extractable: true })
|
|
19
19
|
const { kty, crv, x, y } = await exportJWK(privateKey)
|
|
20
20
|
const publicJWK = { kty, crv, x, y }
|
|
@@ -27,17 +27,17 @@ export async function importAISigningKey(pem: string): Promise<AISigningKey> {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Imports the configured signing key at startup, so a key that cannot sign stops the runtime
|
|
30
|
-
* instead of failing every token request after discovery has advertised
|
|
30
|
+
* instead of failing every token request after discovery has advertised services.
|
|
31
31
|
*/
|
|
32
|
-
export async function
|
|
33
|
-
config: Pick<CloudServerConfig, '
|
|
34
|
-
): Promise<
|
|
35
|
-
if (!config.
|
|
32
|
+
export async function loadServicesSigningKey(
|
|
33
|
+
config: Pick<CloudServerConfig, 'servicesSigningKey'>
|
|
34
|
+
): Promise<ServicesSigningKey | undefined> {
|
|
35
|
+
if (!config.servicesSigningKey) return undefined
|
|
36
36
|
try {
|
|
37
|
-
return await
|
|
37
|
+
return await importServicesSigningKey(config.servicesSigningKey)
|
|
38
38
|
} catch {
|
|
39
|
-
throw new CloudConfigError('
|
|
39
|
+
throw new CloudConfigError('Services signing key must be a PKCS #8 P-256 private key')
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export const
|
|
43
|
+
export const SERVICES_SIGNING_ALGORITHM = ALGORITHM
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { parseCreateServicesToken } from '#cloud/contract'
|
|
2
|
+
import type { CloudActor } from '#cloud/server/auth'
|
|
3
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
+
import { DocumentNotFoundError } from '#cloud/server/documents'
|
|
5
|
+
import { CLOUD_RATE_LIMITS, createRouteRateLimiter } from '#cloud/server/rate-limit'
|
|
6
|
+
import { validatedJSON } from '#cloud/server/validation'
|
|
7
|
+
import { Hono } from 'hono'
|
|
8
|
+
import type { Kysely } from 'kysely'
|
|
9
|
+
|
|
10
|
+
import { ServicesNotEntitledError } from './errors'
|
|
11
|
+
import type { ServicesTokenService } from './tokens'
|
|
12
|
+
|
|
13
|
+
export type ServicesRouteEnvironment = { Variables: { actor: CloudActor } }
|
|
14
|
+
|
|
15
|
+
/** Without a service, the deployment offers no services and the token route is not found. */
|
|
16
|
+
export function createServicesRoutes(
|
|
17
|
+
service: ServicesTokenService | undefined,
|
|
18
|
+
rateLimit?: { database: Kysely<CloudDatabase>; secret: string }
|
|
19
|
+
) {
|
|
20
|
+
const router = new Hono<ServicesRouteEnvironment>()
|
|
21
|
+
if (service && rateLimit) {
|
|
22
|
+
router.use(
|
|
23
|
+
'/services/token',
|
|
24
|
+
createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.servicesToken)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
return router.post(
|
|
28
|
+
'/services/token',
|
|
29
|
+
validatedJSON(parseCreateServicesToken),
|
|
30
|
+
async (context) => {
|
|
31
|
+
if (!service) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
32
|
+
try {
|
|
33
|
+
return context.json(
|
|
34
|
+
await service.issue(context.get('actor'), context.req.valid('json').workspaceId)
|
|
35
|
+
)
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error instanceof DocumentNotFoundError) {
|
|
38
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
39
|
+
}
|
|
40
|
+
if (error instanceof ServicesNotEntitledError) {
|
|
41
|
+
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
42
|
+
}
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
}
|
|
@@ -1,59 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CLOUD_SERVICES_TOKEN_AUDIENCE, type ServicesToken } from '#cloud/contract'
|
|
2
2
|
import type { CloudActor } from '#cloud/server/auth'
|
|
3
3
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
4
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
5
5
|
import { workspaceRole } from '#cloud/server/documents/repository'
|
|
6
6
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
7
7
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
8
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
8
9
|
import { SignJWT } from 'jose'
|
|
9
10
|
import type { Kysely } from 'kysely'
|
|
10
11
|
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
12
|
+
import { ServicesNotEntitledError } from './errors'
|
|
13
|
+
import { SERVICES_SIGNING_ALGORITHM, type ServicesSigningKey } from './keys'
|
|
13
14
|
|
|
14
|
-
export type
|
|
15
|
+
export type ServicesTokenServiceOptions = {
|
|
15
16
|
database: Kysely<CloudDatabase>
|
|
16
17
|
policy: CloudPolicy
|
|
17
18
|
deploymentMode: 'official' | 'self-hosted'
|
|
18
19
|
/** The Cloud public URL, which services check as the token's `iss`. */
|
|
19
20
|
issuer: string
|
|
20
|
-
signingKey:
|
|
21
|
+
signingKey: ServicesSigningKey
|
|
21
22
|
lifetimeSeconds: number
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export function
|
|
25
|
+
export function createServicesTokenService(options: ServicesTokenServiceOptions) {
|
|
25
26
|
const { privateKey, publicJWK } = options.signingKey
|
|
26
27
|
|
|
27
28
|
return {
|
|
28
29
|
/**
|
|
29
|
-
* Signs a token that lets a workspace member reach the
|
|
30
|
-
* Entitlements are read for every token, so a plan change reaches the service within one
|
|
30
|
+
* Signs a token that lets a workspace member reach the services on the workspace's behalf,
|
|
31
|
+
* spending its owner's credits. Entitlements are read for every token, so a plan change reaches the service within one
|
|
31
32
|
* token lifetime.
|
|
32
33
|
*/
|
|
33
|
-
async issue(actor: CloudActor, workspaceId: string): Promise<
|
|
34
|
+
async issue(actor: CloudActor, workspaceId: string): Promise<ServicesToken> {
|
|
34
35
|
if (!(await workspaceRole(options.database, actor.userId, workspaceId))) {
|
|
35
36
|
throw new DocumentNotFoundError()
|
|
36
37
|
}
|
|
37
|
-
const context =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
const context = await workspacePolicyContext(
|
|
39
|
+
options.database,
|
|
40
|
+
{ workspaceId },
|
|
41
|
+
{ actorId: actor.userId, deploymentMode: options.deploymentMode }
|
|
42
|
+
)
|
|
43
|
+
if (!context) throw new DocumentNotFoundError()
|
|
43
44
|
const [enabled, allowance] = await Promise.all([
|
|
44
|
-
options.policy.boolean(CLOUD_FEATURE_KEYS.
|
|
45
|
+
options.policy.boolean(CLOUD_FEATURE_KEYS.cloudServices, false, context),
|
|
45
46
|
options.policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context)
|
|
46
47
|
])
|
|
47
|
-
if (!enabled) throw new
|
|
48
|
+
if (!enabled) throw new ServicesNotEntitledError()
|
|
48
49
|
const issuedAt = Math.floor(Date.now() / 1000)
|
|
49
50
|
const expiresAt = issuedAt + options.lifetimeSeconds
|
|
50
51
|
const token = await new SignJWT({
|
|
51
52
|
workspaceId,
|
|
53
|
+
// Credits are the owner's, shared by every workspace they own.
|
|
54
|
+
accountId: context.targetingKey,
|
|
52
55
|
monthlyCreditAllowance: allowance < 0 ? null : allowance
|
|
53
56
|
})
|
|
54
|
-
.setProtectedHeader({ alg:
|
|
57
|
+
.setProtectedHeader({ alg: SERVICES_SIGNING_ALGORITHM, kid: publicJWK.kid, typ: 'JWT' })
|
|
55
58
|
.setIssuer(options.issuer)
|
|
56
|
-
.setAudience(
|
|
59
|
+
.setAudience(CLOUD_SERVICES_TOKEN_AUDIENCE)
|
|
57
60
|
.setSubject(actor.userId)
|
|
58
61
|
.setIssuedAt(issuedAt)
|
|
59
62
|
.setExpirationTime(expiresAt)
|
|
@@ -67,4 +70,4 @@ export function createAITokenService(options: AITokenServiceOptions) {
|
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
export type
|
|
73
|
+
export type ServicesTokenService = ReturnType<typeof createServicesTokenService>
|
|
@@ -6,11 +6,13 @@ import type {
|
|
|
6
6
|
UpdateDocumentShareInput
|
|
7
7
|
} from '#cloud/contract'
|
|
8
8
|
import type { CloudActor } from '#cloud/server/auth'
|
|
9
|
-
import type
|
|
9
|
+
import { isBannedNow, type CloudDatabase } from '#cloud/server/db'
|
|
10
10
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
11
11
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
12
12
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
13
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
13
14
|
import type { Kysely, UpdateObject } from 'kysely'
|
|
15
|
+
import * as v from 'valibot'
|
|
14
16
|
|
|
15
17
|
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
16
18
|
|
|
@@ -22,6 +24,26 @@ import { createSharingPolicy } from '../policy'
|
|
|
22
24
|
|
|
23
25
|
const SHARE_SECRET_BYTES = 24
|
|
24
26
|
|
|
27
|
+
/** An account ID as the user table keys it; other IDs in `createdBy` name no account. */
|
|
28
|
+
const accountIdSchema = v.pipe(v.string(), v.uuid())
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether any of these people is banned, for a ban that has not run out. A link stops working
|
|
32
|
+
* while whoever made it, or made its document, is banned, so what a banned person put up is no
|
|
33
|
+
* longer public. The IDs are matched as the user table keys them, so the lookup uses its index.
|
|
34
|
+
*/
|
|
35
|
+
async function anyBanned(database: Kysely<CloudDatabase>, userIds: string[]): Promise<boolean> {
|
|
36
|
+
const accounts = userIds.filter((id) => v.is(accountIdSchema, id))
|
|
37
|
+
if (accounts.length === 0) return false
|
|
38
|
+
const banned = await database
|
|
39
|
+
.selectFrom('user')
|
|
40
|
+
.select('id')
|
|
41
|
+
.where('id', 'in', accounts)
|
|
42
|
+
.where(isBannedNow)
|
|
43
|
+
.executeTakeFirst()
|
|
44
|
+
return banned !== undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
25
47
|
export type DocumentShareCapability = {
|
|
26
48
|
share: DocumentShare
|
|
27
49
|
secret: string
|
|
@@ -34,7 +56,10 @@ export type ResolvedSharePrincipal =
|
|
|
34
56
|
|
|
35
57
|
export type ResolvedDocumentShare = {
|
|
36
58
|
documentId: string
|
|
59
|
+
/** What this person may do: the link's permission, or view for a guest who may not edit. */
|
|
37
60
|
permission: DocumentPermission
|
|
61
|
+
/** What the link lets someone signed in to the server do. */
|
|
62
|
+
linkPermission: DocumentPermission
|
|
38
63
|
principal: ResolvedSharePrincipal
|
|
39
64
|
roomEpoch: number
|
|
40
65
|
}
|
|
@@ -50,6 +75,33 @@ export function createCapabilityService(
|
|
|
50
75
|
) {
|
|
51
76
|
const sharingPolicy = createSharingPolicy(database, options)
|
|
52
77
|
|
|
78
|
+
/**
|
|
79
|
+
* What a guest may do through a link: edit only where guests may edit, otherwise view where
|
|
80
|
+
* guests may view, so an edit link still shows the document to someone not signed in. `null`
|
|
81
|
+
* means the guest signs in first. Without a policy, guests get the link's own permission.
|
|
82
|
+
*/
|
|
83
|
+
async function guestPermission(
|
|
84
|
+
documentId: string,
|
|
85
|
+
linkPermission: DocumentPermission
|
|
86
|
+
): Promise<DocumentPermission | null> {
|
|
87
|
+
const policy = options.policy
|
|
88
|
+
if (!policy) return linkPermission
|
|
89
|
+
// A workspace takes its owner's plan.
|
|
90
|
+
const context = await workspacePolicyContext(
|
|
91
|
+
database,
|
|
92
|
+
{ documentId },
|
|
93
|
+
{ deploymentMode: options.deploymentMode ?? 'self-hosted' }
|
|
94
|
+
)
|
|
95
|
+
if (!context) return null
|
|
96
|
+
if (
|
|
97
|
+
linkPermission === 'edit' &&
|
|
98
|
+
(await policy.boolean(CLOUD_FEATURE_KEYS.guestEdit, false, context))
|
|
99
|
+
) {
|
|
100
|
+
return 'edit'
|
|
101
|
+
}
|
|
102
|
+
return (await policy.boolean(CLOUD_FEATURE_KEYS.guestView, false, context)) ? 'view' : null
|
|
103
|
+
}
|
|
104
|
+
|
|
53
105
|
async function updateActiveShare(
|
|
54
106
|
userId: string,
|
|
55
107
|
documentId: string,
|
|
@@ -205,38 +257,30 @@ export function createCapabilityService(
|
|
|
205
257
|
'documentShare.permission',
|
|
206
258
|
'documentShare.secretHash',
|
|
207
259
|
'documentShare.roomEpoch',
|
|
208
|
-
'documentShare.expiresAt'
|
|
260
|
+
'documentShare.expiresAt',
|
|
261
|
+
'documentShare.createdBy as linkMaker',
|
|
262
|
+
'document.createdBy as documentMaker'
|
|
209
263
|
])
|
|
210
264
|
.where('documentShare.id', '=', shareId)
|
|
211
265
|
.where('documentShare.revokedAt', 'is', null)
|
|
212
266
|
.where('document.deletedAt', 'is', null)
|
|
267
|
+
// A document an administrator took down opens for nobody.
|
|
268
|
+
.where('document.disabledAt', 'is', null)
|
|
213
269
|
.executeTakeFirst()
|
|
214
270
|
if (
|
|
215
271
|
!row ||
|
|
216
272
|
!capabilityHashMatches(row.secretHash, input.secret) ||
|
|
217
|
-
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now())
|
|
273
|
+
(row.expiresAt && new Date(row.expiresAt).getTime() <= Date.now()) ||
|
|
274
|
+
(await anyBanned(database, [row.linkMaker, row.documentMaker]))
|
|
218
275
|
) {
|
|
219
276
|
throw new DocumentShareInvalidError()
|
|
220
277
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
const featureKey =
|
|
228
|
-
row.permission === 'edit' ? CLOUD_FEATURE_KEYS.guestEdit : CLOUD_FEATURE_KEYS.guestView
|
|
229
|
-
if (
|
|
230
|
-
!(await options.policy.boolean(featureKey, false, {
|
|
231
|
-
targetingKey: document.workspaceId,
|
|
232
|
-
workspaceId: document.workspaceId,
|
|
233
|
-
documentId: row.documentId,
|
|
234
|
-
deploymentMode: options.deploymentMode ?? 'self-hosted'
|
|
235
|
-
}))
|
|
236
|
-
) {
|
|
237
|
-
// Guests are not let in here; people signed in to the server are.
|
|
238
|
-
throw new DocumentShareSignInRequiredError()
|
|
239
|
-
}
|
|
278
|
+
const permission = actor
|
|
279
|
+
? row.permission
|
|
280
|
+
: await guestPermission(row.documentId, row.permission)
|
|
281
|
+
if (!permission) {
|
|
282
|
+
// Guests are not let in here; people signed in to the server are.
|
|
283
|
+
throw new DocumentShareSignInRequiredError()
|
|
240
284
|
}
|
|
241
285
|
await database
|
|
242
286
|
.updateTable('documentShare')
|
|
@@ -245,7 +289,8 @@ export function createCapabilityService(
|
|
|
245
289
|
.execute()
|
|
246
290
|
return {
|
|
247
291
|
documentId: row.documentId,
|
|
248
|
-
permission
|
|
292
|
+
permission,
|
|
293
|
+
linkPermission: row.permission,
|
|
249
294
|
principal: actor
|
|
250
295
|
? { kind: 'user', userId: actor.userId, name: actor.name, email: actor.email }
|
|
251
296
|
: {
|
|
@@ -20,6 +20,10 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
20
20
|
return rows.map(grantContract)
|
|
21
21
|
},
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Changes the permission of someone who already has access. Access itself is only given by an
|
|
25
|
+
* invitation the person accepts, so nobody can put a document in a stranger's files.
|
|
26
|
+
*/
|
|
23
27
|
async putGrant(
|
|
24
28
|
userId: string,
|
|
25
29
|
documentId: string,
|
|
@@ -28,23 +32,11 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
28
32
|
): Promise<DocumentGrant> {
|
|
29
33
|
await requireSharingAccess(database, userId, documentId)
|
|
30
34
|
const row = await database
|
|
31
|
-
.
|
|
32
|
-
.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
)
|
|
35
|
+
.updateTable('documentGrant')
|
|
36
|
+
.set({ permission: input.permission, createdBy: userId, updatedAt: new Date() })
|
|
37
|
+
.where('documentId', '=', documentId)
|
|
38
|
+
.where('userId', '=', targetUserId)
|
|
39
|
+
.where('revokedAt', 'is', null)
|
|
48
40
|
.returning([
|
|
49
41
|
'id',
|
|
50
42
|
'documentId',
|
|
@@ -54,7 +46,8 @@ export function createGrantService(database: Kysely<CloudDatabase>) {
|
|
|
54
46
|
'createdAt',
|
|
55
47
|
'updatedAt'
|
|
56
48
|
])
|
|
57
|
-
.
|
|
49
|
+
.executeTakeFirst()
|
|
50
|
+
if (!row) throw new DocumentNotFoundError()
|
|
58
51
|
return grantContract(row)
|
|
59
52
|
},
|
|
60
53
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents'
|
|
2
|
+
import { InvitationLimitError } from '#cloud/server/invitations'
|
|
2
3
|
import {
|
|
3
4
|
DocumentShareInvalidError,
|
|
4
5
|
DocumentShareSignInRequiredError,
|
|
@@ -21,6 +22,9 @@ export function sharingDomainError(context: Context, error: unknown): Response |
|
|
|
21
22
|
if (error instanceof InvitationDeliveryError) {
|
|
22
23
|
return context.json({ error: { code: 'invitation_delivery_failed' as const } }, 502)
|
|
23
24
|
}
|
|
25
|
+
if (error instanceof InvitationLimitError) {
|
|
26
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
27
|
+
}
|
|
24
28
|
if (error instanceof DocumentForbiddenError) {
|
|
25
29
|
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
26
30
|
}
|
|
@@ -11,7 +11,12 @@ import { cloudInvitationURL } from '#cloud/contract'
|
|
|
11
11
|
import type { CloudActor } from '#cloud/server/auth'
|
|
12
12
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
13
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
14
|
-
import
|
|
14
|
+
import {
|
|
15
|
+
spendInvitationBudget,
|
|
16
|
+
type DocumentInvitationMessage,
|
|
17
|
+
type InvitationDelivery,
|
|
18
|
+
type InvitationOutbox
|
|
19
|
+
} from '#cloud/server/invitations'
|
|
15
20
|
import type { Kysely } from 'kysely'
|
|
16
21
|
|
|
17
22
|
import { randomHex } from '@open-pencil/scene-graph/random'
|
|
@@ -70,6 +75,8 @@ export function createInvitationService(
|
|
|
70
75
|
'document.name as documentName'
|
|
71
76
|
])
|
|
72
77
|
.where('documentInvitation.id', '=', invitationId)
|
|
78
|
+
// A taken-down document's invitations name nothing until it is restored.
|
|
79
|
+
.where('document.disabledAt', 'is', null)
|
|
73
80
|
.executeTakeFirst()
|
|
74
81
|
if (!invitation || !invitationTokenIsActive(invitation, input.token)) {
|
|
75
82
|
throw new DocumentShareInvalidError()
|
|
@@ -134,26 +141,35 @@ export function createInvitationService(
|
|
|
134
141
|
? await Promise.all([
|
|
135
142
|
database
|
|
136
143
|
.selectFrom('user')
|
|
137
|
-
.select('name')
|
|
144
|
+
.select(['name', 'locale'])
|
|
138
145
|
.where('id', '=', userId)
|
|
139
146
|
.executeTakeFirst(),
|
|
140
147
|
database
|
|
141
148
|
.selectFrom('document')
|
|
142
149
|
.select('name')
|
|
143
150
|
.where('id', '=', documentId)
|
|
144
|
-
.executeTakeFirstOrThrow()
|
|
151
|
+
.executeTakeFirstOrThrow(),
|
|
152
|
+
database
|
|
153
|
+
.selectFrom('user')
|
|
154
|
+
.select('locale')
|
|
155
|
+
.where((expression) =>
|
|
156
|
+
expression(expression.fn('lower', [expression.ref('email')]), '=', input.email)
|
|
157
|
+
)
|
|
158
|
+
.executeTakeFirst()
|
|
145
159
|
])
|
|
146
160
|
: null
|
|
147
161
|
const acceptanceURL =
|
|
148
162
|
deliveryContext && options.publicURL
|
|
149
163
|
? cloudInvitationURL(options.publicURL, id, token)
|
|
150
164
|
: null
|
|
151
|
-
const message =
|
|
165
|
+
const message: DocumentInvitationMessage | null =
|
|
152
166
|
deliveryContext && acceptanceURL
|
|
153
167
|
? {
|
|
154
168
|
deliveryId: id,
|
|
155
169
|
recipientEmail: input.email,
|
|
156
|
-
|
|
170
|
+
// Someone without an account yet reads the inviter's language.
|
|
171
|
+
locale: deliveryContext[2]?.locale ?? deliveryContext[0]?.locale ?? 'en',
|
|
172
|
+
inviterName: deliveryContext[0]?.name ?? null,
|
|
157
173
|
documentName: deliveryContext[1].name,
|
|
158
174
|
permission: input.permission,
|
|
159
175
|
expiresAt: expiresAt.toISOString(),
|
|
@@ -161,6 +177,7 @@ export function createInvitationService(
|
|
|
161
177
|
}
|
|
162
178
|
: null
|
|
163
179
|
const row = await database.transaction().execute(async (transaction) => {
|
|
180
|
+
await spendInvitationBudget(transaction, { senderId: userId, email: input.email })
|
|
164
181
|
const inserted = await transaction
|
|
165
182
|
.insertInto('documentInvitation')
|
|
166
183
|
.values({
|
|
@@ -234,18 +251,21 @@ export function createInvitationService(
|
|
|
234
251
|
if (workspace) return { kind: 'workspace', ...workspace }
|
|
235
252
|
const invitation = await database
|
|
236
253
|
.selectFrom('documentInvitation')
|
|
254
|
+
.innerJoin('document', 'document.id', 'documentInvitation.documentId')
|
|
237
255
|
.select([
|
|
238
|
-
'id',
|
|
239
|
-
'documentId',
|
|
240
|
-
'emailNormalized',
|
|
241
|
-
'permission',
|
|
242
|
-
'invitedBy',
|
|
243
|
-
'tokenHash',
|
|
244
|
-
'expiresAt',
|
|
245
|
-
'acceptedAt',
|
|
246
|
-
'revokedAt'
|
|
256
|
+
'documentInvitation.id',
|
|
257
|
+
'documentInvitation.documentId',
|
|
258
|
+
'documentInvitation.emailNormalized',
|
|
259
|
+
'documentInvitation.permission',
|
|
260
|
+
'documentInvitation.invitedBy',
|
|
261
|
+
'documentInvitation.tokenHash',
|
|
262
|
+
'documentInvitation.expiresAt',
|
|
263
|
+
'documentInvitation.acceptedAt',
|
|
264
|
+
'documentInvitation.revokedAt'
|
|
247
265
|
])
|
|
248
|
-
.where('id', '=', invitationId)
|
|
266
|
+
.where('documentInvitation.id', '=', invitationId)
|
|
267
|
+
// Refused, not consumed, while the document is taken down, so it works again on restore.
|
|
268
|
+
.where('document.disabledAt', 'is', null)
|
|
249
269
|
.executeTakeFirst()
|
|
250
270
|
if (
|
|
251
271
|
!invitation ||
|