@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
|
@@ -32,9 +32,9 @@ export function staticEntitlementsFromTOML(
|
|
|
32
32
|
enabled: value.collaboration?.enabled,
|
|
33
33
|
maximumParticipants: value.collaboration?.maximum_participants
|
|
34
34
|
},
|
|
35
|
-
|
|
36
|
-
enabled: value.
|
|
37
|
-
monthlyCreditAllowance: value.
|
|
35
|
+
services: {
|
|
36
|
+
enabled: value.services?.enabled,
|
|
37
|
+
monthlyCreditAllowance: value.services?.monthly_credit_allowance
|
|
38
38
|
}
|
|
39
39
|
})
|
|
40
40
|
}
|
|
@@ -55,8 +55,8 @@ export function staticEntitlementValues(input: unknown): Record<string, boolean
|
|
|
55
55
|
[CLOUD_FEATURE_KEYS.guestPresence]: entitlements.sharing.guestPresence,
|
|
56
56
|
[CLOUD_FEATURE_KEYS.collaboration]: entitlements.collaboration.enabled,
|
|
57
57
|
[CLOUD_FEATURE_KEYS.maximumParticipants]: entitlements.collaboration.maximumParticipants,
|
|
58
|
-
[CLOUD_FEATURE_KEYS.
|
|
59
|
-
[CLOUD_FEATURE_KEYS.monthlyCreditAllowance]: entitlements.
|
|
58
|
+
[CLOUD_FEATURE_KEYS.cloudServices]: entitlements.services.enabled,
|
|
59
|
+
[CLOUD_FEATURE_KEYS.monthlyCreditAllowance]: entitlements.services.monthlyCreditAllowance
|
|
60
60
|
}
|
|
61
61
|
return pickBy(values, isNotNil) as Record<string, boolean | number>
|
|
62
62
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { createStorageQuotaService } from '#cloud/server/quota'
|
|
3
|
+
import type { Kysely } from 'kysely'
|
|
4
|
+
|
|
5
|
+
import { CLOUD_FEATURE_KEYS } from './keys'
|
|
6
|
+
import type { CloudPolicy } from './policy'
|
|
7
|
+
import { workspacePolicyContext } from './workspace'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Whether the owner of a workspace, or of a document's workspace, holds as much as their plan's
|
|
11
|
+
* storage allows or more. Their workspaces are then read-only until they free space or their plan
|
|
12
|
+
* grows: documents open and download, and can be deleted, but take no edits, uploads, or new
|
|
13
|
+
* documents. A plan that shrinks below what an account holds, as when a subscription lapses,
|
|
14
|
+
* reaches this the same way as filling it.
|
|
15
|
+
*/
|
|
16
|
+
export async function storageLimitReached(
|
|
17
|
+
database: Kysely<CloudDatabase>,
|
|
18
|
+
policy: CloudPolicy | undefined,
|
|
19
|
+
target: { workspaceId: string } | { documentId: string },
|
|
20
|
+
deploymentMode: 'official' | 'self-hosted' = 'self-hosted'
|
|
21
|
+
): Promise<boolean> {
|
|
22
|
+
if (!policy) return false
|
|
23
|
+
const context = await workspacePolicyContext(database, target, { deploymentMode })
|
|
24
|
+
if (!context) return false
|
|
25
|
+
const maximum = await policy.number(
|
|
26
|
+
CLOUD_FEATURE_KEYS.maximumStorageBytes,
|
|
27
|
+
Number.MAX_SAFE_INTEGER,
|
|
28
|
+
context
|
|
29
|
+
)
|
|
30
|
+
if (maximum === Number.MAX_SAFE_INTEGER) return false
|
|
31
|
+
const usage = await createStorageQuotaService(database).snapshot(context.workspaceId)
|
|
32
|
+
return usage !== null && usage.committedBytes >= maximum
|
|
33
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
3
|
+
|
|
4
|
+
import type { CloudPolicyContext } from './keys'
|
|
5
|
+
|
|
6
|
+
type WorkspacePolicyTarget = { workspaceId: string } | { documentId: string }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The policy context for a workspace, or for a document by its workspace: its owner's plan, which
|
|
10
|
+
* every workspace they own takes, so creating more workspaces adds no allowance. Null when the
|
|
11
|
+
* workspace or document is gone.
|
|
12
|
+
*/
|
|
13
|
+
export async function workspacePolicyContext(
|
|
14
|
+
database: Kysely<CloudDatabase> | Transaction<CloudDatabase>,
|
|
15
|
+
target: WorkspacePolicyTarget,
|
|
16
|
+
context: Pick<CloudPolicyContext, 'actorId' | 'deploymentMode'>
|
|
17
|
+
): Promise<(CloudPolicyContext & { workspaceId: string }) | null> {
|
|
18
|
+
const row =
|
|
19
|
+
'documentId' in target
|
|
20
|
+
? await database
|
|
21
|
+
.selectFrom('document')
|
|
22
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
23
|
+
.select(['workspace.id as workspaceId', 'workspace.ownerId'])
|
|
24
|
+
.where('document.id', '=', target.documentId)
|
|
25
|
+
.executeTakeFirst()
|
|
26
|
+
: await database
|
|
27
|
+
.selectFrom('workspace')
|
|
28
|
+
.select(['id as workspaceId', 'ownerId'])
|
|
29
|
+
.where('id', '=', target.workspaceId)
|
|
30
|
+
.executeTakeFirst()
|
|
31
|
+
if (!row) return null
|
|
32
|
+
const policyContext: CloudPolicyContext & { workspaceId: string } = {
|
|
33
|
+
targetingKey: row.ownerId,
|
|
34
|
+
subjectType: 'user',
|
|
35
|
+
workspaceId: row.workspaceId,
|
|
36
|
+
deploymentMode: context.deploymentMode
|
|
37
|
+
}
|
|
38
|
+
if (context.actorId) policyContext.actorId = context.actorId
|
|
39
|
+
if ('documentId' in target) policyContext.documentId = target.documentId
|
|
40
|
+
return policyContext
|
|
41
|
+
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createStorageLimitNoticeService,
|
|
3
|
+
type StorageLimitNoticeOptions,
|
|
4
|
+
type StorageLimitNoticeResult,
|
|
5
|
+
type StorageLimitNoticeService
|
|
6
|
+
} from './notices'
|
|
1
7
|
export { createStorageReconciliationService, type StorageReconciliation } from './reconcile'
|
|
2
8
|
export {
|
|
3
9
|
createStorageQuotaService,
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { CLOUD_ACCOUNT_PLAN_PATH } from '#cloud/contract'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
4
|
+
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
5
|
+
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
6
|
+
import type { Kysely } from 'kysely'
|
|
7
|
+
|
|
8
|
+
export type StorageLimitNoticeResult = {
|
|
9
|
+
/** Accounts told this pass that their storage is full. */
|
|
10
|
+
notified: number
|
|
11
|
+
/** Accounts back under their plan, which are told again if they fill it again. */
|
|
12
|
+
cleared: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type StorageLimitNoticeOptions = {
|
|
16
|
+
policy: CloudPolicy
|
|
17
|
+
email: TransactionalEmailService
|
|
18
|
+
publicURL: string
|
|
19
|
+
/** Whether the server sells plans, so the email offers more storage. */
|
|
20
|
+
upgradable: boolean
|
|
21
|
+
deploymentMode: 'official' | 'self-hosted'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Emails each account once when it holds all the storage its plan allows, which makes its
|
|
26
|
+
* workspaces read-only (`storageLimitReached`), whether it filled its plan or its plan shrank below
|
|
27
|
+
* what it holds, as when a subscription lapses. A pass compares every owner's committed storage
|
|
28
|
+
* with their plan, so it reaches both however the plan changed.
|
|
29
|
+
*/
|
|
30
|
+
export function createStorageLimitNoticeService(
|
|
31
|
+
database: Kysely<CloudDatabase>,
|
|
32
|
+
options: StorageLimitNoticeOptions
|
|
33
|
+
) {
|
|
34
|
+
/**
|
|
35
|
+
* Each account's limit: a number, null when its plan sets none, or undefined when the policy
|
|
36
|
+
* failed to answer, which decides nothing either way until a pass where it answers.
|
|
37
|
+
*/
|
|
38
|
+
async function limit(userId: string): Promise<number | null | undefined> {
|
|
39
|
+
const maximum = await options.policy.numberResolution(
|
|
40
|
+
CLOUD_FEATURE_KEYS.maximumStorageBytes,
|
|
41
|
+
Number.MAX_SAFE_INTEGER,
|
|
42
|
+
{ targetingKey: userId, subjectType: 'user', deploymentMode: options.deploymentMode }
|
|
43
|
+
)
|
|
44
|
+
if (maximum.failed) return undefined
|
|
45
|
+
return maximum.value === Number.MAX_SAFE_INTEGER ? null : maximum.value
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function notify(userId: string, usedBytes: number, limitBytes: number): Promise<boolean> {
|
|
49
|
+
return database.transaction().execute(async (transaction) => {
|
|
50
|
+
const notice = await transaction
|
|
51
|
+
.insertInto('storageLimitNotice')
|
|
52
|
+
.values({ userId })
|
|
53
|
+
.onConflict((conflict) => conflict.column('userId').doNothing())
|
|
54
|
+
.returning('notifiedAt')
|
|
55
|
+
.executeTakeFirst()
|
|
56
|
+
if (!notice) return false
|
|
57
|
+
const user = await transaction
|
|
58
|
+
.selectFrom('user')
|
|
59
|
+
.select(['name', 'email'])
|
|
60
|
+
.where('id', '=', userId)
|
|
61
|
+
.executeTakeFirst()
|
|
62
|
+
if (!user) return false
|
|
63
|
+
await options.email.enqueue(
|
|
64
|
+
{
|
|
65
|
+
idempotencyKey: `storage-limit-reached/${userId}/${new Date(notice.notifiedAt).getTime()}`,
|
|
66
|
+
kind: 'storage-limit-reached',
|
|
67
|
+
recipientEmail: user.email,
|
|
68
|
+
payload: {
|
|
69
|
+
name: user.name,
|
|
70
|
+
usedBytes,
|
|
71
|
+
limitBytes,
|
|
72
|
+
upgradable: options.upgradable,
|
|
73
|
+
actionURL: new URL(
|
|
74
|
+
options.upgradable ? CLOUD_ACCOUNT_PLAN_PATH : '/',
|
|
75
|
+
options.publicURL
|
|
76
|
+
).href
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
transaction
|
|
80
|
+
)
|
|
81
|
+
return true
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
async notifyStorageLimits(): Promise<StorageLimitNoticeResult> {
|
|
87
|
+
const [owners, notices] = await Promise.all([
|
|
88
|
+
database
|
|
89
|
+
.selectFrom('workspace')
|
|
90
|
+
.innerJoin('workspaceStorageUsage', 'workspaceStorageUsage.workspaceId', 'workspace.id')
|
|
91
|
+
.select((expression) => [
|
|
92
|
+
'workspace.ownerId',
|
|
93
|
+
expression.fn.sum<string | number>('workspaceStorageUsage.committedBytes').as('bytes')
|
|
94
|
+
])
|
|
95
|
+
.groupBy('workspace.ownerId')
|
|
96
|
+
.execute(),
|
|
97
|
+
database.selectFrom('storageLimitNotice').select('userId').execute()
|
|
98
|
+
])
|
|
99
|
+
const usage = new Map(owners.map((owner) => [owner.ownerId, Number(owner.bytes)]))
|
|
100
|
+
const result: StorageLimitNoticeResult = { notified: 0, cleared: 0 }
|
|
101
|
+
for (const [userId, usedBytes] of usage) {
|
|
102
|
+
const limitBytes = await limit(userId)
|
|
103
|
+
if (typeof limitBytes === 'number' && usedBytes >= limitBytes) {
|
|
104
|
+
if (await notify(userId, usedBytes, limitBytes)) result.notified++
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// A notice whose account holds less than its plan allows again, or nothing at all, is done.
|
|
108
|
+
for (const { userId } of notices) {
|
|
109
|
+
const usedBytes = usage.get(userId) ?? 0
|
|
110
|
+
const limitBytes = await limit(userId)
|
|
111
|
+
if (limitBytes === undefined) continue
|
|
112
|
+
if (limitBytes !== null && usedBytes >= limitBytes) continue
|
|
113
|
+
await database.deleteFrom('storageLimitNotice').where('userId', '=', userId).execute()
|
|
114
|
+
result.cleared++
|
|
115
|
+
}
|
|
116
|
+
return result
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type StorageLimitNoticeService = ReturnType<typeof createStorageLimitNoticeService>
|
|
@@ -28,6 +28,8 @@ export function createStorageReconciliationService(database: Kysely<CloudDatabas
|
|
|
28
28
|
.select(['storageObject.id', 'storageObject.byteSize'])
|
|
29
29
|
.distinct()
|
|
30
30
|
.where('document.workspaceId', '=', workspace.id)
|
|
31
|
+
// A document deleted forever came off the usage then, though its files wait out a grace.
|
|
32
|
+
.where('document.purgedAt', 'is', null)
|
|
31
33
|
// Images are the workspace's once, whichever revisions use them.
|
|
32
34
|
.union((union) =>
|
|
33
35
|
union
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
2
3
|
import type { Kysely, Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export class StorageQuotaExceededError extends Error {
|
|
@@ -12,37 +13,66 @@ export type StorageQuotaSnapshot = {
|
|
|
12
13
|
|
|
13
14
|
type StorageDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Takes the lock an owner's storage allowance is checked under, until the transaction ends.
|
|
18
|
+
* Reservations hold it while they count and reserve; an ownership transfer holds it for both
|
|
19
|
+
* owners, so no reservation counts a workspace under an owner it is leaving or joining.
|
|
20
|
+
*/
|
|
21
|
+
export function lockStorageAllowance(
|
|
22
|
+
transaction: Transaction<CloudDatabase>,
|
|
23
|
+
ownerId: string
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
return lockForTransaction(transaction, `storage-quota:${ownerId}`)
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
16
|
-
async function
|
|
29
|
+
async function ownerOf(executor: StorageDatabase, workspaceId: string): Promise<string | null> {
|
|
30
|
+
const workspace = await executor
|
|
31
|
+
.selectFrom('workspace')
|
|
32
|
+
.select('ownerId')
|
|
33
|
+
.where('id', '=', workspaceId)
|
|
34
|
+
.executeTakeFirst()
|
|
35
|
+
return workspace?.ownerId ?? null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What `ownerId` holds and has reserved across every workspace they own, which all draw from
|
|
40
|
+
* the owner's one allowance. Both totals come from one statement, so an upload committing
|
|
41
|
+
* meanwhile counts once, as committed or as reserved.
|
|
42
|
+
*/
|
|
43
|
+
async function usage(
|
|
17
44
|
executor: StorageDatabase,
|
|
18
|
-
|
|
45
|
+
ownerId: string,
|
|
19
46
|
now = new Date()
|
|
20
47
|
): Promise<StorageQuotaSnapshot> {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const owned = executor.selectFrom('workspace').select('id').where('ownerId', '=', ownerId)
|
|
49
|
+
const row = await executor
|
|
50
|
+
.selectNoFrom((expression) => [
|
|
51
|
+
expression
|
|
52
|
+
.selectFrom('workspaceStorageUsage')
|
|
53
|
+
.select((sum) =>
|
|
54
|
+
sum.fn.coalesce(sum.fn.sum('committedBytes'), sum.val(0)).as('committedBytes')
|
|
55
|
+
)
|
|
56
|
+
.where('workspaceId', 'in', owned)
|
|
57
|
+
.as('committedBytes'),
|
|
58
|
+
expression
|
|
59
|
+
.selectFrom('uploadStorageReservation')
|
|
60
|
+
.select((sum) => sum.fn.coalesce(sum.fn.sum('bytes'), sum.val(0)).as('reservedBytes'))
|
|
61
|
+
.where('workspaceId', 'in', owned)
|
|
62
|
+
.where('committedAt', 'is', null)
|
|
63
|
+
.where('releasedAt', 'is', null)
|
|
64
|
+
.where('expiresAt', '>', now)
|
|
65
|
+
.as('reservedBytes')
|
|
66
|
+
])
|
|
67
|
+
.executeTakeFirstOrThrow()
|
|
38
68
|
return {
|
|
39
|
-
committedBytes: Number(
|
|
40
|
-
reservedBytes: Number(
|
|
69
|
+
committedBytes: Number(row.committedBytes ?? 0),
|
|
70
|
+
reservedBytes: Number(row.reservedBytes ?? 0)
|
|
41
71
|
}
|
|
42
72
|
}
|
|
43
73
|
|
|
44
74
|
async function reserve(
|
|
45
|
-
executor:
|
|
75
|
+
executor: Transaction<CloudDatabase>,
|
|
46
76
|
input: {
|
|
47
77
|
workspaceId: string
|
|
48
78
|
uploadId: string
|
|
@@ -51,21 +81,25 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
51
81
|
maximumBytes: number | null
|
|
52
82
|
}
|
|
53
83
|
): Promise<void> {
|
|
84
|
+
// The allowance spans the owner's workspaces, so the owner is what reservations queue on.
|
|
85
|
+
// An ownership transfer holds the same lock, so the owner read once it is held stays put.
|
|
86
|
+
let ownerId = await ownerOf(executor, input.workspaceId)
|
|
87
|
+
for (;;) {
|
|
88
|
+
if (ownerId === null) throw new Error('Workspace not found')
|
|
89
|
+
await lockStorageAllowance(executor, ownerId)
|
|
90
|
+
const current = await ownerOf(executor, input.workspaceId)
|
|
91
|
+
if (current === ownerId) break
|
|
92
|
+
ownerId = current
|
|
93
|
+
}
|
|
54
94
|
await executor
|
|
55
95
|
.insertInto('workspaceStorageUsage')
|
|
56
96
|
.values({ workspaceId: input.workspaceId, committedBytes: 0 })
|
|
57
97
|
.onConflict((conflict) => conflict.column('workspaceId').doNothing())
|
|
58
98
|
.execute()
|
|
59
|
-
await executor
|
|
60
|
-
.selectFrom('workspaceStorageUsage')
|
|
61
|
-
.select('workspaceId')
|
|
62
|
-
.where('workspaceId', '=', input.workspaceId)
|
|
63
|
-
.forUpdate()
|
|
64
|
-
.executeTakeFirstOrThrow()
|
|
65
|
-
const usage = await snapshot(executor, input.workspaceId)
|
|
99
|
+
const held = await usage(executor, ownerId)
|
|
66
100
|
if (
|
|
67
101
|
input.maximumBytes !== null &&
|
|
68
|
-
|
|
102
|
+
held.committedBytes + held.reservedBytes + input.bytes > input.maximumBytes
|
|
69
103
|
) {
|
|
70
104
|
throw new StorageQuotaExceededError('Workspace storage quota exceeded')
|
|
71
105
|
}
|
|
@@ -119,7 +153,16 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
119
153
|
}
|
|
120
154
|
|
|
121
155
|
return {
|
|
122
|
-
|
|
156
|
+
/** The pooled usage of the workspace's owner, or null when the workspace is gone. */
|
|
157
|
+
async snapshot(workspaceId: string): Promise<StorageQuotaSnapshot | null> {
|
|
158
|
+
const ownerId = await ownerOf(database, workspaceId)
|
|
159
|
+
return ownerId === null ? null : usage(database, ownerId)
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
/** What `ownerId` holds and has reserved across every workspace they own. */
|
|
163
|
+
async account(ownerId: string): Promise<StorageQuotaSnapshot> {
|
|
164
|
+
return usage(database, ownerId)
|
|
165
|
+
},
|
|
123
166
|
|
|
124
167
|
async reserve(input: {
|
|
125
168
|
workspaceId: string
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { INVITATION_BUDGET } from '#cloud/server/invitations'
|
|
2
3
|
import type { Kysely } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export function createRateLimitCleanupService(database: Kysely<CloudDatabase>) {
|
|
5
6
|
return {
|
|
6
7
|
async cleanupExpired(before: Date): Promise<number> {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const [limits, invitations] = await Promise.all([
|
|
9
|
+
database
|
|
10
|
+
.deleteFrom('apiRateLimit')
|
|
11
|
+
.where('windowStartedAt', '<', before)
|
|
12
|
+
.executeTakeFirst(),
|
|
13
|
+
// Never a send the invitation budget still counts, whatever cutoff the sweep passes.
|
|
14
|
+
database
|
|
15
|
+
.deleteFrom('invitationSend')
|
|
16
|
+
.where(
|
|
17
|
+
'sentAt',
|
|
18
|
+
'<',
|
|
19
|
+
new Date(Math.min(before.getTime(), Date.now() - INVITATION_BUDGET.windowMs))
|
|
20
|
+
)
|
|
21
|
+
.executeTakeFirst()
|
|
22
|
+
])
|
|
23
|
+
return Number(limits.numDeletedRows) + Number(invitations.numDeletedRows)
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
@@ -23,6 +23,9 @@ export type ResourceRateLimitPolicy = RateLimitPolicy & { actorLimit: number }
|
|
|
23
23
|
|
|
24
24
|
export const CLOUD_RATE_LIMITS = {
|
|
25
25
|
publicCapability: { namespace: 'public-capability', windowMs: 60_000, limit: 30 },
|
|
26
|
+
// Everything one visitor reads of the community gallery, across its paths: a page of thumbnails
|
|
27
|
+
// fits, made-up addresses do not each get a fresh budget.
|
|
28
|
+
communityRead: { namespace: 'community-read', windowMs: 60_000, limit: 300 },
|
|
26
29
|
collaborationTicket: {
|
|
27
30
|
namespace: 'collaboration-ticket',
|
|
28
31
|
windowMs: 60_000,
|
|
@@ -44,7 +47,8 @@ export const CLOUD_RATE_LIMITS = {
|
|
|
44
47
|
},
|
|
45
48
|
uploadCreation: { namespace: 'upload-creation', windowMs: 60_000, limit: 120, actorLimit: 600 },
|
|
46
49
|
authenticatedMutation: { namespace: 'authenticated-mutation', windowMs: 60_000, limit: 120 },
|
|
47
|
-
|
|
50
|
+
servicesToken: { namespace: 'services-token', windowMs: 60_000, limit: 30 },
|
|
51
|
+
billingToken: { namespace: 'billing-token', windowMs: 60_000, limit: 30 },
|
|
48
52
|
adminRead: { namespace: 'admin-read', windowMs: 60_000, limit: 300 },
|
|
49
53
|
adminMutation: { namespace: 'admin-mutation', windowMs: 60_000, limit: 30 }
|
|
50
54
|
} as const satisfies Record<string, RateLimitPolicy | ResourceRateLimitPolicy>
|
|
@@ -220,6 +224,6 @@ function setBudgetHeaders(context: Context, use: RateLimitUse, windowMs: number)
|
|
|
220
224
|
export function routeParamResource(name: string) {
|
|
221
225
|
return (context: Context): string =>
|
|
222
226
|
// Lowercased as validation lowercases UUIDs, so one row's spellings share a budget.
|
|
223
|
-
// eslint-disable-next-line open-pencil/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
227
|
+
// eslint-disable-next-line @open-pencil/security/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
224
228
|
context.req.param(name)?.toLowerCase() ?? 'unknown'
|
|
225
229
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { tellReporters, type ReportReplies } from './replies'
|
|
2
|
+
export { createPublicReportRoutes } from './routes'
|
|
3
|
+
export {
|
|
4
|
+
createDocumentReportService,
|
|
5
|
+
DOCUMENT_REPORT_BUDGET,
|
|
6
|
+
DocumentReportLimitError,
|
|
7
|
+
type DocumentReportService,
|
|
8
|
+
type DocumentReportServiceOptions
|
|
9
|
+
} from './service'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
3
|
+
import type { Transaction } from 'kysely'
|
|
4
|
+
|
|
5
|
+
/** How reporters hear back: through email, with the deployment's abuse address to disagree. */
|
|
6
|
+
export type ReportReplies = {
|
|
7
|
+
email?: TransactionalEmailService
|
|
8
|
+
/** Where a reporter writes to disagree with a decision: the deployment's abuse address. */
|
|
9
|
+
contactEmail?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Tells each reporter who left an address what the administrators decided about their report:
|
|
14
|
+
* the document blocked, the report acted on some other way, or the document left as it was. Only
|
|
15
|
+
* a block says `blocked`, so a reporter is never told of one that didn't happen. Each report is answered once, in the transaction that
|
|
16
|
+
* decides it, so a decision that rolls back tells nobody.
|
|
17
|
+
*/
|
|
18
|
+
export async function tellReporters(
|
|
19
|
+
transaction: Transaction<CloudDatabase>,
|
|
20
|
+
replies: ReportReplies,
|
|
21
|
+
reportIds: readonly string[],
|
|
22
|
+
outcome: 'blocked' | 'actioned' | 'dismissed'
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
const { email } = replies
|
|
25
|
+
if (!email || reportIds.length === 0) return
|
|
26
|
+
const reports = await transaction
|
|
27
|
+
.selectFrom('documentReport')
|
|
28
|
+
.innerJoin('document', 'document.id', 'documentReport.documentId')
|
|
29
|
+
.select([
|
|
30
|
+
'documentReport.id',
|
|
31
|
+
'documentReport.replyEmail',
|
|
32
|
+
'documentReport.replyLocale',
|
|
33
|
+
'document.name'
|
|
34
|
+
])
|
|
35
|
+
.where('documentReport.id', 'in', reportIds)
|
|
36
|
+
.where('documentReport.replyEmail', 'is not', null)
|
|
37
|
+
.execute()
|
|
38
|
+
for (const report of reports) {
|
|
39
|
+
if (!report.replyEmail) continue
|
|
40
|
+
await email.enqueue(
|
|
41
|
+
{
|
|
42
|
+
idempotencyKey: `document-report-decided/${report.id}`,
|
|
43
|
+
kind: 'document-report-decided',
|
|
44
|
+
recipientEmail: report.replyEmail,
|
|
45
|
+
fallbackLocale: report.replyLocale,
|
|
46
|
+
payload: {
|
|
47
|
+
documentName: report.name,
|
|
48
|
+
outcome,
|
|
49
|
+
contactEmail: replies.contactEmail ?? null
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
transaction
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { parseFileDocumentReport } from '#cloud/contract'
|
|
2
|
+
import { acceptedLocale } from '#cloud/email'
|
|
3
|
+
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
4
|
+
import { shareActor } from '#cloud/server/sharing/actor'
|
|
5
|
+
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
6
|
+
import { shareParams, validatedJSON } from '#cloud/server/validation'
|
|
7
|
+
import { Hono } from 'hono'
|
|
8
|
+
|
|
9
|
+
import { DocumentReportLimitError, type DocumentReportService } from './service'
|
|
10
|
+
|
|
11
|
+
/** Reports about documents shared by link, which anyone the link lets in may file. */
|
|
12
|
+
export function createPublicReportRoutes(
|
|
13
|
+
service: DocumentReportService,
|
|
14
|
+
resolveSession?: CloudSessionResolver
|
|
15
|
+
) {
|
|
16
|
+
return new Hono().post(
|
|
17
|
+
'/shares/:shareId/reports',
|
|
18
|
+
shareParams,
|
|
19
|
+
validatedJSON(parseFileDocumentReport),
|
|
20
|
+
async (context) => {
|
|
21
|
+
try {
|
|
22
|
+
return await sharingRoute(context, async () => {
|
|
23
|
+
await service.file(
|
|
24
|
+
context.req.valid('param').shareId,
|
|
25
|
+
context.req.valid('json'),
|
|
26
|
+
await shareActor(context.req.raw, resolveSession),
|
|
27
|
+
acceptedLocale(context.req.header('accept-language'))
|
|
28
|
+
)
|
|
29
|
+
return context.json({ received: true as const }, 201)
|
|
30
|
+
})
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error instanceof DocumentReportLimitError) {
|
|
33
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
34
|
+
}
|
|
35
|
+
throw error
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
}
|