@cosmicdrift/kumiko-samples 0.406.0 → 0.410.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.
Files changed (257) hide show
  1. package/package.json +1 -1
  2. package/packages/bundled-features/package.json +31 -4
  3. package/packages/bundled-features/src/account-security/feature.ts +9 -1
  4. package/packages/bundled-features/src/admin-shell/changes.json +6 -0
  5. package/packages/bundled-features/src/admin-shell/feature.ts +4 -1
  6. package/packages/bundled-features/src/agent-tools/agent-manifest.ts +1 -0
  7. package/packages/bundled-features/src/agent-tools/changes.json +6 -0
  8. package/packages/bundled-features/src/agent-tools/tool-catalog.ts +1 -0
  9. package/packages/bundled-features/src/agent-tools/types.ts +4 -0
  10. package/packages/bundled-features/src/audit/event-classification.ts +1 -0
  11. package/packages/bundled-features/src/auth-email-password/bootstrap.ts +1 -0
  12. package/packages/bundled-features/src/auth-email-password/errors.ts +18 -66
  13. package/packages/bundled-features/src/auth-email-password/handlers/login-options.query.ts +1 -1
  14. package/packages/bundled-features/src/auth-email-password/handlers/self-registration-status.query.ts +1 -1
  15. package/packages/bundled-features/src/auth-email-password/web/auth-client.ts +30 -64
  16. package/packages/bundled-features/src/auth-email-password/web/auth-redirect.ts +2 -2
  17. package/packages/bundled-features/src/auth-email-password/web/confirm-account-unlock-screen.tsx +20 -66
  18. package/packages/bundled-features/src/auth-email-password/web/index.ts +1 -0
  19. package/packages/bundled-features/src/auth-email-password/web/read-path-param.ts +8 -0
  20. package/packages/bundled-features/src/auth-email-password/web/return-path.ts +3 -2
  21. package/packages/bundled-features/src/auth-email-password/web/token-confirm-screen.tsx +97 -0
  22. package/packages/bundled-features/src/auth-email-password/web/verify-email-screen.tsx +20 -68
  23. package/packages/bundled-features/src/auth-foundation/boot-checks.ts +22 -15
  24. package/packages/bundled-features/src/auth-foundation/boot-rules.ts +33 -0
  25. package/packages/bundled-features/src/auth-mfa/errors.ts +12 -42
  26. package/packages/bundled-features/src/auth-mfa/feature-exports.ts +6 -45
  27. package/packages/bundled-features/src/auth-mfa/feature.ts +8 -19
  28. package/packages/bundled-features/src/auth-mfa/web/mfa-client.ts +19 -33
  29. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-notify.ts +1 -0
  30. package/packages/bundled-features/src/billing-foundation/web/plan-price.ts +20 -7
  31. package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +1 -0
  32. package/packages/bundled-features/src/channel-in-app/changes.json +7 -0
  33. package/packages/bundled-features/src/channel-in-app/feature.ts +2 -0
  34. package/packages/bundled-features/src/channel-in-app/handlers/read-state-by-keys.query.ts +35 -0
  35. package/packages/bundled-features/src/channel-in-app/in-app-channel.ts +1 -0
  36. package/packages/bundled-features/src/channel-in-app/tables.ts +5 -1
  37. package/packages/bundled-features/src/compliance-profiles/handlers/sub-processors.query.ts +4 -1
  38. package/packages/bundled-features/src/contact/challenge.ts +41 -0
  39. package/packages/bundled-features/src/contact/changes.json +33 -0
  40. package/packages/bundled-features/src/contact/client/contact-client-source.ts +203 -0
  41. package/packages/bundled-features/src/contact/client/contact-website-client.ts +29 -0
  42. package/packages/bundled-features/src/contact/client/index.ts +9 -0
  43. package/packages/bundled-features/src/contact/entity.ts +168 -0
  44. package/packages/bundled-features/src/contact/feature.ts +174 -0
  45. package/packages/bundled-features/src/contact/handlers/access.ts +17 -0
  46. package/packages/bundled-features/src/contact/handlers/attempts.ts +76 -0
  47. package/packages/bundled-features/src/contact/handlers/challenge.query.ts +44 -0
  48. package/packages/bundled-features/src/contact/handlers/forward-request.write.ts +98 -0
  49. package/packages/bundled-features/src/contact/handlers/mark-done.write.ts +40 -0
  50. package/packages/bundled-features/src/contact/handlers/notify-reporter-in-app.write.ts +48 -0
  51. package/packages/bundled-features/src/contact/handlers/open-count.query.ts +15 -0
  52. package/packages/bundled-features/src/contact/handlers/open-count.ts +17 -0
  53. package/packages/bundled-features/src/contact/handlers/replies.query.ts +29 -0
  54. package/packages/bundled-features/src/contact/handlers/reply.write.ts +197 -0
  55. package/packages/bundled-features/src/contact/handlers/report.write.ts +126 -0
  56. package/packages/bundled-features/src/contact/handlers/request-delivery.query.ts +143 -0
  57. package/packages/bundled-features/src/contact/handlers/request-detail.query.ts +50 -0
  58. package/packages/bundled-features/src/contact/handlers/request-list.query.ts +14 -0
  59. package/packages/bundled-features/src/contact/handlers/send-reply-mail.write.ts +124 -0
  60. package/packages/bundled-features/src/contact/handlers/send-test.write.ts +45 -0
  61. package/packages/bundled-features/src/contact/handlers/submit.write.ts +194 -0
  62. package/packages/bundled-features/src/contact/handlers/take-over-uploads.ts +47 -0
  63. package/packages/bundled-features/src/contact/i18n.ts +192 -0
  64. package/packages/bundled-features/src/contact/index.ts +29 -0
  65. package/packages/bundled-features/src/contact/lib.ts +176 -0
  66. package/packages/bundled-features/src/contact/mail.ts +401 -0
  67. package/packages/bundled-features/src/contact/options.ts +95 -0
  68. package/packages/bundled-features/src/contact/panels.ts +28 -0
  69. package/packages/bundled-features/src/contact/payloads.ts +106 -0
  70. package/packages/bundled-features/src/contact/pow.ts +22 -0
  71. package/packages/bundled-features/src/contact/screens.ts +507 -0
  72. package/packages/bundled-features/src/contact/subject.ts +13 -0
  73. package/packages/bundled-features/src/contact/user-agent.ts +39 -0
  74. package/packages/bundled-features/src/contact/user-data-hooks.ts +180 -0
  75. package/packages/bundled-features/src/contact/web/constants.ts +160 -0
  76. package/packages/bundled-features/src/contact/web/index.ts +29 -0
  77. package/packages/bundled-features/src/crypto-shredding/handlers/forget-subject.write.ts +5 -16
  78. package/packages/bundled-features/src/data-retention/changes.json +6 -0
  79. package/packages/bundled-features/src/data-retention/feature.ts +16 -2
  80. package/packages/bundled-features/src/data-retention/index.ts +5 -0
  81. package/packages/bundled-features/src/data-retention/run-retention-cleanup.ts +138 -41
  82. package/packages/bundled-features/src/data-retention/tenant-destruction-hold.ts +196 -0
  83. package/packages/bundled-features/src/delivery/address-opt-out.ts +1 -0
  84. package/packages/bundled-features/src/delivery/changes.json +7 -0
  85. package/packages/bundled-features/src/delivery/channel-context.ts +2 -0
  86. package/packages/bundled-features/src/delivery/delivery-service.ts +29 -5
  87. package/packages/bundled-features/src/delivery/events.ts +2 -0
  88. package/packages/bundled-features/src/delivery/feature.ts +3 -2
  89. package/packages/bundled-features/src/delivery/handlers/attempts-by-keys.query.ts +55 -0
  90. package/packages/bundled-features/src/delivery/handlers/log.query.ts +1 -1
  91. package/packages/bundled-features/src/delivery/jobs.ts +5 -0
  92. package/packages/bundled-features/src/delivery/tables.ts +13 -2
  93. package/packages/bundled-features/src/delivery/types.ts +3 -0
  94. package/packages/bundled-features/src/delivery/unsubscribe-page.ts +2 -0
  95. package/packages/bundled-features/src/delivery/web/delivery-channel-cell.tsx +1 -1
  96. package/packages/bundled-features/src/delivery/web/delivery-error-cell.tsx +1 -1
  97. package/packages/bundled-features/src/delivery/web/delivery-status-cell.tsx +1 -1
  98. package/packages/bundled-features/src/delivery/web/delivery-time-cell.tsx +3 -2
  99. package/packages/bundled-features/src/delivery/web/delivery-type-cell.tsx +1 -1
  100. package/packages/bundled-features/src/delivery/web/public-names.ts +1 -0
  101. package/packages/bundled-features/src/document-ingest-foundation/boot-checks.ts +14 -9
  102. package/packages/bundled-features/src/document-ingest-foundation/boot-rules.ts +22 -0
  103. package/packages/bundled-features/src/document-ingest-foundation/changes.json +6 -0
  104. package/packages/bundled-features/src/document-ingest-foundation/events.ts +17 -0
  105. package/packages/bundled-features/src/document-ingest-foundation/feature.ts +39 -1
  106. package/packages/bundled-features/src/document-ingest-foundation/index.ts +10 -0
  107. package/packages/bundled-features/src/document-ingest-foundation/report-ingest-progress.ts +43 -0
  108. package/packages/bundled-features/src/document-ingest-foundation/tables.ts +43 -0
  109. package/packages/bundled-features/src/file-derivatives/handlers/public-variant-by-file-ref.query.ts +1 -1
  110. package/packages/bundled-features/src/file-derivatives/handlers/public-variant.query.ts +1 -1
  111. package/packages/bundled-features/src/files-tenant-data/changes.json +6 -0
  112. package/packages/bundled-features/src/files-tenant-data/hooks.ts +4 -1
  113. package/packages/bundled-features/src/folders/web/folder-manager.tsx +8 -9
  114. package/packages/bundled-features/src/folders/web/folder-section.tsx +1 -1
  115. package/packages/bundled-features/src/form-draft-user-data/index.ts +2 -0
  116. package/packages/bundled-features/src/inbound-mail-foundation/changes.json +13 -0
  117. package/packages/bundled-features/src/inbound-mail-foundation/entities.ts +2 -0
  118. package/packages/bundled-features/src/inbound-mail-foundation/events.ts +2 -0
  119. package/packages/bundled-features/src/inbound-mail-foundation/feature.ts +6 -6
  120. package/packages/bundled-features/src/inbound-mail-foundation/handlers/attach-message-content.write.ts +7 -0
  121. package/packages/bundled-features/src/inbound-mail-foundation/handlers/ingest-message.write.ts +6 -0
  122. package/packages/bundled-features/src/inbound-mail-foundation/handlers/list-messages.query.ts +6 -2
  123. package/packages/bundled-features/src/inbound-mail-foundation/handlers/message-content.query.ts +3 -0
  124. package/packages/bundled-features/src/inbound-mail-foundation/handlers/message-content.ts +19 -0
  125. package/packages/bundled-features/src/inbound-mail-foundation/projection.ts +2 -0
  126. package/packages/bundled-features/src/inbound-mail-foundation/sanitize-inbound-html.ts +150 -0
  127. package/packages/bundled-features/src/inbound-mail-foundation/types.ts +1 -0
  128. package/packages/bundled-features/src/jobs/job-run-logger.ts +24 -25
  129. package/packages/bundled-features/src/ledger/handlers/reports.query.ts +46 -31
  130. package/packages/bundled-features/src/ledger/recurring.ts +1 -0
  131. package/packages/bundled-features/src/managed-pages/handlers/branding.query.ts +1 -1
  132. package/packages/bundled-features/src/managed-pages/handlers/by-slug.query.ts +1 -1
  133. package/packages/bundled-features/src/managed-pages/handlers/by-tenant-published.query.ts +1 -1
  134. package/packages/bundled-features/src/notes-history/web/notes-section.tsx +3 -1
  135. package/packages/bundled-features/src/personal-access-tokens/feature.ts +9 -17
  136. package/packages/bundled-features/src/personal-access-tokens/handlers/create.write.ts +2 -7
  137. package/packages/bundled-features/src/secrets/changes.json +6 -0
  138. package/packages/bundled-features/src/secrets/handlers/delete.write.ts +8 -4
  139. package/packages/bundled-features/src/secrets/handlers/list.query.ts +6 -3
  140. package/packages/bundled-features/src/secrets/handlers/set.write.ts +9 -4
  141. package/packages/bundled-features/src/secrets/secret-scope.ts +33 -0
  142. package/packages/bundled-features/src/seo/handlers/seo-config.query.ts +1 -1
  143. package/packages/bundled-features/src/sessions/changes.json +6 -0
  144. package/packages/bundled-features/src/sessions/feature.ts +21 -18
  145. package/packages/bundled-features/src/sessions/handlers/last-activity-probe.query.ts +53 -0
  146. package/packages/bundled-features/src/sessions/handlers/last-activity-probe.route.ts +39 -0
  147. package/packages/bundled-features/src/sessions/screens.ts +1 -0
  148. package/packages/bundled-features/src/sessions/session-callbacks.ts +13 -12
  149. package/packages/bundled-features/src/sessions/web/i18n.ts +4 -1
  150. package/packages/bundled-features/src/shared/assert-billing-live.ts +14 -0
  151. package/packages/bundled-features/src/shared/collect-extension-options.ts +12 -0
  152. package/packages/bundled-features/src/shared/index.ts +16 -0
  153. package/packages/bundled-features/src/shared/late-bound-slot.ts +19 -0
  154. package/packages/bundled-features/src/shared/mail-locale-templates.ts +7 -1
  155. package/packages/bundled-features/src/shared/price-env-shape.ts +44 -0
  156. package/packages/bundled-features/src/shared/purge-row-file-refs.ts +1 -1
  157. package/packages/bundled-features/src/shared/read-feature-export-fn.ts +16 -0
  158. package/packages/bundled-features/src/shared/signup-handover.ts +3 -4
  159. package/packages/bundled-features/src/shared/tenant-destruction-hold.ts +49 -0
  160. package/packages/bundled-features/src/shared/unprocessable.ts +19 -0
  161. package/packages/bundled-features/src/shared/web/index.ts +3 -0
  162. package/packages/bundled-features/src/subscription-paddle/checkout-config.query.ts +1 -1
  163. package/packages/bundled-features/src/subscription-paddle/feature.ts +7 -15
  164. package/packages/bundled-features/src/subscription-paddle/runtime.ts +4 -7
  165. package/packages/bundled-features/src/subscription-stripe/feature.ts +7 -15
  166. package/packages/bundled-features/src/subscription-stripe/runtime.ts +4 -7
  167. package/packages/bundled-features/src/tags/entity.ts +1 -0
  168. package/packages/bundled-features/src/tags/web/tag-chip.tsx +14 -0
  169. package/packages/bundled-features/src/tags/web/tag-filter.tsx +4 -5
  170. package/packages/bundled-features/src/tags/web/tag-manager.tsx +1 -1
  171. package/packages/bundled-features/src/tags/web/tag-section.tsx +3 -6
  172. package/packages/bundled-features/src/tags/web/tags-cell.tsx +4 -5
  173. package/packages/bundled-features/src/template-resolver/compile-document-body.ts +27 -15
  174. package/packages/bundled-features/src/template-resolver/handlers/by-slug.query.ts +1 -1
  175. package/packages/bundled-features/src/template-resolver/handlers/by-tenant.query.ts +12 -9
  176. package/packages/bundled-features/src/template-resolver/handlers/discard-draft.write.ts +1 -0
  177. package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +1 -0
  178. package/packages/bundled-features/src/template-resolver/handlers/send-test.write.ts +5 -5
  179. package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +3 -0
  180. package/packages/bundled-features/src/template-resolver/linked-resources.ts +11 -6
  181. package/packages/bundled-features/src/template-resolver/store-template-resource.ts +15 -12
  182. package/packages/bundled-features/src/template-resolver/web/document-images.ts +6 -6
  183. package/packages/bundled-features/src/template-resolver/web/document-insert-fields.tsx +41 -36
  184. package/packages/bundled-features/src/template-resolver/web/document-schema.ts +11 -12
  185. package/packages/bundled-features/src/template-resolver/web/tiptap-attribute-parse-rule.ts +19 -0
  186. package/packages/bundled-features/src/tenant/i18n.ts +3 -0
  187. package/packages/bundled-features/src/tenant/schema/tenant.ts +1 -0
  188. package/packages/bundled-features/src/tenant/web/member-roles-cell.tsx +1 -1
  189. package/packages/bundled-features/src/tenant/web/member-status-cell.tsx +1 -1
  190. package/packages/bundled-features/src/tenant-inactivity/changes.json +8 -0
  191. package/packages/bundled-features/src/tenant-inactivity/check-inactive-tenants.ts +260 -0
  192. package/packages/bundled-features/src/tenant-inactivity/constants.ts +12 -0
  193. package/packages/bundled-features/src/tenant-inactivity/email-templates.ts +24 -0
  194. package/packages/bundled-features/src/tenant-inactivity/feature.ts +66 -0
  195. package/packages/bundled-features/src/tenant-inactivity/handlers/inactivity-state.query.ts +49 -0
  196. package/packages/bundled-features/src/tenant-inactivity/handlers/keep-account.write.ts +59 -0
  197. package/packages/bundled-features/src/tenant-inactivity/inactivity-plan.ts +187 -0
  198. package/packages/bundled-features/src/tenant-inactivity/index.ts +5 -0
  199. package/packages/bundled-features/src/tenant-inactivity/keep-token.ts +44 -0
  200. package/packages/bundled-features/src/tenant-inactivity/mail-notifications.ts +62 -0
  201. package/packages/bundled-features/src/tenant-inactivity/options.ts +38 -0
  202. package/packages/bundled-features/src/tenant-inactivity/remote-activity.ts +32 -0
  203. package/packages/bundled-features/src/tenant-inactivity/state-store.ts +71 -0
  204. package/packages/bundled-features/src/tenant-inactivity/state-table.ts +50 -0
  205. package/packages/bundled-features/src/tenant-lifecycle/boot-checks.ts +8 -6
  206. package/packages/bundled-features/src/tenant-lifecycle/boot-rules.ts +16 -0
  207. package/packages/bundled-features/src/tenant-lifecycle/changes.json +18 -0
  208. package/packages/bundled-features/src/tenant-lifecycle/feature.ts +24 -1
  209. package/packages/bundled-features/src/tenant-lifecycle/handlers/request-destruction.write.ts +12 -87
  210. package/packages/bundled-features/src/tenant-lifecycle/index.ts +4 -0
  211. package/packages/bundled-features/src/tenant-lifecycle/request-tenant-destruction.ts +108 -0
  212. package/packages/bundled-features/src/tenant-lifecycle/retention-config-destroy-hooks.ts +62 -0
  213. package/packages/bundled-features/src/tenant-lifecycle/run-tenant-destroy.ts +58 -5
  214. package/packages/bundled-features/src/tenant-lifecycle/stages.ts +31 -2
  215. package/packages/bundled-features/src/tenant-lifecycle/testing.ts +1 -1
  216. package/packages/bundled-features/src/tier-engine/caps-for-tier-from-map.ts +16 -0
  217. package/packages/bundled-features/src/tier-engine/changes.json +6 -0
  218. package/packages/bundled-features/src/tier-engine/compose-app.ts +21 -20
  219. package/packages/bundled-features/src/tier-engine/index.ts +1 -0
  220. package/packages/bundled-features/src/user-data-rights/feature.ts +2 -0
  221. package/packages/bundled-features/src/user-data-rights/handlers/confirm-deletion-by-token.write.ts +4 -1
  222. package/packages/bundled-features/src/user-data-rights/handlers/download-by-token.query.ts +1 -0
  223. package/packages/bundled-features/src/user-data-rights/handlers/request-deletion-by-email.write.ts +4 -1
  224. package/packages/bundled-features/src/user-data-rights/run-forget-cleanup.ts +2 -0
  225. package/packages/bundled-features/src/user-data-rights/web/privacy-center-screen.tsx +1 -1
  226. package/packages/bundled-features/src/user-data-rights/zip-path.ts +1 -0
  227. package/packages/bundled-features/src/user-data-rights-defaults/hooks/config-value.userdata-hook.ts +8 -27
  228. package/packages/bundled-features/src/user-data-rights-defaults/hooks/delivery-attempt.userdata-hook.ts +11 -8
  229. package/packages/bundled-features/src/user-data-rights-defaults/hooks/forget-own-rows-delete-hook.ts +42 -0
  230. package/packages/bundled-features/src/user-data-rights-defaults/hooks/notification-preference.userdata-hook.ts +8 -27
  231. package/packages/bundled-features/src/user-profile/handlers/change-email.write.ts +2 -6
  232. package/packages/bundled-features/src/user-profile/web/profile-screen.tsx +2 -3
  233. package/packages/bundled-features/src/waitlist/changes.json +6 -0
  234. package/packages/bundled-features/src/waitlist/handlers/submit.write.ts +1 -2
  235. package/samples/apps/styleguide/src/features/widgets/i18n.ts +6 -0
  236. package/samples/apps/styleguide/src/features/widgets/web/InboxReaderExamples.tsx +158 -0
  237. package/samples/apps/styleguide/src/features/widgets/web/Widgets.tsx +2 -0
  238. package/samples/apps/styleguide/src/features/widgets/web/sample-pdf.ts +28 -0
  239. package/samples/apps/use-all-bundled/feature-manifest.json +113 -2
  240. package/samples/apps/use-all-bundled/kumiko/migrations/0063_contact.rebuild.json +4 -0
  241. package/samples/apps/use-all-bundled/package.json +1 -0
  242. package/samples/apps/use-all-bundled/src/app/client.tsx +3 -0
  243. package/samples/apps/use-all-bundled/src/app/screens-feature.ts +46 -2
  244. package/samples/apps/use-all-bundled/src/run-config.ts +23 -1
  245. package/samples/recipes/anonymous-access/README.md +2 -0
  246. package/samples/recipes/anonymous-access/src/feature.ts +7 -1
  247. package/samples/recipes/contact/README.md +52 -0
  248. package/samples/recipes/contact/package.json +12 -0
  249. package/samples/recipes/contact/src/feature.ts +27 -0
  250. package/samples/recipes/delivery-notifications/src/feature.ts +3 -0
  251. package/samples/recipes/record-detail-layout/src/feature.ts +9 -1
  252. package/samples/recipes/search/README.md +2 -0
  253. package/samples/recipes/search/src/feature.ts +27 -2
  254. package/samples/recipes/secrets-demo/README.md +1 -0
  255. package/samples/recipes/secrets-demo/src/feature.ts +56 -0
  256. package/samples/recipes/tenant-inactivity/README.md +21 -0
  257. package/samples/recipes/tenant-inactivity/package.json +12 -0
@@ -0,0 +1,176 @@
1
+ import {
2
+ createEventStoreExecutor,
3
+ fetchOne,
4
+ selectMany,
5
+ type TenantDb,
6
+ } from "@cosmicdrift/kumiko-framework/db";
7
+ import {
8
+ type AppContext,
9
+ access,
10
+ createSystemUser,
11
+ type HandlerContext,
12
+ type SessionUser,
13
+ SYSTEM_TENANT_ID,
14
+ } from "@cosmicdrift/kumiko-framework/engine";
15
+ import { InternalError } from "@cosmicdrift/kumiko-framework/errors";
16
+ import { propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
17
+ import { decryptStoredPii } from "../shared/index.ts";
18
+ import { tenantTable } from "../tenant/index.ts";
19
+ import { TierEngineQueries } from "../tier-engine/index.ts";
20
+ import { USER_STATUS, userTable } from "../user/index.ts";
21
+ import {
22
+ contactReplyEntity,
23
+ contactReplyTable,
24
+ contactRequestEntity,
25
+ contactRequestTable,
26
+ } from "./entity.ts";
27
+ import {
28
+ CONTACT_REPLY_CHANNELS,
29
+ CONTACT_REPLY_ENTITY,
30
+ CONTACT_REQUEST_ENTITY,
31
+ ContactErrors,
32
+ type ContactPhoneMode,
33
+ type ContactReplyChannels,
34
+ } from "./web/constants.ts";
35
+
36
+ export const IN_APP_FEATURE = "channel-in-app";
37
+ export const TELEGRAM_FEATURE = "channel-telegram";
38
+
39
+ export const CONTACT_CROSS_TENANT_REASON = "contact is platform-wide (systemScope)";
40
+
41
+ export const SYSTEM_ADMIN_ROLE = "SystemAdmin";
42
+ const PII_DECRYPT_REASON = "contact:user-lookup";
43
+
44
+ export const adminAccess = { roles: access.systemAdmin } as const;
45
+ export const internalAccess = { roles: access.system } as const;
46
+
47
+ export const requestExecutor = createEventStoreExecutor(contactRequestTable, contactRequestEntity, {
48
+ entityName: CONTACT_REQUEST_ENTITY,
49
+ });
50
+
51
+ export const replyExecutor = createEventStoreExecutor(contactReplyTable, contactReplyEntity, {
52
+ entityName: CONTACT_REPLY_ENTITY,
53
+ });
54
+
55
+ const MAX_REPLIES_PER_REQUEST = 200;
56
+
57
+ export async function listRepliesOf(db: TenantDb, requestId: string) {
58
+ const found = await replyExecutor.list(
59
+ {
60
+ filter: { field: "requestId", op: "eq", value: requestId },
61
+ sort: "sentAt",
62
+ sortDirection: "asc",
63
+ limit: MAX_REPLIES_PER_REQUEST,
64
+ },
65
+ platformActor(),
66
+ db,
67
+ );
68
+ return found.rows;
69
+ }
70
+
71
+ export function normalizeEmail(email: string): string {
72
+ return email.toLowerCase();
73
+ }
74
+
75
+ export function contactDb(ctx: Pick<AppContext, "systemDb">): TenantDb {
76
+ if (!ctx.systemDb) {
77
+ throw new InternalError({
78
+ message: "contact requires ctx.systemDb — is r.systemScope() set on the feature?",
79
+ });
80
+ }
81
+ return ctx.systemDb.acknowledgeCrossTenant(CONTACT_CROSS_TENANT_REASON);
82
+ }
83
+
84
+ // Requests and replies are systemStream rows, so every write targets the system tenant's stream.
85
+ export function platformActor(): SessionUser {
86
+ return createSystemUser(SYSTEM_TENANT_ID);
87
+ }
88
+
89
+ export async function listSystemAdminIds(db: TenantDb): Promise<readonly string[]> {
90
+ const rows = await selectMany<{ readonly id: string }>(
91
+ db,
92
+ userTable,
93
+ { isDeleted: false, status: USER_STATUS.Active, roles: SYSTEM_ADMIN_ROLE },
94
+ { fields: ["id"], orderBy: { col: "id" } },
95
+ );
96
+ return rows.map((r) => r.id);
97
+ }
98
+
99
+ export type UserLabel = { readonly name: string; readonly email: string | null };
100
+
101
+ type UserLabelRow = { readonly displayName: string; readonly email: string };
102
+
103
+ export async function loadUserLabel(db: TenantDb, userId: string): Promise<UserLabel | null> {
104
+ const row = await fetchOne<UserLabelRow>(db, userTable, { id: userId, isDeleted: false });
105
+ if (!row) return null;
106
+ const email = await decryptStoredPii(row.email, "email", PII_DECRYPT_REASON);
107
+ const name = await decryptStoredPii(row.displayName, "displayName", PII_DECRYPT_REASON);
108
+ return { name, email };
109
+ }
110
+
111
+ export async function sendBestEffort(
112
+ ctx: Pick<HandlerContext, "notify" | "obs">,
113
+ label: string,
114
+ send: (notify: NonNullable<HandlerContext["notify"]>) => Promise<unknown>,
115
+ ): Promise<void> {
116
+ try {
117
+ if (!ctx.notify) {
118
+ throw new InternalError({ message: "ctx.notify unavailable — mount the delivery feature" });
119
+ }
120
+ await send(ctx.notify);
121
+ } catch (err) {
122
+ ctx.obs.log?.warn(`[contact] ${label} failed`, {
123
+ error: err instanceof Error ? err.message : String(err),
124
+ });
125
+ }
126
+ }
127
+
128
+ export async function pickReplyChannels(
129
+ ctx: Pick<HandlerContext, "hasFeature">,
130
+ reporterUserId: string | null | undefined,
131
+ ): Promise<ContactReplyChannels> {
132
+ const hasInApp = reporterUserId && (await ctx.hasFeature(IN_APP_FEATURE));
133
+ return hasInApp ? CONTACT_REPLY_CHANNELS.EmailAndInApp : CONTACT_REPLY_CHANNELS.Email;
134
+ }
135
+
136
+ export function phoneProblem(
137
+ mode: ContactPhoneMode,
138
+ phone: string | undefined,
139
+ ): (typeof ContactErrors)[keyof typeof ContactErrors] | null {
140
+ const present = phone !== undefined && phone !== "";
141
+ if (mode === "off") return present ? ContactErrors.phoneNotAllowed : null;
142
+ if (mode === "required") return present ? null : ContactErrors.phoneRequired;
143
+ return null;
144
+ }
145
+
146
+ export async function loadTenantName(db: TenantDb, tenantId: string): Promise<string | null> {
147
+ const row = await fetchOne<{ name: string }>(db, tenantTable, { id: tenantId });
148
+ return row ? decryptStoredPii(row.name, "name", PII_DECRYPT_REASON) : null;
149
+ }
150
+
151
+ // The tier engine only knows tier ids, so the operators' label is the id with a capital.
152
+ function planDisplayName(tier: string): string {
153
+ return tier.charAt(0).toUpperCase() + tier.slice(1);
154
+ }
155
+
156
+ // The plan is context for the operators, so a missing tier-engine or a failed lookup leaves it empty.
157
+ export async function loadTenantPlan(
158
+ ctx: Pick<HandlerContext, "dispatch" | "registry" | "obs">,
159
+ tenantId: string,
160
+ ): Promise<string | null> {
161
+ if (ctx.registry.getQueryHandler(TierEngineQueries.getTenantTier) === undefined) return null;
162
+ try {
163
+ const assignment = await ctx.dispatch.queryAs(
164
+ createSystemUser(SYSTEM_TENANT_ID, [SYSTEM_ADMIN_ROLE]),
165
+ TierEngineQueries.getTenantTier,
166
+ { tenantId },
167
+ );
168
+ const tier = propertyOf(assignment, "tier");
169
+ return typeof tier === "string" && tier !== "" ? planDisplayName(tier) : null;
170
+ } catch (err) {
171
+ ctx.obs.log?.warn("[contact] tenant plan lookup failed", {
172
+ error: err instanceof Error ? err.message : String(err),
173
+ });
174
+ return null;
175
+ }
176
+ }
@@ -0,0 +1,401 @@
1
+ import {
2
+ type NotificationLocalizedTemplate,
3
+ type TemplateVariables,
4
+ templateVariable,
5
+ } from "@cosmicdrift/kumiko-framework/engine";
6
+ import {
7
+ mailHeading,
8
+ mailParagraph,
9
+ mailParagraphWithLink,
10
+ mailRepeatedRows,
11
+ } from "../shared/index.ts";
12
+ import { CONTACT_KIND, type ContactKind } from "./web/constants.ts";
13
+
14
+ export type ContactMailLocale = "en" | "de";
15
+
16
+ const QUOTE_MAX_CHARS = 280;
17
+
18
+ function languageOf(locale: string | undefined): string {
19
+ return (locale ?? "").split("-")[0]?.toLowerCase() ?? "";
20
+ }
21
+
22
+ export function resolveContactMailLocale(locale: string | undefined): ContactMailLocale {
23
+ return languageOf(locale) === "de" ? "de" : "en";
24
+ }
25
+
26
+ export function truncateText(text: string, maxChars: number): string {
27
+ const chars = [...text];
28
+ return chars.length <= maxChars
29
+ ? text
30
+ : `${chars
31
+ .slice(0, maxChars - 1)
32
+ .join("")
33
+ .trimEnd()}…`;
34
+ }
35
+
36
+ const KIND_LABELS: Readonly<Record<ContactMailLocale, Readonly<Record<ContactKind, string>>>> = {
37
+ en: {
38
+ [CONTACT_KIND.Question]: "Question",
39
+ [CONTACT_KIND.Contact]: "Contact request",
40
+ [CONTACT_KIND.Bug]: "Problem",
41
+ [CONTACT_KIND.Wish]: "Wish",
42
+ },
43
+ de: {
44
+ [CONTACT_KIND.Question]: "Frage",
45
+ [CONTACT_KIND.Contact]: "Kontaktwunsch",
46
+ [CONTACT_KIND.Bug]: "Problem",
47
+ [CONTACT_KIND.Wish]: "Wunsch",
48
+ },
49
+ };
50
+
51
+ export function contactKindLabel(kind: string, locale: string | undefined): string {
52
+ const labels: Readonly<Record<string, string>> = KIND_LABELS[resolveContactMailLocale(locale)];
53
+ return labels[kind] ?? kind;
54
+ }
55
+
56
+ export function formatContactDate(atEpochMs: number, locale: string | undefined): string {
57
+ return new Intl.DateTimeFormat(resolveContactMailLocale(locale), {
58
+ dateStyle: "long",
59
+ timeZone: "UTC",
60
+ }).format(atEpochMs);
61
+ }
62
+
63
+ function toParagraphRows(message: string): readonly { readonly text: string }[] {
64
+ return message
65
+ .split(/\r?\n/)
66
+ .map((line) => line.trim())
67
+ .filter((line) => line !== "")
68
+ .map((text) => ({ text }));
69
+ }
70
+
71
+ // The notification names only the kind, the app and the inbox link: requests carry personal
72
+ // data, and mail, in-app and Telegram are not places that can be shredded or recalled.
73
+ type NewRequestStrings = {
74
+ readonly header: string;
75
+ readonly linkIntro: string;
76
+ readonly linkLabel: string;
77
+ readonly title: Readonly<Record<ContactKind, (appName: string) => string>>;
78
+ };
79
+
80
+ const NEW_REQUEST_STRINGS: Readonly<Record<ContactMailLocale, NewRequestStrings>> = {
81
+ en: {
82
+ header: "New request",
83
+ linkIntro: "Open it in the admin:",
84
+ linkLabel: "Show request",
85
+ title: {
86
+ [CONTACT_KIND.Question]: (app) => `New question on ${app}`,
87
+ [CONTACT_KIND.Contact]: (app) => `New contact request on ${app}`,
88
+ [CONTACT_KIND.Bug]: (app) => `New problem report on ${app}`,
89
+ [CONTACT_KIND.Wish]: (app) => `New wish on ${app}`,
90
+ },
91
+ },
92
+ de: {
93
+ header: "Neue Meldung",
94
+ linkIntro: "Im Admin öffnen:",
95
+ linkLabel: "Meldung anzeigen",
96
+ title: {
97
+ [CONTACT_KIND.Question]: (app) => `Neue Frage auf ${app}`,
98
+ [CONTACT_KIND.Contact]: (app) => `Neuer Kontaktwunsch auf ${app}`,
99
+ [CONTACT_KIND.Bug]: (app) => `Neue Problemmeldung auf ${app}`,
100
+ [CONTACT_KIND.Wish]: (app) => `Neuer Wunsch auf ${app}`,
101
+ },
102
+ },
103
+ };
104
+
105
+ export const CONTACT_NEW_REQUEST_VARIABLES: TemplateVariables = {
106
+ subject: templateVariable.text({ required: true }),
107
+ intro: templateVariable.text({ required: true }),
108
+ requestUrl: templateVariable.link(),
109
+ };
110
+
111
+ export type NewRequestMailArgs = {
112
+ readonly kind: string;
113
+ readonly appName: string;
114
+ readonly link: string | undefined;
115
+ readonly locale: string | undefined;
116
+ };
117
+
118
+ export function contactNewRequestVariables(args: NewRequestMailArgs) {
119
+ const titles: Readonly<Record<string, (appName: string) => string>> =
120
+ NEW_REQUEST_STRINGS[resolveContactMailLocale(args.locale)].title;
121
+ const title =
122
+ (titles[args.kind] ?? titles[CONTACT_KIND.Question])?.(args.appName) ?? args.appName;
123
+ return {
124
+ subject: title,
125
+ intro: title,
126
+ ...(args.link !== undefined && { requestUrl: args.link }),
127
+ inAppTitle: title,
128
+ inAppBody: args.appName,
129
+ telegramTitle: title,
130
+ telegramBody: args.link ?? args.appName,
131
+ };
132
+ }
133
+
134
+ export function contactNewRequestTemplate(): NotificationLocalizedTemplate {
135
+ return Object.fromEntries(
136
+ Object.entries(NEW_REQUEST_STRINGS).map(([locale, t]) => [
137
+ locale,
138
+ {
139
+ body: [
140
+ mailHeading(t.header),
141
+ mailParagraph("{intro}"),
142
+ mailParagraphWithLink(t.linkIntro, t.linkLabel, "requestUrl", { showIf: "requestUrl" }),
143
+ ].join(""),
144
+ meta: { subject: "{{subject}}", category: "transactional" as const },
145
+ },
146
+ ]),
147
+ );
148
+ }
149
+
150
+ type ConfirmationStrings = {
151
+ readonly subject: string;
152
+ readonly header: string;
153
+ readonly intro: (appName: string) => string;
154
+ readonly quoteIntro: string;
155
+ };
156
+
157
+ const CONFIRMATION_STRINGS: Readonly<Record<ContactMailLocale, ConfirmationStrings>> = {
158
+ en: {
159
+ subject: "Your message to {{appName}}",
160
+ header: "Thanks for your message",
161
+ intro: (appName) =>
162
+ `We received your message to the ${appName} team and will get back to you personally. Here is a copy:`,
163
+ quoteIntro: "Your message:",
164
+ },
165
+ de: {
166
+ subject: "Deine Nachricht an {{appName}}",
167
+ header: "Danke für deine Nachricht",
168
+ intro: (appName) =>
169
+ `Deine Nachricht ist beim ${appName}-Team angekommen, wir melden uns persönlich bei dir. Hier ist eine Kopie:`,
170
+ quoteIntro: "Deine Nachricht:",
171
+ },
172
+ };
173
+
174
+ type AcknowledgementStrings = {
175
+ readonly subject: string;
176
+ readonly header: string;
177
+ readonly intro: (appName: string) => string;
178
+ };
179
+
180
+ const ACKNOWLEDGEMENT_STRINGS: Readonly<Record<ContactMailLocale, AcknowledgementStrings>> = {
181
+ en: {
182
+ subject: "Your message to {{appName}}",
183
+ header: "Thanks for your message",
184
+ intro: (appName) =>
185
+ `We received your message to the ${appName} team and will get back to you personally.`,
186
+ },
187
+ de: {
188
+ subject: "Deine Nachricht an {{appName}}",
189
+ header: "Danke für deine Nachricht",
190
+ intro: (appName) =>
191
+ `Deine Nachricht ist beim ${appName}-Team angekommen, wir melden uns persönlich bei dir.`,
192
+ },
193
+ };
194
+
195
+ export const CONTACT_CONFIRMATION_VARIABLES: TemplateVariables = {
196
+ appName: templateVariable.text({ required: true }),
197
+ intro: templateVariable.text({ required: true }),
198
+ quoteIntro: templateVariable.text({ required: true }),
199
+ paragraphs: templateVariable.list(
200
+ { text: templateVariable.text({ required: true }) },
201
+ { required: true },
202
+ ),
203
+ };
204
+
205
+ export function contactConfirmationVariables(args: {
206
+ readonly appName: string;
207
+ readonly message: string;
208
+ readonly locale: string | undefined;
209
+ }) {
210
+ const t = CONFIRMATION_STRINGS[resolveContactMailLocale(args.locale)];
211
+ return {
212
+ appName: args.appName,
213
+ intro: t.intro(args.appName),
214
+ quoteIntro: t.quoteIntro,
215
+ paragraphs: toParagraphRows(args.message),
216
+ };
217
+ }
218
+
219
+ // Website senders are unverified addresses: the mail must not echo anything they typed, or the
220
+ // form becomes a mail relay. Only the signed-in admin report gets the copy above.
221
+ export const CONTACT_ACKNOWLEDGEMENT_VARIABLES: TemplateVariables = {
222
+ appName: templateVariable.text({ required: true }),
223
+ intro: templateVariable.text({ required: true }),
224
+ };
225
+
226
+ export function contactAcknowledgementVariables(args: {
227
+ readonly appName: string;
228
+ readonly locale: string | undefined;
229
+ }) {
230
+ const t = ACKNOWLEDGEMENT_STRINGS[resolveContactMailLocale(args.locale)];
231
+ return { appName: args.appName, intro: t.intro(args.appName) };
232
+ }
233
+
234
+ export function contactAcknowledgementTemplate(): NotificationLocalizedTemplate {
235
+ return Object.fromEntries(
236
+ Object.entries(ACKNOWLEDGEMENT_STRINGS).map(([locale, t]) => [
237
+ locale,
238
+ {
239
+ body: [mailHeading(t.header), mailParagraph("{intro}")].join(""),
240
+ meta: { subject: t.subject, category: "transactional" as const },
241
+ },
242
+ ]),
243
+ );
244
+ }
245
+
246
+ export function contactConfirmationTemplate(): NotificationLocalizedTemplate {
247
+ return Object.fromEntries(
248
+ Object.entries(CONFIRMATION_STRINGS).map(([locale, t]) => [
249
+ locale,
250
+ {
251
+ body: [
252
+ mailHeading(t.header),
253
+ mailParagraph("{intro}"),
254
+ mailParagraph("{quoteIntro}"),
255
+ mailRepeatedRows("paragraphs", "text"),
256
+ ].join(""),
257
+ meta: { subject: t.subject, category: "transactional" as const },
258
+ },
259
+ ]),
260
+ );
261
+ }
262
+
263
+ type ReplyStrings = { readonly footer: string; readonly quoteIntro: (date: string) => string };
264
+
265
+ const REPLY_STRINGS: Readonly<Record<ContactMailLocale, ReplyStrings>> = {
266
+ en: {
267
+ footer: "You can reply directly to this email.",
268
+ quoteIntro: (date) => `Your request from ${date}:`,
269
+ },
270
+ de: {
271
+ footer: "Du kannst direkt auf diese Mail antworten.",
272
+ quoteIntro: (date) => `Deine Meldung vom ${date}:`,
273
+ },
274
+ };
275
+
276
+ export const CONTACT_REPLY_VARIABLES: TemplateVariables = {
277
+ subject: templateVariable.text({ required: true }),
278
+ paragraphs: templateVariable.list(
279
+ { text: templateVariable.text({ required: true }) },
280
+ { required: true },
281
+ ),
282
+ signedBy: templateVariable.text({ required: true }),
283
+ quoteIntro: templateVariable.text({ required: true }),
284
+ quote: templateVariable.text({ required: true }),
285
+ footer: templateVariable.text({ required: true }),
286
+ };
287
+
288
+ export function contactReplyVariables(args: {
289
+ readonly requestSubject: string;
290
+ readonly requestMessage: string;
291
+ readonly requestSubmittedAt: number;
292
+ readonly replyMessage: string;
293
+ readonly authorName: string;
294
+ readonly appName: string;
295
+ readonly locale: string | undefined;
296
+ }) {
297
+ const t = REPLY_STRINGS[resolveContactMailLocale(args.locale)];
298
+ return {
299
+ subject: `Re: ${args.requestSubject}`,
300
+ paragraphs: toParagraphRows(args.replyMessage),
301
+ signedBy: `${args.authorName}, ${args.appName}`,
302
+ quoteIntro: t.quoteIntro(formatContactDate(args.requestSubmittedAt, args.locale)),
303
+ quote: truncateText(args.requestMessage.replace(/\s+/g, " "), QUOTE_MAX_CHARS),
304
+ footer: t.footer,
305
+ };
306
+ }
307
+
308
+ export function contactReplyTemplate(): NotificationLocalizedTemplate {
309
+ return Object.fromEntries(
310
+ Object.keys(REPLY_STRINGS).map((locale) => [
311
+ locale,
312
+ {
313
+ body: [
314
+ mailRepeatedRows("paragraphs", "text"),
315
+ mailParagraph("{signedBy}"),
316
+ mailParagraph("{quoteIntro}"),
317
+ mailParagraph("{quote}"),
318
+ mailParagraph("{footer}"),
319
+ ].join(""),
320
+ meta: { subject: "{{subject}}", category: "transactional" as const },
321
+ },
322
+ ]),
323
+ );
324
+ }
325
+
326
+ type TestStrings = { readonly subject: string; readonly header: string; readonly intro: string };
327
+
328
+ const TEST_STRINGS: Readonly<Record<ContactMailLocale, TestStrings>> = {
329
+ en: {
330
+ subject: "Test message from {{appName}}",
331
+ header: "Test message",
332
+ intro: "This is a test message of the contact forwarding.",
333
+ },
334
+ de: {
335
+ subject: "Testnachricht von {{appName}}",
336
+ header: "Testnachricht",
337
+ intro: "Das ist eine Testnachricht der Weiterleitung von Meldungen.",
338
+ },
339
+ };
340
+
341
+ export const CONTACT_TEST_VARIABLES: TemplateVariables = {
342
+ appName: templateVariable.text({ required: true }),
343
+ };
344
+
345
+ export function contactTestVariables(args: {
346
+ readonly appName: string;
347
+ readonly locale: string | undefined;
348
+ }) {
349
+ const t = TEST_STRINGS[resolveContactMailLocale(args.locale)];
350
+ return {
351
+ appName: args.appName,
352
+ telegramTitle: t.subject.replace("{{appName}}", args.appName),
353
+ telegramBody: t.intro,
354
+ };
355
+ }
356
+
357
+ export function contactTestTemplate(): NotificationLocalizedTemplate {
358
+ return Object.fromEntries(
359
+ Object.entries(TEST_STRINGS).map(([locale, t]) => [
360
+ locale,
361
+ {
362
+ body: [mailHeading(t.header), mailParagraph(t.intro)].join(""),
363
+ meta: { subject: t.subject, category: "transactional" as const },
364
+ },
365
+ ]),
366
+ );
367
+ }
368
+
369
+ const REPLY_IN_APP_STRINGS: Readonly<
370
+ Record<ContactMailLocale, (authorName: string) => { title: string; body: string }>
371
+ > = {
372
+ en: (authorName) => ({
373
+ title: "Answer to your request",
374
+ body: `${authorName} replied. The answer is in your inbox.`,
375
+ }),
376
+ de: (authorName) => ({
377
+ title: "Antwort auf deine Meldung",
378
+ body: `${authorName} hat geantwortet. Die Antwort liegt in deinem Postfach.`,
379
+ }),
380
+ };
381
+
382
+ export function contactReplyInAppData(args: {
383
+ readonly authorName: string;
384
+ readonly locale: string | undefined;
385
+ }): { readonly inAppTitle: string; readonly inAppBody: string } {
386
+ const copy = REPLY_IN_APP_STRINGS[resolveContactMailLocale(args.locale)](args.authorName);
387
+ return { inAppTitle: copy.title, inAppBody: copy.body };
388
+ }
389
+
390
+ function stringField(data: Readonly<Record<string, unknown>>, key: string): string {
391
+ const value = data[key];
392
+ return typeof value === "string" ? value : "";
393
+ }
394
+
395
+ export function telegramTemplate(data: Readonly<Record<string, unknown>>) {
396
+ return { title: stringField(data, "telegramTitle"), body: stringField(data, "telegramBody") };
397
+ }
398
+
399
+ export function inAppTemplate(data: Readonly<Record<string, unknown>>) {
400
+ return { title: stringField(data, "inAppTitle"), body: stringField(data, "inAppBody") };
401
+ }
@@ -0,0 +1,95 @@
1
+ import type {
2
+ PayloadRateLimitOption,
3
+ RateLimitDeclaration,
4
+ RateLimitOption,
5
+ } from "@cosmicdrift/kumiko-types/handlers";
6
+ import {
7
+ CONTACT_DEFAULT_CHALLENGE_TTL_MINUTES,
8
+ CONTACT_DEFAULT_MIN_SUBMIT_SECONDS,
9
+ CONTACT_POW_BITS,
10
+ type ContactPhoneMode,
11
+ } from "./web/constants.ts";
12
+
13
+ export type ContactOptions = {
14
+ /** Name shown in the notification mails ("Testnachricht von <appName>") and in the hint of the report form. */
15
+ readonly appName: string;
16
+ /** Default { phone: "off" }. Controls both the public form (challenge query reports it) and the admin report form. */
17
+ readonly fields?: { readonly phone?: ContactPhoneMode };
18
+ /** Shown as "Version" in the request origin; omitted when unset. */
19
+ readonly appVersion?: string;
20
+ /** Default false. true requires channel-telegram and adds the `telegramChat` config key, the `send-test` handler and the forward to that chat. */
21
+ readonly telegram?: boolean;
22
+ /** Screen the admin report form's Cancel button navigates to (short id or `<feature>:screen:<id>`). Default: no Cancel button. */
23
+ readonly reportCancelTarget?: string;
24
+ /** Screens the sender and tenant links in the request detail header open (short id or `<feature>:screen:<id>`). Default `user-edit` and `tenant-edit`; `false` shows plain text. */
25
+ readonly detailLinks?: {
26
+ readonly user?: string | false;
27
+ readonly tenant?: string | false;
28
+ };
29
+ /** Language of the notification mails and Telegram texts SystemAdmins receive. Default "en". */
30
+ readonly adminLocale?: string;
31
+ /** Deep link to the request in the admin, put into the notification mail and the Telegram text. Omitted: no link. */
32
+ readonly requestLink?: (requestId: string) => string;
33
+ /** Per-IP limit of the public submit. Default 5 per IP per 10 minutes. */
34
+ readonly rateLimit?: RateLimitDeclaration;
35
+ /** Default: 3 per email address per day. `[]` disables. */
36
+ readonly emailRateLimits?: readonly PayloadRateLimitOption[];
37
+ readonly spamProtection?: {
38
+ /** Default 3. Submits faster than this after the challenge was issued are silently dropped. */
39
+ readonly minSubmitSeconds?: number;
40
+ /** Leading zero bits of SHA-256(challenge + ":" + nonce). Default 19, min 8, max 26. */
41
+ readonly proofOfWorkBits?: number;
42
+ /** Default 30. */
43
+ readonly challengeTtlMinutes?: number;
44
+ };
45
+ /** Retention of requests and replies, data-retention keepFor syntax. Default "365d" counted from submittedAt. */
46
+ readonly retentionKeepFor?: string;
47
+ };
48
+
49
+ export const DEFAULT_SUBMIT_RATE_LIMIT: RateLimitOption = {
50
+ per: "ip+handler",
51
+ limit: 5,
52
+ windowSeconds: 10 * 60,
53
+ };
54
+
55
+ export const REPORT_RATE_LIMIT: RateLimitOption = {
56
+ per: "user+handler",
57
+ limit: 10,
58
+ windowSeconds: 60 * 60,
59
+ };
60
+
61
+ export const CHALLENGE_RATE_LIMIT: RateLimitOption = {
62
+ per: "ip+handler",
63
+ limit: 30,
64
+ windowSeconds: 10 * 60,
65
+ };
66
+
67
+ export const DEFAULT_SUBMIT_EMAIL_RATE_LIMITS: readonly PayloadRateLimitOption[] = [
68
+ { per: { payloadField: "email" }, limit: 3, windowSeconds: 24 * 60 * 60, normalize: "email" },
69
+ ];
70
+
71
+ export const DEFAULT_RETENTION_KEEP_FOR = "365d";
72
+
73
+ export type ContactSettings = {
74
+ readonly phone: ContactPhoneMode;
75
+ readonly proofOfWorkBits: number;
76
+ readonly minSubmitMs: number;
77
+ readonly challengeTtlSeconds: number;
78
+ };
79
+
80
+ export function resolveContactSettings(opts: ContactOptions): ContactSettings {
81
+ const bits = opts.spamProtection?.proofOfWorkBits ?? CONTACT_POW_BITS.default;
82
+ if (!Number.isInteger(bits) || bits < CONTACT_POW_BITS.min || bits > CONTACT_POW_BITS.max) {
83
+ throw new Error(
84
+ `contact: spamProtection.proofOfWorkBits must be an integer between ${CONTACT_POW_BITS.min} and ${CONTACT_POW_BITS.max}, got ${bits}`,
85
+ );
86
+ }
87
+ return {
88
+ phone: opts.fields?.phone ?? "off",
89
+ proofOfWorkBits: bits,
90
+ minSubmitMs:
91
+ (opts.spamProtection?.minSubmitSeconds ?? CONTACT_DEFAULT_MIN_SUBMIT_SECONDS) * 1000,
92
+ challengeTtlSeconds:
93
+ (opts.spamProtection?.challengeTtlMinutes ?? CONTACT_DEFAULT_CHALLENGE_TTL_MINUTES) * 60,
94
+ };
95
+ }
@@ -0,0 +1,28 @@
1
+ import type { DashboardStatPanel } from "@cosmicdrift/kumiko-framework/ui-types";
2
+ import {
3
+ CONTACT_FEATURE,
4
+ CONTACT_STATUS,
5
+ ContactQueries,
6
+ ContactScreens,
7
+ } from "./web/constants.ts";
8
+
9
+ // Stat tile for a dashboard: how many contact requests still wait for a first answer.
10
+ export function contactOpenRequestsPanels(): readonly DashboardStatPanel[] {
11
+ return [
12
+ {
13
+ kind: "stat",
14
+ id: `${CONTACT_FEATURE}-open-requests`,
15
+ label: "contact:overview.open",
16
+ query: ContactQueries.requestList,
17
+ params: {
18
+ filters: [{ field: "status", op: "eq", value: CONTACT_STATUS.Open }],
19
+ limit: 1,
20
+ totalCount: true,
21
+ },
22
+ ignoreScreenFilter: true,
23
+ valueField: "total",
24
+ tone: "warn",
25
+ navigate: { screen: ContactScreens.requestList },
26
+ },
27
+ ];
28
+ }