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.
Files changed (392) hide show
  1. package/LICENSE +10 -0
  2. package/README.md +46 -2
  3. package/app/app.config.ts +35 -0
  4. package/app/assets/brand/characters/bright/bird.svg +1 -0
  5. package/app/assets/brand/characters/bright/bunny.svg +1 -0
  6. package/app/assets/brand/characters/bright/elephant.svg +1 -0
  7. package/app/assets/brand/characters/bright/fox.svg +1 -0
  8. package/app/assets/brand/characters/bright/frog.svg +1 -0
  9. package/app/assets/brand/characters/bright/octopus.svg +1 -0
  10. package/app/assets/brand/characters/muted/bee.svg +1 -0
  11. package/app/assets/brand/characters/muted/bird.svg +1 -0
  12. package/app/assets/brand/characters/muted/butterfly.svg +1 -0
  13. package/app/assets/brand/characters/muted/chick.svg +1 -0
  14. package/app/assets/brand/characters/muted/cow.svg +1 -0
  15. package/app/assets/brand/characters/muted/deer.svg +1 -0
  16. package/app/assets/brand/characters/muted/duck.svg +1 -0
  17. package/app/assets/brand/characters/muted/elephant.svg +1 -0
  18. package/app/assets/brand/characters/muted/fox.svg +1 -0
  19. package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
  20. package/app/assets/brand/characters/muted/koala.svg +1 -0
  21. package/app/assets/brand/characters/muted/ladybug.svg +1 -0
  22. package/app/assets/brand/characters/muted/lion.svg +1 -0
  23. package/app/assets/brand/characters/muted/mouse.svg +1 -0
  24. package/app/assets/brand/characters/muted/octopus.svg +1 -0
  25. package/app/assets/brand/characters/muted/owl.svg +1 -0
  26. package/app/assets/brand/characters/muted/panda.svg +1 -0
  27. package/app/assets/brand/characters/muted/penguin.svg +1 -0
  28. package/app/assets/brand/characters/muted/pig.svg +1 -0
  29. package/app/assets/brand/characters/muted/poodle.svg +1 -0
  30. package/app/assets/brand/characters/muted/raccoon.svg +1 -0
  31. package/app/assets/brand/characters/muted/seal.svg +1 -0
  32. package/app/assets/brand/characters/muted/sheep.svg +1 -0
  33. package/app/assets/brand/characters/muted/snail.svg +1 -0
  34. package/app/assets/brand/characters/muted/turtle.svg +1 -0
  35. package/app/assets/brand/characters/muted/whale.svg +1 -0
  36. package/app/assets/brand/logo/favicon.svg +1 -0
  37. package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
  38. package/app/assets/brand/logo/logo-primary.svg +1 -0
  39. package/app/assets/brand/logo/mascot-reversed.svg +1 -0
  40. package/app/assets/brand/logo/mascot.svg +1 -0
  41. package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
  42. package/app/assets/brand/logo/wordmark.svg +1 -0
  43. package/app/assets/css/cms-admin.css +607 -0
  44. package/app/components/CmsBlocks.vue +30 -0
  45. package/app/components/CmsCell.vue +16 -0
  46. package/app/components/CmsCellBadge.vue +18 -0
  47. package/app/components/CmsCellBlocks.vue +9 -0
  48. package/app/components/CmsCellBoolean.vue +12 -0
  49. package/app/components/CmsCellCode.vue +7 -0
  50. package/app/components/CmsCellColor.vue +13 -0
  51. package/app/components/CmsCellDate.vue +10 -0
  52. package/app/components/CmsCellExcerpt.vue +21 -0
  53. package/app/components/CmsCellGallery.vue +10 -0
  54. package/app/components/CmsCellGroup.vue +23 -0
  55. package/app/components/CmsCellNumber.vue +7 -0
  56. package/app/components/CmsCellRelation.vue +8 -0
  57. package/app/components/CmsCellText.vue +12 -0
  58. package/app/components/CmsCellThumb.vue +18 -0
  59. package/app/components/CmsConfirmDialog.vue +55 -0
  60. package/app/components/CmsContent.vue +103 -0
  61. package/app/components/CmsDayPicker.vue +44 -0
  62. package/app/components/CmsEmptyState.vue +35 -0
  63. package/app/components/CmsField.vue +111 -0
  64. package/app/components/CmsFieldBlocks.vue +353 -0
  65. package/app/components/CmsFieldBoolean.vue +8 -0
  66. package/app/components/CmsFieldColor.vue +42 -0
  67. package/app/components/CmsFieldDate.vue +131 -0
  68. package/app/components/CmsFieldGallery.vue +114 -0
  69. package/app/components/CmsFieldGroup.vue +53 -0
  70. package/app/components/CmsFieldMedia.vue +219 -0
  71. package/app/components/CmsFieldNumber.vue +11 -0
  72. package/app/components/CmsFieldRelationMany.vue +54 -0
  73. package/app/components/CmsFieldRelationOne.vue +47 -0
  74. package/app/components/CmsFieldRichText.vue +45 -0
  75. package/app/components/CmsFieldSelect.vue +41 -0
  76. package/app/components/CmsFieldSlug.vue +46 -0
  77. package/app/components/CmsFieldText.vue +75 -0
  78. package/app/components/CmsFormSections.vue +150 -0
  79. package/app/components/CmsHighlight.vue +22 -0
  80. package/app/components/CmsImage.vue +39 -0
  81. package/app/components/CmsLinkPicker.vue +122 -0
  82. package/app/components/CmsLinkTarget.vue +35 -0
  83. package/app/components/CmsListFilters.vue +107 -0
  84. package/app/components/CmsLiveNotice.vue +64 -0
  85. package/app/components/CmsLoadError.vue +24 -0
  86. package/app/components/CmsMascot.vue +10 -0
  87. package/app/components/CmsMediaLibrary.vue +114 -0
  88. package/app/components/CmsMediaPreview.vue +34 -0
  89. package/app/components/CmsMediaThumb.vue +22 -0
  90. package/app/components/CmsMoment.vue +42 -0
  91. package/app/components/CmsNav.vue +39 -0
  92. package/app/components/CmsPage.vue +86 -0
  93. package/app/components/CmsPasswordInput.vue +22 -0
  94. package/app/components/CmsPresence.vue +29 -0
  95. package/app/components/CmsPreviewPanel.vue +131 -0
  96. package/app/components/CmsPublishReview.vue +102 -0
  97. package/app/components/CmsRevisions.vue +195 -0
  98. package/app/components/CmsRichTextEditor.vue +290 -0
  99. package/app/components/CmsSearch.vue +99 -0
  100. package/app/components/CmsSection.vue +20 -0
  101. package/app/components/CmsSingletonForm.vue +283 -0
  102. package/app/components/CmsStatusBadge.vue +21 -0
  103. package/app/components/CmsTime.vue +16 -0
  104. package/app/components/CmsTranslationRef.vue +27 -0
  105. package/app/components/CmsTreeList.vue +453 -0
  106. package/app/components/CmsUnsavedText.vue +34 -0
  107. package/app/components/CmsWordmark.vue +10 -0
  108. package/app/composables/useCmsAdminHead.ts +18 -0
  109. package/app/composables/useCmsCollab.ts +165 -0
  110. package/app/composables/useCmsConfirm.ts +39 -0
  111. package/app/composables/useCmsEntryActions.ts +75 -0
  112. package/app/composables/useCmsFocus.ts +13 -0
  113. package/app/composables/useCmsForm.ts +273 -0
  114. package/app/composables/useCmsI18n.ts +185 -0
  115. package/app/composables/useCmsLinkTargets.ts +84 -0
  116. package/app/composables/useCmsLive.ts +199 -0
  117. package/app/composables/useCmsSearch.ts +62 -0
  118. package/app/composables/useCmsSeo.ts +99 -0
  119. package/app/composables/useCmsSignedIn.ts +24 -0
  120. package/app/composables/useCmsUiLocale.ts +24 -0
  121. package/app/i18n/cs.ts +781 -0
  122. package/app/i18n/de.ts +781 -0
  123. package/app/i18n/en.ts +782 -0
  124. package/app/i18n/types.ts +22 -0
  125. package/app/layouts/admin.vue +224 -0
  126. package/app/layouts/cms-auth.vue +37 -0
  127. package/app/middleware/cms-api-not-a-page.global.ts +10 -0
  128. package/app/middleware/cms-auth.global.ts +16 -0
  129. package/app/pages/admin/[entity]/[id].vue +1109 -0
  130. package/app/pages/admin/[entity]/index.vue +733 -0
  131. package/app/pages/admin/account.vue +117 -0
  132. package/app/pages/admin/forgot.vue +50 -0
  133. package/app/pages/admin/index.vue +184 -0
  134. package/app/pages/admin/invite/[token].vue +73 -0
  135. package/app/pages/admin/login.vue +84 -0
  136. package/app/pages/admin/media.vue +494 -0
  137. package/app/pages/admin/privacy.vue +193 -0
  138. package/app/pages/admin/reset/[token].vue +52 -0
  139. package/app/pages/admin/setup.vue +87 -0
  140. package/app/pages/admin/styleguide.vue +261 -0
  141. package/app/pages/admin/team.vue +258 -0
  142. package/app/plugins/cmsLivePreview.client.ts +20 -0
  143. package/app/plugins/cmsSignedOut.client.ts +31 -0
  144. package/app/utils/cmsBlockType.ts +11 -0
  145. package/app/utils/cmsBrand.ts +38 -0
  146. package/app/utils/cmsDiff.ts +83 -0
  147. package/app/utils/cmsFetch.ts +7 -0
  148. package/app/utils/cmsFocusInvalid.ts +22 -0
  149. package/app/utils/cmsIssues.ts +128 -0
  150. package/app/utils/cmsJson.ts +28 -0
  151. package/app/utils/cmsMedia.ts +63 -0
  152. package/app/utils/cmsSections.ts +154 -0
  153. package/app/utils/cmsTime.ts +41 -0
  154. package/cli/backup.ts +713 -0
  155. package/cli/breaking.ts +174 -0
  156. package/cli/content-migrate.ts +239 -0
  157. package/cli/db.ts +86 -0
  158. package/cli/export.ts +336 -0
  159. package/cli/generate.ts +146 -0
  160. package/cli/import.ts +643 -0
  161. package/cli/index.ts +140 -0
  162. package/cli/links-convert.ts +291 -0
  163. package/cli/seed-engine.ts +705 -0
  164. package/cli/seed.ts +339 -0
  165. package/cli/status.ts +49 -0
  166. package/cli/tar.ts +304 -0
  167. package/cli/transfer.ts +677 -0
  168. package/cli/up.ts +27 -0
  169. package/cli/upgrade.ts +60 -0
  170. package/cli/versions.ts +60 -0
  171. package/forms/components/CmsForm.vue +186 -0
  172. package/forms/index.ts +203 -0
  173. package/forms/messages.ts +104 -0
  174. package/forms/retention.ts +48 -0
  175. package/forms/server/form.get.ts +12 -0
  176. package/forms/server/form.post.ts +109 -0
  177. package/forms/server/formSettings.ts +12 -0
  178. package/forms/server/forms.ts +127 -0
  179. package/module.ts +373 -0
  180. package/nuxt.config.ts +43 -0
  181. package/package.json +98 -3
  182. package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
  183. package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
  184. package/server/api/cms/[entity]/[id].delete.ts +52 -0
  185. package/server/api/cms/[entity]/[id].get.ts +21 -0
  186. package/server/api/cms/[entity]/[id].patch.ts +116 -0
  187. package/server/api/cms/[entity]/_export.get.ts +69 -0
  188. package/server/api/cms/[entity]/_move.post.ts +81 -0
  189. package/server/api/cms/[entity]/_order.post.ts +49 -0
  190. package/server/api/cms/[entity]/_tree.get.ts +41 -0
  191. package/server/api/cms/[entity]/index.get.ts +62 -0
  192. package/server/api/cms/[entity]/index.post.ts +38 -0
  193. package/server/api/cms/[entity]/options.get.ts +55 -0
  194. package/server/api/cms/_auth/accept.post.ts +33 -0
  195. package/server/api/cms/_auth/forgot.post.ts +39 -0
  196. package/server/api/cms/_auth/invite/[token].get.ts +18 -0
  197. package/server/api/cms/_auth/login.post.ts +42 -0
  198. package/server/api/cms/_auth/logout.post.ts +7 -0
  199. package/server/api/cms/_auth/me.get.ts +5 -0
  200. package/server/api/cms/_auth/me.patch.ts +32 -0
  201. package/server/api/cms/_auth/password.post.ts +29 -0
  202. package/server/api/cms/_auth/reset.post.ts +34 -0
  203. package/server/api/cms/_auth/setup.post.ts +46 -0
  204. package/server/api/cms/_auth/status.get.ts +18 -0
  205. package/server/api/cms/_collab/session.get.ts +30 -0
  206. package/server/api/cms/_collab/socket.ts +51 -0
  207. package/server/api/cms/_collab/unsaved.delete.ts +16 -0
  208. package/server/api/cms/_graph.get.ts +20 -0
  209. package/server/api/cms/_links/resolve.post.ts +17 -0
  210. package/server/api/cms/_links.get.ts +81 -0
  211. package/server/api/cms/_live.ts +30 -0
  212. package/server/api/cms/_media/[id]/usage.get.ts +14 -0
  213. package/server/api/cms/_media/[id].delete.ts +29 -0
  214. package/server/api/cms/_media/[id].get.ts +13 -0
  215. package/server/api/cms/_media/[id].patch.ts +64 -0
  216. package/server/api/cms/_media/exists.post.ts +15 -0
  217. package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
  218. package/server/api/cms/_media/index.get.ts +44 -0
  219. package/server/api/cms/_media/index.post.ts +147 -0
  220. package/server/api/cms/_media/raw/[...key].get.ts +85 -0
  221. package/server/api/cms/_media/raw/[...key].head.ts +2 -0
  222. package/server/api/cms/_overview.get.ts +87 -0
  223. package/server/api/cms/_preview.post.ts +17 -0
  224. package/server/api/cms/_privacy/delete.post.ts +14 -0
  225. package/server/api/cms/_privacy/export.post.ts +25 -0
  226. package/server/api/cms/_privacy/lookup.post.ts +27 -0
  227. package/server/api/cms/_search.get.ts +17 -0
  228. package/server/api/cms/_singleton/[name].get.ts +19 -0
  229. package/server/api/cms/_singleton/[name].put.ts +76 -0
  230. package/server/api/cms/_users/[id]/resend.post.ts +25 -0
  231. package/server/api/cms/_users/[id].delete.ts +21 -0
  232. package/server/api/cms/_users/[id].patch.ts +45 -0
  233. package/server/api/cms/_users/index.get.ts +28 -0
  234. package/server/api/cms/_users/index.post.ts +39 -0
  235. package/server/api/content/[entity]/[slug].get.ts +74 -0
  236. package/server/api/content/[entity]/index.get.ts +100 -0
  237. package/server/api/content/_global/[name].get.ts +23 -0
  238. package/server/api/content/_search.get.ts +28 -0
  239. package/server/api/content/_sitemap.get.ts +8 -0
  240. package/server/api/content/_tree/[entity].get.ts +31 -0
  241. package/server/api/content-types.ts.get.ts +17 -0
  242. package/server/api/health/live.get.ts +10 -0
  243. package/server/api/health/ready.get.ts +21 -0
  244. package/server/api/openapi.get.ts +48 -0
  245. package/server/api/openapi.json.get.ts +32 -0
  246. package/server/db/driver.ts +8 -0
  247. package/server/mail/mailer.ts +43 -0
  248. package/server/middleware/cms-0-headers.ts +37 -0
  249. package/server/middleware/cms-0-migrate-gate.ts +14 -0
  250. package/server/middleware/cms-csrf.ts +28 -0
  251. package/server/middleware/cms-limits.ts +20 -0
  252. package/server/middleware/cms-redirects.ts +21 -0
  253. package/server/middleware/cms-session.ts +41 -0
  254. package/server/middleware/cms-z-plugin-access.ts +15 -0
  255. package/server/plugins/cms-0-config-check.ts +35 -0
  256. package/server/plugins/cms-changes.ts +53 -0
  257. package/server/plugins/cms-content-etag.ts +21 -0
  258. package/server/plugins/cms-errors.ts +45 -0
  259. package/server/plugins/cms-migrate.ts +5 -0
  260. package/server/plugins/cms-plugins.ts +17 -0
  261. package/server/plugins/cms-search.ts +12 -0
  262. package/server/plugins/cms-setup-code.ts +15 -0
  263. package/server/routes/sitemap.xml.get.ts +25 -0
  264. package/server/store/redis.ts +95 -0
  265. package/server/store/types.ts +21 -0
  266. package/server/tasks/cms/retention.ts +10 -0
  267. package/server/tasks/cms/scheduled.ts +10 -0
  268. package/server/tasks/cms/search-reindex.ts +12 -0
  269. package/server/utils/cmsAudit.ts +29 -0
  270. package/server/utils/cmsAuth.ts +143 -0
  271. package/server/utils/cmsBlocks.ts +47 -0
  272. package/server/utils/cmsChanges.ts +222 -0
  273. package/server/utils/cmsCollab.ts +286 -0
  274. package/server/utils/cmsCollabServer.ts +451 -0
  275. package/server/utils/cmsCollate.ts +27 -0
  276. package/server/utils/cmsContent.ts +376 -0
  277. package/server/utils/cmsCrud.ts +361 -0
  278. package/server/utils/cmsCsv.ts +24 -0
  279. package/server/utils/cmsDb.ts +23 -0
  280. package/server/utils/cmsDemo.ts +36 -0
  281. package/server/utils/cmsEmails.ts +125 -0
  282. package/server/utils/cmsErrors.ts +40 -0
  283. package/server/utils/cmsFold.ts +17 -0
  284. package/server/utils/cmsImageSizes.ts +51 -0
  285. package/server/utils/cmsJson.ts +6 -0
  286. package/server/utils/cmsLinkAddress.ts +50 -0
  287. package/server/utils/cmsLinks.ts +248 -0
  288. package/server/utils/cmsListQuery.ts +93 -0
  289. package/server/utils/cmsLive.ts +393 -0
  290. package/server/utils/cmsLocales.ts +96 -0
  291. package/server/utils/cmsMail.ts +28 -0
  292. package/server/utils/cmsMediaGc.ts +188 -0
  293. package/server/utils/cmsMediaKeys.ts +62 -0
  294. package/server/utils/cmsMigrate.ts +86 -0
  295. package/server/utils/cmsPreviewDraft.ts +72 -0
  296. package/server/utils/cmsPrivacy.ts +176 -0
  297. package/server/utils/cmsPublished.ts +57 -0
  298. package/server/utils/cmsRange.ts +38 -0
  299. package/server/utils/cmsRateLimit.ts +82 -0
  300. package/server/utils/cmsRedirects.ts +160 -0
  301. package/server/utils/cmsRetention.ts +61 -0
  302. package/server/utils/cmsRevisions.ts +103 -0
  303. package/server/utils/cmsScheduler.ts +45 -0
  304. package/server/utils/cmsScrub.ts +107 -0
  305. package/server/utils/cmsSearch.ts +354 -0
  306. package/server/utils/cmsSession.ts +102 -0
  307. package/server/utils/cmsSetupCode.ts +97 -0
  308. package/server/utils/cmsSitemap.ts +176 -0
  309. package/server/utils/cmsStorage.ts +580 -0
  310. package/server/utils/cmsStore.ts +109 -0
  311. package/server/utils/cmsTiming.ts +14 -0
  312. package/server/utils/cmsTooMany.ts +9 -0
  313. package/server/utils/cmsTranslationWrite.ts +186 -0
  314. package/server/utils/cmsTree.ts +251 -0
  315. package/server/utils/cmsTreeAddress.ts +65 -0
  316. package/server/utils/cmsUpload.ts +228 -0
  317. package/server/utils/cmsWebhooks.ts +104 -0
  318. package/shared/json.ts +11 -0
  319. package/shared/richtext-editor.ts +56 -0
  320. package/shared/richtext-normalize.ts +33 -0
  321. package/shared/richtext-paste.ts +151 -0
  322. package/shared/storage-delivery.ts +53 -0
  323. package/src/blocks.ts +624 -0
  324. package/src/changes.ts +54 -0
  325. package/src/content-shape.ts +340 -0
  326. package/src/deprecate.ts +64 -0
  327. package/src/descriptor.ts +149 -0
  328. package/src/docs.ts +27 -0
  329. package/src/emitter.ts +94 -0
  330. package/src/entity.ts +624 -0
  331. package/src/errorReport.ts +41 -0
  332. package/src/fields.ts +465 -0
  333. package/src/i18n.ts +26 -0
  334. package/src/index.ts +54 -0
  335. package/src/linkref.ts +31 -0
  336. package/src/links.ts +179 -0
  337. package/src/locales.ts +39 -0
  338. package/src/media.ts +48 -0
  339. package/src/migrate.ts +236 -0
  340. package/src/openapi.ts +377 -0
  341. package/src/paths.ts +88 -0
  342. package/src/plugin.ts +505 -0
  343. package/src/presets.ts +230 -0
  344. package/src/richtext.ts +88 -0
  345. package/src/sdk.ts +181 -0
  346. package/src/search.ts +312 -0
  347. package/src/seed.ts +225 -0
  348. package/src/setupCode.ts +30 -0
  349. package/src/system.ts +219 -0
  350. package/src/tree.ts +421 -0
  351. package/src/upgrades.ts +137 -0
  352. package/starter/.env.example +10 -0
  353. package/starter/README.md +65 -0
  354. package/starter/app/app.config.ts +10 -0
  355. package/starter/app/app.vue +12 -0
  356. package/starter/app/assets/css/main.css +15 -0
  357. package/starter/app/components/global/BlockContact.vue +15 -0
  358. package/starter/app/components/global/BlockCta.vue +15 -0
  359. package/starter/app/components/global/BlockHero.vue +23 -0
  360. package/starter/app/components/global/BlockImage.vue +13 -0
  361. package/starter/app/components/global/BlockText.vue +12 -0
  362. package/starter/app/composables/useSite.ts +77 -0
  363. package/starter/app/error.vue +28 -0
  364. package/starter/app/layouts/default.vue +106 -0
  365. package/starter/app/lib/cms-content.ts +152 -0
  366. package/starter/app/pages/[...slug].vue +66 -0
  367. package/starter/app/pages/hledat.vue +59 -0
  368. package/starter/app/pages/index.vue +20 -0
  369. package/starter/cms.config.ts +137 -0
  370. package/starter/compose.yaml +15 -0
  371. package/starter/drizzle.config.ts +8 -0
  372. package/starter/gitignore.txt +8 -0
  373. package/starter/nuxt.config.ts +13 -0
  374. package/starter/package.json +38 -0
  375. package/starter/scripts/typecheck.mjs +38 -0
  376. package/starter/seed/index.ts +297 -0
  377. package/starter/seed/media/about.jpg +0 -0
  378. package/starter/seed/media/hero.jpg +0 -0
  379. package/starter/seed/media/logo.png +0 -0
  380. package/starter/seed/media/news-bread.jpg +0 -0
  381. package/starter/seed/media/news-morning.jpg +0 -0
  382. package/starter/server/db/migrations/0000_init.sql +269 -0
  383. package/starter/server/db/migrations/0001_page_tree.sql +5 -0
  384. package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
  385. package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
  386. package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
  387. package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
  388. package/starter/server/db/migrations/meta/_journal.json +27 -0
  389. package/starter/server/db/schema.ts +253 -0
  390. package/starter/server/plugins/sitemap.ts +11 -0
  391. package/starter/server/routes/robots.txt.get.ts +12 -0
  392. package/starter/tsconfig.json +9 -0
@@ -0,0 +1,393 @@
1
+ import { eq, inArray, type CmsChange } from 'tinyzoo'
2
+
3
+ /**
4
+ * Realtime for the admin: who else is looking at (or editing) the same content, and
5
+ * what changed while you were looking. One WebSocket per admin tab
6
+ * (/api/cms/_live, see api/cms/_live.ts) watches a few topics:
7
+ *
8
+ * list:<entity> a collection's list
9
+ * entry:<entity>:<id> one entry (or a global's row)
10
+ *
11
+ * Identity comes from the signed session checked at connect time, never from what a
12
+ * browser says about itself, and is re-checked every heartbeat (a disabled account or
13
+ * a password change drops the socket). Watching a topic needs read access to it.
14
+ *
15
+ * Several replicas: presence is shared over the store's `live` channel. Each
16
+ * instance announces its own people when they change and again every heartbeat;
17
+ * others forget an announcement that was not repeated in time (a crashed replica's
18
+ * people disappear on their own). Content changes arrive through onCmsChange, which
19
+ * already covers changes saved on other replicas.
20
+ *
21
+ * Server → browser messages:
22
+ * { type: 'presence', topic, people: [{ id, name, editing }] } (including you)
23
+ * { type: 'change', topic, change: { type, entity, id, locale, by, byName, at } }
24
+ * { type: 'error', topic?, message }
25
+ * Browser → server:
26
+ * { type: 'watch' | 'unwatch', topic }
27
+ * { type: 'editing', topic, editing: boolean }
28
+ */
29
+
30
+ /** The bits of a crossws peer this hub uses (crossws is not a direct dependency). */
31
+ export interface LiveSocket {
32
+ id: string
33
+ send: (data: string) => unknown
34
+ close: (code?: number, reason?: string) => void
35
+ }
36
+
37
+ interface Person { id: string; name: string; editing: boolean }
38
+ /**
39
+ * `rowFiltered`: the person may read only some rows of this collection — list
40
+ * changes reach them without ids, and an entry watch is re-checked every heartbeat.
41
+ */
42
+ interface Watch { editing: boolean; rowFiltered: boolean }
43
+ interface LocalPeer {
44
+ socket: LiveSocket
45
+ user: CmsUser
46
+ sv: number
47
+ topics: Map<string, Watch>
48
+ /** Messages are handled one at a time, in order. */
49
+ queue: Promise<void>
50
+ openedAt: number
51
+ /** Last sign of life (any message, including the answer to our ping). */
52
+ seenAt: number
53
+ /** Messages in the current rate window. */
54
+ burst: { at: number; n: number }
55
+ }
56
+
57
+ const LIVE_CHANNEL = 'live'
58
+ /** The test build shortens it (CMS_TEST_LIVE_HEARTBEAT_MS); the other limits below stay, so a quiet test tab is not dropped. */
59
+ const HEARTBEAT_MS = 20_000
60
+ const REMOTE_TTL_MS = 50_000
61
+ /** A socket that has not answered for this long is gone (sleeping laptop, dead network). */
62
+ const SILENT_MS = 65_000
63
+ /** Sockets are re-opened (and re-authorised) at least this often. */
64
+ const MAX_SOCKET_AGE_MS = 12 * 60 * 60_000
65
+ const MAX_TOPICS = 20
66
+ const MAX_MESSAGE = 1024
67
+ const MAX_SOCKETS_PER_USER = 10
68
+ const RATE_WINDOW_MS = 10_000
69
+ const RATE_MAX = 60
70
+
71
+ const local = new Map<string, LocalPeer>()
72
+ /** topic → `${instance}:${peer}` → what another replica said about one of its peers. */
73
+ const remote = new Map<string, Map<string, { person: Person; at: number }>>()
74
+
75
+ let started: Promise<void> | null = null
76
+ let timer: ReturnType<typeof setInterval> | null = null
77
+ let stopChanges: (() => void) | null = null
78
+
79
+ /**
80
+ * Start listening to other replicas and to content changes (once, on the first
81
+ * connection). Runs in the background: a slow or absent Redis never holds up a
82
+ * socket — presence then stays on this instance until it is back.
83
+ */
84
+ function start(): void {
85
+ if (started) return
86
+ timer = setInterval(() => { void heartbeat() }, cmsTimerMs('CMS_TEST_LIVE_HEARTBEAT_MS', HEARTBEAT_MS))
87
+ ;(timer as any).unref?.()
88
+ stopChanges = onCmsChange((change) => { void deliverChange(change).catch((err) => console.error('[cms] realtime delivery failed', scrubError(err))) })
89
+ started = (async () => {
90
+ const store = await useCmsStore()
91
+ await store.subscribe(LIVE_CHANNEL, (raw) => {
92
+ try {
93
+ onRemote(JSON.parse(raw))
94
+ }
95
+ catch { /* not ours */ }
96
+ })
97
+ })().catch((err) => console.error('[cms] realtime cannot hear other instances', scrubError(err)))
98
+ }
99
+
100
+ /** Stop timers and listeners (server shutdown, dev reload). */
101
+ export function stopLive(): void {
102
+ if (timer) clearInterval(timer)
103
+ timer = null
104
+ stopChanges?.()
105
+ stopChanges = null
106
+ started = null
107
+ for (const p of local.values()) p.socket.close(1001, 'Server shutting down')
108
+ local.clear()
109
+ remote.clear()
110
+ }
111
+
112
+ // ── connections ──
113
+
114
+ export function liveOpen(socket: LiveSocket, user: CmsUser, sv: number): void {
115
+ const mine = [...local.values()].filter((p) => p.user.id === user.id)
116
+ if (mine.length >= MAX_SOCKETS_PER_USER) {
117
+ // Too many tabs: the oldest one gives way.
118
+ const oldest = mine.sort((x, y) => x.openedAt - y.openedAt)[0]!
119
+ oldest.socket.close(4429, 'Too many open tabs')
120
+ liveClose(oldest.socket)
121
+ }
122
+ const now = Date.now()
123
+ local.set(socket.id, { socket, user, sv, topics: new Map(), queue: Promise.resolve(), openedAt: now, seenAt: now, burst: { at: now, n: 0 } })
124
+ start()
125
+ }
126
+
127
+ export function liveClose(socket: LiveSocket): void {
128
+ const p = local.get(socket.id)
129
+ if (!p) return
130
+ local.delete(socket.id)
131
+ for (const topic of p.topics.keys()) {
132
+ void announce(topic, socket.id, null)
133
+ broadcastPresence(topic)
134
+ }
135
+ }
136
+
137
+ export function liveMessage(socket: LiveSocket, text: string): Promise<void> {
138
+ const p = local.get(socket.id)
139
+ if (!p) return Promise.resolve()
140
+ const now = Date.now()
141
+ p.seenAt = now
142
+ if (now - p.burst.at > RATE_WINDOW_MS) p.burst = { at: now, n: 0 }
143
+ if (++p.burst.n > RATE_MAX) {
144
+ socket.close(4408, 'Too many messages')
145
+ liveClose(socket)
146
+ return Promise.resolve()
147
+ }
148
+ p.queue = p.queue.then(() => handle(p, text)).catch((err) => console.error('[cms] realtime message failed', scrubError(err)))
149
+ return p.queue
150
+ }
151
+
152
+ async function handle(p: LocalPeer, text: string): Promise<void> {
153
+ if (!local.has(p.socket.id)) return // closed meanwhile
154
+ if (text.length > MAX_MESSAGE) return send(p, { type: 'error', message: 'Message too large' })
155
+ let msg: any
156
+ try {
157
+ msg = JSON.parse(text)
158
+ }
159
+ catch {
160
+ return
161
+ }
162
+ if (msg?.type === 'pong') return
163
+ const topic = typeof msg?.topic === 'string' ? msg.topic : ''
164
+ if (msg?.type === 'watch') {
165
+ if (p.topics.has(topic)) return broadcastPresence(topic)
166
+ if (p.topics.size >= MAX_TOPICS) return send(p, { type: 'error', topic, message: 'Watching too many things at once' })
167
+ const access = await mayWatch(p.user, topic)
168
+ if (!access) return send(p, { type: 'error', topic, message: 'Not found or not permitted' })
169
+ if (!local.has(p.socket.id) || p.topics.size >= MAX_TOPICS) return
170
+ // `editing` may come with the watch (a tab with unsaved changes reconnecting).
171
+ p.topics.set(topic, { editing: msg.editing === true, rowFiltered: access.rowFiltered })
172
+ void announce(topic, p.socket.id, personOf(p, topic))
173
+ broadcastPresence(topic)
174
+ }
175
+ else if (msg?.type === 'unwatch') {
176
+ if (!p.topics.delete(topic)) return
177
+ void announce(topic, p.socket.id, null)
178
+ broadcastPresence(topic)
179
+ }
180
+ else if (msg?.type === 'editing') {
181
+ const w = p.topics.get(topic)
182
+ if (!w || w.editing === !!msg.editing) return
183
+ w.editing = !!msg.editing
184
+ void announce(topic, p.socket.id, personOf(p, topic))
185
+ broadcastPresence(topic)
186
+ }
187
+ }
188
+
189
+ // ── topics and access ──
190
+
191
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
192
+
193
+ export function parseTopic(topic: string): { kind: 'list' | 'entry'; entity: string; id?: string } | null {
194
+ const m = /^(list|entry):([a-zA-Z][\w-]{0,63})(?::([0-9a-fA-F-]{36}))?$/.exec(topic)
195
+ if (!m) return null
196
+ if (m[1] === 'list' && m[3]) return null
197
+ if (m[1] === 'entry' && !(m[3] && UUID.test(m[3]))) return null
198
+ return { kind: m[1] as 'list' | 'entry', entity: m[2]!, id: m[3]?.toLowerCase() }
199
+ }
200
+
201
+ /** May this person watch the topic: read access to the collection, and to the row for an entry. */
202
+ async function mayWatch(user: CmsUser, topic: string): Promise<{ rowFiltered: boolean } | null> {
203
+ const t = parseTopic(topic)
204
+ if (!t || topic !== (t.kind === 'list' ? `list:${t.entity}` : `entry:${t.entity}:${t.id}`)) return null
205
+ try {
206
+ const { db, reg } = useCms()
207
+ const e = reg.byName.get(t.entity)
208
+ if (!e) return null
209
+ const rowFilter = authorize(e, 'read', user)
210
+ if (t.kind === 'list') return { rowFiltered: rowFilter !== undefined }
211
+ const [row] = await db.select({ id: e.table.id }).from(e.table).where(both(eq(e.table.id, t.id!), rowFilter))
212
+ return row ? { rowFiltered: rowFilter !== undefined } : null
213
+ }
214
+ catch {
215
+ return null
216
+ }
217
+ }
218
+
219
+ // ── presence ──
220
+
221
+ /** How colleagues see someone: their name, never their full email address. */
222
+ export function displayName(name?: string | null, email?: string | null): string {
223
+ return name?.trim() || (email ? email.split('@')[0]! : '')
224
+ }
225
+
226
+ const personOf = (p: LocalPeer, topic: string): Person => ({ id: p.user.id, name: displayName(p.user.name, p.user.email), editing: !!p.topics.get(topic)?.editing })
227
+
228
+ /** Everyone on a topic, one entry per person (several tabs: editing if any tab is). */
229
+ export function presenceOf(topic: string): Person[] {
230
+ const byId = new Map<string, Person>()
231
+ const add = (person: Person) => {
232
+ const seen = byId.get(person.id)
233
+ if (seen) seen.editing ||= person.editing
234
+ else byId.set(person.id, { ...person })
235
+ }
236
+ for (const p of local.values()) if (p.topics.has(topic)) add(personOf(p, topic))
237
+ const now = Date.now()
238
+ for (const { person, at } of remote.get(topic)?.values() ?? []) if (now - at < REMOTE_TTL_MS) add(person)
239
+ return [...byId.values()].sort((a, b) => a.name.localeCompare(b.name))
240
+ }
241
+
242
+ function broadcastPresence(topic: string): void {
243
+ const people = presenceOf(topic)
244
+ for (const p of local.values()) if (p.topics.has(topic)) send(p, { type: 'presence', topic, people })
245
+ }
246
+
247
+ let lastAnnounceError = 0
248
+
249
+ async function announce(topic: string, peerId: string, person: Person | null): Promise<void> {
250
+ try {
251
+ const store = await useCmsStore()
252
+ await store.publish(LIVE_CHANNEL, JSON.stringify({ origin: instanceId(), topic, peer: peerId, person }))
253
+ }
254
+ catch (err) {
255
+ // One line a minute is enough while Redis is down.
256
+ if (Date.now() - lastAnnounceError > 60_000) {
257
+ lastAnnounceError = Date.now()
258
+ console.error('[cms] realtime announce failed', safeErrorMessage(err))
259
+ }
260
+ }
261
+ }
262
+
263
+ function onRemote(msg: any): void {
264
+ if (!msg || msg.origin === instanceId() || typeof msg.topic !== 'string' || typeof msg.peer !== 'string') return
265
+ if (!parseTopic(msg.topic)) return
266
+ const key = `${msg.origin}:${msg.peer}`
267
+ let map = remote.get(msg.topic)
268
+ const person = msg.person
269
+ if (person && typeof person.id === 'string' && typeof person.name === 'string') {
270
+ if (!map) remote.set(msg.topic, map = new Map())
271
+ const before = map.get(key)?.person
272
+ map.set(key, { person: { id: person.id, name: person.name, editing: !!person.editing }, at: Date.now() })
273
+ // A repeat of the same announcement (heartbeat) changes nothing anyone sees.
274
+ if (before && before.id === person.id && before.editing === !!person.editing) return
275
+ }
276
+ else {
277
+ if (!map?.delete(key)) return
278
+ if (!map.size) remote.delete(msg.topic)
279
+ }
280
+ broadcastPresence(msg.topic)
281
+ }
282
+
283
+ /**
284
+ * Every heartbeat: repeat our people to the other replicas, forget theirs that went
285
+ * quiet, and drop sockets whose account may no longer be used.
286
+ */
287
+ async function heartbeat(): Promise<void> {
288
+ const now0 = Date.now()
289
+ for (const p of [...local.values()]) {
290
+ // Gone quiet (no answer to the last pings) or open too long: drop it — a live
291
+ // browser reconnects at once, and is authorised afresh.
292
+ if (now0 - p.seenAt > SILENT_MS || now0 - p.openedAt > MAX_SOCKET_AGE_MS) {
293
+ p.socket.close(4000, 'Reconnect')
294
+ liveClose(p.socket)
295
+ continue
296
+ }
297
+ // Keeps proxies from closing an idle socket, and asks for a sign of life.
298
+ send(p, { type: 'ping' })
299
+ }
300
+ for (const [id, p] of local) for (const topic of p.topics.keys()) void announce(topic, id, personOf(p, topic))
301
+ const now = Date.now()
302
+ for (const [topic, map] of remote) {
303
+ let changed = false
304
+ for (const [key, v] of map) if (now - v.at >= REMOTE_TTL_MS) { map.delete(key); changed = true }
305
+ if (!map.size) remote.delete(topic)
306
+ if (changed) broadcastPresence(topic)
307
+ }
308
+ await recheckAccounts().catch((err) => console.error('[cms] realtime account check failed', scrubError(err)))
309
+ await recheckRowWatches().catch((err) => console.error('[cms] realtime access check failed', scrubError(err)))
310
+ }
311
+
312
+ /** Entry watches under a row rule (authors' own entries): still allowed? */
313
+ async function recheckRowWatches(): Promise<void> {
314
+ for (const p of [...local.values()]) {
315
+ for (const [topic, w] of [...p.topics]) {
316
+ if (!w.rowFiltered || !topic.startsWith('entry:')) continue
317
+ if (await mayWatch(p.user, topic)) continue
318
+ if (!p.topics.delete(topic)) continue
319
+ send(p, { type: 'error', topic, message: 'Not found or not permitted' })
320
+ void announce(topic, p.socket.id, null)
321
+ broadcastPresence(topic)
322
+ }
323
+ }
324
+ }
325
+
326
+ async function recheckAccounts(): Promise<void> {
327
+ const ids = [...new Set([...local.values()].map((p) => p.user.id))]
328
+ if (!ids.length) return
329
+ const { db, reg } = useCms()
330
+ const rows: any[] = await db.select({ id: reg.cmsUsers.id, status: reg.cmsUsers.status, sv: reg.cmsUsers.sessionVersion, role: reg.cmsUsers.role })
331
+ .from(reg.cmsUsers).where(inArray(reg.cmsUsers.id, ids))
332
+ const byId = new Map(rows.map((r) => [r.id, r]))
333
+ for (const p of [...local.values()]) {
334
+ const u = byId.get(p.user.id)
335
+ // A changed role reconnects too, so watches are re-authorised under the new one.
336
+ if (!u || u.status !== 'active' || u.sv !== p.sv || u.role !== p.user.role) {
337
+ p.socket.close(4401, 'Session no longer valid')
338
+ liveClose(p.socket)
339
+ }
340
+ }
341
+ }
342
+
343
+ // ── content changes ──
344
+
345
+ const names = new Map<string, { name: string; at: number }>()
346
+
347
+ async function nameOf(userId: string | null): Promise<string | null> {
348
+ if (!userId) return null
349
+ const hit = names.get(userId)
350
+ if (hit && Date.now() - hit.at < 60_000) return hit.name
351
+ try {
352
+ const { db, reg } = useCms()
353
+ const [u] = await db.select({ name: reg.cmsUsers.name, email: reg.cmsUsers.email }).from(reg.cmsUsers).where(eq(reg.cmsUsers.id, userId))
354
+ const name = u ? displayName(u.name, u.email) || null : null
355
+ if (name) names.set(userId, { name, at: Date.now() })
356
+ if (names.size > 500) names.clear()
357
+ return name
358
+ }
359
+ catch {
360
+ return null
361
+ }
362
+ }
363
+
364
+ async function deliverChange(change: CmsChange): Promise<void> {
365
+ const topics = [`list:${change.entity}`, `entry:${change.entity}:${change.id}`]
366
+ const recipients = [...local.values()].filter((p) => topics.some((t) => p.topics.has(t)))
367
+ if (!recipients.length) return
368
+ // Only what a watcher needs to react — not the public flag or website addresses.
369
+ const body = { type: change.type, entity: change.entity, id: change.id, locale: change.locale, version: change.version, by: change.by, byName: await nameOf(change.by), at: change.at }
370
+ for (const p of recipients) {
371
+ // Looked up again: the person may have stopped watching while we fetched the name.
372
+ if (p.topics.has(topics[1]!)) {
373
+ send(p, { type: 'change', topic: topics[1], change: body })
374
+ continue
375
+ }
376
+ const list = p.topics.get(topics[0]!)
377
+ if (!list) continue
378
+ // Someone who may read only some rows learns that the list changed, not which row.
379
+ send(p, { type: 'change', topic: topics[0], change: list.rowFiltered ? { ...body, id: null, version: null, by: null, byName: null } : body })
380
+ }
381
+ }
382
+
383
+ function send(p: LocalPeer, msg: unknown): void {
384
+ try {
385
+ p.socket.send(JSON.stringify(msg))
386
+ }
387
+ catch { /* closing */ }
388
+ }
389
+
390
+ /** For tests and diagnostics. */
391
+ export function liveStats() {
392
+ return { sockets: local.size, remoteTopics: remote.size, running: !!timer }
393
+ }
@@ -0,0 +1,96 @@
1
+ import { and, eq, inArray } from 'tinyzoo'
2
+ import type { CmsEntity, CmsRegistry } from 'tinyzoo'
3
+
4
+ /**
5
+ * Content translations. Each entry's own row holds the default language; the
6
+ * `<entity>_locales` table holds one row per (entry, language) with the fields marked
7
+ * `.localized()`. Reading in a language overlays its non-empty values on the entry —
8
+ * anything untranslated falls back to the default language.
9
+ */
10
+
11
+ /** Is `locale` a translation (a configured language other than the default)? */
12
+ export function isTranslationLocale(reg: CmsRegistry, locale: string | null | undefined): locale is string {
13
+ return !!reg.locales && !!locale && locale !== reg.locales.default && reg.locales.locales.includes(locale)
14
+ }
15
+
16
+ /**
17
+ * The configured language closest to what a caller wrote: an exact one first, then the
18
+ * same ignoring case ("EN"), then the same language without its region ("en-US" → "en",
19
+ * or "pt-BR" → "pt"); undefined when the site has no such language.
20
+ */
21
+ export function closestLocale(configured: readonly string[], asked: string): string | undefined {
22
+ const wanted = asked.trim().replace(/_/g, '-').toLowerCase()
23
+ if (!wanted) return undefined
24
+ return configured.find((l) => l === asked)
25
+ ?? configured.find((l) => l.toLowerCase() === wanted)
26
+ ?? configured.find((l) => l.toLowerCase() === wanted.split('-')[0])
27
+ ?? configured.find((l) => l.toLowerCase().split('-')[0] === wanted.split('-')[0])
28
+ }
29
+
30
+ /**
31
+ * The language a request asks for (`?locale=`): null when content is single-language,
32
+ * the default when not given, the closest configured language for "en-US" or "EN" (a
33
+ * browser's language works as it is), 400 for a language the site does not have.
34
+ */
35
+ export function requestedLocale(event: any, reg: CmsRegistry): string | null {
36
+ if (!reg.locales) return null
37
+ const raw = getQuery(event).locale
38
+ if (raw === undefined || raw === '') return reg.locales.default
39
+ const locale = String(raw)
40
+ const found = closestLocale(reg.locales.locales, locale)
41
+ if (!found) {
42
+ throw createError({ statusCode: 400, statusMessage: `Unknown language "${locale.slice(0, 20)}" (available: ${reg.locales.locales.join(', ')})` })
43
+ }
44
+ return found
45
+ }
46
+
47
+ const isEmpty = (v: unknown): boolean => v === null || v === undefined || v === ''
48
+ || (Array.isArray(v) && v.length === 0)
49
+ || (typeof v === 'object' && v !== null && (v as any).type === 'doc'
50
+ // An empty editor saves one empty paragraph: still nothing written.
51
+ && !((v as any).content ?? []).some((n: any) => n?.type !== 'paragraph' || n.content?.length))
52
+ // A group with nothing filled in (an object whose values are all empty): untranslated.
53
+ || (typeof v === 'object' && v !== null && !Array.isArray(v) && (v as any).type !== 'doc' && Object.values(v).every((x) => typeof x !== 'boolean' && isEmpty(x)))
54
+
55
+ /** The translation rows of `ids` in `locale`, by parent id. */
56
+ export async function translationRows(db: any, e: CmsEntity, ids: string[], locale: string): Promise<Map<string, any>> {
57
+ const out = new Map<string, any>()
58
+ if (!e.localesTable || !ids.length) return out
59
+ const t = e.localesTable
60
+ const rows: any[] = await db.select().from(t).where(and(inArray(t.parentId, ids), eq(t.locale, locale)))
61
+ for (const r of rows) out.set(r.parentId, r)
62
+ return out
63
+ }
64
+
65
+ /** Overlay `locale`'s non-empty translated values on `rows` (in place): per-field fallback. */
66
+ export async function overlayTranslations(db: any, reg: CmsRegistry, e: CmsEntity, rows: any[], locale: string | null): Promise<void> {
67
+ if (!isTranslationLocale(reg, locale) || !e.localized?.length || !rows.length) return
68
+ const byParent = await translationRows(db, e, rows.map((r) => r.id), locale)
69
+ for (const row of rows) {
70
+ const tr = byParent.get(row.id)
71
+ if (tr) mergeTranslation(e, row, tr)
72
+ }
73
+ }
74
+
75
+ /** Put a translation row's non-empty values on `row` (in place): per-field fallback. */
76
+ export function mergeTranslation(e: CmsEntity, row: Record<string, any>, tr: Record<string, any>): Record<string, any> {
77
+ for (const key of e.localized) if (!isEmpty(tr[key])) row[key] = tr[key]
78
+ return row
79
+ }
80
+
81
+ /** For each id, the languages it is available in: the default plus every translation. */
82
+ export async function availableLocales(db: any, reg: CmsRegistry, e: CmsEntity, ids: string[]): Promise<Map<string, string[]>> {
83
+ const out = new Map<string, string[]>()
84
+ if (!reg.locales) return out
85
+ for (const id of ids) out.set(id, [reg.locales.default])
86
+ if (!e.localesTable || !ids.length) return out
87
+ const t = e.localesTable
88
+ const rows: any[] = await db.select({ parentId: t.parentId, locale: t.locale }).from(t).where(inArray(t.parentId, ids))
89
+ for (const r of rows) {
90
+ if (!reg.locales.locales.includes(r.locale)) continue
91
+ out.get(r.parentId)?.push(r.locale)
92
+ }
93
+ // Keep the configured order.
94
+ for (const [id, list] of out) out.set(id, reg.locales.locales.filter((l) => list.includes(l)))
95
+ return out
96
+ }
@@ -0,0 +1,28 @@
1
+ import { deliverMail } from '../mail/mailer'
2
+
3
+ export interface CmsMailMessage { to: string; subject: string; text: string; html: string; /** Where replies go (a form's sender). */ replyTo?: string }
4
+ export interface CmsMailConfig {
5
+ transport: 'smtp' | 'file' | 'log'
6
+ from: string
7
+ smtp: { host: string; port: number | string; secure: boolean; user: string; pass: string }
8
+ file: { dir: string }
9
+ }
10
+
11
+ export function cmsMailConfig(): CmsMailConfig {
12
+ const m = ((useRuntimeConfig().cms as any)?.mail ?? {}) as Partial<CmsMailConfig>
13
+ return {
14
+ transport: (m.transport as CmsMailConfig['transport']) || 'log',
15
+ from: m.from || 'CMS <no-reply@localhost>',
16
+ smtp: { host: '', port: 587, secure: false, user: '', pass: '', ...(m.smtp ?? {}) },
17
+ file: { dir: '.cms-mail', ...(m.file ?? {}) },
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Send an email through the configured transport. Throws on failure (callers decide).
23
+ * Returns whether it was really sent: false with the "log" transport (it is only
24
+ * written to the log), so an invitation can say the link has to be passed on.
25
+ */
26
+ export async function sendCmsMail(msg: CmsMailMessage): Promise<boolean> {
27
+ return deliverMail(msg, cmsMailConfig())
28
+ }