@open-workspace/email 0.4.2 → 0.5.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 (268) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  4. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  5. package/drizzle/mailbox/meta/_journal.json +7 -0
  6. package/drizzle/mailbox/migrations.js +3 -1
  7. package/migrations/0002_after_0_4_2.sql +35 -0
  8. package/migrations/meta/0002_snapshot.json +807 -0
  9. package/migrations/meta/_journal.json +7 -0
  10. package/package.json +23 -7
  11. package/shared/attachment-preview.ts +18 -8
  12. package/shared/folder-order.ts +31 -6
  13. package/shared/mail-headers.ts +3 -3
  14. package/shared/mail-html.ts +25 -0
  15. package/shared/mail-import.ts +0 -7
  16. package/shared/mail-received.ts +4 -4
  17. package/shared/mail-rule-actions.ts +47 -0
  18. package/shared/mail-sender-intel.ts +5 -1
  19. package/shared/mail.ts +70 -4
  20. package/shared/office-preview.ts +0 -4
  21. package/shared/pdfjs-assets.ts +1 -3
  22. package/shared/rich-content.ts +35 -0
  23. package/shared/signature.ts +15 -0
  24. package/src/admin/address-auto-reply.tsx +52 -0
  25. package/src/admin/blocked-senders.tsx +86 -66
  26. package/src/admin/forward-destinations.tsx +140 -0
  27. package/src/admin/mail-archive.tsx +134 -0
  28. package/src/admin/team-signature.tsx +90 -0
  29. package/src/client.tsx +130 -16
  30. package/src/mail/attachment-preview-dialog.tsx +13 -5
  31. package/src/mail/auto-reply-banner.tsx +61 -0
  32. package/src/mail/blocked-senders.tsx +124 -91
  33. package/src/mail/compose-window-chrome.tsx +20 -8
  34. package/src/mail/compose-window.tsx +391 -365
  35. package/src/mail/composer-asset-bookmarks.ts +47 -12
  36. package/src/mail/composer-attachment-list.tsx +42 -17
  37. package/src/mail/composer-editor-document.ts +54 -10
  38. package/src/mail/composer-editor.tsx +44 -44
  39. package/src/mail/composer-link-text.ts +42 -0
  40. package/src/mail/composer-recipient-fields.tsx +25 -16
  41. package/src/mail/composer-recipients.ts +62 -16
  42. package/src/mail/composer-session.ts +107 -23
  43. package/src/mail/composer-toolbar.tsx +15 -1
  44. package/src/mail/composer-upload-client.ts +29 -1
  45. package/src/mail/composer-upload-progress.ts +2 -2
  46. package/src/mail/conversation-actions-model.ts +56 -0
  47. package/src/mail/conversation-list.tsx +263 -128
  48. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  49. package/src/mail/conversation-message.tsx +257 -102
  50. package/src/mail/conversation-order.ts +12 -0
  51. package/src/mail/conversation-reply.tsx +313 -189
  52. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  53. package/src/mail/conversation-view.tsx +315 -85
  54. package/src/mail/delivery-indicator.tsx +33 -12
  55. package/src/mail/draft-attachments.ts +194 -0
  56. package/src/mail/email-html-body.tsx +97 -53
  57. package/src/mail/folder-cleanup-bar.tsx +193 -0
  58. package/src/mail/folder-glyph.tsx +5 -0
  59. package/src/mail/folder-manager.tsx +71 -38
  60. package/src/mail/mail-app-bar.tsx +15 -3
  61. package/src/mail/mail-dock.tsx +19 -3
  62. package/src/mail/mail-focus.ts +48 -0
  63. package/src/mail/mail-folders.ts +14 -2
  64. package/src/mail/mail-layout.css +20 -0
  65. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  66. package/src/mail/mail-nav-rail.tsx +7 -4
  67. package/src/mail/mail-nav.tsx +66 -46
  68. package/src/mail/mail-prefetch.ts +30 -9
  69. package/src/mail/mail-primitives.tsx +6 -4
  70. package/src/mail/mail-queries.ts +122 -20
  71. package/src/mail/mail-query-cache.ts +1085 -173
  72. package/src/mail/mail-search-params.ts +35 -0
  73. package/src/mail/mail-shell.tsx +32 -9
  74. package/src/mail/mailbox-ai-settings.tsx +56 -42
  75. package/src/mail/mailbox-realtime.tsx +4 -12
  76. package/src/mail/mailbox-rules.tsx +898 -0
  77. package/src/mail/message-actions.tsx +38 -26
  78. package/src/mail/message-attachments.tsx +20 -12
  79. package/src/mail/message-body.ts +53 -0
  80. package/src/mail/message-format.ts +8 -2
  81. package/src/mail/message-header.tsx +3 -1
  82. package/src/mail/message-translation.tsx +34 -67
  83. package/src/mail/original-message-sheet.tsx +63 -50
  84. package/src/mail/original-message.ts +6 -6
  85. package/src/mail/pane-splitter.tsx +3 -3
  86. package/src/mail/pdf-preview.tsx +70 -62
  87. package/src/mail/plain-message-text.tsx +28 -0
  88. package/src/mail/print-message.ts +31 -36
  89. package/src/mail/reader-pane.tsx +134 -103
  90. package/src/mail/recipient-combobox.tsx +52 -29
  91. package/src/mail/recipient-field-row.tsx +3 -0
  92. package/src/mail/rich-content.ts +128 -0
  93. package/src/mail/sanitize-email-html.ts +53 -32
  94. package/src/mail/sender-actions.tsx +48 -46
  95. package/src/mail/sender-authentication.tsx +1 -1
  96. package/src/mail/sender-dialogs.tsx +67 -43
  97. package/src/mail/sender-security.tsx +12 -11
  98. package/src/mail/signature.ts +43 -0
  99. package/src/mail/translation-html.ts +3 -3
  100. package/src/mail/types.ts +12 -0
  101. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  102. package/src/mail/use-composer-core.ts +286 -0
  103. package/src/mail/use-composer-navigation.ts +120 -0
  104. package/src/mail/use-composer-submission.ts +154 -19
  105. package/src/mail/use-conversation-actions.ts +292 -0
  106. package/src/mail/use-conversation-position.ts +51 -0
  107. package/src/mail/use-conversation-selection.ts +192 -89
  108. package/src/mail/use-draft-autosave.ts +394 -0
  109. package/src/mail/use-drafts.ts +194 -0
  110. package/src/mail/use-mail-controller.ts +758 -213
  111. package/src/mail/use-mail-data.ts +210 -100
  112. package/src/mail/use-mail-safety.ts +19 -38
  113. package/src/mail/use-mail-send.ts +18 -6
  114. package/src/mail/use-pdf-translation.ts +1 -1
  115. package/src/mail/use-reply-cc.ts +61 -0
  116. package/src/mail/use-visible-message-read.ts +128 -64
  117. package/src/pwa/push-key.ts +18 -0
  118. package/src/pwa/push-subscription.ts +88 -26
  119. package/src/settings/auto-reply-editor.tsx +454 -0
  120. package/src/settings/auto-reply-page.tsx +59 -0
  121. package/src/settings/mail-apps-page.tsx +52 -43
  122. package/src/settings/notifications-page.tsx +82 -67
  123. package/src/settings/rich-content-editor.tsx +182 -0
  124. package/src/settings/settings-queries.ts +57 -0
  125. package/src/settings/signature-editor.tsx +142 -0
  126. package/src/settings/signature-page.tsx +70 -0
  127. package/worker/admin/blocked-sender-routes.ts +10 -16
  128. package/worker/backups/eml.ts +185 -0
  129. package/worker/backups/file-parts.ts +140 -0
  130. package/worker/backups/mail-backup.ts +644 -0
  131. package/worker/backups/settings-backup.ts +866 -0
  132. package/worker/backups/stream-eml.ts +73 -0
  133. package/worker/db/schema.ts +89 -5
  134. package/worker/jmap/attachment-blobs.ts +113 -0
  135. package/worker/jmap/blobs.ts +162 -0
  136. package/worker/jmap/draft-content.ts +42 -0
  137. package/worker/jmap/drafts.ts +70 -0
  138. package/worker/jmap/email-body.ts +152 -0
  139. package/worker/jmap/email-methods.ts +1223 -0
  140. package/worker/jmap/eventsource.ts +9 -8
  141. package/worker/jmap/import-methods.ts +205 -0
  142. package/worker/jmap/mailbox-methods.ts +187 -0
  143. package/worker/jmap/method-core.ts +379 -0
  144. package/worker/jmap/methods.ts +119 -1816
  145. package/worker/jmap/push-subscription-methods.ts +373 -0
  146. package/worker/jmap/push-types.ts +46 -2
  147. package/worker/jmap/push.ts +24 -31
  148. package/worker/jmap/routes.ts +144 -153
  149. package/worker/jmap/submission-methods.ts +395 -0
  150. package/worker/mail/access.ts +35 -12
  151. package/worker/mail/ai-classification.ts +2 -2
  152. package/worker/mail/archive-object.ts +96 -0
  153. package/worker/mail/archive-routes.ts +35 -0
  154. package/worker/mail/archive.ts +700 -0
  155. package/worker/mail/auth-headers.ts +57 -43
  156. package/worker/mail/auto-reply-routes.ts +260 -0
  157. package/worker/mail/auto-reply.ts +243 -0
  158. package/worker/mail/bimi.ts +53 -36
  159. package/worker/mail/bootstrap.ts +11 -3
  160. package/worker/mail/capabilities.ts +69 -0
  161. package/worker/mail/classification-signals.ts +1 -1
  162. package/worker/mail/confusables-data.ts +9 -0
  163. package/worker/mail/conversation-cursor.ts +6 -15
  164. package/worker/mail/delivery-events.ts +9 -7
  165. package/worker/mail/delivery-facts.ts +1 -1
  166. package/worker/mail/demo-routes.ts +3 -3
  167. package/worker/mail/doh.ts +3 -3
  168. package/worker/mail/download-links.ts +153 -0
  169. package/worker/mail/downloads.ts +2 -6
  170. package/worker/mail/draft-routes.ts +473 -0
  171. package/worker/mail/draft-sender.ts +54 -0
  172. package/worker/mail/edge-cache.ts +41 -55
  173. package/worker/mail/email-routing.ts +123 -0
  174. package/worker/mail/forward-destinations.ts +114 -0
  175. package/worker/mail/forward-downloads.ts +115 -0
  176. package/worker/mail/forward-html.ts +85 -0
  177. package/worker/mail/header-metadata.ts +2 -5
  178. package/worker/mail/impersonation.ts +102 -101
  179. package/worker/mail/import-routes.ts +29 -82
  180. package/worker/mail/import.ts +130 -0
  181. package/worker/mail/inbound-delivery.ts +6 -5
  182. package/worker/mail/inbound-threading.ts +15 -2
  183. package/worker/mail/inbound.ts +58 -28
  184. package/worker/mail/ip-intel.ts +8 -17
  185. package/worker/mail/isolate-cache.ts +1 -1
  186. package/worker/mail/mailbox-deletion.ts +33 -12
  187. package/worker/mail/mailbox-directory.ts +39 -65
  188. package/worker/mail/mailing-list.ts +29 -58
  189. package/worker/mail/message-security.ts +19 -8
  190. package/worker/mail/message-view.ts +12 -10
  191. package/worker/mail/office-conversion.ts +3 -5
  192. package/worker/mail/office-preview-container.ts +3 -8
  193. package/worker/mail/outbound-content.ts +139 -65
  194. package/worker/mail/outbound-delivery.ts +20 -17
  195. package/worker/mail/outbound-service.ts +34 -9
  196. package/worker/mail/outbound.ts +146 -75
  197. package/worker/mail/outgoing-context.ts +60 -30
  198. package/worker/mail/participants.ts +10 -15
  199. package/worker/mail/plain-body.ts +39 -0
  200. package/worker/mail/push-endpoint.ts +17 -0
  201. package/worker/mail/push-notifications.ts +53 -62
  202. package/worker/mail/quote-parser.ts +36 -24
  203. package/worker/mail/receive.ts +146 -39
  204. package/worker/mail/recipients.ts +1 -1
  205. package/worker/mail/remote-proxy.ts +3 -3
  206. package/worker/mail/repeat.ts +1 -1
  207. package/worker/mail/reply-plan.ts +124 -26
  208. package/worker/mail/rfc.ts +15 -7
  209. package/worker/mail/rich-content.ts +207 -0
  210. package/worker/mail/routes.ts +698 -304
  211. package/worker/mail/routing.ts +274 -0
  212. package/worker/mail/rules.ts +230 -0
  213. package/worker/mail/safety-routes.ts +88 -86
  214. package/worker/mail/schemas.ts +222 -31
  215. package/worker/mail/search.ts +25 -2
  216. package/worker/mail/security-settings.ts +4 -10
  217. package/worker/mail/send-message.ts +238 -0
  218. package/worker/mail/sender-intel.ts +12 -20
  219. package/worker/mail/signature-routes.ts +206 -0
  220. package/worker/mail/signatures.ts +195 -0
  221. package/worker/mail/spamhaus.ts +28 -16
  222. package/worker/mail/storage-keys.ts +35 -0
  223. package/worker/mail/unsubscribe.ts +19 -41
  224. package/worker/mail/uploads.ts +192 -80
  225. package/worker/mail/webhook-source.ts +3 -2
  226. package/worker/mail/workspace-safety.ts +194 -60
  227. package/worker/mailbox/demo.ts +13 -15
  228. package/worker/mailbox/email-index.ts +65 -35
  229. package/worker/mailbox/folder-model.ts +0 -20
  230. package/worker/mailbox/import-placement.ts +21 -36
  231. package/worker/mailbox/inbound-decision.ts +114 -0
  232. package/worker/mailbox/inbox-counts.ts +45 -25
  233. package/worker/mailbox/index.ts +756 -3201
  234. package/worker/mailbox/jmap-change-log.ts +195 -0
  235. package/worker/mailbox/jmap-email-rows.ts +186 -0
  236. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  237. package/worker/mailbox/jmap-placement.ts +139 -0
  238. package/worker/mailbox/jmap-query-filter.ts +231 -0
  239. package/worker/mailbox/jmap-store.ts +167 -299
  240. package/worker/mailbox/mailbox-auto-reply.ts +316 -0
  241. package/worker/mailbox/mailbox-backup.ts +538 -0
  242. package/worker/mailbox/mailbox-drafts.ts +417 -0
  243. package/worker/mailbox/mailbox-folders.ts +242 -0
  244. package/worker/mailbox/mailbox-import.ts +455 -0
  245. package/worker/mailbox/mailbox-inbound.ts +934 -0
  246. package/worker/mailbox/mailbox-jmap.ts +294 -0
  247. package/worker/mailbox/mailbox-outbound.ts +413 -0
  248. package/worker/mailbox/mailbox-rules.ts +336 -0
  249. package/worker/mailbox/mailbox-senders.ts +294 -0
  250. package/worker/mailbox/mailbox-storage.ts +438 -0
  251. package/worker/mailbox/mailbox-views.ts +891 -0
  252. package/worker/mailbox/model.ts +1 -0
  253. package/worker/mailbox/office-preview-jobs.ts +59 -47
  254. package/worker/mailbox/placement.ts +933 -0
  255. package/worker/mailbox/restore-thread.ts +52 -0
  256. package/worker/mailbox/retention.ts +36 -0
  257. package/worker/mailbox/schema.ts +200 -1
  258. package/worker/mailbox/senders.ts +216 -178
  259. package/worker/mailbox/spam-undo-filing.ts +138 -0
  260. package/worker/mailbox/spam-undo.ts +253 -0
  261. package/worker/mailbox/support.ts +416 -0
  262. package/worker/mcp/mail-action-tools.ts +161 -1
  263. package/worker/mcp/mail-compose-tools.ts +7 -1
  264. package/worker/mcp/mail-safety-tools.ts +4 -4
  265. package/worker/module.ts +23 -4
  266. package/src/mail/use-compose-submission.ts +0 -208
  267. package/src/mail/use-reply-submission.ts +0 -114
  268. package/worker/mail/capped-body.ts +0 -36
package/README.md CHANGED
@@ -18,11 +18,16 @@ OpenWorkspace({
18
18
  so a zone with another provider's MX records must have them removed first, or
19
19
  the deploy fails.
20
20
 
21
- It takes one option, `pushContact`: the `mailto:` or `https:` contact push
22
- services see. Without it they see the app's URL.
21
+ It takes three options. `pushContact` is the `mailto:` or `https:` contact push
22
+ services see; without it they see the app's URL. `retention` is how many days
23
+ a conversation stays in Trash or Spam before it is deleted, as
24
+ `{ trash, spam }`: Spam defaults to 30, and Trash (or a folder set to `null`)
25
+ keeps mail until someone empties it. `archive: { years }` keeps a write-once
26
+ copy of every received and sent message in a bucket of its own, locked for
27
+ that many years.
23
28
 
24
- The module adds two permissions administrators can give to members: "Block
25
- senders" and "Import mail". It reads one optional secret, `SPAMHAUS_DQS_KEY`,
29
+ The module adds three permissions administrators can give to members: "Block
30
+ senders", "Import mail" and "Mail archive". It reads one optional secret, `SPAMHAUS_DQS_KEY`,
26
31
  for Spamhaus blocklist checks.
27
32
 
28
33
  ## Documentation
package/alchemy/index.ts CHANGED
@@ -7,6 +7,7 @@ import { VapidKeys } from "@open-workspace/core/alchemy";
7
7
  import type { WorkspaceModuleInfra } from "@open-workspace/core/alchemy";
8
8
  import { AlchemyContext } from "alchemy/AlchemyContext";
9
9
  import { Random } from "alchemy/Random";
10
+ import * as RemovalPolicy from "alchemy/RemovalPolicy";
10
11
  import * as Effect from "effect/Effect";
11
12
  import * as Option from "effect/Option";
12
13
  import { z } from "zod";
@@ -57,12 +58,29 @@ const deliveryEvents = [
57
58
  export type EmailInfraProps = {
58
59
  /** Contact for push services (mailto: or https:); defaults to the app's URL. */
59
60
  pushContact?: string;
61
+ /**
62
+ * Days a conversation stays in Trash or Spam before it is deleted
63
+ * permanently. Spam defaults to 30; Trash, or a folder set to null, keeps
64
+ * mail until someone empties it.
65
+ */
66
+ retention?: { trash?: number | null; spam?: number | null };
67
+ /**
68
+ * A write-once copy of every received and sent message in a separate R2
69
+ * bucket whose lock rule keeps each object `years` after it is written
70
+ * (worker/mail/archive.ts, docs/configuration.md). Off by default.
71
+ */
72
+ archive?: { years: number };
60
73
  };
61
74
 
62
75
  /** Optional secrets the module reads, bound when set in the environment or `.env`. */
63
76
  const emailSecrets = [
64
77
  // Spamhaus Data Query Service key, for blocklist checks.
65
78
  "SPAMHAUS_DQS_KEY",
79
+ // Email Routing's API (worker/mail/email-routing.ts). Deployed, `Email()`
80
+ // mints and binds them; under `alchemy dev` they're bound only when set,
81
+ // as the stack tests do for their fake API.
82
+ "EMAIL_ROUTING_ACCOUNT_ID",
83
+ "EMAIL_ROUTING_API_TOKEN",
66
84
  ] as const;
67
85
 
68
86
  function emailResources() {
@@ -108,9 +126,112 @@ const mailStorage = (origin: string, dev: boolean) => {
108
126
  return Cloudflare.R2.Bucket("MailStorage", props);
109
127
  };
110
128
 
129
+ /**
130
+ * An account API token that may only manage Email Routing destination
131
+ * addresses, for forwards (worker/mail/email-routing.ts), and its account.
132
+ * Minted when deployed; `alchemy dev` has no Email Routing to call.
133
+ */
134
+ const emailRoutingToken = () =>
135
+ Effect.gen(function* () {
136
+ const { accountId } = yield* yield* Cloudflare.CloudflareEnvironment;
137
+
138
+ const token = yield* Cloudflare.ApiToken.AccountApiToken(
139
+ "EmailRoutingToken",
140
+ {
141
+ policies: [
142
+ {
143
+ effect: "allow",
144
+ permissionGroups: ["Email Routing Addresses Write"],
145
+ resources: { [`com.cloudflare.api.account.${accountId}`]: "*" },
146
+ },
147
+ ],
148
+ }
149
+ );
150
+
151
+ return {
152
+ EMAIL_ROUTING_ACCOUNT_ID: token.accountId,
153
+ EMAIL_ROUTING_API_TOKEN: token.value,
154
+ };
155
+ });
156
+
111
157
  /** The contact push services see, bound only when configured. */
112
158
  type PushSubjectEnv = { VAPID_SUBJECT?: string };
113
159
 
160
+ /** `Email({ retention })` in days, bound only for folders that delete mail. */
161
+ type RetentionEnv = {
162
+ TRASH_RETENTION_DAYS?: string;
163
+ SPAM_RETENTION_DAYS?: string;
164
+ };
165
+
166
+ function retentionEnv({
167
+ trash = null,
168
+ spam = 30,
169
+ }: EmailInfraProps["retention"] = {}) {
170
+ const env: RetentionEnv = {};
171
+
172
+ for (const [name, days] of [
173
+ ["TRASH_RETENTION_DAYS", trash],
174
+ ["SPAM_RETENTION_DAYS", spam],
175
+ ] as const) {
176
+ if (days === null) continue;
177
+
178
+ if (!Number.isInteger(days) || days < 1) {
179
+ throw new Error(
180
+ `Email({ retention }) takes whole days of 1 or more, not ${days}`
181
+ );
182
+ }
183
+
184
+ env[name] = String(days);
185
+ }
186
+
187
+ return env;
188
+ }
189
+
190
+ /** The mail archive's bucket and its lock period, bound only with `archive`. */
191
+ type ArchiveEnv = {
192
+ MAIL_ARCHIVE?: ReturnType<typeof Cloudflare.R2.Bucket>;
193
+ MAIL_ARCHIVE_YEARS?: string;
194
+ };
195
+
196
+ const SECONDS_PER_YEAR = 365.25 * 24 * 60 * 60;
197
+
198
+ /**
199
+ * The archive bucket, declared with its lock rule: R2 refuses to delete or
200
+ * overwrite an object until it is `years` old. Changing `years` and
201
+ * deploying changes the rule, and with it the lock on objects already
202
+ * written. Dropping `archive` leaves the bucket, its objects and its rule
203
+ * as they are (retained): Alchemy stops tracking it, and nothing is deleted.
204
+ * Under `alchemy dev` (and the stack tests) the local bucket goes as usual.
205
+ */
206
+ function archiveEnv(archive: EmailInfraProps["archive"], dev: boolean) {
207
+ const env: ArchiveEnv = {};
208
+
209
+ if (!archive) return env;
210
+
211
+ if (!Number.isInteger(archive.years) || archive.years < 1) {
212
+ throw new Error(
213
+ `Email({ archive }) takes whole years of 1 or more, not ${archive.years}`
214
+ );
215
+ }
216
+
217
+ env.MAIL_ARCHIVE = Cloudflare.R2.Bucket("MailArchive", {
218
+ locks: [
219
+ {
220
+ id: "mail-archive-retention",
221
+ enabled: true,
222
+ prefix: "",
223
+ condition: {
224
+ type: "Age",
225
+ maxAgeSeconds: Math.ceil(archive.years * SECONDS_PER_YEAR),
226
+ },
227
+ },
228
+ ],
229
+ }).pipe(RemovalPolicy.retain(!dev));
230
+ env.MAIL_ARCHIVE_YEARS = String(archive.years);
231
+
232
+ return env;
233
+ }
234
+
114
235
  /** The module's Worker bindings; the Worker's `Env` is derived from them (`EmailEnv`). */
115
236
  const emailEnv = (
116
237
  props: EmailInfraProps,
@@ -130,6 +251,10 @@ const emailEnv = (
130
251
 
131
252
  if (props.pushContact) pushSubject.VAPID_SUBJECT = props.pushContact;
132
253
 
254
+ const emailRouting: Partial<
255
+ Effect.Success<ReturnType<typeof emailRoutingToken>>
256
+ > = dev ? {} : yield* emailRoutingToken();
257
+
133
258
  return {
134
259
  /**
135
260
  * S3 credentials for the mail bucket alone, as JSON, for presigned
@@ -141,6 +266,9 @@ const emailEnv = (
141
266
  VAPID_PUBLIC_KEY: vapid.publicKey,
142
267
  VAPID_PRIVATE_KEY: vapid.privateKey,
143
268
  ...pushSubject,
269
+ ...retentionEnv(props.retention),
270
+ ...emailRouting,
271
+ ...archiveEnv(props.archive, dev),
144
272
  MAIL_STORAGE: bucket,
145
273
  /** Signs the remote image proxy's tokens (mail/remote-token.ts). */
146
274
  MAIL_REMOTE_KEY: remoteKey.text,
@@ -186,7 +314,7 @@ export type EmailEnv = Cloudflare.InferEnv<
186
314
  * MX and SPF records, sends every address to the Worker, onboards the domain
187
315
  * to Email Sending, and queues its delivery events.
188
316
  */
189
- export function Email(props: EmailInfraProps = {}): WorkspaceModuleInfra {
317
+ export function Email(props: EmailInfraProps = {}) {
190
318
  const resources = emailResources();
191
319
 
192
320
  const {
@@ -269,5 +397,5 @@ export function Email(props: EmailInfraProps = {}): WorkspaceModuleInfra {
269
397
  });
270
398
  }
271
399
  }),
272
- };
400
+ } satisfies WorkspaceModuleInfra;
273
401
  }
@@ -0,0 +1,180 @@
1
+ CREATE TABLE `auto_reply_log` (
2
+ `sender_address` text PRIMARY KEY NOT NULL,
3
+ `last_replied_at` integer NOT NULL,
4
+ `last_original_email_id` text NOT NULL,
5
+ `last_outbound_message_id` text
6
+ );
7
+ --> statement-breakpoint
8
+ CREATE INDEX `auto_reply_log_replied_idx` ON `auto_reply_log` (`last_replied_at`);--> statement-breakpoint
9
+ CREATE TABLE `auto_reply_settings` (
10
+ `id` integer PRIMARY KEY NOT NULL,
11
+ `enabled` integer NOT NULL,
12
+ `starts_at` integer,
13
+ `ends_at` integer,
14
+ `reply_interval_days` integer NOT NULL,
15
+ `inside` text NOT NULL,
16
+ `outside` text NOT NULL,
17
+ `updated_by` text NOT NULL,
18
+ `updated_at` integer NOT NULL
19
+ );
20
+ --> statement-breakpoint
21
+ CREATE TABLE `drafts` (
22
+ `user_id` text NOT NULL,
23
+ `id` text NOT NULL,
24
+ `kind` text NOT NULL,
25
+ `source_email_id` text,
26
+ `conversation_id` text,
27
+ `reply_mode` text,
28
+ `to_json` text NOT NULL,
29
+ `cc_json` text NOT NULL,
30
+ `bcc_json` text NOT NULL,
31
+ `reply_to` text,
32
+ `subject` text NOT NULL,
33
+ `body_text` text NOT NULL,
34
+ `body_html` text,
35
+ `editor_html` text,
36
+ `attachments_json` text DEFAULT '[]' NOT NULL,
37
+ `version` integer NOT NULL,
38
+ `updated_at` integer NOT NULL,
39
+ PRIMARY KEY(`user_id`, `id`)
40
+ );
41
+ --> statement-breakpoint
42
+ CREATE INDEX `drafts_user_updated_idx` ON `drafts` (`user_id`,`updated_at`);--> statement-breakpoint
43
+ CREATE TABLE `jmap_changes` (
44
+ `seq` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
45
+ `type` text NOT NULL,
46
+ `id` text NOT NULL,
47
+ `kind` text NOT NULL,
48
+ `user_id` text
49
+ );
50
+ --> statement-breakpoint
51
+ CREATE INDEX `jmap_changes_type_seq_idx` ON `jmap_changes` (`type`,`seq`);--> statement-breakpoint
52
+ CREATE TABLE `mail_rules` (
53
+ `id` text PRIMARY KEY NOT NULL,
54
+ `position` integer NOT NULL,
55
+ `name` text NOT NULL,
56
+ `enabled` integer NOT NULL,
57
+ `match` text NOT NULL,
58
+ `conditions_json` text NOT NULL,
59
+ `actions_json` text NOT NULL,
60
+ `stop_processing` integer NOT NULL,
61
+ `updated_at` integer NOT NULL
62
+ );
63
+ --> statement-breakpoint
64
+ CREATE INDEX `mail_rules_position_idx` ON `mail_rules` (`position`);--> statement-breakpoint
65
+ ALTER TABLE `conversations` ADD `state_changed_at` integer;--> statement-breakpoint
66
+ CREATE INDEX `conversations_state_changed_idx` ON `conversations` (`mailbox_state`,`state_changed_at`);--> statement-breakpoint
67
+ ALTER TABLE `emails` ADD `backup_sha256` text;--> statement-breakpoint
68
+ -- JMAP change log. A message deleted with its conversation logs no thread
69
+ -- update after the thread's destruction; a draft is a thread of its own.
70
+ CREATE TRIGGER `jmap_email_created` AFTER INSERT ON `emails` BEGIN
71
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Email', NEW.id, 'created'), ('Thread', NEW.conversation_id, 'updated');
72
+ END;
73
+ --> statement-breakpoint
74
+ CREATE TRIGGER `jmap_email_updated` AFTER UPDATE ON `emails` WHEN OLD.backup_sha256 IS NEW.backup_sha256 BEGIN
75
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Email', NEW.id, 'updated'), ('Thread', NEW.conversation_id, 'updated');
76
+ INSERT INTO jmap_changes (type, id, kind) SELECT 'Thread', OLD.conversation_id, 'updated' WHERE OLD.conversation_id IS NOT NEW.conversation_id;
77
+ END;
78
+ --> statement-breakpoint
79
+ CREATE TRIGGER `jmap_email_destroyed` AFTER DELETE ON `emails` BEGIN
80
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Email', OLD.id, 'destroyed');
81
+ INSERT INTO jmap_changes (type, id, kind)
82
+ SELECT 'Thread', OLD.conversation_id, 'updated'
83
+ WHERE EXISTS (SELECT 1 FROM conversations WHERE id = OLD.conversation_id);
84
+ END;
85
+ --> statement-breakpoint
86
+ CREATE TRIGGER `jmap_thread_created` AFTER INSERT ON `conversations` BEGIN
87
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Thread', NEW.id, 'created');
88
+ END;
89
+ --> statement-breakpoint
90
+ CREATE TRIGGER `jmap_thread_moved` AFTER UPDATE ON `conversations` WHEN OLD.mailbox_state IS NOT NEW.mailbox_state OR OLD.folder_id IS NOT NEW.folder_id BEGIN
91
+ INSERT INTO jmap_changes (type, id, kind) SELECT 'Email', id, 'updated' FROM emails WHERE conversation_id = NEW.id;
92
+ END;
93
+ --> statement-breakpoint
94
+ CREATE TRIGGER `jmap_thread_destroyed` AFTER DELETE ON `conversations` BEGIN
95
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Thread', OLD.id, 'destroyed');
96
+ END;
97
+ --> statement-breakpoint
98
+ CREATE TRIGGER `jmap_email_read` AFTER INSERT ON `email_read_states` BEGIN
99
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Email', NEW.email_id, 'updated', NEW.user_id);
100
+ END;
101
+ --> statement-breakpoint
102
+ CREATE TRIGGER `jmap_email_unread` AFTER DELETE ON `email_read_states` BEGIN
103
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Email', OLD.email_id, 'updated', OLD.user_id);
104
+ END;
105
+ --> statement-breakpoint
106
+ CREATE TRIGGER `jmap_draft_created` AFTER INSERT ON `drafts` BEGIN
107
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Email', NEW.id, 'created', NEW.user_id);
108
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Thread', NEW.id, 'created', NEW.user_id);
109
+ END;
110
+ --> statement-breakpoint
111
+ CREATE TRIGGER `jmap_draft_updated` AFTER UPDATE ON `drafts` BEGIN
112
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Email', NEW.id, 'updated', NEW.user_id);
113
+ END;
114
+ --> statement-breakpoint
115
+ CREATE TRIGGER `jmap_draft_destroyed` AFTER DELETE ON `drafts` BEGIN
116
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Email', OLD.id, 'destroyed', OLD.user_id);
117
+ INSERT INTO jmap_changes (type, id, kind, user_id) VALUES ('Thread', OLD.id, 'destroyed', OLD.user_id);
118
+ END;
119
+ --> statement-breakpoint
120
+ CREATE TRIGGER `jmap_folder_created` AFTER INSERT ON `folders` BEGIN
121
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', NEW.id, 'created');
122
+ END;
123
+ --> statement-breakpoint
124
+ CREATE TRIGGER `jmap_folder_updated` AFTER UPDATE ON `folders` BEGIN
125
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', NEW.id, 'updated');
126
+ END;
127
+ --> statement-breakpoint
128
+ CREATE TRIGGER `jmap_folder_destroyed` AFTER DELETE ON `folders` BEGIN
129
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', OLD.id, 'destroyed');
130
+ END;
131
+ --> statement-breakpoint
132
+ CREATE TRIGGER `jmap_system_folder_placed` AFTER INSERT ON `system_folder_order` BEGIN
133
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', NEW.folder_id, 'updated');
134
+ END;
135
+ --> statement-breakpoint
136
+ CREATE TRIGGER `jmap_system_folder_moved` AFTER UPDATE ON `system_folder_order` BEGIN
137
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', NEW.folder_id, 'updated');
138
+ END;
139
+ --> statement-breakpoint
140
+ CREATE TRIGGER `jmap_system_folder_reset` AFTER DELETE ON `system_folder_order` BEGIN
141
+ INSERT INTO jmap_changes (type, id, kind) VALUES ('Mailbox', OLD.folder_id, 'updated');
142
+ END;
143
+ --> statement-breakpoint
144
+ CREATE TRIGGER `jmap_changes_pruned` AFTER INSERT ON `jmap_changes` WHEN NEW.seq % 1000 = 0 BEGIN
145
+ DELETE FROM jmap_changes WHERE seq <= NEW.seq - 100000;
146
+ END;
147
+ --> statement-breakpoint
148
+ ALTER TABLE `emails` ADD `raw_size` integer;--> statement-breakpoint
149
+ ALTER TABLE `emails` ADD `sent_at` integer;--> statement-breakpoint
150
+ ALTER TABLE `drafts` ADD `forwarded_attachment_ids` text;--> statement-breakpoint
151
+ -- Search covers the address headers and keeps quoted text in a column of its
152
+ -- own: a preview may itself come from a quote, so it stays out of authored
153
+ -- hits. Existing mail and new arrivals use the same projection; only address
154
+ -- values become search terms, and From and To can be searched on their own.
155
+ CREATE VIEW email_search_document AS
156
+ SELECT id AS email_id, conversation_id, subject,
157
+ (SELECT group_concat(atom, ' ') FROM json_tree(from_json) WHERE type = 'text') AS sender,
158
+ (SELECT group_concat(atom, ' ') FROM json_tree(to_json) WHERE type = 'text') AS recipients,
159
+ (SELECT group_concat(atom, ' ') FROM json_tree(json_array(json(reply_to_json), json(cc_json), json(bcc_json)))
160
+ WHERE type = 'text') AS other_addresses,
161
+ coalesce(body_text, '') || ' ' || CASE WHEN coalesce(quoted_text, '') = '' THEN coalesce(preview, '') ELSE '' END AS body,
162
+ coalesce(quoted_text, '') AS quoted
163
+ FROM emails;
164
+ --> statement-breakpoint
165
+ DROP TABLE email_search;
166
+ --> statement-breakpoint
167
+ CREATE VIRTUAL TABLE email_search USING fts5(
168
+ email_id UNINDEXED, conversation_id UNINDEXED, subject, sender, recipients, other_addresses, body, quoted,
169
+ tokenize = 'unicode61 remove_diacritics 2'
170
+ );
171
+ --> statement-breakpoint
172
+ INSERT INTO email_search SELECT * FROM email_search_document;--> statement-breakpoint
173
+ CREATE TABLE `spam_undo_receipts` (
174
+ `id` text PRIMARY KEY NOT NULL,
175
+ `user_id` text NOT NULL,
176
+ `receipt` text NOT NULL,
177
+ `expires_at` integer NOT NULL
178
+ );
179
+ --> statement-breakpoint
180
+ CREATE INDEX `spam_undo_receipts_expires_at_idx` ON `spam_undo_receipts` (`expires_at`);