@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,8 +1,8 @@
1
- import {
2
- blockPatternsFor,
3
- type FeedbackLabel,
4
- type SenderBlock,
5
- type SenderStatus,
1
+ import { blockPatternsFor } from "../../shared/mail-feedback";
2
+ import type {
3
+ FeedbackLabel,
4
+ SenderBlock,
5
+ SenderStatus,
6
6
  } from "../../shared/mail-feedback";
7
7
  import type { SenderHistory } from "../mail/message-security";
8
8
  import type { NewEmail } from "./schema";
@@ -10,7 +10,10 @@ import type { NewEmail } from "./schema";
10
10
  function normalized(address: string | null | undefined) {
11
11
  const value = address?.trim().toLocaleLowerCase() ?? "";
12
12
  const at = value.lastIndexOf("@");
13
- return at > 0 && at < value.length - 1 ? { address: value, domain: value.slice(at + 1) } : null;
13
+
14
+ return at > 0 && at < value.length - 1
15
+ ? { address: value, domain: value.slice(at + 1) }
16
+ : null;
14
17
  }
15
18
 
16
19
  function timestamp(value: NewEmail["timelineAt"]) {
@@ -23,8 +26,10 @@ function timestamp(value: NewEmail["timelineAt"]) {
23
26
  */
24
27
  export function recordCorrespondents(sql: SqlStorage, email: NewEmail) {
25
28
  const at = timestamp(email.timelineAt);
29
+
26
30
  if (email.direction === "incoming") {
27
31
  const sender = normalized(email.fromJson[0]?.address);
32
+
28
33
  if (!sender) return;
29
34
  sql.exec(
30
35
  `insert into senders (address, domain, first_seen_at, last_seen_at, message_count)
@@ -36,15 +41,24 @@ export function recordCorrespondents(sql: SqlStorage, email: NewEmail) {
36
41
  sender.address,
37
42
  sender.domain,
38
43
  at,
39
- at,
44
+ at
40
45
  );
46
+
41
47
  return;
42
48
  }
49
+
43
50
  const recipients = new Map<string, string>();
44
- for (const recipient of [...(email.toJson ?? []), ...(email.ccJson ?? []), ...(email.bccJson ?? [])]) {
51
+
52
+ for (const recipient of [
53
+ ...(email.toJson ?? []),
54
+ ...(email.ccJson ?? []),
55
+ ...(email.bccJson ?? []),
56
+ ]) {
45
57
  const value = normalized(recipient.address);
58
+
46
59
  if (value) recipients.set(value.address, value.domain);
47
60
  }
61
+
48
62
  for (const [address, domain] of recipients) {
49
63
  sql.exec(
50
64
  `insert into senders (address, domain, outbound_count, last_outbound_at)
@@ -54,7 +68,7 @@ export function recordCorrespondents(sql: SqlStorage, email: NewEmail) {
54
68
  last_outbound_at = max(coalesce(last_outbound_at, excluded.last_outbound_at), excluded.last_outbound_at)`,
55
69
  address,
56
70
  domain,
57
- at,
71
+ at
58
72
  );
59
73
  }
60
74
  }
@@ -66,60 +80,115 @@ type SenderHistoryRow = {
66
80
  };
67
81
 
68
82
  export function senderHistory(sql: SqlStorage, addresses: string[]) {
69
- const unique = [...new Set(addresses.map((address) => address.toLocaleLowerCase()))].slice(0, 100);
83
+ const unique = [
84
+ ...new Set(addresses.map((address) => address.toLocaleLowerCase())),
85
+ ].slice(0, 100);
86
+
70
87
  const result = new Map<string, SenderHistory>();
88
+
71
89
  if (!unique.length) return result;
72
- const rows = sql.exec<SenderHistoryRow>(
73
- `select address, message_count, outbound_count from senders where address in (${unique.map(() => "?").join(", ")})`,
74
- ...unique,
75
- ).toArray();
90
+
91
+ const rows = sql
92
+ .exec<SenderHistoryRow>(
93
+ `select address, message_count, outbound_count from senders where address in (${unique.map(() => "?").join(", ")})`,
94
+ ...unique
95
+ )
96
+ .toArray();
97
+
76
98
  for (const row of rows) {
77
99
  result.set(row.address, {
78
100
  inboundCount: Number(row.message_count),
79
101
  outboundCount: Number(row.outbound_count),
80
102
  });
81
103
  }
104
+
82
105
  return result;
83
106
  }
84
107
 
85
108
  type BlockRow = { address: string; blocked_at: number };
86
109
 
87
110
  /** The most specific mailbox block covering an address, if any. */
88
- export function mailboxBlockFor(sql: SqlStorage, address: string): SenderBlock | null {
111
+ export function mailboxBlockFor(
112
+ sql: SqlStorage,
113
+ address: string
114
+ ): SenderBlock | null {
89
115
  const patterns = blockPatternsFor(address);
116
+
90
117
  if (!patterns.length) return null;
91
- const rows = sql.exec<BlockRow>(
92
- `select address, blocked_at from senders
118
+
119
+ const rows = sql
120
+ .exec<BlockRow>(
121
+ `select address, blocked_at from senders
93
122
  where blocked_at is not null and address in (${patterns.map(() => "?").join(", ")})`,
94
- ...patterns,
95
- ).toArray();
96
- const pattern = patterns.find((candidate) => rows.some((row) => row.address === candidate));
123
+ ...patterns
124
+ )
125
+ .toArray();
126
+
127
+ const pattern = patterns.find((candidate) =>
128
+ rows.some((row) => row.address === candidate)
129
+ );
130
+
97
131
  const row = rows.find((candidate) => candidate.address === pattern);
98
- return row ? blockFromPattern(row.address, Number(row.blocked_at), false) : null;
132
+
133
+ return row
134
+ ? blockFromPattern(row.address, Number(row.blocked_at), false)
135
+ : null;
99
136
  }
100
137
 
101
- export function blockFromPattern(pattern: string, at: number, workspace: boolean): SenderBlock {
102
- return { pattern, scope: pattern.startsWith("@") ? "domain" : "address", workspace, at };
138
+ export function blockFromPattern(
139
+ pattern: string,
140
+ at: number,
141
+ workspace: boolean
142
+ ): SenderBlock {
143
+ return {
144
+ pattern,
145
+ scope: pattern.startsWith("@") ? "domain" : "address",
146
+ workspace,
147
+ at,
148
+ };
103
149
  }
104
150
 
105
151
  /** First workspace-wide pattern (address before domains) that covers an address. */
106
- export function workspaceBlockFor(address: string, patterns: ReadonlyMap<string, number>) {
107
- const pattern = blockPatternsFor(address).find((candidate) => patterns.has(candidate));
108
- return pattern ? blockFromPattern(pattern, patterns.get(pattern)!, true) : null;
152
+ export function workspaceBlockFor(
153
+ address: string,
154
+ patterns: ReadonlyMap<string, number>
155
+ ) {
156
+ for (const pattern of blockPatternsFor(address)) {
157
+ const blockedAt = patterns.get(pattern);
158
+
159
+ if (blockedAt !== undefined) {
160
+ return blockFromPattern(pattern, blockedAt, true);
161
+ }
162
+ }
163
+
164
+ return null;
109
165
  }
110
166
 
111
- type StatusRow = { address: string; trusted_at: number | null; reported_at: number | null };
167
+ type StatusRow = {
168
+ address: string;
169
+ trusted_at: number | null;
170
+ reported_at: number | null;
171
+ };
112
172
 
113
173
  /** Block, trust and report state of each address in this mailbox (lowercased keys). */
114
174
  export function senderStatuses(sql: SqlStorage, addresses: string[]) {
115
- const unique = [...new Set(addresses.map((address) => address.toLocaleLowerCase()))].slice(0, 100);
175
+ const unique = [
176
+ ...new Set(addresses.map((address) => address.toLocaleLowerCase())),
177
+ ].slice(0, 100);
178
+
116
179
  const result = new Map<string, SenderStatus>();
180
+
117
181
  if (!unique.length) return result;
118
- const rows = sql.exec<StatusRow>(
119
- `select address, trusted_at, reported_at from senders where address in (${unique.map(() => "?").join(", ")})`,
120
- ...unique,
121
- ).toArray();
182
+
183
+ const rows = sql
184
+ .exec<StatusRow>(
185
+ `select address, trusted_at, reported_at from senders where address in (${unique.map(() => "?").join(", ")})`,
186
+ ...unique
187
+ )
188
+ .toArray();
189
+
122
190
  const byAddress = new Map(rows.map((row) => [row.address, row]));
191
+
123
192
  for (const address of unique) {
124
193
  const row = byAddress.get(address);
125
194
  result.set(address, {
@@ -128,41 +197,59 @@ export function senderStatuses(sql: SqlStorage, addresses: string[]) {
128
197
  reportedAt: row?.reported_at == null ? null : Number(row.reported_at),
129
198
  });
130
199
  }
200
+
131
201
  return result;
132
202
  }
133
203
 
134
204
  function patternDomain(pattern: string) {
135
- return pattern.startsWith("@") ? pattern.slice(1) : pattern.slice(pattern.lastIndexOf("@") + 1);
205
+ return pattern.startsWith("@")
206
+ ? pattern.slice(1)
207
+ : pattern.slice(pattern.lastIndexOf("@") + 1);
136
208
  }
137
209
 
138
210
  /** Blocking clears trust: the two decisions are mutually exclusive. */
139
- export function setSenderBlocked(sql: SqlStorage, pattern: string, blocked: boolean, now: number) {
211
+ export function setSenderBlocked(
212
+ sql: SqlStorage,
213
+ pattern: string,
214
+ blocked: boolean,
215
+ now: number
216
+ ) {
140
217
  if (!blocked) {
141
218
  sql.exec("update senders set blocked_at = null where address = ?", pattern);
219
+
142
220
  return;
143
221
  }
222
+
144
223
  sql.exec(
145
224
  `insert into senders (address, domain, blocked_at) values (?, ?, ?)
146
225
  on conflict (address) do update set blocked_at = excluded.blocked_at, trusted_at = null`,
147
226
  pattern,
148
227
  patternDomain(pattern),
149
- now,
228
+ now
150
229
  );
151
230
  }
152
231
 
153
232
  /** Trusting an address also lifts a block on that exact address. */
154
- export function setSenderTrusted(sql: SqlStorage, address: string, trusted: boolean, now: number) {
233
+ export function setSenderTrusted(
234
+ sql: SqlStorage,
235
+ address: string,
236
+ trusted: boolean,
237
+ now: number
238
+ ) {
155
239
  const value = address.toLocaleLowerCase();
240
+
156
241
  if (!trusted) {
157
242
  sql.exec("update senders set trusted_at = null where address = ?", value);
243
+
158
244
  return;
159
245
  }
246
+
160
247
  sql.exec(
161
248
  `insert into senders (address, domain, trusted_at) values (?, ?, ?)
162
249
  on conflict (address) do update set trusted_at = excluded.trusted_at, blocked_at = null`,
163
250
  value,
164
251
  patternDomain(value),
165
- now,
252
+ now
166
253
  );
167
254
  }
168
255
 
@@ -174,24 +261,30 @@ export type BlockedSenderItem = {
174
261
  };
175
262
 
176
263
  export function listBlockedSenders(sql: SqlStorage): BlockedSenderItem[] {
177
- return sql.exec<{ address: string; blocked_at: number; message_count: number }>(
178
- "select address, blocked_at, message_count from senders where blocked_at is not null order by blocked_at desc limit 500",
179
- ).toArray().map((row) => ({
180
- pattern: row.address,
181
- scope: row.address.startsWith("@") ? "domain" : "address",
182
- blockedAt: Number(row.blocked_at),
183
- messageCount: Number(row.message_count),
184
- }));
264
+ return sql
265
+ .exec<{ address: string; blocked_at: number; message_count: number }>(
266
+ "select address, blocked_at, message_count from senders where blocked_at is not null order by blocked_at desc limit 500"
267
+ )
268
+ .toArray()
269
+ .map((row) => ({
270
+ pattern: row.address,
271
+ scope: row.address.startsWith("@") ? "domain" : "address",
272
+ blockedAt: Number(row.blocked_at),
273
+ messageCount: Number(row.message_count),
274
+ }));
185
275
  }
186
276
 
187
- export function recordSenderFeedback(sql: SqlStorage, input: {
188
- id: string;
189
- address: string;
190
- emailId: string | null;
191
- label: FeedbackLabel;
192
- userId: string | null;
193
- now: number;
194
- }) {
277
+ export function recordSenderFeedback(
278
+ sql: SqlStorage,
279
+ input: {
280
+ id: string;
281
+ address: string;
282
+ emailId: string | null;
283
+ label: FeedbackLabel;
284
+ userId: string | null;
285
+ now: number;
286
+ }
287
+ ) {
195
288
  const address = input.address.toLocaleLowerCase();
196
289
  const domain = patternDomain(address);
197
290
  sql.exec(
@@ -203,37 +296,50 @@ export function recordSenderFeedback(sql: SqlStorage, input: {
203
296
  input.emailId,
204
297
  input.label,
205
298
  input.userId,
206
- input.now,
299
+ input.now
207
300
  );
301
+
208
302
  if (input.label === "not_spam") return;
209
303
  sql.exec(
210
304
  `insert into senders (address, domain, reported_at) values (?, ?, ?)
211
305
  on conflict (address) do update set reported_at = excluded.reported_at, trusted_at = null`,
212
306
  address,
213
307
  domain,
214
- input.now,
308
+ input.now
215
309
  );
216
310
  }
217
311
 
218
- /** Report history for the classifier: what people in this mailbox said about the sender before. */
312
+ /**
313
+ * Report history for the classifier: what people in this mailbox said about
314
+ * the sender before. Every label other than "not_spam" counts as a report.
315
+ */
219
316
  export function senderFeedbackSummary(sql: SqlStorage, address: string) {
220
317
  const value = address.toLocaleLowerCase();
221
318
  const domain = patternDomain(value);
222
- const rows = sql.exec<{ label: FeedbackLabel; same_address: number; count: number }>(
223
- `select label, address = ? as same_address, count(*) as count
224
- from sender_feedback where domain = ? group by label, address = ?`,
225
- value,
226
- domain,
227
- value,
228
- ).toArray();
229
- const count = (label: FeedbackLabel, sameAddress: boolean) => rows
230
- .filter((row) => row.label === label && (!sameAddress || Boolean(row.same_address)))
231
- .reduce((total, row) => total + Number(row.count), 0);
319
+
320
+ const rows = sql
321
+ .exec<{ not_spam: number; same_address: number; count: number }>(
322
+ `select label = 'not_spam' as not_spam, address = ? as same_address, count(*) as count
323
+ from sender_feedback where domain = ? group by label = 'not_spam', address = ?`,
324
+ value,
325
+ domain,
326
+ value
327
+ )
328
+ .toArray();
329
+
330
+ const count = (notSpam: boolean, sameAddress: boolean) =>
331
+ rows
332
+ .filter(
333
+ (row) =>
334
+ Boolean(row.not_spam) === notSpam &&
335
+ (!sameAddress || Boolean(row.same_address))
336
+ )
337
+ .reduce((total, row) => total + Number(row.count), 0);
338
+
232
339
  return {
233
- spamReports: count("spam", true),
234
- phishingReports: count("phishing", true),
235
- notSpam: count("not_spam", true),
236
- domainReports: count("spam", false) + count("phishing", false),
340
+ spamReports: count(false, true),
341
+ notSpam: count(true, true),
342
+ domainReports: count(false, false),
237
343
  };
238
344
  }
239
345