@cossackframework/cms 0.0.0-stage → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +147 -2
- package/agent-docs.json +259 -0
- package/app/README.md +78 -0
- package/app/cossack-recipe.json +43 -0
- package/app/src/auth-bot-check.ts +32 -0
- package/app/src/auth-bot-token.ts +11 -0
- package/app/src/auth-input.ts +35 -0
- package/app/src/auth-options.ts +18 -0
- package/app/src/auth-providers.ts +16 -0
- package/app/src/auth.ts +1082 -0
- package/app/src/brand.ts +68 -0
- package/app/src/cms/ai.ts +54 -0
- package/app/src/cms/analytics.ts +33 -0
- package/app/src/cms/block-errors.ts +55 -0
- package/app/src/cms/block-inserter.ts +111 -0
- package/app/src/cms/block-panel.ts +68 -0
- package/app/src/cms/block-rows.ts +44 -0
- package/app/src/cms/block-tree.ts +585 -0
- package/app/src/cms/blocks.css +708 -0
- package/app/src/cms/builder.ts +126 -0
- package/app/src/cms/cache.ts +44 -0
- package/app/src/cms/carousel.client.ts +52 -0
- package/app/src/cms/collab/presence.ts +69 -0
- package/app/src/cms/collab/rich-text-collab.client.ts +197 -0
- package/app/src/cms/collab/rich-text-notes.client.ts +138 -0
- package/app/src/cms/collab/room.ts +881 -0
- package/app/src/cms/compare.client.ts +47 -0
- package/app/src/cms/condition-control.ts +109 -0
- package/app/src/cms/content-api.ts +143 -0
- package/app/src/cms/content.ts +524 -0
- package/app/src/cms/countdown.client.ts +37 -0
- package/app/src/cms/editor-layout-editor.ts +260 -0
- package/app/src/cms/editor-layout-view.ts +37 -0
- package/app/src/cms/editor.ts +381 -0
- package/app/src/cms/entry.ts +26 -0
- package/app/src/cms/feeds.ts +145 -0
- package/app/src/cms/field-controls.ts +222 -0
- package/app/src/cms/field-group-editor.ts +105 -0
- package/app/src/cms/field-group-view.ts +211 -0
- package/app/src/cms/form-view.ts +203 -0
- package/app/src/cms/form.client.ts +203 -0
- package/app/src/cms/guards.ts +27 -0
- package/app/src/cms/handoff.ts +202 -0
- package/app/src/cms/headings.ts +51 -0
- package/app/src/cms/icons.ts +80 -0
- package/app/src/cms/import.client.ts +16 -0
- package/app/src/cms/index.ts +191 -0
- package/app/src/cms/lightbox.client.ts +109 -0
- package/app/src/cms/machine.ts +118 -0
- package/app/src/cms/maintenance.ts +97 -0
- package/app/src/cms/manage.ts +1236 -0
- package/app/src/cms/mcp-tools.ts +388 -0
- package/app/src/cms/mcp.ts +242 -0
- package/app/src/cms/media-chooser.ts +123 -0
- package/app/src/cms/media-usage.ts +57 -0
- package/app/src/cms/middleware.ts +199 -0
- package/app/src/cms/middlewares.ts +13 -0
- package/app/src/cms/network-ui.ts +36 -0
- package/app/src/cms/network-view.ts +38 -0
- package/app/src/cms/notes-announce.ts +65 -0
- package/app/src/cms/notes-rail.ts +207 -0
- package/app/src/cms/notification-rules.ts +66 -0
- package/app/src/cms/recovery.ts +82 -0
- package/app/src/cms/replace.ts +63 -0
- package/app/src/cms/reserved-paths.ts +19 -0
- package/app/src/cms/rich-text-field.ts +325 -0
- package/app/src/cms/rich-text-schema.ts +91 -0
- package/app/src/cms/rich-text.client.ts +255 -0
- package/app/src/cms/search.client.ts +116 -0
- package/app/src/cms/seo-head.ts +78 -0
- package/app/src/cms/seo.ts +241 -0
- package/app/src/cms/settings-draft.ts +218 -0
- package/app/src/cms/settings-file.ts +127 -0
- package/app/src/cms/settings-form.ts +180 -0
- package/app/src/cms/settings-languages.ts +125 -0
- package/app/src/cms/settings-nav.ts +102 -0
- package/app/src/cms/settings-network.ts +151 -0
- package/app/src/cms/settings-permalinks.ts +114 -0
- package/app/src/cms/settings-scroll.ts +10 -0
- package/app/src/cms/settings-search.ts +77 -0
- package/app/src/cms/settings-section.ts +38 -0
- package/app/src/cms/share.client.ts +27 -0
- package/app/src/cms/site-editing.ts +219 -0
- package/app/src/cms/site-editor.client.ts +1008 -0
- package/app/src/cms/site-editor.ts +99 -0
- package/app/src/cms/site-export.ts +60 -0
- package/app/src/cms/site-forms.ts +143 -0
- package/app/src/cms/site-import.ts +28 -0
- package/app/src/cms/site-session.ts +36 -0
- package/app/src/cms/site-taskbar.ts +77 -0
- package/app/src/cms/site-view.ts +1014 -0
- package/app/src/cms/site.ts +1322 -0
- package/app/src/cms/sitemaps.ts +131 -0
- package/app/src/cms/stat.client.ts +56 -0
- package/app/src/cms/style-controls.ts +252 -0
- package/app/src/cms/tabs.client.ts +36 -0
- package/app/src/cms/taskbar.ts +134 -0
- package/app/src/cms/term-icons.ts +10 -0
- package/app/src/cms/traffic.ts +26 -0
- package/app/src/cms/ui.ts +170 -0
- package/app/src/cms/updates.ts +124 -0
- package/app/src/cms/uploads.ts +26 -0
- package/app/src/cms/worker.ts +13 -0
- package/app/src/config/auth.ts +30 -0
- package/app/src/config/cms.ts +18 -0
- package/app/src/dashboard/access.ts +344 -0
- package/app/src/dashboard/agent-icons.ts +44 -0
- package/app/src/dashboard/app-passwords.ts +74 -0
- package/app/src/dashboard/avatar-field.ts +58 -0
- package/app/src/dashboard/bulk.ts +53 -0
- package/app/src/dashboard/head.ts +29 -0
- package/app/src/dashboard/icons.ts +15 -0
- package/app/src/dashboard/list-table.ts +120 -0
- package/app/src/dashboard/modules/roles.ts +15 -0
- package/app/src/dashboard/modules/sessions.ts +15 -0
- package/app/src/dashboard/modules/settings.ts +15 -0
- package/app/src/dashboard/modules/users.ts +18 -0
- package/app/src/dashboard/page.ts +67 -0
- package/app/src/dashboard/registry.ts +13 -0
- package/app/src/dashboard/session-list.ts +98 -0
- package/app/src/dashboard/tabs.ts +26 -0
- package/app/src/dashboard/time-ago.ts +16 -0
- package/app/src/dashboard/types.ts +40 -0
- package/app/src/dashboard/user-sections.ts +172 -0
- package/app/src/lib/permissions.ts +84 -0
- package/app/src/middlewares/auth.ts +117 -0
- package/app/src/pages/(site)/[...path]/index.ts +609 -0
- package/app/src/pages/(site)/feed/[...rest]/index.ts +13 -0
- package/app/src/pages/(site)/feed/index.ts +12 -0
- package/app/src/pages/(site)/index.ts +11 -0
- package/app/src/pages/(site)/indexnow/[file]/index.ts +18 -0
- package/app/src/pages/(site)/layout.ts +219 -0
- package/app/src/pages/(site)/llms.txt/index.ts +23 -0
- package/app/src/pages/(site)/manifest.webmanifest/index.ts +29 -0
- package/app/src/pages/(site)/media/[id]/[name]/index.ts +59 -0
- package/app/src/pages/(site)/preview/[collection]/[id]/index.ts +115 -0
- package/app/src/pages/(site)/robots.txt/index.ts +29 -0
- package/app/src/pages/(site)/search/index.ts +36 -0
- package/app/src/pages/(site)/sitemap.xml/index.ts +26 -0
- package/app/src/pages/(site)/sitemaps/[file]/index.ts +33 -0
- package/app/src/pages/api/auth/session.ts +48 -0
- package/app/src/pages/api/cms/appearance/areas/[name].ts +12 -0
- package/app/src/pages/api/cms/appearance/areas/index.ts +6 -0
- package/app/src/pages/api/cms/appearance/locations/[location].ts +6 -0
- package/app/src/pages/api/cms/appearance/locations/index.ts +6 -0
- package/app/src/pages/api/cms/appearance/menus/[id].ts +12 -0
- package/app/src/pages/api/cms/appearance/menus/index.ts +13 -0
- package/app/src/pages/api/cms/appearance/themes/[name]/activate.ts +6 -0
- package/app/src/pages/api/cms/appearance/themes/index.ts +6 -0
- package/app/src/pages/api/cms/cache/purge.ts +6 -0
- package/app/src/pages/api/cms/collections/index.ts +6 -0
- package/app/src/pages/api/cms/commands/[plugin]/[command].ts +26 -0
- package/app/src/pages/api/cms/comments/[id].ts +9 -0
- package/app/src/pages/api/cms/comments/index.ts +126 -0
- package/app/src/pages/api/cms/comments/queue.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/autosave.ts +27 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/blocks/[block].ts +16 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/notes.ts +18 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/publish.ts +11 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/index.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/restore.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/index.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/unpublish.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry].ts +42 -0
- package/app/src/pages/api/cms/content/[collection]/index.ts +24 -0
- package/app/src/pages/api/cms/export/[collection].ts +37 -0
- package/app/src/pages/api/cms/forms/[slug]/index.ts +173 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/[submission]/files/[file].ts +25 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/export.ts +34 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/index.ts +24 -0
- package/app/src/pages/api/cms/import/[collection].ts +23 -0
- package/app/src/pages/api/cms/mcp.ts +29 -0
- package/app/src/pages/api/cms/me/index.ts +6 -0
- package/app/src/pages/api/cms/me/recent-blocks.ts +37 -0
- package/app/src/pages/api/cms/me/tokens/[id].ts +6 -0
- package/app/src/pages/api/cms/me/tokens/index.ts +13 -0
- package/app/src/pages/api/cms/media/[id].ts +6 -0
- package/app/src/pages/api/cms/media.ts +26 -0
- package/app/src/pages/api/cms/menus/[location].ts +14 -0
- package/app/src/pages/api/cms/network/sites/[id]/export.ts +15 -0
- package/app/src/pages/api/cms/network/sites/[id]/index.ts +9 -0
- package/app/src/pages/api/cms/network/sites/index.ts +14 -0
- package/app/src/pages/api/cms/notes/[id].ts +13 -0
- package/app/src/pages/api/cms/notes/index.ts +6 -0
- package/app/src/pages/api/cms/payments/webhook.ts +28 -0
- package/app/src/pages/api/cms/plugins/[name]/grant.ts +6 -0
- package/app/src/pages/api/cms/plugins/[name]/revoke.ts +6 -0
- package/app/src/pages/api/cms/plugins/index.ts +6 -0
- package/app/src/pages/api/cms/recovery.ts +29 -0
- package/app/src/pages/api/cms/redirects/index.ts +9 -0
- package/app/src/pages/api/cms/roles/index.ts +6 -0
- package/app/src/pages/api/cms/schema/[collection].ts +13 -0
- package/app/src/pages/api/cms/search.ts +16 -0
- package/app/src/pages/api/cms/settings/[group].ts +9 -0
- package/app/src/pages/api/cms/settings/index.ts +9 -0
- package/app/src/pages/api/cms/site-export.ts +34 -0
- package/app/src/pages/api/cms/taxonomies/index.ts +6 -0
- package/app/src/pages/api/cms/terms/[taxonomy]/[id].ts +12 -0
- package/app/src/pages/api/cms/terms/[taxonomy]/index.ts +9 -0
- package/app/src/pages/api/cms/uploads/[id]/complete.ts +15 -0
- package/app/src/pages/api/cms/uploads/[id]/index.ts +15 -0
- package/app/src/pages/api/cms/uploads/index.ts +26 -0
- package/app/src/pages/api/cms/users/[id]/index.ts +12 -0
- package/app/src/pages/api/cms/users/[id]/meta.ts +40 -0
- package/app/src/pages/api/cms/users/index.ts +12 -0
- package/app/src/pages/api/cms/webhooks/[id].ts +6 -0
- package/app/src/pages/api/cms/webhooks/index.ts +9 -0
- package/app/src/pages/auth/[provider]/callback.ts +11 -0
- package/app/src/pages/auth/[provider]/redirect.ts +9 -0
- package/app/src/pages/auth/forgot-password/index.ts +75 -0
- package/app/src/pages/auth/layout.ts +32 -0
- package/app/src/pages/auth/login/index.ts +198 -0
- package/app/src/pages/auth/magic/index.ts +68 -0
- package/app/src/pages/auth/register/index.ts +117 -0
- package/app/src/pages/auth/reset-password/index.ts +65 -0
- package/app/src/pages/auth/setup/index.ts +194 -0
- package/app/src/pages/auth/two-factor/index.ts +67 -0
- package/app/src/pages/auth/verify/index.ts +62 -0
- package/app/src/pages/dashboard/(cms)/appearance/customize/index.ts +263 -0
- package/app/src/pages/dashboard/(cms)/appearance/dashboard-menu/index.ts +241 -0
- package/app/src/pages/dashboard/(cms)/appearance/index.ts +89 -0
- package/app/src/pages/dashboard/(cms)/appearance/menus/[id]/index.ts +697 -0
- package/app/src/pages/dashboard/(cms)/appearance/menus/index.ts +145 -0
- package/app/src/pages/dashboard/(cms)/appearance/widgets/index.ts +279 -0
- package/app/src/pages/dashboard/(cms)/blocks/index.ts +84 -0
- package/app/src/pages/dashboard/(cms)/calendar/index.ts +92 -0
- package/app/src/pages/dashboard/(cms)/collections/[name]/index.ts +525 -0
- package/app/src/pages/dashboard/(cms)/collections/index.ts +72 -0
- package/app/src/pages/dashboard/(cms)/collections/new/index.ts +66 -0
- package/app/src/pages/dashboard/(cms)/comments/index.ts +237 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/index.ts +2382 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/revisions/index.ts +126 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/index.ts +693 -0
- package/app/src/pages/dashboard/(cms)/field-groups/[id]/index.ts +193 -0
- package/app/src/pages/dashboard/(cms)/field-groups/index.ts +65 -0
- package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/[submission]/index.ts +279 -0
- package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/index.ts +170 -0
- package/app/src/pages/dashboard/(cms)/forms/index.ts +171 -0
- package/app/src/pages/dashboard/(cms)/media/[id]/index.ts +317 -0
- package/app/src/pages/dashboard/(cms)/media/index.ts +156 -0
- package/app/src/pages/dashboard/(cms)/media/new/index.ts +9 -0
- package/app/src/pages/dashboard/(cms)/media/summary/index.ts +74 -0
- package/app/src/pages/dashboard/(cms)/my-sites/index.ts +63 -0
- package/app/src/pages/dashboard/(cms)/network/index.ts +53 -0
- package/app/src/pages/dashboard/(cms)/network/plugins/index.ts +110 -0
- package/app/src/pages/dashboard/(cms)/network/sites/[id]/index.ts +243 -0
- package/app/src/pages/dashboard/(cms)/network/sites/index.ts +186 -0
- package/app/src/pages/dashboard/(cms)/network/sites/new/index.ts +77 -0
- package/app/src/pages/dashboard/(cms)/network/themes/index.ts +82 -0
- package/app/src/pages/dashboard/(cms)/network/updates/index.ts +48 -0
- package/app/src/pages/dashboard/(cms)/network/users/[id]/index.ts +176 -0
- package/app/src/pages/dashboard/(cms)/network/users/index.ts +174 -0
- package/app/src/pages/dashboard/(cms)/network/users/new/index.ts +66 -0
- package/app/src/pages/dashboard/(cms)/plugins/index.ts +172 -0
- package/app/src/pages/dashboard/(cms)/profile/public/index.ts +105 -0
- package/app/src/pages/dashboard/(cms)/registrations/index.ts +70 -0
- package/app/src/pages/dashboard/(cms)/settings/index.ts +359 -0
- package/app/src/pages/dashboard/(cms)/settings/json/index.ts +202 -0
- package/app/src/pages/dashboard/(cms)/settings/layout.ts +67 -0
- package/app/src/pages/dashboard/(cms)/taxonomies/[name]/index.ts +85 -0
- package/app/src/pages/dashboard/(cms)/taxonomies/index.ts +98 -0
- package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/[id]/index.ts +244 -0
- package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/index.ts +148 -0
- package/app/src/pages/dashboard/(cms)/tools/activity/index.ts +94 -0
- package/app/src/pages/dashboard/(cms)/tools/automations/index.ts +131 -0
- package/app/src/pages/dashboard/(cms)/tools/cache/index.ts +92 -0
- package/app/src/pages/dashboard/(cms)/tools/export/index.ts +99 -0
- package/app/src/pages/dashboard/(cms)/tools/health/index.ts +189 -0
- package/app/src/pages/dashboard/(cms)/tools/jobs/index.ts +93 -0
- package/app/src/pages/dashboard/(cms)/tools/links/index.ts +115 -0
- package/app/src/pages/dashboard/(cms)/tools/redirects/index.ts +161 -0
- package/app/src/pages/dashboard/(cms)/tools/replace/index.ts +124 -0
- package/app/src/pages/dashboard/(cms)/tools/schema/index.ts +54 -0
- package/app/src/pages/dashboard/(cms)/tools/seo/index.ts +156 -0
- package/app/src/pages/dashboard/(cms)/tools/updates/index.ts +114 -0
- package/app/src/pages/dashboard/(cms)/tools/webhooks/index.ts +182 -0
- package/app/src/pages/dashboard/index.ts +214 -0
- package/app/src/pages/dashboard/layout.ts +320 -0
- package/app/src/pages/dashboard/profile/index.ts +211 -0
- package/app/src/pages/dashboard/profile/security/index.ts +255 -0
- package/app/src/pages/dashboard/roles/[id]/index.ts +116 -0
- package/app/src/pages/dashboard/roles/index.ts +134 -0
- package/app/src/pages/dashboard/roles/new/index.ts +95 -0
- package/app/src/pages/dashboard/sessions/index.ts +105 -0
- package/app/src/pages/dashboard/users/[id]/index.ts +472 -0
- package/app/src/pages/dashboard/users/index.ts +299 -0
- package/app/src/pages/dashboard/users/new/index.ts +123 -0
- package/app/src/passkeys.client.ts +18 -0
- package/app/src/plugins/default-theme/assets/cover.svg +31 -0
- package/app/src/plugins/default-theme/assets/logo.svg +8 -0
- package/app/src/plugins/default-theme/parts/comments.ts +66 -0
- package/app/src/plugins/default-theme/parts/list.ts +38 -0
- package/app/src/plugins/default-theme/parts/media.ts +15 -0
- package/app/src/plugins/default-theme/parts/options.ts +45 -0
- package/app/src/plugins/default-theme/plugin.json +275 -0
- package/app/src/plugins/default-theme/templates/archive.ts +14 -0
- package/app/src/plugins/default-theme/templates/entry-blank.ts +13 -0
- package/app/src/plugins/default-theme/templates/entry.ts +27 -0
- package/app/src/plugins/default-theme/templates/home.ts +8 -0
- package/app/src/plugins/default-theme/templates/layout.ts +40 -0
- package/app/src/plugins/default-theme/templates/not-found.ts +10 -0
- package/app/src/plugins/default-theme/templates/search.ts +32 -0
- package/app/src/services/rbac.ts +65 -0
- package/app/src/services/roles.ts +89 -0
- package/app/src/services/users.ts +173 -0
- package/package.json +123 -3
- package/src/automations.ts +95 -0
- package/src/bindings.ts +138 -0
- package/src/block-references.ts +239 -0
- package/src/block-shape.ts +155 -0
- package/src/blocks.ts +1157 -0
- package/src/cache.ts +54 -0
- package/src/checks.ts +198 -0
- package/src/cloudflare-workers.d.ts +13 -0
- package/src/cloudflare.ts +61 -0
- package/src/collab.ts +372 -0
- package/src/collections.ts +421 -0
- package/src/comments.ts +46 -0
- package/src/cron.ts +95 -0
- package/src/dashboard-menu.ts +105 -0
- package/src/diff.ts +113 -0
- package/src/editor-layout.ts +316 -0
- package/src/errors.ts +50 -0
- package/src/field-groups.ts +498 -0
- package/src/fields.ts +411 -0
- package/src/form-builder.ts +1053 -0
- package/src/forms.ts +109 -0
- package/src/highlight.ts +134 -0
- package/src/hooks.ts +510 -0
- package/src/icon-catalog.ts +55 -0
- package/src/icons.ts +128 -0
- package/src/images.ts +149 -0
- package/src/index.ts +100 -0
- package/src/isolate-core.ts +245 -0
- package/src/isolate.ts +19 -0
- package/src/isolation.ts +69 -0
- package/src/mail.ts +182 -0
- package/src/manifest.ts +476 -0
- package/src/media.ts +193 -0
- package/src/menus.ts +287 -0
- package/src/module.ts +32 -0
- package/src/modules/content.ts +64 -0
- package/src/modules/forms.ts +35 -0
- package/src/node.ts +66 -0
- package/src/notes.ts +237 -0
- package/src/permissions.ts +139 -0
- package/src/plugin-settings.ts +114 -0
- package/src/records.ts +56 -0
- package/src/redirects.ts +59 -0
- package/src/resource.ts +245 -0
- package/src/richtext.ts +402 -0
- package/src/schema-json.ts +112 -0
- package/src/search.ts +89 -0
- package/src/seo.ts +477 -0
- package/src/server/accounts.ts +418 -0
- package/src/server/ai.ts +33 -0
- package/src/server/api-tokens.ts +91 -0
- package/src/server/areas.ts +116 -0
- package/src/server/audit.ts +94 -0
- package/src/server/auth.ts +89 -0
- package/src/server/automations.ts +190 -0
- package/src/server/autosaves.ts +69 -0
- package/src/server/brand-icons.ts +50 -0
- package/src/server/brands.ts +86 -0
- package/src/server/budget.ts +97 -0
- package/src/server/cache.ts +104 -0
- package/src/server/capabilities.ts +333 -0
- package/src/server/cloudflare.ts +92 -0
- package/src/server/comments.ts +237 -0
- package/src/server/drafts.ts +176 -0
- package/src/server/field-groups.ts +88 -0
- package/src/server/files.ts +780 -0
- package/src/server/forms.ts +726 -0
- package/src/server/handoffs.ts +107 -0
- package/src/server/host-schema.ts +312 -0
- package/src/server/host.ts +2892 -0
- package/src/server/images.ts +34 -0
- package/src/server/index.ts +59 -0
- package/src/server/indexnow.ts +91 -0
- package/src/server/isolation.ts +183 -0
- package/src/server/locked.ts +45 -0
- package/src/server/mail.ts +93 -0
- package/src/server/markdown.ts +24 -0
- package/src/server/menus.ts +286 -0
- package/src/server/network-policy.ts +60 -0
- package/src/server/network.ts +665 -0
- package/src/server/notes.ts +275 -0
- package/src/server/outbox.ts +213 -0
- package/src/server/path-moves.ts +33 -0
- package/src/server/payments.ts +59 -0
- package/src/server/previews.ts +62 -0
- package/src/server/purge.ts +140 -0
- package/src/server/reconcile.ts +38 -0
- package/src/server/redirects.ts +183 -0
- package/src/server/repository.ts +1107 -0
- package/src/server/schedules.ts +105 -0
- package/src/server/schema.ts +562 -0
- package/src/server/security.ts +391 -0
- package/src/server/seed.ts +52 -0
- package/src/server/shared-fields.ts +34 -0
- package/src/server/site-export.ts +151 -0
- package/src/server/site-import.ts +482 -0
- package/src/server/site-search.ts +289 -0
- package/src/server/site-settings.ts +286 -0
- package/src/server/sites.ts +243 -0
- package/src/server/stores.ts +314 -0
- package/src/server/stripe.ts +173 -0
- package/src/server/taxonomies.ts +415 -0
- package/src/server/tokens.ts +25 -0
- package/src/server/traffic.ts +118 -0
- package/src/server/users.ts +653 -0
- package/src/server/webhooks.ts +245 -0
- package/src/settings-controls.ts +54 -0
- package/src/settings-document.ts +111 -0
- package/src/settings.ts +426 -0
- package/src/storage/cloudflare.ts +94 -0
- package/src/storage/index.ts +30 -0
- package/src/storage/node.ts +114 -0
- package/src/taxonomy.ts +51 -0
- package/src/theme.ts +1135 -0
- package/src/to-markdown.ts +179 -0
- package/src/updates.ts +126 -0
- package/src/version.ts +2 -0
- package/src/vite.d.ts +25 -0
- package/src/vite.js +199 -0
- package/src/webhooks.ts +107 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comments on entries, with moderation: pending, approved, spam and trash. The host checks
|
|
3
|
+
* the entry and the Discussion settings before posting; this class keeps the rows.
|
|
4
|
+
*/
|
|
5
|
+
import type { RomeDB, Statement, Value } from '@cossackframework/romedb';
|
|
6
|
+
import { CmsError } from '../errors';
|
|
7
|
+
import { containsPattern, inList } from './users';
|
|
8
|
+
import { checkComment, COMMENT_STATUSES, type CommentRow, type CommentShape, type CommentStatus } from '../comments';
|
|
9
|
+
import { defineAction } from '@cossackframework/core/hooks';
|
|
10
|
+
import { readPaged, requestBatches } from './budget';
|
|
11
|
+
|
|
12
|
+
/** A deleted collection, queued in the batch that drops it, so its comments go in a retried job. */
|
|
13
|
+
export interface CollectionDeletedEvent {
|
|
14
|
+
readonly eventId: string;
|
|
15
|
+
readonly topic: 'collection.deleted';
|
|
16
|
+
readonly at: number;
|
|
17
|
+
readonly resource: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Runs the deleted collection's cleanup from the outbox. The host handles it; plugins cannot hook it. */
|
|
21
|
+
export const collectionDeleted = defineAction<CollectionDeletedEvent>('cms.collection.deleted', { version: 1, timeoutMs: 60_000 });
|
|
22
|
+
|
|
23
|
+
/** The outbox row that queues `collectionDeleted`, for the batch that drops the collection. */
|
|
24
|
+
export function collectionDeletedStatement(resource: string, eventId: string, at: number): Statement {
|
|
25
|
+
const event: CollectionDeletedEvent = { eventId, topic: 'collection.deleted', at, resource };
|
|
26
|
+
return {
|
|
27
|
+
rql: 'INSERT INTO cms_outbox (id, topic, payload, created_at, available_at, attempts, lease_until) VALUES ($eventId, $topic, $payload, $at, $at, 0, 0)',
|
|
28
|
+
params: { $eventId: eventId, $topic: event.topic, $payload: JSON.stringify(event), $at: at },
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CommentListOptions {
|
|
33
|
+
readonly status?: CommentStatus;
|
|
34
|
+
readonly resource?: string;
|
|
35
|
+
readonly recordId?: string;
|
|
36
|
+
readonly search?: string;
|
|
37
|
+
readonly offset?: number;
|
|
38
|
+
readonly limit?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const WRITE = { kind: 'cms.comments', retryConflicts: 5 } as const;
|
|
42
|
+
const COLUMNS = 'id, resource, record_id, parent_id, user_id, author_name, author_email, author_url, content, status, created_at, updated_at';
|
|
43
|
+
const ID = /^[A-Za-z0-9_-]{1,80}$/;
|
|
44
|
+
|
|
45
|
+
export class CmsComments {
|
|
46
|
+
readonly #db: RomeDB;
|
|
47
|
+
readonly #now: () => number;
|
|
48
|
+
|
|
49
|
+
/** Called after a committed change with the entries whose comments changed, for the host to purge their pages. */
|
|
50
|
+
readonly #changed: ((entries: readonly { resource: string; recordId: string }[]) => void | Promise<void>) | undefined;
|
|
51
|
+
|
|
52
|
+
constructor(db: RomeDB, options: { now?: () => number; changed?: (entries: readonly { resource: string; recordId: string }[]) => void | Promise<void> } = {}) {
|
|
53
|
+
this.#db = db;
|
|
54
|
+
this.#now = options.now ?? Date.now;
|
|
55
|
+
this.#changed = options.changed;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async get(id: string): Promise<CommentShape | null> {
|
|
59
|
+
if (!ID.test(id)) return null;
|
|
60
|
+
const result = await this.#db.read([{ rql: `SELECT ${COLUMNS} FROM cms_comments WHERE id = $id`, params: { $id: id } }]);
|
|
61
|
+
const row = result.results[0]!.rows[0];
|
|
62
|
+
return row ? commentOf(row) : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Comments for the moderation screen, newest first. */
|
|
66
|
+
async list(options: CommentListOptions = {}): Promise<{ items: CommentShape[]; total: number }> {
|
|
67
|
+
const { where, params } = conditions(options);
|
|
68
|
+
const limit = Math.min(Math.max(options.limit ?? 20, 1), 100);
|
|
69
|
+
const offset = Math.min(Math.max(options.offset ?? 0, 0), 10_000);
|
|
70
|
+
const counted = await this.#db.read([{ rql: `SELECT count(*) FROM cms_comments${where}`, ...(Object.keys(params).length ? { params } : {}) }]);
|
|
71
|
+
// Comments hold up to 5000 characters each: a page of long ones may come in smaller reads.
|
|
72
|
+
const rows = await readPaged(this.#db, (size, at) => ({
|
|
73
|
+
rql: `SELECT ${COLUMNS} FROM cms_comments${where} ORDER BY created_at DESC, id DESC LIMIT $limit OFFSET $offset`, params: { ...params, $limit: size, $offset: at },
|
|
74
|
+
}), limit, offset, { afterSequence: counted.sequence });
|
|
75
|
+
return { items: rows.map(commentOf), total: Number(counted.results[0]!.rows[0]![0] as bigint) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** How many comments have each status. */
|
|
79
|
+
async counts(): Promise<{ [K in CommentStatus]: number }> {
|
|
80
|
+
const result = await this.#db.read(COMMENT_STATUSES.map(status => ({ rql: 'SELECT count(*) FROM cms_comments WHERE status = $status', params: { $status: status } })));
|
|
81
|
+
return Object.fromEntries(COMMENT_STATUSES.map((status, index) => [status, Number(result.results[index]!.rows[0]![0] as bigint)])) as { [K in CommentStatus]: number };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** An entry's comments with one status, oldest first, at most 500, read in pages that fit the result budget. */
|
|
85
|
+
async forEntry(resource: string, recordId: string, status: CommentStatus = 'approved'): Promise<CommentShape[]> {
|
|
86
|
+
const rows = await readPaged(this.#db, (limit, offset) => ({
|
|
87
|
+
rql: `SELECT ${COLUMNS} FROM cms_comments WHERE resource = $resource AND record_id = $record AND status = $status ORDER BY created_at, id LIMIT $limit OFFSET $offset`,
|
|
88
|
+
params: { $resource: resource, $record: recordId, $status: status, $limit: limit, $offset: offset },
|
|
89
|
+
}), 500, 0);
|
|
90
|
+
return rows.map(commentOf);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Approved comment counts of entries, for archive lists. */
|
|
94
|
+
async approvedCounts(resource: string, recordIds: readonly string[]): Promise<Map<string, number>> {
|
|
95
|
+
const ids = [...new Set(recordIds.filter(id => ID.test(id)))].slice(0, 100);
|
|
96
|
+
if (!ids.length) return new Map();
|
|
97
|
+
const list = inList('r', ids);
|
|
98
|
+
const result = await this.#db.read([{
|
|
99
|
+
rql: `SELECT record_id, count(*) FROM cms_comments WHERE resource = $resource AND status = 'approved' AND record_id IN ${list.rql} GROUP BY record_id`,
|
|
100
|
+
params: { $resource: resource, ...list.params },
|
|
101
|
+
}]);
|
|
102
|
+
return new Map(result.results[0]!.rows.map(([id, count]) => [id as string, Number(count as bigint)]));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether the email has an approved comment, for "approve returning commenters". */
|
|
106
|
+
async hasApproved(email: string): Promise<boolean> {
|
|
107
|
+
if (!email) return false;
|
|
108
|
+
const result = await this.#db.read([{ rql: "SELECT 1 FROM cms_comments WHERE author_email = $email AND status = 'approved' LIMIT 1", params: { $email: email.toLowerCase() } }]);
|
|
109
|
+
return result.results[0]!.rows.length > 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The insert statement for a new comment, so callers can batch it. */
|
|
113
|
+
/** The comment's row; with `guard` (an SQL condition, such as the entry still existing), only while it holds. */
|
|
114
|
+
insert(row: Omit<CommentRow, 'createdAt' | 'updatedAt'>, guard?: { readonly rql: string; readonly params: { readonly [key: string]: Value } }): Statement {
|
|
115
|
+
const now = this.#now();
|
|
116
|
+
const values = '$id, $resource, $record, $parent, $user, $name, $email, $url, $content, $status, $ip, $agent, $now, $now';
|
|
117
|
+
return {
|
|
118
|
+
rql: `INSERT INTO cms_comments (id, resource, record_id, parent_id, user_id, author_name, author_email, author_url, content, status, ip_hash, user_agent, created_at, updated_at)
|
|
119
|
+
${guard ? `SELECT ${values} WHERE ${guard.rql}` : `VALUES (${values})`}`,
|
|
120
|
+
params: {
|
|
121
|
+
...guard?.params,
|
|
122
|
+
$id: row.id, $resource: row.resource, $record: row.recordId, $parent: row.parentId, $user: row.userId, $name: row.authorName, $email: row.authorEmail.toLowerCase(),
|
|
123
|
+
$url: row.authorUrl, $content: row.content, $status: row.status, $ip: row.ipHash, $agent: row.userAgent, $now: now,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Add a comment; with `guard`, refused as `CMS_NOT_FOUND` when it no longer holds, such as for an entry just deleted. */
|
|
129
|
+
async create(row: Omit<CommentRow, 'createdAt' | 'updatedAt'>, guard?: { readonly rql: string; readonly params: { readonly [key: string]: Value } }): Promise<CommentShape> {
|
|
130
|
+
const result = await this.#db.write([this.insert(row, guard)], WRITE);
|
|
131
|
+
if (result.results[0]!.affected === 0n) throw new CmsError('CMS_NOT_FOUND', 'The entry is gone.');
|
|
132
|
+
if (row.status === 'approved') await this.#changed?.([{ resource: row.resource, recordId: row.recordId }]);
|
|
133
|
+
return (await this.get(row.id))!;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Approve, unapprove, mark as spam or trash comments. */
|
|
137
|
+
async setStatus(ids: readonly string[], status: CommentStatus): Promise<number> {
|
|
138
|
+
if (!COMMENT_STATUSES.includes(status)) throw new CmsError('CMS_VALIDATION', 'Unknown comment status.');
|
|
139
|
+
const wanted = [...new Set(ids.filter(id => ID.test(id)))].slice(0, 100);
|
|
140
|
+
if (!wanted.length) return 0;
|
|
141
|
+
const list = inList('c', wanted);
|
|
142
|
+
const entries = this.#changed ? await this.#entriesOf(list) : [];
|
|
143
|
+
const result = await this.#db.write([{ rql: `UPDATE cms_comments SET status = $status, updated_at = $now WHERE id IN ${list.rql}`, params: { $status: status, $now: this.#now(), ...list.params } }], WRITE);
|
|
144
|
+
if (entries.length) await this.#changed?.(entries);
|
|
145
|
+
return Number(result.results[0]!.affected);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Edit a comment's text or author. */
|
|
149
|
+
async update(id: string, input: { content?: string; authorName?: string; authorEmail?: string; authorUrl?: string }): Promise<CommentShape> {
|
|
150
|
+
const current = await this.get(id);
|
|
151
|
+
if (!current) throw new CmsError('CMS_NOT_FOUND', 'Comment not found.');
|
|
152
|
+
const next = checkComment({
|
|
153
|
+
content: input.content ?? current.content, authorName: input.authorName ?? current.authorName,
|
|
154
|
+
authorEmail: input.authorEmail ?? current.authorEmail, authorUrl: input.authorUrl ?? current.authorUrl,
|
|
155
|
+
}, false);
|
|
156
|
+
await this.#db.write([{
|
|
157
|
+
rql: 'UPDATE cms_comments SET content = $content, author_name = $name, author_email = $email, author_url = $url, updated_at = $now WHERE id = $id',
|
|
158
|
+
params: { $content: next.content, $name: next.authorName, $email: next.authorEmail.toLowerCase(), $url: next.authorUrl, $now: this.#now(), $id: id },
|
|
159
|
+
}], WRITE);
|
|
160
|
+
if (current.status === 'approved') await this.#changed?.([{ resource: current.resource, recordId: current.recordId }]);
|
|
161
|
+
return (await this.get(id))!;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Delete comments permanently. Their replies move up to the deleted comment's parent. */
|
|
165
|
+
async delete(ids: readonly string[]): Promise<void> {
|
|
166
|
+
const comments = (await Promise.all([...new Set(ids)].slice(0, 100).map(id => this.get(id)))).filter((comment): comment is CommentShape => comment !== null);
|
|
167
|
+
if (!comments.length) return;
|
|
168
|
+
const deleting = new Map(comments.map(comment => [comment.id, comment]));
|
|
169
|
+
// A reply moves up to its nearest ancestor that stays, whatever order the IDs came in.
|
|
170
|
+
const keptParent = (comment: CommentShape): string | null => {
|
|
171
|
+
let parent = comment.parentId;
|
|
172
|
+
for (let depth = 0; parent && deleting.has(parent) && depth <= deleting.size; depth++) parent = deleting.get(parent)!.parentId;
|
|
173
|
+
return parent && deleting.has(parent) ? null : parent;
|
|
174
|
+
};
|
|
175
|
+
const statements: Statement[] = [];
|
|
176
|
+
for (const comment of comments) {
|
|
177
|
+
statements.push({ rql: 'UPDATE cms_comments SET parent_id = $parent WHERE parent_id = $id', params: { $parent: keptParent(comment), $id: comment.id } });
|
|
178
|
+
statements.push({ rql: 'DELETE FROM cms_comments WHERE id = $id', params: { $id: comment.id } });
|
|
179
|
+
}
|
|
180
|
+
// Each comment's two statements stay in one request.
|
|
181
|
+
for (const batch of await requestBatches(this.#db, statements, { group: 2 })) await this.#db.write(batch, WRITE);
|
|
182
|
+
await this.#changed?.(comments.filter(comment => comment.status === 'approved').map(comment => ({ resource: comment.resource, recordId: comment.recordId })));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The entries of these comments, for a purge. */
|
|
186
|
+
async #entriesOf(list: { rql: string; params: { [name: string]: Value } }): Promise<{ resource: string; recordId: string }[]> {
|
|
187
|
+
const result = await this.#db.read([{ rql: `SELECT DISTINCT resource, record_id FROM cms_comments WHERE id IN ${list.rql}`, params: list.params }]);
|
|
188
|
+
return result.results[0]!.rows.map(([resource, recordId]) => ({ resource: resource as string, recordId: recordId as string }));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Delete an entry's comments, after the entry is deleted. */
|
|
192
|
+
/** Delete an entry's comments posted up to its delete at `before`: an entry made again under the same ID keeps later ones. */
|
|
193
|
+
async deleteForEntry(resource: string, recordId: string, before: number): Promise<void> {
|
|
194
|
+
await this.#deleteWhere('resource = $resource AND record_id = $record AND created_at <= $before', { $resource: resource, $record: recordId, $before: before });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Delete every comment on a resource's entries, after the resource (a collection) is deleted
|
|
199
|
+
* at `before`: comments posted later belong to a collection made again under the same name.
|
|
200
|
+
*/
|
|
201
|
+
async deleteForResource(resource: string, before: number): Promise<void> {
|
|
202
|
+
await this.#deleteWhere('resource = $resource AND created_at <= $before', { $resource: resource, $before: before });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Delete matching comments a thousand at a time, until none are left. */
|
|
206
|
+
async #deleteWhere(where: string, params: { [key: string]: Value }): Promise<void> {
|
|
207
|
+
for (;;) {
|
|
208
|
+
const result = await this.#db.write([{ rql: `DELETE FROM cms_comments WHERE id IN (SELECT id FROM cms_comments WHERE ${where} LIMIT ${DELETE_BATCH})`, params }], WRITE);
|
|
209
|
+
if (Number(result.results[0]!.affected) < DELETE_BATCH) return;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** How many comments one delete removes. */
|
|
215
|
+
const DELETE_BATCH = 1000;
|
|
216
|
+
|
|
217
|
+
function conditions(options: CommentListOptions): { where: string; params: { [key: string]: Value } } {
|
|
218
|
+
const parts: string[] = [];
|
|
219
|
+
const params: { [key: string]: Value } = {};
|
|
220
|
+
if (options.status) { parts.push('status = $status'); params.$status = options.status; }
|
|
221
|
+
else parts.push("status != 'trash' AND status != 'spam'");
|
|
222
|
+
if (options.resource) { parts.push('resource = $resource'); params.$resource = options.resource; }
|
|
223
|
+
if (options.recordId) { parts.push('record_id = $record'); params.$record = options.recordId; }
|
|
224
|
+
if (options.search?.trim()) {
|
|
225
|
+
parts.push("(lower(content) LIKE $q ESCAPE '\\' OR lower(author_name) LIKE $q ESCAPE '\\' OR author_email LIKE $q ESCAPE '\\')");
|
|
226
|
+
params.$q = containsPattern(options.search.trim().slice(0, 100));
|
|
227
|
+
}
|
|
228
|
+
return { where: parts.length ? ` WHERE ${parts.join(' AND ')}` : '', params };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function commentOf(row: readonly Value[]): CommentShape {
|
|
232
|
+
return Object.freeze({
|
|
233
|
+
id: row[0] as string, resource: row[1] as string, recordId: row[2] as string, parentId: row[3] as string | null, userId: row[4] as string | null,
|
|
234
|
+
authorName: row[5] as string, authorEmail: row[6] as string, authorUrl: row[7] as string, content: row[8] as string, status: row[9] as CommentStatus,
|
|
235
|
+
createdAt: Number(row[10] as bigint), updatedAt: Number(row[11] as bigint),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared drafts: what an entry's room (every editor of the entry together) holds between saves,
|
|
3
|
+
* one row per entry, so a restart or an evicted instance loses at most the room's write delay.
|
|
4
|
+
* The row also names who is editing, for the entries list. A draft is capped at what an entry
|
|
5
|
+
* can hold and still be saved (`saveBytes`).
|
|
6
|
+
*/
|
|
7
|
+
import { statementBytes, type RomeDB, type Value } from '@cossackframework/romedb';
|
|
8
|
+
import { CmsError } from '../errors';
|
|
9
|
+
|
|
10
|
+
const WRITE = { kind: 'cms.drafts', retryConflicts: 5 } as const;
|
|
11
|
+
const KEY = /^[A-Za-z0-9_-]{1,80}$/;
|
|
12
|
+
|
|
13
|
+
/** Room a save takes besides its values: its statements, guards, audit and search rows. */
|
|
14
|
+
const SAVE_OVERHEAD = 6144;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The bytes saving these values takes in a RomeDB request, from above: the fields as the client
|
|
18
|
+
* sends them (typed, so nested rich text and blocks weigh about three times their JSON), plus the
|
|
19
|
+
* JSON quoted again three times (the search text, the revision and the event), plus the rest of
|
|
20
|
+
* the save. A save under the database's request size fits.
|
|
21
|
+
*/
|
|
22
|
+
export function saveBytes(values: unknown, shared: readonly string[] = []): number {
|
|
23
|
+
const json = JSON.stringify(values ?? {});
|
|
24
|
+
let bytes = typedBytes(values, json) + 3 * requestBytes(json) + SAVE_OVERHEAD;
|
|
25
|
+
// Fields the same in every language go out a second time, with their search text, to the
|
|
26
|
+
// entry's other translations in the same request.
|
|
27
|
+
if (shared.length && values && typeof values === 'object' && !Array.isArray(values)) {
|
|
28
|
+
const subset = Object.fromEntries(shared.filter(name => Object.hasOwn(values, name)).map(name => [name, (values as Record<string, unknown>)[name]]));
|
|
29
|
+
if (Object.keys(subset).length) {
|
|
30
|
+
const subsetJson = JSON.stringify(subset);
|
|
31
|
+
bytes += typedBytes(subset, subsetJson) + requestBytes(subsetJson);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return bytes;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function typedBytes(values: unknown, json: string): number {
|
|
38
|
+
try { return statementBytes([{ rql: '', params: { $values: values as Value } }]); } catch { return 4 * encoder.encode(json).length; }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Refused as `CMS_LIMIT` when saving the values would not fit one request to this database. */
|
|
42
|
+
export async function checkDraftSize(db: RomeDB, json: string, what: string, shared: readonly string[] = []): Promise<void> {
|
|
43
|
+
const limit = await db.maxRequestBytes();
|
|
44
|
+
// A save carries well under eight times the JSON (twelve with shared fields): a draft that
|
|
45
|
+
// small fits without measuring it.
|
|
46
|
+
if ((shared.length ? 12 : 8) * requestBytes(json) + SAVE_OVERHEAD <= limit) return;
|
|
47
|
+
if (saveBytes(JSON.parse(json) as unknown, shared) > limit) throw new CmsError('CMS_LIMIT', `The ${what} is too large: an entry this size could not be saved. Shorten it, or split it into several entries.`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const encoder = new TextEncoder();
|
|
51
|
+
|
|
52
|
+
/** The bytes a string parameter takes in a RomeDB request: UTF-8, quoted and escaped as JSON. */
|
|
53
|
+
export function requestBytes(value: string): number {
|
|
54
|
+
return encoder.encode(JSON.stringify(value)).length;
|
|
55
|
+
}
|
|
56
|
+
/** An editor the room has not reported for this long is no longer editing. */
|
|
57
|
+
export const EDITING_WINDOW = 90_000;
|
|
58
|
+
|
|
59
|
+
export interface Draft {
|
|
60
|
+
/** The record's revision the draft started from or was last saved as. */
|
|
61
|
+
readonly baseRev: number;
|
|
62
|
+
readonly values: Record<string, unknown>;
|
|
63
|
+
/** Rich text step log versions by document path. */
|
|
64
|
+
readonly versions: Record<string, number>;
|
|
65
|
+
/** User IDs in the room when the row was last written. */
|
|
66
|
+
readonly editors: readonly string[];
|
|
67
|
+
readonly at: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface DraftInput {
|
|
71
|
+
readonly baseRev: number;
|
|
72
|
+
readonly values: Record<string, unknown>;
|
|
73
|
+
readonly versions: Record<string, number>;
|
|
74
|
+
readonly editors: readonly string[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The fields of a resource that are the same in every language, which a save also gives its other translations. */
|
|
78
|
+
export type SharedFieldsOf = (resource: string) => readonly string[];
|
|
79
|
+
|
|
80
|
+
export class CmsDrafts {
|
|
81
|
+
readonly #db: RomeDB;
|
|
82
|
+
readonly #now: () => number;
|
|
83
|
+
readonly #shared: SharedFieldsOf;
|
|
84
|
+
|
|
85
|
+
constructor(db: RomeDB, options: { now?: () => number; sharedFields?: SharedFieldsOf } = {}) {
|
|
86
|
+
this.#db = db;
|
|
87
|
+
this.#now = options.now ?? Date.now;
|
|
88
|
+
this.#shared = options.sharedFields ?? (() => []);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Store the room's draft, replacing the entry's previous one. */
|
|
92
|
+
async save(resource: string, recordId: string, draft: DraftInput): Promise<number> {
|
|
93
|
+
check(recordId);
|
|
94
|
+
if (!Number.isSafeInteger(draft.baseRev) || draft.baseRev < 0) throw new CmsError('CMS_VALIDATION', 'baseRev must be a nonnegative integer.');
|
|
95
|
+
const data = JSON.stringify(draft.values ?? {});
|
|
96
|
+
await checkDraftSize(this.#db, data, 'draft', this.#shared(resource));
|
|
97
|
+
const at = this.#now();
|
|
98
|
+
await this.#db.write([{
|
|
99
|
+
rql: `INSERT INTO cms_drafts (id, resource, record_id, base_rev, data, logs, editors, editors_at, at) VALUES ($id, $resource, $record, $rev, $data, $logs, $editors, $at, $at)
|
|
100
|
+
ON CONFLICT (id) DO UPDATE SET base_rev = excluded.base_rev, data = excluded.data, logs = excluded.logs, editors = excluded.editors, editors_at = excluded.editors_at, at = excluded.at`,
|
|
101
|
+
params: { $id: key(resource, recordId), $resource: resource, $record: recordId, $rev: draft.baseRev, $data: data, $logs: JSON.stringify(draft.versions ?? {}), $editors: JSON.stringify(editorList(draft.editors)), $at: at },
|
|
102
|
+
}], WRITE);
|
|
103
|
+
return at;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async get(resource: string, recordId: string): Promise<Draft | null> {
|
|
107
|
+
check(recordId);
|
|
108
|
+
const result = await this.#db.read([{ rql: 'SELECT base_rev, data, logs, editors, at FROM cms_drafts WHERE id = $id', params: { $id: key(resource, recordId) } }]);
|
|
109
|
+
const row = result.results[0]!.rows[0];
|
|
110
|
+
if (!row) return null;
|
|
111
|
+
try {
|
|
112
|
+
const values = JSON.parse(row[1] as string) as unknown;
|
|
113
|
+
const versions = JSON.parse(row[2] as string) as unknown;
|
|
114
|
+
return {
|
|
115
|
+
baseRev: Number(row[0] as bigint), at: Number(row[4] as bigint),
|
|
116
|
+
values: values && typeof values === 'object' && !Array.isArray(values) ? values as Record<string, unknown> : {},
|
|
117
|
+
versions: versions && typeof versions === 'object' && !Array.isArray(versions) ? versions as Record<string, number> : {},
|
|
118
|
+
editors: editorList(JSON.parse(row[3] as string)),
|
|
119
|
+
};
|
|
120
|
+
} catch { return null; }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Who is in the entry's room now, without touching the draft itself: the room reports its
|
|
125
|
+
* editors as they come and go. An entry without a draft row gets one holding no values.
|
|
126
|
+
*/
|
|
127
|
+
async editing(resource: string, recordId: string, editors: readonly string[]): Promise<void> {
|
|
128
|
+
check(recordId);
|
|
129
|
+
const at = this.#now();
|
|
130
|
+
await this.#db.write([{
|
|
131
|
+
rql: `INSERT INTO cms_drafts (id, resource, record_id, base_rev, data, logs, editors, editors_at, at) VALUES ($id, $resource, $record, 0, '{}', '{}', $editors, $at, 0)
|
|
132
|
+
ON CONFLICT (id) DO UPDATE SET editors = excluded.editors, editors_at = excluded.editors_at`,
|
|
133
|
+
params: { $id: key(resource, recordId), $resource: resource, $record: recordId, $editors: JSON.stringify(editorList(editors)), $at: at },
|
|
134
|
+
}], WRITE);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The editors in each entry's room within the window, by record ID; entries nobody edits are left out. */
|
|
138
|
+
async editors(resource: string, recordIds: readonly string[], window = EDITING_WINDOW): Promise<Map<string, string[]>> {
|
|
139
|
+
const wanted = new Set(recordIds);
|
|
140
|
+
const found = new Map<string, string[]>();
|
|
141
|
+
if (!wanted.size) return found;
|
|
142
|
+
const result = await this.#db.read([{ rql: 'SELECT record_id, editors FROM cms_drafts WHERE resource = $resource AND editors_at > $since LIMIT 1000', params: { $resource: resource, $since: this.#now() - window } }]);
|
|
143
|
+
for (const row of result.results[0]!.rows) {
|
|
144
|
+
const id = row[0] as string;
|
|
145
|
+
if (!wanted.has(id)) continue;
|
|
146
|
+
try { const editors = editorList(JSON.parse(row[1] as string)); if (editors.length) found.set(id, editors); } catch { /* an unreadable row names nobody */ }
|
|
147
|
+
}
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The entry was saved as `rev` from the draft: a reopened room starts from the record again. */
|
|
152
|
+
async discard(resource: string, recordId: string): Promise<void> {
|
|
153
|
+
check(recordId);
|
|
154
|
+
await this.#db.write([{ rql: 'DELETE FROM cms_drafts WHERE id = $id', params: { $id: key(resource, recordId) } }], WRITE);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Drop drafts untouched for `olderThanMs` (default 30 days). */
|
|
158
|
+
async purge(olderThanMs = 30 * 24 * 60 * 60 * 1000): Promise<void> {
|
|
159
|
+
const before = this.#now() - olderThanMs;
|
|
160
|
+
const old = await this.#db.read([{ rql: 'SELECT 1 FROM cms_drafts WHERE at <= $before AND editors_at <= $before LIMIT 1', params: { $before: before } }]);
|
|
161
|
+
if (!old.results[0]!.rows.length) return;
|
|
162
|
+
await this.#db.write([{ rql: 'DELETE FROM cms_drafts WHERE id IN (SELECT id FROM cms_drafts WHERE at <= $before AND editors_at <= $before LIMIT 500)', params: { $before: before } }], WRITE);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function key(resource: string, recordId: string): string {
|
|
167
|
+
return `${resource}:${recordId}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function check(recordId: string): void {
|
|
171
|
+
if (!KEY.test(recordId)) throw new CmsError('CMS_VALIDATION', 'Invalid entry.');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function editorList(value: unknown): string[] {
|
|
175
|
+
return Array.isArray(value) ? [...new Set(value.filter((entry): entry is string => typeof entry === 'string' && KEY.test(entry)))].slice(0, 100) : [];
|
|
176
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Field groups: one `cms_field_groups` row per group, its definition as JSON. The host
|
|
3
|
+
* validates a group against the block types and its targets' own fields, merges its fields
|
|
4
|
+
* into the targets' resources and plans the schema; this store keeps the rows and a per-isolate
|
|
5
|
+
* cache the host drops on every write.
|
|
6
|
+
*/
|
|
7
|
+
import { ClientError, type RomeDB, type Statement } from '@cossackframework/romedb';
|
|
8
|
+
import { CmsError } from '../errors';
|
|
9
|
+
import { groupsForTarget, type FieldGroup, type FieldGroupTarget } from '../field-groups';
|
|
10
|
+
import { principalId, type Principal } from '../permissions';
|
|
11
|
+
import { readPaged } from './budget';
|
|
12
|
+
|
|
13
|
+
const WRITE = { kind: 'cms.fieldGroups', retryConflicts: 5 } as const;
|
|
14
|
+
const LIMIT = 200;
|
|
15
|
+
|
|
16
|
+
export class CmsFieldGroups {
|
|
17
|
+
readonly #db: RomeDB;
|
|
18
|
+
readonly #now: () => number;
|
|
19
|
+
#cache: Promise<FieldGroup[]> | undefined;
|
|
20
|
+
|
|
21
|
+
constructor(db: RomeDB, options: { now?: () => number } = {}) {
|
|
22
|
+
this.#db = db;
|
|
23
|
+
this.#now = options.now ?? Date.now;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Every group, in order then by title. Cached per isolate until a write made here. */
|
|
27
|
+
all(): Promise<FieldGroup[]> {
|
|
28
|
+
this.#cache ??= this.#read().then(groups => groups, error => { this.#cache = undefined; throw error; });
|
|
29
|
+
return this.#cache;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async get(id: string): Promise<FieldGroup | null> {
|
|
33
|
+
return (await this.all()).find(group => group.id === id) ?? null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The groups attached to a target, in their order there. */
|
|
37
|
+
async forTarget(target: FieldGroupTarget): Promise<FieldGroup[]> {
|
|
38
|
+
return groupsForTarget(await this.all(), target);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Insert or replace a group the host has checked. */
|
|
42
|
+
async put(group: FieldGroup, principal: Principal): Promise<void> {
|
|
43
|
+
try {
|
|
44
|
+
await this.#db.write([this.putStatement(group, principal)], WRITE);
|
|
45
|
+
} finally {
|
|
46
|
+
this.#cache = undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The statement `put` writes, for a batch the host writes itself; `reset()` after it. */
|
|
51
|
+
putStatement(group: FieldGroup, principal: Principal): Statement {
|
|
52
|
+
return {
|
|
53
|
+
rql: 'INSERT INTO cms_field_groups (id, definition, sort, updated_at, updated_by) VALUES ($id, $definition, $sort, $now, $by) ON CONFLICT (id) DO UPDATE SET definition = excluded.definition, sort = excluded.sort, updated_at = excluded.updated_at, updated_by = excluded.updated_by',
|
|
54
|
+
params: { $id: group.id, $definition: JSON.stringify(group), $sort: group.order ?? 0, $now: this.#now(), $by: principalId(principal) },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async remove(id: string): Promise<boolean> {
|
|
59
|
+
try {
|
|
60
|
+
const result = await this.#db.write([{ rql: 'DELETE FROM cms_field_groups WHERE id = $id', params: { $id: id } }], WRITE);
|
|
61
|
+
return result.results[0]!.affected === 1n;
|
|
62
|
+
} finally {
|
|
63
|
+
this.#cache = undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Forget the cache, after a write made elsewhere. */
|
|
68
|
+
reset(): void {
|
|
69
|
+
this.#cache = undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async #read(): Promise<FieldGroup[]> {
|
|
73
|
+
let rows: readonly (readonly unknown[])[] = [];
|
|
74
|
+
try {
|
|
75
|
+
// Definitions are JSON of many fields: they may come in smaller reads.
|
|
76
|
+
rows = await readPaged(this.#db, (size, at) => ({ rql: 'SELECT definition FROM cms_field_groups ORDER BY sort, id LIMIT $limit OFFSET $offset', params: { $limit: size, $offset: at } }), LIMIT, 0);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// Before setup the table does not exist; there are no groups.
|
|
79
|
+
if (!(error instanceof ClientError) || !/cms_field_groups|no such table|not found/i.test(error.message)) throw new CmsError('CMS_UNAVAILABLE', 'Field groups could not be read.', { cause: error });
|
|
80
|
+
}
|
|
81
|
+
return rows.flatMap(([definition]) => {
|
|
82
|
+
try {
|
|
83
|
+
const group = JSON.parse(definition as string) as FieldGroup;
|
|
84
|
+
return [Object.freeze({ ...group, blocks: group.blocks ?? [] })];
|
|
85
|
+
} catch { return []; }
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|