tinyzoo 0.0.0-stage → 0.2.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 +10 -0
- package/README.md +46 -2
- package/app/app.config.ts +35 -0
- package/app/assets/brand/characters/bright/bird.svg +1 -0
- package/app/assets/brand/characters/bright/bunny.svg +1 -0
- package/app/assets/brand/characters/bright/elephant.svg +1 -0
- package/app/assets/brand/characters/bright/fox.svg +1 -0
- package/app/assets/brand/characters/bright/frog.svg +1 -0
- package/app/assets/brand/characters/bright/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/bee.svg +1 -0
- package/app/assets/brand/characters/muted/bird.svg +1 -0
- package/app/assets/brand/characters/muted/butterfly.svg +1 -0
- package/app/assets/brand/characters/muted/chick.svg +1 -0
- package/app/assets/brand/characters/muted/cow.svg +1 -0
- package/app/assets/brand/characters/muted/deer.svg +1 -0
- package/app/assets/brand/characters/muted/duck.svg +1 -0
- package/app/assets/brand/characters/muted/elephant.svg +1 -0
- package/app/assets/brand/characters/muted/fox.svg +1 -0
- package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
- package/app/assets/brand/characters/muted/koala.svg +1 -0
- package/app/assets/brand/characters/muted/ladybug.svg +1 -0
- package/app/assets/brand/characters/muted/lion.svg +1 -0
- package/app/assets/brand/characters/muted/mouse.svg +1 -0
- package/app/assets/brand/characters/muted/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/owl.svg +1 -0
- package/app/assets/brand/characters/muted/panda.svg +1 -0
- package/app/assets/brand/characters/muted/penguin.svg +1 -0
- package/app/assets/brand/characters/muted/pig.svg +1 -0
- package/app/assets/brand/characters/muted/poodle.svg +1 -0
- package/app/assets/brand/characters/muted/raccoon.svg +1 -0
- package/app/assets/brand/characters/muted/seal.svg +1 -0
- package/app/assets/brand/characters/muted/sheep.svg +1 -0
- package/app/assets/brand/characters/muted/snail.svg +1 -0
- package/app/assets/brand/characters/muted/turtle.svg +1 -0
- package/app/assets/brand/characters/muted/whale.svg +1 -0
- package/app/assets/brand/logo/favicon.svg +1 -0
- package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
- package/app/assets/brand/logo/logo-primary.svg +1 -0
- package/app/assets/brand/logo/mascot-reversed.svg +1 -0
- package/app/assets/brand/logo/mascot.svg +1 -0
- package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
- package/app/assets/brand/logo/wordmark.svg +1 -0
- package/app/assets/css/cms-admin.css +607 -0
- package/app/components/CmsBlocks.vue +30 -0
- package/app/components/CmsCell.vue +16 -0
- package/app/components/CmsCellBadge.vue +18 -0
- package/app/components/CmsCellBlocks.vue +9 -0
- package/app/components/CmsCellBoolean.vue +12 -0
- package/app/components/CmsCellCode.vue +7 -0
- package/app/components/CmsCellColor.vue +13 -0
- package/app/components/CmsCellDate.vue +10 -0
- package/app/components/CmsCellExcerpt.vue +21 -0
- package/app/components/CmsCellGallery.vue +10 -0
- package/app/components/CmsCellGroup.vue +23 -0
- package/app/components/CmsCellNumber.vue +7 -0
- package/app/components/CmsCellRelation.vue +8 -0
- package/app/components/CmsCellText.vue +12 -0
- package/app/components/CmsCellThumb.vue +18 -0
- package/app/components/CmsConfirmDialog.vue +55 -0
- package/app/components/CmsContent.vue +103 -0
- package/app/components/CmsDayPicker.vue +44 -0
- package/app/components/CmsEmptyState.vue +35 -0
- package/app/components/CmsField.vue +111 -0
- package/app/components/CmsFieldBlocks.vue +353 -0
- package/app/components/CmsFieldBoolean.vue +8 -0
- package/app/components/CmsFieldColor.vue +42 -0
- package/app/components/CmsFieldDate.vue +131 -0
- package/app/components/CmsFieldGallery.vue +114 -0
- package/app/components/CmsFieldGroup.vue +53 -0
- package/app/components/CmsFieldMedia.vue +219 -0
- package/app/components/CmsFieldNumber.vue +11 -0
- package/app/components/CmsFieldRelationMany.vue +54 -0
- package/app/components/CmsFieldRelationOne.vue +47 -0
- package/app/components/CmsFieldRichText.vue +45 -0
- package/app/components/CmsFieldSelect.vue +41 -0
- package/app/components/CmsFieldSlug.vue +46 -0
- package/app/components/CmsFieldText.vue +75 -0
- package/app/components/CmsFormSections.vue +150 -0
- package/app/components/CmsHighlight.vue +22 -0
- package/app/components/CmsImage.vue +39 -0
- package/app/components/CmsLinkPicker.vue +122 -0
- package/app/components/CmsLinkTarget.vue +35 -0
- package/app/components/CmsListFilters.vue +107 -0
- package/app/components/CmsLiveNotice.vue +64 -0
- package/app/components/CmsLoadError.vue +24 -0
- package/app/components/CmsMascot.vue +10 -0
- package/app/components/CmsMediaLibrary.vue +114 -0
- package/app/components/CmsMediaPreview.vue +34 -0
- package/app/components/CmsMediaThumb.vue +22 -0
- package/app/components/CmsMoment.vue +42 -0
- package/app/components/CmsNav.vue +39 -0
- package/app/components/CmsPage.vue +86 -0
- package/app/components/CmsPasswordInput.vue +22 -0
- package/app/components/CmsPresence.vue +29 -0
- package/app/components/CmsPreviewPanel.vue +131 -0
- package/app/components/CmsPublishReview.vue +102 -0
- package/app/components/CmsRevisions.vue +195 -0
- package/app/components/CmsRichTextEditor.vue +290 -0
- package/app/components/CmsSearch.vue +99 -0
- package/app/components/CmsSection.vue +20 -0
- package/app/components/CmsSingletonForm.vue +283 -0
- package/app/components/CmsStatusBadge.vue +21 -0
- package/app/components/CmsTime.vue +16 -0
- package/app/components/CmsTranslationRef.vue +27 -0
- package/app/components/CmsTreeList.vue +453 -0
- package/app/components/CmsUnsavedText.vue +34 -0
- package/app/components/CmsWordmark.vue +10 -0
- package/app/composables/useCmsAdminHead.ts +18 -0
- package/app/composables/useCmsCollab.ts +165 -0
- package/app/composables/useCmsConfirm.ts +39 -0
- package/app/composables/useCmsEntryActions.ts +75 -0
- package/app/composables/useCmsFocus.ts +13 -0
- package/app/composables/useCmsForm.ts +273 -0
- package/app/composables/useCmsI18n.ts +185 -0
- package/app/composables/useCmsLinkTargets.ts +84 -0
- package/app/composables/useCmsLive.ts +199 -0
- package/app/composables/useCmsSearch.ts +62 -0
- package/app/composables/useCmsSeo.ts +99 -0
- package/app/composables/useCmsSignedIn.ts +24 -0
- package/app/composables/useCmsUiLocale.ts +24 -0
- package/app/i18n/cs.ts +781 -0
- package/app/i18n/de.ts +781 -0
- package/app/i18n/en.ts +782 -0
- package/app/i18n/types.ts +22 -0
- package/app/layouts/admin.vue +224 -0
- package/app/layouts/cms-auth.vue +37 -0
- package/app/middleware/cms-api-not-a-page.global.ts +10 -0
- package/app/middleware/cms-auth.global.ts +16 -0
- package/app/pages/admin/[entity]/[id].vue +1109 -0
- package/app/pages/admin/[entity]/index.vue +733 -0
- package/app/pages/admin/account.vue +117 -0
- package/app/pages/admin/forgot.vue +50 -0
- package/app/pages/admin/index.vue +184 -0
- package/app/pages/admin/invite/[token].vue +73 -0
- package/app/pages/admin/login.vue +84 -0
- package/app/pages/admin/media.vue +494 -0
- package/app/pages/admin/privacy.vue +193 -0
- package/app/pages/admin/reset/[token].vue +52 -0
- package/app/pages/admin/setup.vue +87 -0
- package/app/pages/admin/styleguide.vue +261 -0
- package/app/pages/admin/team.vue +258 -0
- package/app/plugins/cmsLivePreview.client.ts +20 -0
- package/app/plugins/cmsSignedOut.client.ts +31 -0
- package/app/utils/cmsBlockType.ts +11 -0
- package/app/utils/cmsBrand.ts +38 -0
- package/app/utils/cmsDiff.ts +83 -0
- package/app/utils/cmsFetch.ts +7 -0
- package/app/utils/cmsFocusInvalid.ts +22 -0
- package/app/utils/cmsIssues.ts +128 -0
- package/app/utils/cmsJson.ts +28 -0
- package/app/utils/cmsMedia.ts +63 -0
- package/app/utils/cmsSections.ts +154 -0
- package/app/utils/cmsTime.ts +41 -0
- package/cli/backup.ts +713 -0
- package/cli/breaking.ts +174 -0
- package/cli/content-migrate.ts +239 -0
- package/cli/db.ts +86 -0
- package/cli/export.ts +336 -0
- package/cli/generate.ts +146 -0
- package/cli/import.ts +643 -0
- package/cli/index.ts +140 -0
- package/cli/links-convert.ts +291 -0
- package/cli/seed-engine.ts +705 -0
- package/cli/seed.ts +339 -0
- package/cli/status.ts +49 -0
- package/cli/tar.ts +304 -0
- package/cli/transfer.ts +677 -0
- package/cli/up.ts +27 -0
- package/cli/upgrade.ts +60 -0
- package/cli/versions.ts +60 -0
- package/forms/components/CmsForm.vue +186 -0
- package/forms/index.ts +203 -0
- package/forms/messages.ts +104 -0
- package/forms/retention.ts +48 -0
- package/forms/server/form.get.ts +12 -0
- package/forms/server/form.post.ts +109 -0
- package/forms/server/formSettings.ts +12 -0
- package/forms/server/forms.ts +127 -0
- package/module.ts +373 -0
- package/nuxt.config.ts +43 -0
- package/package.json +98 -3
- package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
- package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
- package/server/api/cms/[entity]/[id].delete.ts +52 -0
- package/server/api/cms/[entity]/[id].get.ts +21 -0
- package/server/api/cms/[entity]/[id].patch.ts +116 -0
- package/server/api/cms/[entity]/_export.get.ts +69 -0
- package/server/api/cms/[entity]/_move.post.ts +81 -0
- package/server/api/cms/[entity]/_order.post.ts +49 -0
- package/server/api/cms/[entity]/_tree.get.ts +41 -0
- package/server/api/cms/[entity]/index.get.ts +62 -0
- package/server/api/cms/[entity]/index.post.ts +38 -0
- package/server/api/cms/[entity]/options.get.ts +55 -0
- package/server/api/cms/_auth/accept.post.ts +33 -0
- package/server/api/cms/_auth/forgot.post.ts +39 -0
- package/server/api/cms/_auth/invite/[token].get.ts +18 -0
- package/server/api/cms/_auth/login.post.ts +42 -0
- package/server/api/cms/_auth/logout.post.ts +7 -0
- package/server/api/cms/_auth/me.get.ts +5 -0
- package/server/api/cms/_auth/me.patch.ts +32 -0
- package/server/api/cms/_auth/password.post.ts +29 -0
- package/server/api/cms/_auth/reset.post.ts +34 -0
- package/server/api/cms/_auth/setup.post.ts +46 -0
- package/server/api/cms/_auth/status.get.ts +18 -0
- package/server/api/cms/_collab/session.get.ts +30 -0
- package/server/api/cms/_collab/socket.ts +51 -0
- package/server/api/cms/_collab/unsaved.delete.ts +16 -0
- package/server/api/cms/_graph.get.ts +20 -0
- package/server/api/cms/_links/resolve.post.ts +17 -0
- package/server/api/cms/_links.get.ts +81 -0
- package/server/api/cms/_live.ts +30 -0
- package/server/api/cms/_media/[id]/usage.get.ts +14 -0
- package/server/api/cms/_media/[id].delete.ts +29 -0
- package/server/api/cms/_media/[id].get.ts +13 -0
- package/server/api/cms/_media/[id].patch.ts +64 -0
- package/server/api/cms/_media/exists.post.ts +15 -0
- package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
- package/server/api/cms/_media/index.get.ts +44 -0
- package/server/api/cms/_media/index.post.ts +147 -0
- package/server/api/cms/_media/raw/[...key].get.ts +85 -0
- package/server/api/cms/_media/raw/[...key].head.ts +2 -0
- package/server/api/cms/_overview.get.ts +87 -0
- package/server/api/cms/_preview.post.ts +17 -0
- package/server/api/cms/_privacy/delete.post.ts +14 -0
- package/server/api/cms/_privacy/export.post.ts +25 -0
- package/server/api/cms/_privacy/lookup.post.ts +27 -0
- package/server/api/cms/_search.get.ts +17 -0
- package/server/api/cms/_singleton/[name].get.ts +19 -0
- package/server/api/cms/_singleton/[name].put.ts +76 -0
- package/server/api/cms/_users/[id]/resend.post.ts +25 -0
- package/server/api/cms/_users/[id].delete.ts +21 -0
- package/server/api/cms/_users/[id].patch.ts +45 -0
- package/server/api/cms/_users/index.get.ts +28 -0
- package/server/api/cms/_users/index.post.ts +39 -0
- package/server/api/content/[entity]/[slug].get.ts +74 -0
- package/server/api/content/[entity]/index.get.ts +100 -0
- package/server/api/content/_global/[name].get.ts +23 -0
- package/server/api/content/_search.get.ts +28 -0
- package/server/api/content/_sitemap.get.ts +8 -0
- package/server/api/content/_tree/[entity].get.ts +31 -0
- package/server/api/content-types.ts.get.ts +17 -0
- package/server/api/health/live.get.ts +10 -0
- package/server/api/health/ready.get.ts +21 -0
- package/server/api/openapi.get.ts +48 -0
- package/server/api/openapi.json.get.ts +32 -0
- package/server/db/driver.ts +8 -0
- package/server/mail/mailer.ts +43 -0
- package/server/middleware/cms-0-headers.ts +37 -0
- package/server/middleware/cms-0-migrate-gate.ts +14 -0
- package/server/middleware/cms-csrf.ts +28 -0
- package/server/middleware/cms-limits.ts +20 -0
- package/server/middleware/cms-redirects.ts +21 -0
- package/server/middleware/cms-session.ts +41 -0
- package/server/middleware/cms-z-plugin-access.ts +15 -0
- package/server/plugins/cms-0-config-check.ts +35 -0
- package/server/plugins/cms-changes.ts +53 -0
- package/server/plugins/cms-content-etag.ts +21 -0
- package/server/plugins/cms-errors.ts +45 -0
- package/server/plugins/cms-migrate.ts +5 -0
- package/server/plugins/cms-plugins.ts +17 -0
- package/server/plugins/cms-search.ts +12 -0
- package/server/plugins/cms-setup-code.ts +15 -0
- package/server/routes/sitemap.xml.get.ts +25 -0
- package/server/store/redis.ts +95 -0
- package/server/store/types.ts +21 -0
- package/server/tasks/cms/retention.ts +10 -0
- package/server/tasks/cms/scheduled.ts +10 -0
- package/server/tasks/cms/search-reindex.ts +12 -0
- package/server/utils/cmsAudit.ts +29 -0
- package/server/utils/cmsAuth.ts +143 -0
- package/server/utils/cmsBlocks.ts +47 -0
- package/server/utils/cmsChanges.ts +222 -0
- package/server/utils/cmsCollab.ts +286 -0
- package/server/utils/cmsCollabServer.ts +451 -0
- package/server/utils/cmsCollate.ts +27 -0
- package/server/utils/cmsContent.ts +376 -0
- package/server/utils/cmsCrud.ts +361 -0
- package/server/utils/cmsCsv.ts +24 -0
- package/server/utils/cmsDb.ts +23 -0
- package/server/utils/cmsDemo.ts +36 -0
- package/server/utils/cmsEmails.ts +125 -0
- package/server/utils/cmsErrors.ts +40 -0
- package/server/utils/cmsFold.ts +17 -0
- package/server/utils/cmsImageSizes.ts +51 -0
- package/server/utils/cmsJson.ts +6 -0
- package/server/utils/cmsLinkAddress.ts +50 -0
- package/server/utils/cmsLinks.ts +248 -0
- package/server/utils/cmsListQuery.ts +93 -0
- package/server/utils/cmsLive.ts +393 -0
- package/server/utils/cmsLocales.ts +96 -0
- package/server/utils/cmsMail.ts +28 -0
- package/server/utils/cmsMediaGc.ts +188 -0
- package/server/utils/cmsMediaKeys.ts +62 -0
- package/server/utils/cmsMigrate.ts +86 -0
- package/server/utils/cmsPreviewDraft.ts +72 -0
- package/server/utils/cmsPrivacy.ts +176 -0
- package/server/utils/cmsPublished.ts +57 -0
- package/server/utils/cmsRange.ts +38 -0
- package/server/utils/cmsRateLimit.ts +82 -0
- package/server/utils/cmsRedirects.ts +160 -0
- package/server/utils/cmsRetention.ts +61 -0
- package/server/utils/cmsRevisions.ts +103 -0
- package/server/utils/cmsScheduler.ts +45 -0
- package/server/utils/cmsScrub.ts +107 -0
- package/server/utils/cmsSearch.ts +354 -0
- package/server/utils/cmsSession.ts +102 -0
- package/server/utils/cmsSetupCode.ts +97 -0
- package/server/utils/cmsSitemap.ts +176 -0
- package/server/utils/cmsStorage.ts +580 -0
- package/server/utils/cmsStore.ts +109 -0
- package/server/utils/cmsTiming.ts +14 -0
- package/server/utils/cmsTooMany.ts +9 -0
- package/server/utils/cmsTranslationWrite.ts +186 -0
- package/server/utils/cmsTree.ts +251 -0
- package/server/utils/cmsTreeAddress.ts +65 -0
- package/server/utils/cmsUpload.ts +228 -0
- package/server/utils/cmsWebhooks.ts +104 -0
- package/shared/json.ts +11 -0
- package/shared/richtext-editor.ts +56 -0
- package/shared/richtext-normalize.ts +33 -0
- package/shared/richtext-paste.ts +151 -0
- package/shared/storage-delivery.ts +53 -0
- package/src/blocks.ts +624 -0
- package/src/changes.ts +54 -0
- package/src/content-shape.ts +340 -0
- package/src/deprecate.ts +64 -0
- package/src/descriptor.ts +149 -0
- package/src/docs.ts +27 -0
- package/src/emitter.ts +94 -0
- package/src/entity.ts +624 -0
- package/src/errorReport.ts +41 -0
- package/src/fields.ts +465 -0
- package/src/i18n.ts +26 -0
- package/src/index.ts +54 -0
- package/src/linkref.ts +31 -0
- package/src/links.ts +179 -0
- package/src/locales.ts +39 -0
- package/src/media.ts +48 -0
- package/src/migrate.ts +236 -0
- package/src/openapi.ts +377 -0
- package/src/paths.ts +88 -0
- package/src/plugin.ts +505 -0
- package/src/presets.ts +230 -0
- package/src/richtext.ts +88 -0
- package/src/sdk.ts +181 -0
- package/src/search.ts +312 -0
- package/src/seed.ts +225 -0
- package/src/setupCode.ts +30 -0
- package/src/system.ts +219 -0
- package/src/tree.ts +421 -0
- package/src/upgrades.ts +137 -0
- package/starter/.env.example +10 -0
- package/starter/README.md +65 -0
- package/starter/app/app.config.ts +10 -0
- package/starter/app/app.vue +12 -0
- package/starter/app/assets/css/main.css +15 -0
- package/starter/app/components/global/BlockContact.vue +15 -0
- package/starter/app/components/global/BlockCta.vue +15 -0
- package/starter/app/components/global/BlockHero.vue +23 -0
- package/starter/app/components/global/BlockImage.vue +13 -0
- package/starter/app/components/global/BlockText.vue +12 -0
- package/starter/app/composables/useSite.ts +77 -0
- package/starter/app/error.vue +28 -0
- package/starter/app/layouts/default.vue +106 -0
- package/starter/app/lib/cms-content.ts +152 -0
- package/starter/app/pages/[...slug].vue +66 -0
- package/starter/app/pages/hledat.vue +59 -0
- package/starter/app/pages/index.vue +20 -0
- package/starter/cms.config.ts +137 -0
- package/starter/compose.yaml +15 -0
- package/starter/drizzle.config.ts +8 -0
- package/starter/gitignore.txt +8 -0
- package/starter/nuxt.config.ts +13 -0
- package/starter/package.json +38 -0
- package/starter/scripts/typecheck.mjs +38 -0
- package/starter/seed/index.ts +297 -0
- package/starter/seed/media/about.jpg +0 -0
- package/starter/seed/media/hero.jpg +0 -0
- package/starter/seed/media/logo.png +0 -0
- package/starter/seed/media/news-bread.jpg +0 -0
- package/starter/seed/media/news-morning.jpg +0 -0
- package/starter/server/db/migrations/0000_init.sql +269 -0
- package/starter/server/db/migrations/0001_page_tree.sql +5 -0
- package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
- package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
- package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
- package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
- package/starter/server/db/migrations/meta/_journal.json +27 -0
- package/starter/server/db/schema.ts +253 -0
- package/starter/server/plugins/sitemap.ts +11 -0
- package/starter/server/routes/robots.txt.get.ts +12 -0
- package/starter/tsconfig.json +9 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { and, eq, gt, isNull, ne, sql } from 'tinyzoo'
|
|
2
|
+
import type { CmsRegistry } from 'tinyzoo'
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Built-in accounts: sessions (sealed cookie via nuxt-auth-utils), one-time links
|
|
7
|
+
* for invites and password resets, and the helpers the auth and team endpoints share.
|
|
8
|
+
*
|
|
9
|
+
* Password hashing is nuxt-auth-utils' scrypt.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type CmsAuthMode = 'builtin' | 'external'
|
|
13
|
+
|
|
14
|
+
/** 'builtin' = the CMS owns users and sign-in; 'external' = the host sets event.context.cmsUser. */
|
|
15
|
+
export function cmsAuthMode(): CmsAuthMode {
|
|
16
|
+
return ((useRuntimeConfig().cms as any)?.auth?.mode === 'external') ? 'external' : 'builtin'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function assertBuiltinAuth(): void {
|
|
20
|
+
if (cmsAuthMode() !== 'builtin') throw createError({ statusCode: 404, statusMessage: 'Not found' })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const normalizeEmail = (email: string) => email.trim().toLowerCase()
|
|
24
|
+
|
|
25
|
+
/** Password rules, shown to people up front: at least 10 characters. */
|
|
26
|
+
export const passwordSchema = z.string().min(10, 'At least 10 characters').max(200)
|
|
27
|
+
export const emailSchema = z.string().trim().toLowerCase().pipe(z.email())
|
|
28
|
+
|
|
29
|
+
// ── one-time links ─────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
const toBase64Url = (bytes: Uint8Array) =>
|
|
32
|
+
btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
|
33
|
+
|
|
34
|
+
export async function sha256Hex(text: string): Promise<string> {
|
|
35
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))
|
|
36
|
+
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join('')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const INVITE_TTL_MS = 7 * 24 * 60 * 60 * 1000
|
|
40
|
+
export const RESET_TTL_MS = 60 * 60 * 1000
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create a one-time link secret for a user. Earlier unused links of the same kind
|
|
44
|
+
* stop working (a resent invite replaces the old one). Only the hash is stored.
|
|
45
|
+
*/
|
|
46
|
+
export async function issueToken(db: any, reg: CmsRegistry, opts: { userId: string; kind: 'invite' | 'reset'; ttlMs: number; createdBy?: string | null }): Promise<string> {
|
|
47
|
+
const secret = toBase64Url(crypto.getRandomValues(new Uint8Array(32)))
|
|
48
|
+
const t = reg.cmsTokens
|
|
49
|
+
await db.transaction(async (tx: any) => {
|
|
50
|
+
await tx.update(t).set({ usedAt: new Date() })
|
|
51
|
+
.where(and(eq(t.userId, opts.userId), eq(t.kind, opts.kind), isNull(t.usedAt)))
|
|
52
|
+
await tx.insert(t).values({
|
|
53
|
+
userId: opts.userId,
|
|
54
|
+
kind: opts.kind,
|
|
55
|
+
tokenHash: await sha256Hex(secret),
|
|
56
|
+
expiresAt: new Date(Date.now() + opts.ttlMs),
|
|
57
|
+
createdBy: opts.createdBy ?? null,
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
return secret
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The valid (unused, unexpired) token row for a secret, with its user — or null. */
|
|
64
|
+
export async function findToken(db: any, reg: CmsRegistry, secret: string, kind: 'invite' | 'reset') {
|
|
65
|
+
if (!secret || secret.length > 200) return null
|
|
66
|
+
const t = reg.cmsTokens
|
|
67
|
+
const [row] = await db.select().from(t).where(and(
|
|
68
|
+
eq(t.tokenHash, await sha256Hex(secret)), eq(t.kind, kind), isNull(t.usedAt), gt(t.expiresAt, new Date()),
|
|
69
|
+
))
|
|
70
|
+
if (!row) return null
|
|
71
|
+
const [user] = await db.select().from(reg.cmsUsers).where(eq(reg.cmsUsers.id, row.userId))
|
|
72
|
+
return user ? { token: row, user } : null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── sessions ───────────────────────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
/** What the browser's session carries (and `useUserSession()` exposes). */
|
|
78
|
+
export function sessionUser(u: any) {
|
|
79
|
+
return { id: u.id as string, email: u.email as string, name: (u.name as string) || u.email, role: u.role as string, locale: (u.locale as string | null) ?? null }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Sign the person in on this browser (replaces any previous session). */
|
|
83
|
+
export async function startCmsSession(event: any, u: any): Promise<void> {
|
|
84
|
+
await replaceUserSession(event, { user: sessionUser(u), cms: { uid: u.id as string, sv: u.sessionVersion as number } } as any)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ── links in emails ────────────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Absolute URL of an admin page for links in emails. From `cms.publicUrl`
|
|
91
|
+
* (NUXT_CMS_PUBLIC_URL) — never from the request's Host header, which a client
|
|
92
|
+
* controls (a forged Host would put an attacker's domain in a reset link). Outside
|
|
93
|
+
* production the request origin is accepted so local development needs no setup.
|
|
94
|
+
*/
|
|
95
|
+
export function adminLink(event: any, path: string): string {
|
|
96
|
+
const configured = String((useRuntimeConfig().cms as any)?.publicUrl || '').replace(/\/+$/, '')
|
|
97
|
+
if (configured) return configured + path
|
|
98
|
+
if (import.meta.dev || process.env.NODE_ENV !== 'production' || process.env.CMS_ALLOW_REQUEST_ORIGIN === '1') {
|
|
99
|
+
return getRequestURL(event).origin + path
|
|
100
|
+
}
|
|
101
|
+
throw createError({ statusCode: 500, statusMessage: 'cms.publicUrl (NUXT_CMS_PUBLIC_URL) must be set to send links by email' })
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The client IP for rate limiting. `cms.auth.ipHeader` (e.g. 'cf-connecting-ip' on
|
|
106
|
+
* Cloudflare) wins when set; otherwise X-Forwarded-For is read through exactly
|
|
107
|
+
* `cms.auth.trustedProxies` hops (default 1: one ingress / load balancer, as on
|
|
108
|
+
* Pushbase). Set 0 when the app is reached directly.
|
|
109
|
+
*/
|
|
110
|
+
export function clientIp(event: any): string {
|
|
111
|
+
const auth = ((useRuntimeConfig().cms as any)?.auth ?? {}) as { ipHeader?: string; trustedProxies?: number }
|
|
112
|
+
if (auth.ipHeader) {
|
|
113
|
+
const v = getRequestHeader(event, auth.ipHeader)
|
|
114
|
+
if (v) return v.trim().slice(0, 64)
|
|
115
|
+
}
|
|
116
|
+
const socketIp = getRequestIP(event) || undefined
|
|
117
|
+
return pickClientIp(getRequestHeader(event, 'x-forwarded-for'), socketIp, Number(auth.trustedProxies ?? 1))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Equal-time sign-in failures: verify against a fixed hash when the account does not exist. */
|
|
121
|
+
let dummyHash: Promise<string> | undefined
|
|
122
|
+
export function dummyPasswordHash(): Promise<string> {
|
|
123
|
+
dummyHash ??= hashPassword('not-a-real-password-just-for-timing')
|
|
124
|
+
return dummyHash
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Advisory lock serialising changes that could remove the last active administrator. */
|
|
128
|
+
export const ADMIN_GUARD_LOCK = 72_011_002
|
|
129
|
+
|
|
130
|
+
/** 409 unless some other active administrator exists (never lock everyone out). */
|
|
131
|
+
export async function assertAnotherActiveAdmin(db: any, reg: CmsRegistry, exceptId: string): Promise<void> {
|
|
132
|
+
const u = reg.cmsUsers
|
|
133
|
+
const [row] = await db.select({ n: sql<number>`count(*)::int` }).from(u)
|
|
134
|
+
.where(and(eq(u.role, 'admin'), eq(u.status, 'active'), ne(u.id, exceptId)))
|
|
135
|
+
if ((row?.n ?? 0) === 0) {
|
|
136
|
+
throw createError({ statusCode: 409, statusMessage: 'There must always be at least one active administrator' })
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The setup code first-run setup requires, or '' when none is configured. */
|
|
141
|
+
export function setupToken(): string {
|
|
142
|
+
return String((useRuntimeConfig().cms as any)?.auth?.setupToken || '')
|
|
143
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { logUpgradeFailure, upgradeStoredRow } from 'tinyzoo'
|
|
2
|
+
import type { CmsEntity } from 'tinyzoo'
|
|
3
|
+
import type { ZodTypeAny } from 'zod'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Stored JSON (blocks, lists, rich text) is brought to the current shape wherever it
|
|
7
|
+
* is READ, so a deploy never has to rewrite it (`tinyzoo content:migrate` does that
|
|
8
|
+
* when wanted). One place for every read site: the content API, the admin's form,
|
|
9
|
+
* the search index, revisions. Mutates `row` (a fresh database row, or a copy) and
|
|
10
|
+
* returns it.
|
|
11
|
+
*
|
|
12
|
+
* A block whose `upgrade` throws is logged once (entity, record, block id) and left
|
|
13
|
+
* as stored. With `admin`, it is also flagged (`__upgradeFailed`) so the editor can
|
|
14
|
+
* show it as needing attention instead of as a normal block, and every block and list
|
|
15
|
+
* item carries its `v` (version 1 included), so the editor sends it back and a save
|
|
16
|
+
* from an editor that is behind a deploy is recognised.
|
|
17
|
+
*/
|
|
18
|
+
export function upgradeStoredJson<R extends Record<string, any>>(e: CmsEntity, row: R, opts: { admin?: boolean; recordId?: string } = {}): R {
|
|
19
|
+
const where = `${e.name} ${opts.recordId ?? row.id ?? ''}`.trim()
|
|
20
|
+
return upgradeStoredRow(e.fields, row, {
|
|
21
|
+
markFailed: !!opts.admin,
|
|
22
|
+
stamp: !!opts.admin,
|
|
23
|
+
where,
|
|
24
|
+
onError: (issue) => logUpgradeFailure(issue, where),
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The same on a copy: a translation or revision row that must stay as loaded. */
|
|
29
|
+
export function upgradedCopy<R extends Record<string, any>>(e: CmsEntity, row: R, opts: { admin?: boolean; recordId?: string } = {}): R {
|
|
30
|
+
return upgradeStoredJson(e, { ...row }, opts)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The schema a write is checked with: `e.insertSchema` / `e.updateSchema`, with the
|
|
35
|
+
* blocks and list fields told what the row holds now (`stored`: the raw row, or the
|
|
36
|
+
* translation row for a translation; nothing for a new row). Blocks that are behind,
|
|
37
|
+
* whose upgrade fails, or whose type is gone are only kept when they are stored
|
|
38
|
+
* already — see `blocksSchema`.
|
|
39
|
+
*/
|
|
40
|
+
export function writeSchema(e: CmsEntity, mode: 'create' | 'update', stored?: Record<string, any> | null): ZodTypeAny {
|
|
41
|
+
const base: any = mode === 'create' ? e.insertSchema : e.updateSchema
|
|
42
|
+
const own: Record<string, ZodTypeAny> = {}
|
|
43
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
44
|
+
if (fld.type === 'blocks' || fld.type === 'group') own[key] = fld.zod(mode, { stored: stored?.[key] })
|
|
45
|
+
}
|
|
46
|
+
return Object.keys(own).length ? base.extend(own) : base
|
|
47
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { and, eq, fillPathTemplate, indexTree, inArray, pathTemplateKeys, sitePath, sitePrefix, type CmsChange, type CmsChangeType, type CmsEntity, type CmsRegistry } from 'tinyzoo'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One place every content write reports to. A change:
|
|
5
|
+
*
|
|
6
|
+
* - clears this instance's caches built from content (redirects, sitemap) at once;
|
|
7
|
+
* - is published on the shared store's `changes` channel, so the other replicas clear
|
|
8
|
+
* theirs too and the realtime hub can tell people looking at the same content;
|
|
9
|
+
* - runs the `cms:change` Nitro hook, for host apps that react in-process;
|
|
10
|
+
* - when it can change the public website, is sent to the configured webhooks
|
|
11
|
+
* (signed; see cmsWebhooks.ts) so the site can purge its cache.
|
|
12
|
+
*
|
|
13
|
+
* Reporting never fails the write that triggered it.
|
|
14
|
+
*/
|
|
15
|
+
export const CHANGES_CHANNEL = 'changes'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Identifies this process on the shared channel, so it can skip its own messages.
|
|
19
|
+
* Made on first use.
|
|
20
|
+
*/
|
|
21
|
+
let _instanceId: string | undefined
|
|
22
|
+
export function instanceId(): string {
|
|
23
|
+
return (_instanceId ??= crypto.randomUUID())
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The address prefix of a language ('/en'), per `defineLocales({ prefix })`. */
|
|
27
|
+
export function localePrefix(reg: CmsRegistry, locale: string | null): string {
|
|
28
|
+
return sitePrefix(reg.locales, locale)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A path template filled in from a row ('/blog/:slug' → '/blog/hello'), or null when a part is empty. */
|
|
32
|
+
export const fillPath = fillPathTemplate
|
|
33
|
+
|
|
34
|
+
/** Placeholder keys of `content.path` (every language's, for a per-language map). */
|
|
35
|
+
export const pathKeys = pathTemplateKeys
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What visitors can see of one entry right now: whether it is live (public
|
|
39
|
+
* collection, and its `content.published` rule holds — the same rule the content
|
|
40
|
+
* API applies, scheduling included) and its addresses in every language.
|
|
41
|
+
*/
|
|
42
|
+
export interface ChangeSnapshot {
|
|
43
|
+
live: boolean
|
|
44
|
+
paths: { locale: string | null; path: string }[]
|
|
45
|
+
/** Page tree: which of the entry and everything below it are live (the entry's own pages' addresses are in `paths`). */
|
|
46
|
+
liveIds?: string[]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* In a page tree an entry's reach is its whole subtree: unpublishing, moving or renaming
|
|
51
|
+
* it changes what visitors see of every page below it, so `paths` lists theirs too.
|
|
52
|
+
*/
|
|
53
|
+
async function treeSnapshot(db: any, e: CmsEntity, id: string): Promise<ChangeSnapshot> {
|
|
54
|
+
const { reg } = useCms()
|
|
55
|
+
const nodes = await loadTreeNodes(db, reg, e)
|
|
56
|
+
const ids = subtreeIds(nodes, id)
|
|
57
|
+
const live = await liveAmong(db, e, ids)
|
|
58
|
+
const index = indexTree(nodes)
|
|
59
|
+
const paths = ids.filter((i) => live.has(i)).flatMap((i) => treeVersions(reg, e, index, i).map((v) => ({ locale: v.locale, path: v.loc })))
|
|
60
|
+
return { live: live.has(id), paths, liveIds: [...live] }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function changeSnapshot(e: CmsEntity, id: string, tx?: any): Promise<ChangeSnapshot> {
|
|
64
|
+
const none: ChangeSnapshot = { live: false, paths: [] }
|
|
65
|
+
if (!e.content?.public) return none
|
|
66
|
+
const { db: appDb, reg } = useCms()
|
|
67
|
+
const db = tx ?? appDb
|
|
68
|
+
if (e.tree) return treeSnapshot(db, e, id)
|
|
69
|
+
const keys = !e.singleton ? pathKeys(e.content.path).filter((k) => e.table[k]) : []
|
|
70
|
+
const cols: Record<string, any> = { id: e.table.id }
|
|
71
|
+
for (const k of keys) cols[k] = e.table[k]
|
|
72
|
+
const [row] = await db.select(cols).from(e.table).where(both(eq(e.table.id, id), publishedCond(e)))
|
|
73
|
+
if (!row) return none
|
|
74
|
+
const out: ChangeSnapshot = { live: true, paths: [] }
|
|
75
|
+
if (!keys.length) return out
|
|
76
|
+
const defaultLocale = reg.locales?.default ?? null
|
|
77
|
+
const base = sitePath(reg.locales, e.content.path, defaultLocale, row)
|
|
78
|
+
if (base) out.paths.push({ locale: defaultLocale, path: base })
|
|
79
|
+
const t = e.localesTable
|
|
80
|
+
const others = reg.locales?.locales.filter((l) => l !== defaultLocale) ?? []
|
|
81
|
+
if (t && others.length) {
|
|
82
|
+
const tcols: Record<string, any> = { locale: t.locale }
|
|
83
|
+
for (const k of keys) if (e.localized.includes(k)) tcols[k] = t[k]
|
|
84
|
+
const rows: any[] = await db.select(tcols).from(t).where(and(eq(t.parentId, id), inArray(t.locale, others)))
|
|
85
|
+
for (const tr of rows) {
|
|
86
|
+
// Untranslated parts of the address fall back to the default language.
|
|
87
|
+
const merged = { ...row }
|
|
88
|
+
for (const k of keys) if (tr[k] !== null && tr[k] !== undefined && tr[k] !== '') merged[k] = tr[k]
|
|
89
|
+
const p = sitePath(reg.locales, e.content.path, tr.locale, merged)
|
|
90
|
+
if (p) out.paths.push({ locale: tr.locale, path: p })
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return out
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Did what visitors could reach change: live or not, or the address in some language? */
|
|
97
|
+
export function addressesDiffer(before: ChangeSnapshot, after: ChangeSnapshot): boolean {
|
|
98
|
+
const key = (s: ChangeSnapshot) => JSON.stringify(s.live ? [...s.paths].map((p) => `${p.locale ?? ''}=${p.path}`).sort() : null)
|
|
99
|
+
return key(before) !== key(after)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface ReportChange {
|
|
103
|
+
type: CmsChangeType
|
|
104
|
+
entity: CmsEntity
|
|
105
|
+
id: string
|
|
106
|
+
/** What visitors saw before the write (changeSnapshot); omit for a new entry. */
|
|
107
|
+
before?: ChangeSnapshot | null
|
|
108
|
+
/** The row after the write, for its version. */
|
|
109
|
+
after?: Record<string, any> | null
|
|
110
|
+
/** The language a translation change is about. */
|
|
111
|
+
locale?: string | null
|
|
112
|
+
/** Page tree: addresses that moved with this write (see `CmsChange.moved`). */
|
|
113
|
+
moved?: { id: string; locale: string | null; from: string; to: string }[]
|
|
114
|
+
/** Nothing was saved (a scheduled entry went live): no revision. */
|
|
115
|
+
noRevision?: boolean
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function reportChange(event: any, r: ReportChange): Promise<void> {
|
|
119
|
+
try {
|
|
120
|
+
const e = r.entity
|
|
121
|
+
const locale = r.locale ?? null
|
|
122
|
+
const deleted = r.type === 'entry.deleted'
|
|
123
|
+
const before = r.before ?? { live: false, paths: [] }
|
|
124
|
+
const after = deleted ? { live: false, paths: [] } : await changeSnapshot(e, r.id)
|
|
125
|
+
// Only addresses a visitor could have reached, in the language the change is about.
|
|
126
|
+
const paths = new Set<string>()
|
|
127
|
+
for (const snap of [before, after]) {
|
|
128
|
+
if (!snap.live) continue
|
|
129
|
+
for (const p of snap.paths) if (!locale || p.locale === locale) paths.add(p.path)
|
|
130
|
+
}
|
|
131
|
+
// Links follow their target: when this entry's address (or whether it can be reached at
|
|
132
|
+
// all) changed, everything that links to it reads differently now. Those pages go in
|
|
133
|
+
// `paths`, and the public globals (menus, settings) that link to it are named in `globals`:
|
|
134
|
+
// whatever shows them — usually every page — is stale too.
|
|
135
|
+
const globals = new Set<string>()
|
|
136
|
+
const { db, reg } = useCms()
|
|
137
|
+
const addressChanged = !deleted && !!e.content?.path && !e.singleton && addressesDiffer(before, after)
|
|
138
|
+
// An entry that went live at an address a redirect leaves from would be hidden by it: the redirect goes.
|
|
139
|
+
// (Writes that can do it in their own transaction have done so already; this catches every other path.)
|
|
140
|
+
if (addressChanged && after.live) {
|
|
141
|
+
await dropRedirectsFrom(db, reg, after.paths.map((p) => p.path)).catch((err) => console.error('[cms] could not remove redirects from the new addresses', scrubError(err)))
|
|
142
|
+
}
|
|
143
|
+
if (addressChanged && hasLinkFields(reg)) {
|
|
144
|
+
// In a page tree the whole subtree changed address (or went away) with the entry: what links to any of those pages reads differently too.
|
|
145
|
+
const ids = new Set([r.id, ...(r.moved ?? []).map((m) => m.id), ...(before.liveIds ?? []), ...(after.liveIds ?? [])])
|
|
146
|
+
for (const dep of await linksTo(db, reg, e, [...ids], 50).catch(() => [])) {
|
|
147
|
+
const target = reg.byName.get(dep.entity)
|
|
148
|
+
if (!target?.content?.public || (dep.locale && locale && dep.locale !== locale)) continue
|
|
149
|
+
if (target.singleton) globals.add(target.name)
|
|
150
|
+
else for (const p of (await changeSnapshot(target, dep.id)).paths) if (!locale || p.locale === locale) paths.add(p.path)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const change: CmsChange = {
|
|
154
|
+
type: r.type,
|
|
155
|
+
entity: e.name,
|
|
156
|
+
id: r.id,
|
|
157
|
+
locale,
|
|
158
|
+
public: before.live || after.live,
|
|
159
|
+
paths: [...paths],
|
|
160
|
+
...(globals.size ? { globals: [...globals] } : {}),
|
|
161
|
+
...(r.moved?.length ? { moved: r.moved } : {}),
|
|
162
|
+
by: event?.context?.cmsUser?.id ?? null,
|
|
163
|
+
version: r.after?.updatedAt ? new Date(r.after.updatedAt).toISOString() : null,
|
|
164
|
+
at: new Date().toISOString(),
|
|
165
|
+
}
|
|
166
|
+
// What was saved, for the history (not for deletions).
|
|
167
|
+
if (!r.noRevision && r.type !== 'entry.deleted' && r.type !== 'translation.deleted') {
|
|
168
|
+
await recordRevision(event, e, r.id, r.type === 'translation.updated' ? locale : null, r.type)
|
|
169
|
+
}
|
|
170
|
+
// The search index follows every write (its own failure never stops the rest).
|
|
171
|
+
if (r.type !== 'global.updated') {
|
|
172
|
+
await syncSearchIndex(e, r.id, r.type).catch((err) => console.error('[cms] could not update the search index', scrubError(err)))
|
|
173
|
+
}
|
|
174
|
+
applyChangeLocally(change)
|
|
175
|
+
notifyChangeListeners(change)
|
|
176
|
+
// Neither the other replicas nor the host's hooks hold up the response.
|
|
177
|
+
const background: Promise<unknown>[] = [
|
|
178
|
+
useCmsStore().then((store) => store.publish(CHANGES_CHANNEL, JSON.stringify({ origin: instanceId(), change })))
|
|
179
|
+
.catch((err) => console.error('[cms] could not publish change', scrubError(err))),
|
|
180
|
+
Promise.resolve(useNitroApp().hooks.callHook('cms:change', change))
|
|
181
|
+
.catch((err: unknown) => console.error('[cms] cms:change hook failed', scrubError(err))),
|
|
182
|
+
]
|
|
183
|
+
if (change.public) background.push(sendWebhooks(change))
|
|
184
|
+
const all = Promise.all(background)
|
|
185
|
+
if (typeof event?.waitUntil === 'function') event.waitUntil(all)
|
|
186
|
+
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
console.error('[cms] reporting a change failed', scrubError(err))
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Clear what this instance derived from content the change touched. */
|
|
193
|
+
export function applyChangeLocally(change: Pick<CmsChange, 'entity'>): void {
|
|
194
|
+
const { reg } = useCms()
|
|
195
|
+
const e = reg.byName.get(change.entity)
|
|
196
|
+
if (!e) return
|
|
197
|
+
if ((e as any).preset === 'redirects') invalidateRedirects()
|
|
198
|
+
// A page's address moved or it went away: redirects to it (stored as a reference) point somewhere else now.
|
|
199
|
+
if (e.content?.path) {
|
|
200
|
+
invalidateSitemap()
|
|
201
|
+
invalidateRedirects()
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const listeners = new Set<(change: CmsChange) => void>()
|
|
206
|
+
|
|
207
|
+
/** Run `fn` for every change — saved here or on another replica. Returns an unsubscribe function. */
|
|
208
|
+
export function onCmsChange(fn: (change: CmsChange) => void): () => void {
|
|
209
|
+
listeners.add(fn)
|
|
210
|
+
return () => listeners.delete(fn)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function notifyChangeListeners(change: CmsChange): void {
|
|
214
|
+
for (const fn of listeners) {
|
|
215
|
+
try {
|
|
216
|
+
fn(change)
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
console.error('[cms] change listener failed', scrubError(err))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|