@open-workspace/email 0.2.0 → 0.4.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 (223) hide show
  1. package/README.md +23 -20
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/0000_dry_moon_knight.sql +77 -0
  11. package/migrations/meta/0000_snapshot.json +248 -106
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +78 -26
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +90 -34
  127. package/worker/jmap/eventsource.ts +169 -0
  128. package/worker/jmap/methods.ts +1716 -361
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +207 -0
  131. package/worker/jmap/routes.ts +448 -86
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +84 -52
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +283 -171
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +871 -542
  184. package/worker/mail/safety-routes.ts +329 -202
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +153 -91
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +597 -227
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1761 -737
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +7 -14
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -126
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/migrations/0000_email.sql +0 -53
  218. package/shared/mail-rules.ts +0 -156
  219. package/worker/admin/report-routes.ts +0 -164
  220. package/worker/mail/reported-messages.ts +0 -68
  221. package/worker/mailbox/legacy-migrations.ts +0 -41
  222. package/worker/mailbox/migrations.ts +0 -10
  223. package/worker/mailbox/rules.ts +0 -110
@@ -1,4 +1,7 @@
1
+ import { emailSchema } from "@open-workspace/core/module";
1
2
  import { z } from "zod";
3
+
4
+ import { MOVABLE_SYSTEM_FOLDER_IDS } from "../../shared/folder-order";
2
5
  import {
3
6
  mailboxStates,
4
7
  MAX_BULK_CONVERSATION_COUNT,
@@ -12,8 +15,6 @@ import {
12
15
  MAX_TRANSLATION_CHARACTERS,
13
16
  MAX_TRANSLATION_SEGMENTS,
14
17
  } from "../../shared/mail-translation";
15
- import { emailSchema } from "@open-workspace/core/worker/auth/schemas.ts";
16
- import { hasActions, hasConditions, MAX_MAIL_RULES } from "../../shared/mail-rules";
17
18
  import { dedupeRecipientFields, recipientCount } from "./recipients";
18
19
 
19
20
  export const folderSchema = z.string().trim().min(1).max(80);
@@ -27,14 +28,15 @@ export const createFolderSchema = z.object({
27
28
  export const renameFolderSchema = createFolderSchema;
28
29
 
29
30
  export const mailboxAiConfigurationSchema = z.object({
30
- instructions: z.string().trim().max(4_000),
31
+ instructions: z.string().trim().max(4000),
31
32
  confidenceThreshold: z.number().int().min(50).max(100),
32
33
  });
33
34
 
35
+ /** Every folder but Inbox, in order: the movable system folders and the custom ones. */
34
36
  export const reorderFoldersSchema = z.object({
35
37
  folderIds: z
36
38
  .array(folderSchema)
37
- .max(MAX_CUSTOM_FOLDER_COUNT)
39
+ .max(MAX_CUSTOM_FOLDER_COUNT + MOVABLE_SYSTEM_FOLDER_IDS.length)
38
40
  .refine((ids) => new Set(ids).size === ids.length, {
39
41
  message: "Folder order contains duplicate ids",
40
42
  }),
@@ -50,7 +52,9 @@ export const recipientSuggestionQuerySchema = z.object({
50
52
  });
51
53
 
52
54
  export const remoteProxyQuerySchema = mailboxQuerySchema.extend({
53
- url: z.string().trim().min(1).max(4_096),
55
+ /** From the conversation response (`remoteTokens`, for this `url`); see remote-token.ts. */
56
+ token: z.string().min(1).max(512),
57
+ url: z.string().trim().min(1).max(4096),
54
58
  });
55
59
 
56
60
  export const conversationListQuerySchema = mailboxQuerySchema.extend({
@@ -73,9 +77,16 @@ export const messageReadSchema = z.object({
73
77
  });
74
78
 
75
79
  export const messageTranslationSchema = z.object({
76
- language: z.string().trim().min(2).max(35).regex(/^[a-zA-Z]{2,8}(?:-[a-zA-Z0-9]{1,8})*$/u),
77
- segments: z.array(z.string().trim().min(1).max(MAX_TRANSLATION_CHARACTERS))
78
- .min(1).max(MAX_TRANSLATION_SEGMENTS),
80
+ language: z
81
+ .string()
82
+ .trim()
83
+ .min(2)
84
+ .max(35)
85
+ .regex(/^[a-zA-Z]{2,8}(?:-[a-zA-Z0-9]{1,8})*$/u),
86
+ segments: z
87
+ .array(z.string().trim().min(1).max(MAX_TRANSLATION_CHARACTERS))
88
+ .min(1)
89
+ .max(MAX_TRANSLATION_SEGMENTS),
79
90
  });
80
91
 
81
92
  export const createUploadSchema = z.object({
@@ -91,32 +102,35 @@ export const uploadIdSchema = z
91
102
  .max(80)
92
103
  .regex(/^upl_[a-f0-9]{32}$/u, "Attachment upload id is invalid");
93
104
 
94
- export const outboundAttachmentSchema = z.object({
95
- uploadId: uploadIdSchema,
96
- disposition: z.enum(["attachment", "inline"]).default("attachment"),
97
- contentId: z
98
- .string()
99
- .trim()
100
- .min(1)
101
- .max(255)
102
- .regex(/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+$/u, "Content ID is invalid")
103
- .optional(),
104
- }).superRefine((attachment, context) => {
105
- if (attachment.disposition === "inline" && !attachment.contentId) {
106
- context.addIssue({
107
- code: "custom",
108
- path: ["contentId"],
109
- message: "Inline attachments require a content ID",
110
- });
111
- }
112
- if (attachment.disposition === "attachment" && attachment.contentId) {
113
- context.addIssue({
114
- code: "custom",
115
- path: ["contentId"],
116
- message: "Regular attachments cannot have a content ID",
117
- });
118
- }
119
- });
105
+ export const outboundAttachmentSchema = z
106
+ .object({
107
+ uploadId: uploadIdSchema,
108
+ disposition: z.enum(["attachment", "inline"]).default("attachment"),
109
+ contentId: z
110
+ .string()
111
+ .trim()
112
+ .min(1)
113
+ .max(255)
114
+ .regex(/^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+$/u, "Content ID is invalid")
115
+ .optional(),
116
+ })
117
+ .superRefine((attachment, context) => {
118
+ if (attachment.disposition === "inline" && !attachment.contentId) {
119
+ context.addIssue({
120
+ code: "custom",
121
+ path: ["contentId"],
122
+ message: "Inline attachments require a content ID",
123
+ });
124
+ }
125
+
126
+ if (attachment.disposition === "attachment" && attachment.contentId) {
127
+ context.addIssue({
128
+ code: "custom",
129
+ path: ["contentId"],
130
+ message: "Regular attachments cannot have a content ID",
131
+ });
132
+ }
133
+ });
120
134
 
121
135
  export const outboundMessageContentFields = {
122
136
  bodyText: z.string().max(500_000).default(""),
@@ -127,17 +141,19 @@ export const outboundMessageContentFields = {
127
141
  .default([])
128
142
  .refine(
129
143
  (attachments) =>
130
- new Set(attachments.map((file) => file.uploadId)).size === attachments.length,
131
- { message: "Duplicate attachment uploads are not allowed" },
144
+ new Set(attachments.map((file) => file.uploadId)).size ===
145
+ attachments.length,
146
+ { message: "Duplicate attachment uploads are not allowed" }
132
147
  )
133
148
  .refine(
134
149
  (attachments) => {
135
150
  const contentIds = attachments.flatMap((file) =>
136
151
  file.contentId ? [file.contentId.toLocaleLowerCase("en-US")] : []
137
152
  );
153
+
138
154
  return new Set(contentIds).size === contentIds.length;
139
155
  },
140
- { message: "Duplicate attachment content IDs are not allowed" },
156
+ { message: "Duplicate attachment content IDs are not allowed" }
141
157
  ),
142
158
  };
143
159
 
@@ -177,21 +193,21 @@ export const composeSchema = z
177
193
  ...dedupeRecipientFields(value),
178
194
  }))
179
195
  .refine(hasMessageBody, missingMessageBody)
180
- .refine(
181
- (value) => recipientCount(value) > 0,
182
- { message: "Add at least one recipient in To, Cc, or Bcc" },
183
- )
184
- .refine(
185
- (value) => recipientCount(value) <= MAX_MAIL_RECIPIENTS,
186
- { message: `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc` },
187
- );
196
+ .refine((value) => recipientCount(value) > 0, {
197
+ message: "Add at least one recipient in To, Cc, or Bcc",
198
+ })
199
+ .refine((value) => recipientCount(value) <= MAX_MAIL_RECIPIENTS, {
200
+ message: `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc`,
201
+ });
188
202
 
189
- export const replySchema = z.object({
190
- ...outboundRequestFields,
191
- mode: z.enum(replyActionModes),
192
- cc: z.array(emailSchema).max(MAX_MAIL_RECIPIENTS).optional(),
193
- bcc: z.array(emailSchema).max(MAX_MAIL_RECIPIENTS).default([]),
194
- }).refine(hasMessageBody, missingMessageBody);
203
+ export const replySchema = z
204
+ .object({
205
+ ...outboundRequestFields,
206
+ mode: z.enum(replyActionModes),
207
+ cc: z.array(emailSchema).max(MAX_MAIL_RECIPIENTS).optional(),
208
+ bcc: z.array(emailSchema).max(MAX_MAIL_RECIPIENTS).default([]),
209
+ })
210
+ .refine(hasMessageBody, missingMessageBody);
195
211
 
196
212
  export const forwardSchema = z
197
213
  .object({
@@ -203,14 +219,12 @@ export const forwardSchema = z
203
219
  ...value,
204
220
  ...dedupeRecipientFields(value),
205
221
  }))
206
- .refine(
207
- (value) => recipientCount(value) > 0,
208
- { message: "Add at least one recipient in To, Cc, or Bcc" },
209
- )
210
- .refine(
211
- (value) => recipientCount(value) <= MAX_MAIL_RECIPIENTS,
212
- { message: `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc` },
213
- );
222
+ .refine((value) => recipientCount(value) > 0, {
223
+ message: "Add at least one recipient in To, Cc, or Bcc",
224
+ })
225
+ .refine((value) => recipientCount(value) <= MAX_MAIL_RECIPIENTS, {
226
+ message: `An email can have at most ${MAX_MAIL_RECIPIENTS} recipients across To, Cc, and Bcc`,
227
+ });
214
228
 
215
229
  export const attachmentPreflightSchema = z.discriminatedUnion("kind", [
216
230
  z.object({
@@ -238,19 +252,22 @@ export const updateConversationSchema = z
238
252
  mailboxState: mailboxStateSchema.optional(),
239
253
  folderId: z.string().min(1).nullable().optional(),
240
254
  })
241
- .refine((value) => value.mailboxState !== undefined || value.folderId !== undefined, {
242
- message: "No changes supplied",
243
- })
255
+ .refine(
256
+ (value) => value.mailboxState !== undefined || value.folderId !== undefined,
257
+ {
258
+ message: "No changes supplied",
259
+ }
260
+ )
244
261
  .refine(
245
262
  (value) =>
246
- value.folderId === undefined
247
- || value.folderId === null
248
- || value.mailboxState === undefined
249
- || value.mailboxState === "active",
263
+ value.folderId === undefined ||
264
+ value.folderId === null ||
265
+ value.mailboxState === undefined ||
266
+ value.mailboxState === "active",
250
267
  {
251
268
  path: ["mailboxState"],
252
269
  message: "Moving to a custom folder requires the active mailbox state",
253
- },
270
+ }
254
271
  );
255
272
 
256
273
  const bulkConversationIdsSchema = z
@@ -277,39 +294,6 @@ export const bulkConversationActionSchema = z.discriminatedUnion("type", [
277
294
  }),
278
295
  ]);
279
296
 
280
- const ruleText = z.string().trim().max(200).optional().transform((value) => value || undefined);
281
-
282
- export const mailRuleSchema = z.object({
283
- name: z.string().trim().min(1).max(80),
284
- enabled: z.boolean().default(true),
285
- conditions: z.object({
286
- from: ruleText,
287
- to: ruleText,
288
- subjectContains: ruleText,
289
- listId: ruleText,
290
- hasAttachment: z.boolean().optional().transform((value) => value || undefined),
291
- }).refine(hasConditions, { message: "Add at least one condition" }),
292
- actions: z.object({
293
- folderId: z.string().trim().min(1).max(255).optional(),
294
- archive: z.boolean().optional().transform((value) => value || undefined),
295
- spam: z.boolean().optional().transform((value) => value || undefined),
296
- markRead: z.boolean().optional().transform((value) => value || undefined),
297
- neverSpam: z.boolean().optional().transform((value) => value || undefined),
298
- })
299
- .refine(hasActions, { message: "Choose at least one action" })
300
- .refine(
301
- (actions) => [actions.folderId, actions.archive, actions.spam].filter(Boolean).length <= 1,
302
- { message: "Choose one destination: a folder, Archive, or Spam" },
303
- )
304
- .refine((actions) => !(actions.spam && actions.neverSpam), {
305
- message: "A rule cannot both send to Spam and never send to Spam",
306
- }),
307
- });
308
-
309
- export const reorderRulesSchema = z.object({
310
- ruleIds: z.array(z.string().min(1).max(255)).max(MAX_MAIL_RULES),
311
- });
312
-
313
297
  export const blockSenderSchema = z.object({
314
298
  pattern: z.string().trim().min(3).max(320),
315
299
  moveExisting: z.boolean().default(false),
@@ -329,11 +313,6 @@ export const unsubscribeSchema = z.object({
329
313
  method: z.enum(["auto", "link"]).default("auto"),
330
314
  });
331
315
 
332
- export const reportMessageSchema = z.object({
333
- kind: z.enum(["spam", "phishing"]),
334
- });
335
-
336
316
  export const bulkReportSchema = z.object({
337
- kind: z.enum(["spam", "phishing"]),
338
317
  conversationIds: z.array(z.string().trim().min(1).max(255)).min(1).max(100),
339
318
  });
@@ -8,6 +8,7 @@ const MAX_SEARCH_TOKENS = 24;
8
8
  export function buildEmailSearchQuery(value: string) {
9
9
  const tokens = value.normalize("NFKC").match(SEARCH_TOKEN_PATTERN) ?? [];
10
10
  const unique = [...new Set(tokens)].slice(0, MAX_SEARCH_TOKENS);
11
+
11
12
  return unique.length
12
13
  ? unique.map((token) => `"${token}"*`).join(" AND ")
13
14
  : null;
@@ -1,7 +1,13 @@
1
+ import {
2
+ configuredMailDomains,
3
+ createDb,
4
+ mailboxes,
5
+ users,
6
+ } from "@open-workspace/core/module";
1
7
  import { inArray, isNotNull } from "drizzle-orm";
2
- import { createDb } from "@open-workspace/core/worker/db/client.ts";
3
- import { domains, mailboxes, users } from "@open-workspace/core/worker/db/schema.ts";
8
+
4
9
  import { protectedNames } from "./impersonation";
10
+ import { isolateCache, WORKSPACE_CACHE_TTL_MS } from "./isolate-cache";
5
11
  import { DQS_KEY_PATTERN } from "./spamhaus";
6
12
 
7
13
  /** Workspace-wide inputs for sender checks, read from D1 once per request or delivery. */
@@ -11,28 +17,54 @@ export type WorkspaceIdentity = {
11
17
  names: Map<string, string>;
12
18
  };
13
19
 
14
- export async function workspaceIdentity(binding: D1Database): Promise<WorkspaceIdentity> {
20
+ export async function workspaceIdentity(
21
+ binding: D1Database
22
+ ): Promise<WorkspaceIdentity> {
15
23
  const db = createDb(binding);
16
- const [domainRows, userRows, mailboxRows] = await db.batch([
17
- db.select({ name: domains.name }).from(domains),
18
- db.select({ name: users.name }).from(users).where(inArray(users.status, ["active", "invited"])),
24
+
25
+ const [userRows, mailboxRows] = await db.batch([
26
+ db
27
+ .select({ name: users.name })
28
+ .from(users)
29
+ .where(inArray(users.status, ["active", "invited"])),
19
30
  // Shared mailboxes ("Support", "Customer care") are generic names that
20
31
  // vendors use too; only personal mailboxes name a person.
21
- db.select({ name: mailboxes.displayName }).from(mailboxes).where(isNotNull(mailboxes.ownerUserId)),
32
+ db
33
+ .select({ name: mailboxes.displayName })
34
+ .from(mailboxes)
35
+ .where(isNotNull(mailboxes.ownerUserId)),
22
36
  ]);
37
+
23
38
  return {
24
- domains: domainRows.map((row) => row.name.toLocaleLowerCase()),
39
+ domains: [...configuredMailDomains()],
25
40
  names: protectedNames([...userRows, ...mailboxRows].map((row) => row.name)),
26
41
  };
27
42
  }
28
43
 
44
+ const identityCache = isolateCache(WORKSPACE_CACHE_TTL_MS, workspaceIdentity);
45
+
46
+ /**
47
+ * `workspaceIdentity` reused for up to a minute in this isolate, for showing
48
+ * mail: a new person or domain reaches the reader's impersonation and
49
+ * external-sender hints within that minute. Delivery reads it fresh.
50
+ */
51
+ export function cachedWorkspaceIdentity(binding: D1Database) {
52
+ return identityCache.get(binding);
53
+ }
54
+
29
55
  /** The Spamhaus DQS key from the SPAMHAUS_DQS_KEY secret, when set and well-formed. */
30
56
  export function spamhausDqsKey(env: { SPAMHAUS_DQS_KEY?: string }) {
31
57
  const key = env.SPAMHAUS_DQS_KEY?.trim().toLowerCase();
58
+
32
59
  if (!key) return null;
60
+
33
61
  if (!DQS_KEY_PATTERN.test(key)) {
34
- console.error("SPAMHAUS_DQS_KEY is not a Spamhaus DQS key; blocklist checks are off");
62
+ console.error(
63
+ "SPAMHAUS_DQS_KEY is not a Spamhaus DQS key; blocklist checks are off"
64
+ );
65
+
35
66
  return null;
36
67
  }
68
+
37
69
  return key;
38
70
  }