tinyzoo 0.0.0-stage → 0.2.0
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 +99 -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 +392 -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/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 +197 -0
- package/server/utils/cmsWebhooks.ts +101 -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,51 @@
|
|
|
1
|
+
import sharp from 'sharp'
|
|
2
|
+
|
|
3
|
+
// Smaller copies of uploaded pictures, made when a page first asks for them and kept
|
|
4
|
+
// in storage next to the original: a phone photo shown 300 px wide should not cost
|
|
5
|
+
// the visitor 4 MB. Only these widths exist (nobody can make the server produce
|
|
6
|
+
// thousands of sizes); <CmsImage> lists them in srcset and the browser picks one.
|
|
7
|
+
export const CMS_IMAGE_WIDTHS = [320, 640, 960, 1280, 1920] as const
|
|
8
|
+
const RESIZABLE = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/avif'])
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The most pixels a picture may have (width × height): sharp refuses more, and decoding
|
|
12
|
+
* a bigger one would also eat the server's memory. Checked at upload, so a picture that
|
|
13
|
+
* could never be shown at a smaller size is not accepted in the first place.
|
|
14
|
+
*/
|
|
15
|
+
export const MAX_IMAGE_PIXELS = 80_000_000
|
|
16
|
+
|
|
17
|
+
export const isResizableImage = (mime: string | null | undefined) => !!mime && RESIZABLE.has(mime)
|
|
18
|
+
const variantKey = (key: string, width: number) => `_w/${width}/${key}.webp`
|
|
19
|
+
|
|
20
|
+
// One resize per picture and width at a time, even when many visitors ask at once.
|
|
21
|
+
const inflight = new Map<string, Promise<Buffer | null>>()
|
|
22
|
+
|
|
23
|
+
/** The picture `key` at `width` px as WebP: from storage, or made now and stored. */
|
|
24
|
+
export async function imageVariant(key: string, width: number): Promise<Buffer | null> {
|
|
25
|
+
const storage = useCmsStorage()
|
|
26
|
+
const vk = variantKey(key, width)
|
|
27
|
+
const stored = await storage.get(vk)
|
|
28
|
+
if (stored) return stored.body
|
|
29
|
+
let job = inflight.get(vk)
|
|
30
|
+
if (!job) {
|
|
31
|
+
job = (async () => {
|
|
32
|
+
const original = await storage.get(key)
|
|
33
|
+
if (!original) return null
|
|
34
|
+
const out = await sharp(original.body, { failOn: 'none', limitInputPixels: MAX_IMAGE_PIXELS })
|
|
35
|
+
.rotate() // honour the camera's orientation
|
|
36
|
+
.resize({ width, withoutEnlargement: true })
|
|
37
|
+
.webp({ quality: 78 })
|
|
38
|
+
.toBuffer()
|
|
39
|
+
await storage.put(vk, out, 'image/webp')
|
|
40
|
+
return out
|
|
41
|
+
})().finally(() => inflight.delete(vk))
|
|
42
|
+
inflight.set(vk, job)
|
|
43
|
+
}
|
|
44
|
+
return job
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Remove the smaller copies of a picture (with the original). Missing ones are fine. */
|
|
48
|
+
export async function deleteImageVariants(key: string): Promise<void> {
|
|
49
|
+
const storage = useCmsStorage()
|
|
50
|
+
await Promise.all(CMS_IMAGE_WIDTHS.map((w) => storage.delete(variantKey(key, w)).catch(() => {})))
|
|
51
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { and, eq, indexTree, inArray, pathTemplateKeys, sitePath } from 'tinyzoo'
|
|
2
|
+
import type { CmsEntity, CmsRegistry } from 'tinyzoo'
|
|
3
|
+
import { loadTreeNodes, treePathIn } from './cmsTreeAddress'
|
|
4
|
+
|
|
5
|
+
// Explicit imports only (no Nitro auto-imports): `tinyzoo links:convert` uses this too.
|
|
6
|
+
|
|
7
|
+
/** Can a link point at an entry of this collection? It is public and has an address on the website. */
|
|
8
|
+
export const isLinkable = (e: CmsEntity | undefined): e is CmsEntity => !!e && !e.singleton && !!e.content?.public && !!e.content?.path
|
|
9
|
+
|
|
10
|
+
/** The columns an entry's address is built from (`/blog/:slug` → slug). */
|
|
11
|
+
export const addressKeys = (e: CmsEntity): string[] => pathTemplateKeys(e.content?.path).filter((k) => (e.table as any)[k])
|
|
12
|
+
|
|
13
|
+
const filled = (v: unknown) => v !== null && v !== undefined && v !== ''
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The website address of entries of `e` in `locale`: what the picker offers and what a
|
|
17
|
+
* stored link resolves to. `rows` hold `id` and the address columns. As everywhere on the
|
|
18
|
+
* website, a translated part (a slug) comes from the translation and an untranslated
|
|
19
|
+
* one falls back to the default language; a language the collection has no address for
|
|
20
|
+
* (`content.path` per language) links to the default language's page.
|
|
21
|
+
*/
|
|
22
|
+
export async function entryAddresses(db: any, reg: CmsRegistry, e: CmsEntity, rows: Record<string, any>[], locale: string | null): Promise<Map<string, string | null>> {
|
|
23
|
+
const out = new Map<string, string | null>()
|
|
24
|
+
if (!rows.length || !e.content?.path) return out
|
|
25
|
+
// A page tree: the address is the ancestors' parts plus its own (/o-nas/tym), per language.
|
|
26
|
+
if (e.tree) {
|
|
27
|
+
const index = indexTree(await loadTreeNodes(db, reg, e))
|
|
28
|
+
for (const row of rows) out.set(row.id, index.has(row.id) ? treePathIn(reg, e, index, row.id, locale) : null)
|
|
29
|
+
return out
|
|
30
|
+
}
|
|
31
|
+
const keys = addressKeys(e)
|
|
32
|
+
const def = reg.locales?.default ?? null
|
|
33
|
+
const translated = new Map<string, Record<string, any>>()
|
|
34
|
+
const t = e.localesTable
|
|
35
|
+
if (locale && def && locale !== def && t && keys.some((k) => e.localized.includes(k))) {
|
|
36
|
+
const cols: Record<string, any> = { parentId: t.parentId }
|
|
37
|
+
for (const k of keys) if (e.localized.includes(k) && t[k]) cols[k] = t[k]
|
|
38
|
+
const trs: any[] = await db.select(cols).from(t).where(and(inArray(t.parentId, rows.map((r) => r.id)), eq(t.locale, locale)))
|
|
39
|
+
for (const r of trs) translated.set(r.parentId, r)
|
|
40
|
+
}
|
|
41
|
+
for (const row of rows) {
|
|
42
|
+
const merged = { ...row }
|
|
43
|
+
const tr = translated.get(row.id)
|
|
44
|
+
if (tr) for (const k of keys) if (e.localized.includes(k) && filled(tr[k])) merged[k] = tr[k]
|
|
45
|
+
let path = sitePath(reg.locales, e.content.path, locale, merged)
|
|
46
|
+
if (path === null && def && locale !== def) path = sitePath(reg.locales, e.content.path, def, row)
|
|
47
|
+
out.set(row.id, path)
|
|
48
|
+
}
|
|
49
|
+
return out
|
|
50
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { and, chainOf, eq, indexTree, inArray, linkFieldsOf, looksLikeLinkRef, linkRef, linkRefsOf, mapRowLinks, ne, or, parseLinkRef, sql } from 'tinyzoo'
|
|
2
|
+
import type { CmsEntity, CmsRegistry } from 'tinyzoo'
|
|
3
|
+
import { addressKeys, entryAddresses, isLinkable } from './cmsLinkAddress'
|
|
4
|
+
import { loadTreeNodes } from './cmsTreeAddress'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Links that follow their target (see src/links.ts for the format).
|
|
8
|
+
*
|
|
9
|
+
* - `resolveContentLinks`: what visitors get. Every `cms:<entity>/<id>` in the rows —
|
|
10
|
+
* link fields, rich text, inside lists, blocks and groups — becomes the target's
|
|
11
|
+
* current address in the language asked for, or null (a link field) / an unlinked
|
|
12
|
+
* text (rich text) when the target is gone, not live or not public. Nothing
|
|
13
|
+
* reaches a visitor with a `cms:` in it.
|
|
14
|
+
* - `describeLinkRefs`: what the admin shows beside a link: the target's title and
|
|
15
|
+
* address, and whether it is live.
|
|
16
|
+
* - `linksTo`: what links to an entry (a delete is refused while something does).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export interface LinkScope {
|
|
20
|
+
/** The language the content is served in (null: single-language content). */
|
|
21
|
+
locale: string | null
|
|
22
|
+
/** An editor's preview (admin session or content token + `?preview`): drafts count as targets. */
|
|
23
|
+
preview?: boolean
|
|
24
|
+
/** The signed-in previewer: a draft only resolves when this person may read it (a content token has no user and sees every draft). */
|
|
25
|
+
user?: any
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Does any collection or global hold a field a link can be stored in? */
|
|
29
|
+
export function hasLinkFields(reg: CmsRegistry): boolean {
|
|
30
|
+
return reg.entities.some((e) => linkFieldsOf(e.fields as any).length > 0)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The address each reference resolves to for visitors, or null: not a reference to a
|
|
35
|
+
* collection that has addresses, a missing entry, or one that is not live (draft,
|
|
36
|
+
* unpublished, scheduled for later) — unless it is an editor's preview.
|
|
37
|
+
*/
|
|
38
|
+
export async function resolveLinkRefs(db: any, reg: CmsRegistry, refs: Iterable<string>, scope: LinkScope): Promise<Map<string, string | null>> {
|
|
39
|
+
const out = new Map<string, string | null>()
|
|
40
|
+
const wanted = new Map<string, Map<string, string[]>>()
|
|
41
|
+
for (const ref of refs) {
|
|
42
|
+
const parsed = parseLinkRef(ref)
|
|
43
|
+
const e = parsed ? reg.byName.get(parsed.entity) : undefined
|
|
44
|
+
if (!parsed || !isLinkable(e)) {
|
|
45
|
+
out.set(ref, null)
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
const ids = wanted.get(e.name) ?? new Map<string, string[]>()
|
|
49
|
+
ids.set(parsed.id, [...(ids.get(parsed.id) ?? []), ref])
|
|
50
|
+
wanted.set(e.name, ids)
|
|
51
|
+
}
|
|
52
|
+
for (const [name, ids] of wanted) {
|
|
53
|
+
const e = reg.byName.get(name)!
|
|
54
|
+
const cols: Record<string, any> = { id: e.table.id }
|
|
55
|
+
for (const k of addressKeys(e)) cols[k] = (e.table as any)[k]
|
|
56
|
+
// Visitors reach what is live. A signed-in previewer also reaches the drafts they may read
|
|
57
|
+
// (never one a row-level `access.read` rule keeps from them); a content token (no user) reaches every draft.
|
|
58
|
+
const published = publishedCond(e)
|
|
59
|
+
let live = published
|
|
60
|
+
if (scope.preview) {
|
|
61
|
+
live = undefined
|
|
62
|
+
if (scope.user && published) {
|
|
63
|
+
const read = accessWhere(e, 'read', scope.user)
|
|
64
|
+
live = !read.allowed ? published : read.where ? or(published, read.where) : undefined
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const idCond = inArray(e.table.id, [...ids.keys()])
|
|
68
|
+
const rows: any[] = await db.select(cols).from(e.table).where(live ? and(live, idCond) : idCond)
|
|
69
|
+
const addresses = await entryAddresses(db, reg, e, rows, scope.locale)
|
|
70
|
+
// A page under an unpublished page is not reachable on the website (page tree rule),
|
|
71
|
+
// so a visitor's link to it goes nowhere, like a link to a draft.
|
|
72
|
+
if (e.tree && !scope.preview && published) {
|
|
73
|
+
const index = indexTree(await loadTreeNodes(db, reg, e))
|
|
74
|
+
const ancestors = new Set<string>()
|
|
75
|
+
for (const id of addresses.keys()) for (const n of chainOf(index, id)) if (n.id !== id) ancestors.add(n.id)
|
|
76
|
+
const liveAncestors = ancestors.size
|
|
77
|
+
? new Set<string>((await db.select({ id: e.table.id }).from(e.table).where(and(published, inArray(e.table.id, [...ancestors]))) as any[]).map((r) => r.id))
|
|
78
|
+
: new Set<string>()
|
|
79
|
+
for (const id of addresses.keys()) {
|
|
80
|
+
if (chainOf(index, id).some((n) => n.id !== id && !liveAncestors.has(n.id))) addresses.set(id, null)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const [id, list] of ids) for (const ref of list) out.set(ref, addresses.get(id) ?? null)
|
|
84
|
+
}
|
|
85
|
+
return out
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Replace the references in `rows` (rows of `e`, as served: translations already laid
|
|
90
|
+
* over) with addresses. Nothing the rows hold is modified in place except the fields
|
|
91
|
+
* that change, which are assigned new values.
|
|
92
|
+
*/
|
|
93
|
+
export async function resolveContentLinks(db: any, reg: CmsRegistry, e: CmsEntity, rows: Record<string, any>[], scope: LinkScope): Promise<void> {
|
|
94
|
+
if (!rows.length || !linkFieldsOf(e.fields as any).length) return
|
|
95
|
+
const refs = new Set<string>()
|
|
96
|
+
for (const r of rows) linkRefsOf(e.fields as any, r, refs)
|
|
97
|
+
if (!refs.size) return
|
|
98
|
+
const resolved = await resolveLinkRefs(db, reg, refs, scope)
|
|
99
|
+
const follow = (v: string) => (looksLikeLinkRef(v) ? (resolved.get(v) ?? null) : undefined)
|
|
100
|
+
for (const r of rows) Object.assign(r, mapRowLinks(e.fields as any, r, { field: follow, mark: follow }))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type LinkTargetState = 'live' | 'draft' | 'missing'
|
|
104
|
+
export interface LinkTargetInfo {
|
|
105
|
+
entity: string
|
|
106
|
+
id: string
|
|
107
|
+
/** The entry's title in the language asked for. */
|
|
108
|
+
title: string
|
|
109
|
+
/** Its address on the website in that language (null: it has none). */
|
|
110
|
+
path: string | null
|
|
111
|
+
state: LinkTargetState
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* What the admin shows for a stored link reference: the target's title and address, and
|
|
116
|
+
* whether visitors can follow it (`live`), it waits for the entry to be published
|
|
117
|
+
* (`draft`) or the entry is gone (`missing`). An entry this person may not read is
|
|
118
|
+
* `missing` too: telling "not yours" from "not there" would say which ids exist.
|
|
119
|
+
*/
|
|
120
|
+
export async function describeLinkRefs(db: any, reg: CmsRegistry, user: any, refs: string[], locale: string | null): Promise<Record<string, LinkTargetInfo>> {
|
|
121
|
+
const out: Record<string, LinkTargetInfo> = {}
|
|
122
|
+
const gone = (ref: string, entity = '', id = ''): LinkTargetInfo => ({ entity, id, title: '', path: null, state: 'missing' })
|
|
123
|
+
const wanted = new Map<string, Map<string, string[]>>()
|
|
124
|
+
for (const ref of refs) {
|
|
125
|
+
const parsed = parseLinkRef(ref)
|
|
126
|
+
const e = parsed ? reg.byName.get(parsed.entity) : undefined
|
|
127
|
+
if (!parsed || !isLinkable(e)) {
|
|
128
|
+
out[ref] = gone(ref, parsed?.entity, parsed?.id)
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
const ids = wanted.get(e.name) ?? new Map<string, string[]>()
|
|
132
|
+
ids.set(parsed.id, [...(ids.get(parsed.id) ?? []), ref])
|
|
133
|
+
wanted.set(e.name, ids)
|
|
134
|
+
}
|
|
135
|
+
for (const [name, ids] of wanted) {
|
|
136
|
+
const e = reg.byName.get(name)!
|
|
137
|
+
const { allowed, where } = accessWhere(e, 'read', user)
|
|
138
|
+
if (!allowed) {
|
|
139
|
+
for (const [id, list] of ids) for (const ref of list) out[ref] = gone(ref, name, id)
|
|
140
|
+
continue
|
|
141
|
+
}
|
|
142
|
+
const liveCond = publishedCond(e)
|
|
143
|
+
const titleCol = (e.table as any)[e.titleField]
|
|
144
|
+
const cols: Record<string, any> = { id: e.table.id, title: titleCol, live: liveCond ? sql<boolean>`${liveCond}` : sql<boolean>`true` }
|
|
145
|
+
for (const k of addressKeys(e)) cols[k] = (e.table as any)[k]
|
|
146
|
+
const idCond = inArray(e.table.id, [...ids.keys()])
|
|
147
|
+
const rows: any[] = await db.select(cols).from(e.table).where(where ? and(idCond, where) : idCond)
|
|
148
|
+
const addresses = await entryAddresses(db, reg, e, rows, locale)
|
|
149
|
+
// The title in the language being edited, when it is translated.
|
|
150
|
+
const titles = new Map<string, string>(rows.map((r) => [r.id, r.title == null ? '' : String(r.title)]))
|
|
151
|
+
const t = e.localesTable
|
|
152
|
+
if (locale && reg.locales && locale !== reg.locales.default && t && e.localized.includes(e.titleField) && t[e.titleField] && rows.length) {
|
|
153
|
+
const trs: any[] = await db.select({ parentId: t.parentId, title: t[e.titleField] }).from(t).where(and(inArray(t.parentId, rows.map((r) => r.id)), eq(t.locale, locale)))
|
|
154
|
+
for (const r of trs) if (r.title) titles.set(r.parentId, String(r.title))
|
|
155
|
+
}
|
|
156
|
+
const found = new Map(rows.map((r) => [r.id, r]))
|
|
157
|
+
for (const [id, list] of ids) {
|
|
158
|
+
for (const ref of list) {
|
|
159
|
+
const row = found.get(id)
|
|
160
|
+
if (!row) out[ref] = gone(ref, name, id)
|
|
161
|
+
else out[ref] = { entity: name, id, title: titles.get(id) ?? '', path: addresses.get(id) ?? null, state: row.live ? 'live' : 'draft' }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return out
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface LinkedFrom {
|
|
169
|
+
entity: string
|
|
170
|
+
id: string
|
|
171
|
+
/** The entry's title; empty for a global (see `label`). */
|
|
172
|
+
title: string
|
|
173
|
+
/** A global (a menu, the site settings): named by its collection label, not a title. */
|
|
174
|
+
singleton: boolean
|
|
175
|
+
label: unknown
|
|
176
|
+
/** The language of the translation that holds the link; null for the entry itself. */
|
|
177
|
+
locale: string | null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Everything that links to an entry by reference: entries and globals whose link
|
|
182
|
+
* fields or rich text hold `cms:<entity>/<id>`, translations included. Found by text
|
|
183
|
+
* search on the stored JSON, so a link inside a block the config no longer knows
|
|
184
|
+
* still counts. For one entry, its link to itself does not count. At most `perEntity` of each.
|
|
185
|
+
*
|
|
186
|
+
* Given several ids (a page and the pages below it), finds what links to any of them, in
|
|
187
|
+
* one query per collection (and one for its translations); nothing is left out as "itself".
|
|
188
|
+
*/
|
|
189
|
+
export async function linksTo(db: any, reg: CmsRegistry, target: CmsEntity, idOrIds: string | string[], perEntity = 5): Promise<LinkedFrom[]> {
|
|
190
|
+
const targets = Array.isArray(idOrIds) ? idOrIds : [idOrIds]
|
|
191
|
+
const id = targets.length === 1 ? targets[0]! : null
|
|
192
|
+
if (!targets.length) return []
|
|
193
|
+
const refs = targets.map((t) => linkRef(target.name, t).toLowerCase())
|
|
194
|
+
const has = (col: any) => or(...refs.map((ref) => sql`strpos(lower(cast(${col} as text)), ${ref}) > 0`))
|
|
195
|
+
const out: LinkedFrom[] = []
|
|
196
|
+
for (const e of reg.entities) {
|
|
197
|
+
const keys = linkFieldsOf(e.fields as any).map(([k]) => k)
|
|
198
|
+
if (!keys.length) continue
|
|
199
|
+
const own = (e.table as any)
|
|
200
|
+
const titleCol = own[e.titleField] ?? own.id
|
|
201
|
+
const notSelf = id !== null && e.name === target.name ? ne(own.id, id) : undefined
|
|
202
|
+
const direct = or(...keys.map((k) => has(own[k])))
|
|
203
|
+
const found = new Map<string, LinkedFrom>()
|
|
204
|
+
const add = (r: { id: string; title: unknown }, locale: string | null) => {
|
|
205
|
+
if (found.size >= perEntity) return
|
|
206
|
+
found.set(`${r.id}:${locale ?? ''}`, { entity: e.name, id: r.id, title: e.singleton || r.title == null ? '' : String(r.title), singleton: !!e.singleton, label: e.label, locale })
|
|
207
|
+
}
|
|
208
|
+
const rows: any[] = await db.select({ id: own.id, title: titleCol }).from(own).where(notSelf ? and(direct, notSelf) : direct).limit(perEntity)
|
|
209
|
+
for (const r of rows) add(r, null)
|
|
210
|
+
const t = e.localesTable
|
|
211
|
+
const tkeys = keys.filter((k) => e.localized.includes(k) && t?.[k])
|
|
212
|
+
if (t && tkeys.length && found.size < perEntity) {
|
|
213
|
+
const hits: any[] = await db.select({ parentId: t.parentId, locale: t.locale }).from(t).where(or(...tkeys.map((k) => has(t[k])))).limit(perEntity)
|
|
214
|
+
for (const h of hits) {
|
|
215
|
+
if (id !== null && e.name === target.name && h.parentId === id) continue
|
|
216
|
+
const [parent] = await db.select({ id: own.id, title: titleCol }).from(own).where(eq(own.id, h.parentId))
|
|
217
|
+
if (parent) add(parent, h.locale)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
out.push(...found.values())
|
|
221
|
+
}
|
|
222
|
+
return out
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Split entries (`{ entity, id }`) into those this person may read and a count of the
|
|
227
|
+
* rest, so a refusal can name what is in the way without telling anyone the titles of
|
|
228
|
+
* entries a row-level `access.read` rule keeps from them. Globals follow their collection's rule.
|
|
229
|
+
*/
|
|
230
|
+
export async function splitReadable<T extends { entity: string; id: string }>(db: any, reg: CmsRegistry, user: any, items: T[]): Promise<{ visible: T[]; hidden: number }> {
|
|
231
|
+
const byEntity = new Map<string, T[]>()
|
|
232
|
+
for (const it of items) byEntity.set(it.entity, [...(byEntity.get(it.entity) ?? []), it])
|
|
233
|
+
const readable = new Set<string>()
|
|
234
|
+
for (const [name, list] of byEntity) {
|
|
235
|
+
const e = reg.byName.get(name)
|
|
236
|
+
if (!e) continue
|
|
237
|
+
const { allowed, where } = accessWhere(e, 'read', user)
|
|
238
|
+
if (!allowed) continue
|
|
239
|
+
if (!where) {
|
|
240
|
+
for (const it of list) readable.add(`${name}\u0000${it.id}`)
|
|
241
|
+
continue
|
|
242
|
+
}
|
|
243
|
+
const rows: any[] = await db.select({ id: e.table.id }).from(e.table).where(and(inArray(e.table.id, [...new Set(list.map((it) => it.id))]), where))
|
|
244
|
+
for (const r of rows) readable.add(`${name}\u0000${r.id}`)
|
|
245
|
+
}
|
|
246
|
+
const visible = items.filter((it) => readable.has(`${it.entity}\u0000${it.id}`))
|
|
247
|
+
return { visible, hidden: items.length - visible.length }
|
|
248
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { and, eq, gte, inArray, isNotNull, isNull, lte, ne, not, or, propOf, sql } from 'tinyzoo'
|
|
2
|
+
import type { CmsEntity, CmsRegistry } from 'tinyzoo'
|
|
3
|
+
|
|
4
|
+
/** Trigram word similarity a longer search word needs to count as a typo of a word in the text. */
|
|
5
|
+
const TYPO_SIMILARITY = 0.35
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What an admin list shows, as SQL conditions: the user's row rule, the search box
|
|
9
|
+
* (accents ignored), `filter.<field>` filters and the status tab. Shared by the list
|
|
10
|
+
* and its CSV export, so the file holds exactly what the editor sees.
|
|
11
|
+
*/
|
|
12
|
+
export function listConditions(e: CmsEntity, reg: CmsRegistry, query: Record<string, any>, rowFilter: any): any[] {
|
|
13
|
+
const conds: any[] = []
|
|
14
|
+
if (rowFilter) conds.push(rowFilter)
|
|
15
|
+
const search = String(query.q ?? '').trim()
|
|
16
|
+
if (search) {
|
|
17
|
+
const fields = e.admin?.list?.searchable?.length ? e.admin.list.searchable : [e.titleField]
|
|
18
|
+
// Every word must be found (in any order, in any of the fields). Accents and case do
|
|
19
|
+
// not matter ("zimni" finds "Zimní", "pribram" finds "Příbram"), and a longer word
|
|
20
|
+
// may have a typo ("clenek" finds "článek"): trigram word similarity, as the admin's search.
|
|
21
|
+
const words = foldAccents(search).split(/\s+/).filter(Boolean).slice(0, 8)
|
|
22
|
+
for (const word of words) {
|
|
23
|
+
const like = '%' + word.replace(/[\\%_]/g, (c) => '\\' + c) + '%' // LIKE wildcards are literal
|
|
24
|
+
const letters = word.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu, '')
|
|
25
|
+
conds.push(or(...fields.flatMap((fn) => {
|
|
26
|
+
const col = foldedColumn(e.table[fn])
|
|
27
|
+
return letters.length >= 4
|
|
28
|
+
? [sql`${col} like ${like}`, sql`word_similarity(${letters}, ${col}) >= ${TYPO_SIMILARITY}`]
|
|
29
|
+
: [sql`${col} like ${like}`]
|
|
30
|
+
})))
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
conds.push(...listFilters(e, reg, query))
|
|
34
|
+
// Status tabs: ?status=published | scheduled | draft (entities with a publish workflow).
|
|
35
|
+
const p = e.publish
|
|
36
|
+
if (p && ['published', 'scheduled', 'draft'].includes(String(query.status))) {
|
|
37
|
+
const col = e.table[propOf(p.field, e.fields[p.field]!)]
|
|
38
|
+
const live = scheduleCond(e)
|
|
39
|
+
if (query.status === 'draft') conds.push(or(isNull(col), ne(col, p.publishedValue)))
|
|
40
|
+
else if (query.status === 'published') conds.push(both(eq(col, p.publishedValue), live))
|
|
41
|
+
else conds.push(live ? and(eq(col, p.publishedValue), not(live)) : sql`false`)
|
|
42
|
+
}
|
|
43
|
+
return conds
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The list's filters: `filter.<field>=<value>` by field type — a choice (several
|
|
50
|
+
* comma-separated), yes/no, a related entry (to-one or many), has an image or not,
|
|
51
|
+
* and a date range (`filter.<field>.from` / `.to`, ISO dates). Unknown or invalid
|
|
52
|
+
* filters are ignored; values are bound parameters.
|
|
53
|
+
*/
|
|
54
|
+
export function listFilters(e: any, reg: any, query: Record<string, any>): any[] {
|
|
55
|
+
const out: any[] = []
|
|
56
|
+
for (const [param, raw] of Object.entries(query)) {
|
|
57
|
+
if (!param.startsWith('filter.')) continue
|
|
58
|
+
const [key = '', part] = param.slice('filter.'.length).split('.')
|
|
59
|
+
const fld = key && Object.hasOwn(e.fields, key) ? e.fields[key] : null
|
|
60
|
+
if (!fld) continue
|
|
61
|
+
const value = String(Array.isArray(raw) ? raw[0] : raw ?? '').slice(0, 500)
|
|
62
|
+
if (!value) continue
|
|
63
|
+
if (fld.type === 'select') {
|
|
64
|
+
const allowed = new Set<string>(fld.meta?.options ?? fld.options ?? [])
|
|
65
|
+
const picked = value.split(',').filter((v) => !allowed.size || allowed.has(v))
|
|
66
|
+
if (picked.length) out.push(inArray(e.table[propOf(key, fld)], picked))
|
|
67
|
+
}
|
|
68
|
+
else if (fld.type === 'boolean') {
|
|
69
|
+
if (value === 'true' || value === 'false') out.push(eq(e.table[propOf(key, fld)], value === 'true'))
|
|
70
|
+
}
|
|
71
|
+
else if (fld.type === 'media') {
|
|
72
|
+
const col = e.table[`${key}Id`]
|
|
73
|
+
if (value === 'yes') out.push(isNotNull(col))
|
|
74
|
+
else if (value === 'no') out.push(isNull(col))
|
|
75
|
+
}
|
|
76
|
+
else if (fld.relation?.kind === 'many-to-one') {
|
|
77
|
+
if (UUID.test(value)) out.push(eq(e.table[`${key}Id`], value))
|
|
78
|
+
}
|
|
79
|
+
else if (fld.relation?.kind === 'many-to-many') {
|
|
80
|
+
const j = reg.junctionByOwnerField.get(`${e.name}.${key}`)
|
|
81
|
+
if (j && UUID.test(value)) {
|
|
82
|
+
out.push(sql`exists (select 1 from ${j.table} where ${j.table[j.ownerCol]} = ${e.table.id} and ${j.table[j.targetCol]} = ${value})`)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else if (fld.type === 'datetime' && (part === 'from' || part === 'to')) {
|
|
86
|
+
const d = new Date(value)
|
|
87
|
+
if (Number.isNaN(d.getTime())) continue
|
|
88
|
+
const col = e.table[propOf(key, fld)]
|
|
89
|
+
out.push(part === 'from' ? gte(col, d) : lte(col, d))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out
|
|
93
|
+
}
|