@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,431 @@
|
|
|
1
|
+
import { AdminDomainError } from '#cloud/admin/errors'
|
|
2
|
+
import {
|
|
3
|
+
containsPattern,
|
|
4
|
+
countEachFilter,
|
|
5
|
+
listSearch,
|
|
6
|
+
listWindow,
|
|
7
|
+
type AdminListQuery,
|
|
8
|
+
type ListFilters
|
|
9
|
+
} from '#cloud/admin/list'
|
|
10
|
+
import { isUUID } from '#cloud/admin/schema'
|
|
11
|
+
import {
|
|
12
|
+
cloudAdminDocumentFilterSchema,
|
|
13
|
+
type CloudAdminDocument,
|
|
14
|
+
type CloudAdminDocumentFilter
|
|
15
|
+
} from '#cloud/contract'
|
|
16
|
+
import { userIdText, type CloudDatabase } from '#cloud/server/db'
|
|
17
|
+
import { unmarkPurged } from '#cloud/server/documents/purge'
|
|
18
|
+
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
19
|
+
import { tellReporters } from '#cloud/server/reports'
|
|
20
|
+
import { dateString } from '#cloud/server/sharing/contracts'
|
|
21
|
+
import { uniq } from 'es-toolkit'
|
|
22
|
+
import type { ExpressionBuilder, Kysely, Transaction } from 'kysely'
|
|
23
|
+
|
|
24
|
+
type DocumentExpression = ExpressionBuilder<CloudDatabase, 'document'>
|
|
25
|
+
|
|
26
|
+
/** Whether reports about the document are still waiting for a decision. */
|
|
27
|
+
const reported = (expression: DocumentExpression) =>
|
|
28
|
+
expression.exists(
|
|
29
|
+
expression
|
|
30
|
+
.selectFrom('documentReport')
|
|
31
|
+
.select('documentReport.id')
|
|
32
|
+
.whereRef('documentReport.documentId', '=', 'document.id')
|
|
33
|
+
.where('documentReport.status', '=', 'open')
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
const live = (expression: DocumentExpression) => expression('document.deletedAt', 'is', null)
|
|
37
|
+
|
|
38
|
+
const FILTERS: ListFilters<CloudAdminDocumentFilter, 'document'> = {
|
|
39
|
+
all: live,
|
|
40
|
+
reported: (expression) => expression.and([live(expression), reported(expression)]),
|
|
41
|
+
disabled: (expression) =>
|
|
42
|
+
expression.and([live(expression), expression('document.disabledAt', 'is not', null)]),
|
|
43
|
+
purged: (expression) => expression('document.purgedAt', 'is not', null)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What the console lists: live documents, and those deleted forever an administrator can still
|
|
48
|
+
* restore, inside the grace and not yet being destroyed.
|
|
49
|
+
*/
|
|
50
|
+
function listable(graceSince: Date) {
|
|
51
|
+
return (expression: DocumentExpression) =>
|
|
52
|
+
expression.or([
|
|
53
|
+
live(expression),
|
|
54
|
+
expression.and([
|
|
55
|
+
expression('document.purgedAt', '>', graceSince),
|
|
56
|
+
expression('document.cleanupClaimId', 'is', null)
|
|
57
|
+
])
|
|
58
|
+
])
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Documents whose name, ID, or maker's name or email has the search in it. */
|
|
62
|
+
function searchCondition(search: string) {
|
|
63
|
+
const pattern = containsPattern(search)
|
|
64
|
+
return (expression: DocumentExpression) =>
|
|
65
|
+
expression.or([
|
|
66
|
+
expression('document.name', 'ilike', pattern),
|
|
67
|
+
expression(expression.cast('document.id', 'text'), '=', search.toLowerCase()),
|
|
68
|
+
expression.exists(
|
|
69
|
+
expression
|
|
70
|
+
.selectFrom('user')
|
|
71
|
+
.select('user.id')
|
|
72
|
+
.whereRef(userIdText, '=', 'document.createdBy')
|
|
73
|
+
.where((inner) =>
|
|
74
|
+
inner.or([inner('user.email', 'ilike', pattern), inner('user.name', 'ilike', pattern)])
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
])
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The documents with these IDs as the console shows them, in no particular order; with
|
|
82
|
+
* `purgeGraceMs`, when cleanup destroys one deleted forever.
|
|
83
|
+
*/
|
|
84
|
+
export async function adminDocuments(
|
|
85
|
+
database: Kysely<CloudDatabase>,
|
|
86
|
+
documentIds: readonly string[],
|
|
87
|
+
purgeGraceMs?: number
|
|
88
|
+
): Promise<Map<string, CloudAdminDocument>> {
|
|
89
|
+
const ids = uniq(documentIds).filter(isUUID)
|
|
90
|
+
if (ids.length === 0) return new Map()
|
|
91
|
+
const rows = await database
|
|
92
|
+
.selectFrom('document')
|
|
93
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
94
|
+
.select((expression) => [
|
|
95
|
+
'document.id',
|
|
96
|
+
'document.name',
|
|
97
|
+
'document.createdBy',
|
|
98
|
+
'document.createdAt',
|
|
99
|
+
'document.updatedAt',
|
|
100
|
+
'document.disabledAt',
|
|
101
|
+
'document.disabledReason',
|
|
102
|
+
'document.purgedAt',
|
|
103
|
+
'workspace.id as workspaceId',
|
|
104
|
+
'workspace.name as workspaceName',
|
|
105
|
+
expression
|
|
106
|
+
.selectFrom('documentReport')
|
|
107
|
+
.select((inner) => inner.fn.countAll<string>().as('count'))
|
|
108
|
+
.whereRef('documentReport.documentId', '=', 'document.id')
|
|
109
|
+
.where('documentReport.status', '=', 'open')
|
|
110
|
+
.as('openReports')
|
|
111
|
+
])
|
|
112
|
+
.where('document.id', 'in', ids)
|
|
113
|
+
.execute()
|
|
114
|
+
const ownerIds = uniq(rows.map((row) => row.createdBy)).filter(isUUID)
|
|
115
|
+
const owners = ownerIds.length
|
|
116
|
+
? await database
|
|
117
|
+
.selectFrom('user')
|
|
118
|
+
.select(['id', 'name', 'email'])
|
|
119
|
+
.where('id', 'in', ownerIds)
|
|
120
|
+
.execute()
|
|
121
|
+
: []
|
|
122
|
+
const ownerById = new Map(owners.map((owner) => [owner.id, owner]))
|
|
123
|
+
return new Map(
|
|
124
|
+
rows.map((row) => [
|
|
125
|
+
row.id,
|
|
126
|
+
{
|
|
127
|
+
id: row.id,
|
|
128
|
+
name: row.name,
|
|
129
|
+
workspace: { id: row.workspaceId, name: row.workspaceName },
|
|
130
|
+
owner: ownerById.get(row.createdBy) ?? null,
|
|
131
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
132
|
+
updatedAt: dateString(row.updatedAt) ?? '',
|
|
133
|
+
disabledAt: dateString(row.disabledAt),
|
|
134
|
+
disabledReason: row.disabledReason,
|
|
135
|
+
openReports: Number(row.openReports ?? 0),
|
|
136
|
+
purgedAt: dateString(row.purgedAt),
|
|
137
|
+
destroyedAt:
|
|
138
|
+
row.purgedAt === null || purgeGraceMs === undefined
|
|
139
|
+
? null
|
|
140
|
+
: new Date(new Date(row.purgedAt).getTime() + purgeGraceMs).toISOString()
|
|
141
|
+
}
|
|
142
|
+
])
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
type AuditDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
147
|
+
|
|
148
|
+
/** Records an administrator's action on a document, a link, or a report. */
|
|
149
|
+
export async function auditContentAction(
|
|
150
|
+
database: AuditDatabase,
|
|
151
|
+
actorId: string,
|
|
152
|
+
action: string,
|
|
153
|
+
subject: { type: 'document' | 'document-share' | 'document-report'; id: string },
|
|
154
|
+
metadata: Record<string, unknown> = {}
|
|
155
|
+
): Promise<void> {
|
|
156
|
+
await database
|
|
157
|
+
.insertInto('adminAuditEvent')
|
|
158
|
+
.values({
|
|
159
|
+
id: crypto.randomUUID(),
|
|
160
|
+
actorUserId: actorId,
|
|
161
|
+
action,
|
|
162
|
+
subjectType: subject.type,
|
|
163
|
+
subjectId: subject.id,
|
|
164
|
+
metadata
|
|
165
|
+
})
|
|
166
|
+
.execute()
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A document that is not deleted, held until the transaction ends; none is not found. */
|
|
170
|
+
function lockedDocument(transaction: Transaction<CloudDatabase>, documentId: string) {
|
|
171
|
+
return transaction
|
|
172
|
+
.selectFrom('document')
|
|
173
|
+
.select(['id', 'disabledAt'])
|
|
174
|
+
.where('id', '=', documentId)
|
|
175
|
+
.where('deletedAt', 'is', null)
|
|
176
|
+
.forUpdate()
|
|
177
|
+
.executeTakeFirstOrThrow()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type AdminDocumentServiceOptions = {
|
|
181
|
+
/**
|
|
182
|
+
* Ends a taken-down document's live rooms, saving them, so a takedown reaches open tabs at once
|
|
183
|
+
* and a restore loses no edit.
|
|
184
|
+
*/
|
|
185
|
+
closeRooms?: (documentId: string) => void
|
|
186
|
+
/** How long a document deleted forever waits before cleanup destroys it; 0 when not given. */
|
|
187
|
+
purgeGraceMs?: number
|
|
188
|
+
/**
|
|
189
|
+
* Tells the owner of a document's workspace when it is blocked and restored. Without it, or
|
|
190
|
+
* without the server's address its link needs, nobody is told.
|
|
191
|
+
*/
|
|
192
|
+
email?: TransactionalEmailService
|
|
193
|
+
publicURL?: string
|
|
194
|
+
/** Where an owner writes to contest a block: the deployment's abuse address. */
|
|
195
|
+
contactEmail?: string
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The owner of a document's workspace, and the document's name, to tell about a block. */
|
|
199
|
+
async function documentOwner(transaction: Transaction<CloudDatabase>, documentId: string) {
|
|
200
|
+
const document = await transaction
|
|
201
|
+
.selectFrom('document')
|
|
202
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
203
|
+
.select(['document.name', 'workspace.ownerId'])
|
|
204
|
+
.where('document.id', '=', documentId)
|
|
205
|
+
.executeTakeFirst()
|
|
206
|
+
if (!document) return undefined
|
|
207
|
+
const owner = await transaction
|
|
208
|
+
.selectFrom('user')
|
|
209
|
+
.select(['name', 'email'])
|
|
210
|
+
.where((expression) => expression(userIdText(expression), '=', document.ownerId))
|
|
211
|
+
.executeTakeFirst()
|
|
212
|
+
return owner && { ...owner, documentName: document.name }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function createAdminDocumentService(
|
|
216
|
+
database: Kysely<CloudDatabase>,
|
|
217
|
+
options: AdminDocumentServiceOptions = {}
|
|
218
|
+
) {
|
|
219
|
+
const purgeGraceMs = options.purgeGraceMs ?? 0
|
|
220
|
+
const { email, publicURL } = options
|
|
221
|
+
const graceSince = (now: Date) => new Date(now.getTime() - purgeGraceMs)
|
|
222
|
+
return {
|
|
223
|
+
/** A page of documents, newest change first, and how many match the search under each filter. */
|
|
224
|
+
async list(query: AdminListQuery<CloudAdminDocumentFilter> = {}) {
|
|
225
|
+
const search = listSearch(query)
|
|
226
|
+
const filter = query.filter ?? 'all'
|
|
227
|
+
const { limit, offset } = listWindow(query)
|
|
228
|
+
let searched = database.selectFrom('document').where(listable(graceSince(new Date())))
|
|
229
|
+
if (search) searched = searched.where(searchCondition(search))
|
|
230
|
+
const counts = await countEachFilter(
|
|
231
|
+
searched,
|
|
232
|
+
cloudAdminDocumentFilterSchema.options,
|
|
233
|
+
FILTERS
|
|
234
|
+
)
|
|
235
|
+
const page = await searched
|
|
236
|
+
.where(FILTERS[filter])
|
|
237
|
+
.select('document.id')
|
|
238
|
+
.orderBy('document.updatedAt', 'desc')
|
|
239
|
+
.orderBy('document.id')
|
|
240
|
+
.limit(limit)
|
|
241
|
+
.offset(offset)
|
|
242
|
+
.execute()
|
|
243
|
+
const documents = await adminDocuments(
|
|
244
|
+
database,
|
|
245
|
+
page.map((row) => row.id),
|
|
246
|
+
purgeGraceMs
|
|
247
|
+
)
|
|
248
|
+
return {
|
|
249
|
+
documents: page.flatMap((row) => documents.get(row.id) ?? []),
|
|
250
|
+
total: counts[filter],
|
|
251
|
+
counts
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Takes a document down: it opens for nobody, its owner included, its links stop working,
|
|
257
|
+
* and its live rooms close, saved. Its open reports count as acted on. Nothing is deleted and
|
|
258
|
+
* its collaboration epoch stays, so enabling it gives everything back, live edits included.
|
|
259
|
+
*/
|
|
260
|
+
async disable(actorId: string, documentId: string, reason: string | undefined): Promise<void> {
|
|
261
|
+
await database.transaction().execute(async (transaction) => {
|
|
262
|
+
const document = await lockedDocument(transaction, documentId)
|
|
263
|
+
if (document.disabledAt) return
|
|
264
|
+
const disabledAt = new Date()
|
|
265
|
+
await transaction
|
|
266
|
+
.updateTable('document')
|
|
267
|
+
.set({ disabledAt, disabledReason: reason ?? null })
|
|
268
|
+
.where('id', '=', documentId)
|
|
269
|
+
.execute()
|
|
270
|
+
const reports = await transaction
|
|
271
|
+
.updateTable('documentReport')
|
|
272
|
+
.set({ status: 'actioned', resolvedAt: new Date(), resolvedBy: actorId })
|
|
273
|
+
.where('documentId', '=', documentId)
|
|
274
|
+
.where('status', '=', 'open')
|
|
275
|
+
.returning('id')
|
|
276
|
+
.execute()
|
|
277
|
+
// Everyone who reported it and left an address hears it was blocked.
|
|
278
|
+
await tellReporters(
|
|
279
|
+
transaction,
|
|
280
|
+
{ email, contactEmail: options.contactEmail },
|
|
281
|
+
reports.map((report) => report.id),
|
|
282
|
+
'blocked'
|
|
283
|
+
)
|
|
284
|
+
await auditContentAction(
|
|
285
|
+
transaction,
|
|
286
|
+
actorId,
|
|
287
|
+
'document.disabled',
|
|
288
|
+
{ type: 'document', id: documentId },
|
|
289
|
+
reason ? { reason } : {}
|
|
290
|
+
)
|
|
291
|
+
// Told in the same transaction, so a block that rolls back sends nothing.
|
|
292
|
+
const owner = email && publicURL && (await documentOwner(transaction, documentId))
|
|
293
|
+
if (owner) {
|
|
294
|
+
await email.enqueue(
|
|
295
|
+
{
|
|
296
|
+
idempotencyKey: `document-blocked/${documentId}/${disabledAt.getTime()}`,
|
|
297
|
+
kind: 'document-blocked',
|
|
298
|
+
recipientEmail: owner.email,
|
|
299
|
+
payload: {
|
|
300
|
+
name: owner.name || null,
|
|
301
|
+
documentName: owner.documentName,
|
|
302
|
+
reason: reason ?? null,
|
|
303
|
+
contactEmail: options.contactEmail ?? null,
|
|
304
|
+
actionURL: publicURL
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
transaction
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
})
|
|
311
|
+
options.closeRooms?.(documentId)
|
|
312
|
+
},
|
|
313
|
+
|
|
314
|
+
/** Puts a document taken down back where it was. */
|
|
315
|
+
async enable(actorId: string, documentId: string): Promise<void> {
|
|
316
|
+
await database.transaction().execute(async (transaction) => {
|
|
317
|
+
const document = await lockedDocument(transaction, documentId)
|
|
318
|
+
if (!document.disabledAt) return
|
|
319
|
+
await transaction
|
|
320
|
+
.updateTable('document')
|
|
321
|
+
.set({ disabledAt: null, disabledReason: null })
|
|
322
|
+
.where('id', '=', documentId)
|
|
323
|
+
.execute()
|
|
324
|
+
await auditContentAction(transaction, actorId, 'document.enabled', {
|
|
325
|
+
type: 'document',
|
|
326
|
+
id: documentId
|
|
327
|
+
})
|
|
328
|
+
const owner = email && publicURL && (await documentOwner(transaction, documentId))
|
|
329
|
+
if (owner) {
|
|
330
|
+
await email.enqueue(
|
|
331
|
+
{
|
|
332
|
+
idempotencyKey: `document-restored/${documentId}/${Date.now()}`,
|
|
333
|
+
kind: 'document-restored',
|
|
334
|
+
recipientEmail: owner.email,
|
|
335
|
+
payload: {
|
|
336
|
+
name: owner.name || null,
|
|
337
|
+
documentName: owner.documentName,
|
|
338
|
+
actionURL: publicURL
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
transaction
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
})
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Brings back a document someone deleted forever, while its grace lasts: it is live again in
|
|
349
|
+
* its workspace, not in Trash, and counts toward the workspace's storage again, over its plan
|
|
350
|
+
* or not. A document cleanup is destroying, or whose workspace has nobody left in it, can't be.
|
|
351
|
+
*/
|
|
352
|
+
async undelete(actorId: string, documentId: string, now = new Date()): Promise<void> {
|
|
353
|
+
await database.transaction().execute(async (transaction) => {
|
|
354
|
+
const document = await transaction
|
|
355
|
+
.selectFrom('document')
|
|
356
|
+
.select(['id', 'workspaceId', 'purgedAt', 'purgedBytes', 'cleanupClaimId'])
|
|
357
|
+
.where('id', '=', documentId)
|
|
358
|
+
.forUpdate()
|
|
359
|
+
.executeTakeFirst()
|
|
360
|
+
// Cleanup may have destroyed it since the list was loaded.
|
|
361
|
+
if (
|
|
362
|
+
!document ||
|
|
363
|
+
document.purgedAt === null ||
|
|
364
|
+
document.cleanupClaimId !== null ||
|
|
365
|
+
new Date(document.purgedAt) <= graceSince(now)
|
|
366
|
+
) {
|
|
367
|
+
throw new AdminDomainError(
|
|
368
|
+
'document_not_restorable',
|
|
369
|
+
'Only a document deleted forever, inside its grace, can be restored'
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
const member = await transaction
|
|
373
|
+
.selectFrom('workspaceMember')
|
|
374
|
+
.select('userId')
|
|
375
|
+
.where('workspaceId', '=', document.workspaceId)
|
|
376
|
+
.limit(1)
|
|
377
|
+
.executeTakeFirst()
|
|
378
|
+
if (!member) {
|
|
379
|
+
throw new AdminDomainError('workspace_gone', 'The document’s workspace has nobody in it')
|
|
380
|
+
}
|
|
381
|
+
await unmarkPurged(transaction, {
|
|
382
|
+
id: document.id,
|
|
383
|
+
workspaceId: document.workspaceId,
|
|
384
|
+
purgedBytes: Number(document.purgedBytes)
|
|
385
|
+
})
|
|
386
|
+
await auditContentAction(
|
|
387
|
+
transaction,
|
|
388
|
+
actorId,
|
|
389
|
+
'document.undeleted',
|
|
390
|
+
{ type: 'document', id: documentId },
|
|
391
|
+
{ workspaceId: document.workspaceId }
|
|
392
|
+
)
|
|
393
|
+
})
|
|
394
|
+
},
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Revokes a share link for good, as its owner can: the collaboration epoch moves on, so the
|
|
398
|
+
* link's guests lose their tickets, while members keep editing.
|
|
399
|
+
*/
|
|
400
|
+
async revokeShare(actorId: string, shareId: string): Promise<void> {
|
|
401
|
+
await database.transaction().execute(async (transaction) => {
|
|
402
|
+
const share = await transaction
|
|
403
|
+
.selectFrom('documentShare')
|
|
404
|
+
.select(['id', 'documentId', 'revokedAt'])
|
|
405
|
+
.where('id', '=', shareId)
|
|
406
|
+
.forUpdate()
|
|
407
|
+
.executeTakeFirstOrThrow()
|
|
408
|
+
if (share.revokedAt) return
|
|
409
|
+
await transaction
|
|
410
|
+
.updateTable('documentShare')
|
|
411
|
+
.set({ revokedAt: new Date(), updatedAt: new Date() })
|
|
412
|
+
.where('id', '=', shareId)
|
|
413
|
+
.execute()
|
|
414
|
+
await transaction
|
|
415
|
+
.updateTable('document')
|
|
416
|
+
.set((expression) => ({ collaborationEpoch: expression('collaborationEpoch', '+', 1) }))
|
|
417
|
+
.where('id', '=', share.documentId)
|
|
418
|
+
.execute()
|
|
419
|
+
await auditContentAction(
|
|
420
|
+
transaction,
|
|
421
|
+
actorId,
|
|
422
|
+
'document-share.revoked',
|
|
423
|
+
{ type: 'document-share', id: shareId },
|
|
424
|
+
{ documentId: share.documentId }
|
|
425
|
+
)
|
|
426
|
+
})
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export type AdminDocumentService = ReturnType<typeof createAdminDocumentService>
|
package/src/admin/errors.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type AdminErrorCode =
|
|
|
4
4
|
| 'self_admin_action_forbidden'
|
|
5
5
|
| 'email_regeneration_unavailable'
|
|
6
6
|
| 'mfa_required'
|
|
7
|
+
| 'document_not_restorable'
|
|
8
|
+
| 'workspace_gone'
|
|
7
9
|
|
|
8
10
|
export class AdminDomainError extends Error {
|
|
9
11
|
override readonly name = 'AdminDomainError'
|
|
@@ -15,6 +17,12 @@ export class AdminDomainError extends Error {
|
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
const CONFLICTS = new Set<AdminErrorCode>([
|
|
21
|
+
'invalid_enrollment_transition',
|
|
22
|
+
'document_not_restorable',
|
|
23
|
+
'workspace_gone'
|
|
24
|
+
])
|
|
25
|
+
|
|
18
26
|
export function adminErrorStatus(error: AdminDomainError): 400 | 409 {
|
|
19
|
-
return error.code
|
|
27
|
+
return CONFLICTS.has(error.code) ? 409 : 400
|
|
20
28
|
}
|
package/src/admin/index.ts
CHANGED
|
@@ -17,3 +17,9 @@ export {
|
|
|
17
17
|
} from './enrollment/service'
|
|
18
18
|
export { createAdminOperationsService, type AdminOperationsService } from './operations/service'
|
|
19
19
|
export { createAdminUserService, type AdminUserService } from './users/service'
|
|
20
|
+
export {
|
|
21
|
+
createAdminDocumentService,
|
|
22
|
+
type AdminDocumentService,
|
|
23
|
+
type AdminDocumentServiceOptions
|
|
24
|
+
} from './documents/service'
|
|
25
|
+
export { createAdminReportService, type AdminReportService } from './reports/service'
|
|
@@ -19,6 +19,7 @@ export function createAdminOperationsService(
|
|
|
19
19
|
async summary() {
|
|
20
20
|
const [
|
|
21
21
|
pendingEnrollment,
|
|
22
|
+
openReports,
|
|
22
23
|
pendingEmail,
|
|
23
24
|
failedEmail,
|
|
24
25
|
accounts,
|
|
@@ -31,6 +32,11 @@ export function createAdminOperationsService(
|
|
|
31
32
|
.select(({ fn }) => fn.countAll<number>().as('count'))
|
|
32
33
|
.where('status', '=', 'pending')
|
|
33
34
|
.executeTakeFirstOrThrow(),
|
|
35
|
+
database
|
|
36
|
+
.selectFrom('documentReport')
|
|
37
|
+
.select(({ fn }) => fn.countAll<number>().as('count'))
|
|
38
|
+
.where('status', '=', 'open')
|
|
39
|
+
.executeTakeFirstOrThrow(),
|
|
34
40
|
emailCount('pending'),
|
|
35
41
|
emailCount('failed'),
|
|
36
42
|
database
|
|
@@ -56,6 +62,7 @@ export function createAdminOperationsService(
|
|
|
56
62
|
enrollmentMode: config.enrollmentMode,
|
|
57
63
|
emailTransport: config.emailTransport,
|
|
58
64
|
pendingEnrollment: Number(pendingEnrollment.count),
|
|
65
|
+
openReports: Number(openReports.count),
|
|
59
66
|
pendingEmail,
|
|
60
67
|
failedEmail,
|
|
61
68
|
accounts: Number(accounts.count),
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { adminDocuments, auditContentAction } from '#cloud/admin/documents/service'
|
|
2
|
+
import {
|
|
3
|
+
containsPattern,
|
|
4
|
+
countEachFilter,
|
|
5
|
+
listSearch,
|
|
6
|
+
listWindow,
|
|
7
|
+
type AdminListQuery,
|
|
8
|
+
type ListFilters
|
|
9
|
+
} from '#cloud/admin/list'
|
|
10
|
+
import { isUUID } from '#cloud/admin/schema'
|
|
11
|
+
import {
|
|
12
|
+
cloudAdminReportFilterSchema,
|
|
13
|
+
type CloudAdminReport,
|
|
14
|
+
type CloudAdminReportFilter
|
|
15
|
+
} from '#cloud/contract'
|
|
16
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
17
|
+
import { tellReporters, type ReportReplies } from '#cloud/server/reports'
|
|
18
|
+
import { dateString } from '#cloud/server/sharing/contracts'
|
|
19
|
+
import { uniq } from 'es-toolkit'
|
|
20
|
+
import type { ExpressionBuilder, Kysely } from 'kysely'
|
|
21
|
+
|
|
22
|
+
type ReportExpression = ExpressionBuilder<CloudDatabase, 'documentReport'>
|
|
23
|
+
|
|
24
|
+
const FILTERS: ListFilters<CloudAdminReportFilter, 'documentReport'> = {
|
|
25
|
+
open: (expression) => expression('documentReport.status', '=', 'open'),
|
|
26
|
+
actioned: (expression) => expression('documentReport.status', '=', 'actioned'),
|
|
27
|
+
dismissed: (expression) => expression('documentReport.status', '=', 'dismissed'),
|
|
28
|
+
all: (expression) => expression.lit(true)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Reports whose description, reply address, or document's name has the search in it. */
|
|
32
|
+
function searchCondition(search: string) {
|
|
33
|
+
const pattern = containsPattern(search)
|
|
34
|
+
return (expression: ReportExpression) =>
|
|
35
|
+
expression.or([
|
|
36
|
+
expression('documentReport.description', 'ilike', pattern),
|
|
37
|
+
expression('documentReport.replyEmail', 'ilike', pattern),
|
|
38
|
+
expression.exists(
|
|
39
|
+
expression
|
|
40
|
+
.selectFrom('document')
|
|
41
|
+
.select('document.id')
|
|
42
|
+
.whereRef('document.id', '=', 'documentReport.documentId')
|
|
43
|
+
.where('document.name', 'ilike', pattern)
|
|
44
|
+
)
|
|
45
|
+
])
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Reports' administration, which tells each reporter who left an address what it decided. */
|
|
49
|
+
export function createAdminReportService(
|
|
50
|
+
database: Kysely<CloudDatabase>,
|
|
51
|
+
replies: ReportReplies = {}
|
|
52
|
+
) {
|
|
53
|
+
return {
|
|
54
|
+
/** A page of reports, newest first, and how many match the search under each filter. */
|
|
55
|
+
async list(query: AdminListQuery<CloudAdminReportFilter> = {}) {
|
|
56
|
+
const search = listSearch(query)
|
|
57
|
+
const filter = query.filter ?? 'open'
|
|
58
|
+
const { limit, offset } = listWindow(query)
|
|
59
|
+
let searched = database.selectFrom('documentReport')
|
|
60
|
+
if (search) searched = searched.where(searchCondition(search))
|
|
61
|
+
const counts = await countEachFilter(searched, cloudAdminReportFilterSchema.options, FILTERS)
|
|
62
|
+
const rows = await searched
|
|
63
|
+
.where(FILTERS[filter])
|
|
64
|
+
.selectAll()
|
|
65
|
+
.orderBy('createdAt', 'desc')
|
|
66
|
+
.orderBy('id', 'desc')
|
|
67
|
+
.limit(limit)
|
|
68
|
+
.offset(offset)
|
|
69
|
+
.execute()
|
|
70
|
+
const shareIds = uniq(rows.flatMap((row) => (row.shareId ? [row.shareId] : [])))
|
|
71
|
+
const reporterIds = uniq(
|
|
72
|
+
rows.flatMap((row) => (row.reporterUserId ? [row.reporterUserId] : []))
|
|
73
|
+
).filter(isUUID)
|
|
74
|
+
const [documents, shares, reporters] = await Promise.all([
|
|
75
|
+
adminDocuments(
|
|
76
|
+
database,
|
|
77
|
+
rows.map((row) => row.documentId)
|
|
78
|
+
),
|
|
79
|
+
shareIds.length
|
|
80
|
+
? database
|
|
81
|
+
.selectFrom('documentShare')
|
|
82
|
+
.select(['id', 'revokedAt'])
|
|
83
|
+
.where('id', 'in', shareIds)
|
|
84
|
+
.execute()
|
|
85
|
+
: [],
|
|
86
|
+
reporterIds.length
|
|
87
|
+
? database
|
|
88
|
+
.selectFrom('user')
|
|
89
|
+
.select(['id', 'name'])
|
|
90
|
+
.where('id', 'in', reporterIds)
|
|
91
|
+
.execute()
|
|
92
|
+
: []
|
|
93
|
+
])
|
|
94
|
+
const revoked = new Map(shares.map((share) => [share.id, share.revokedAt !== null]))
|
|
95
|
+
const reporterNames = new Map(reporters.map((reporter) => [reporter.id, reporter.name]))
|
|
96
|
+
const reports: CloudAdminReport[] = rows.flatMap((row) => {
|
|
97
|
+
const document = documents.get(row.documentId)
|
|
98
|
+
if (!document) return []
|
|
99
|
+
return [
|
|
100
|
+
{
|
|
101
|
+
id: row.id,
|
|
102
|
+
reason: row.reason,
|
|
103
|
+
description: row.description,
|
|
104
|
+
status: row.status,
|
|
105
|
+
reporter: row.reporterUserId
|
|
106
|
+
? { id: row.reporterUserId, name: reporterNames.get(row.reporterUserId) ?? null }
|
|
107
|
+
: null,
|
|
108
|
+
replyEmail: row.replyEmail,
|
|
109
|
+
createdAt: dateString(row.createdAt) ?? '',
|
|
110
|
+
resolvedAt: dateString(row.resolvedAt),
|
|
111
|
+
document,
|
|
112
|
+
share: row.shareId
|
|
113
|
+
? { id: row.shareId, revoked: revoked.get(row.shareId) ?? true }
|
|
114
|
+
: null
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
})
|
|
118
|
+
return { reports, total: counts[filter], counts }
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
/** Closes an open report, as dismissed or as acted on; a closed one stays as it was. */
|
|
122
|
+
async resolve(
|
|
123
|
+
actorId: string,
|
|
124
|
+
reportId: string,
|
|
125
|
+
status: 'dismissed' | 'actioned'
|
|
126
|
+
): Promise<void> {
|
|
127
|
+
await database.transaction().execute(async (transaction) => {
|
|
128
|
+
const report = await transaction
|
|
129
|
+
.selectFrom('documentReport')
|
|
130
|
+
.select(['id', 'status', 'documentId'])
|
|
131
|
+
.where('id', '=', reportId)
|
|
132
|
+
.forUpdate()
|
|
133
|
+
.executeTakeFirstOrThrow()
|
|
134
|
+
if (report.status !== 'open') return
|
|
135
|
+
await transaction
|
|
136
|
+
.updateTable('documentReport')
|
|
137
|
+
.set({ status, resolvedAt: new Date(), resolvedBy: actorId })
|
|
138
|
+
.where('id', '=', reportId)
|
|
139
|
+
.execute()
|
|
140
|
+
await auditContentAction(
|
|
141
|
+
transaction,
|
|
142
|
+
actorId,
|
|
143
|
+
`document-report.${status}`,
|
|
144
|
+
{ type: 'document-report', id: reportId },
|
|
145
|
+
{ documentId: report.documentId }
|
|
146
|
+
)
|
|
147
|
+
await tellReporters(transaction, replies, [reportId], status)
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export type AdminReportService = ReturnType<typeof createAdminReportService>
|
package/src/admin/schema.ts
CHANGED
|
@@ -21,8 +21,14 @@ export const userMutationSchema = v.object({
|
|
|
21
21
|
reason: v.optional(v.pipe(v.string(), v.trim(), v.maxLength(500)))
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
+
/** Why an administrator takes a document down, which the audit log keeps. */
|
|
25
|
+
export const documentTakedownSchema = v.object({
|
|
26
|
+
reason: v.optional(v.pipe(v.string(), v.trim(), v.maxLength(500)))
|
|
27
|
+
})
|
|
28
|
+
|
|
24
29
|
export type EnrollmentReview = v.InferOutput<typeof enrollmentReviewSchema>
|
|
25
30
|
|
|
26
31
|
export const parseEnrollmentReview = (input: unknown) => v.parse(enrollmentReviewSchema, input)
|
|
27
32
|
export const parseUserMutation = (input: unknown) => v.parse(userMutationSchema, input)
|
|
28
33
|
export const parseUserRoleMutation = (input: unknown) => v.parse(userRoleMutationSchema, input)
|
|
34
|
+
export const parseDocumentTakedown = (input: unknown) => v.parse(documentTakedownSchema, input)
|