@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.
- package/README.md +23 -20
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/0000_dry_moon_knight.sql +77 -0
- package/migrations/meta/0000_snapshot.json +248 -106
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +78 -26
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +90 -34
- package/worker/jmap/eventsource.ts +169 -0
- package/worker/jmap/methods.ts +1716 -361
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +207 -0
- package/worker/jmap/routes.ts +448 -86
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +84 -52
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +283 -171
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +871 -542
- package/worker/mail/safety-routes.ts +329 -202
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +153 -91
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +597 -227
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1761 -737
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +7 -14
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -126
- package/containers/office-preview/Dockerfile +0 -10
- package/migrations/0000_email.sql +0 -53
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/legacy-migrations.ts +0 -41
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 = [
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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(
|
|
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
|
-
|
|
92
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
132
|
+
|
|
133
|
+
return row
|
|
134
|
+
? blockFromPattern(row.address, Number(row.blocked_at), false)
|
|
135
|
+
: null;
|
|
99
136
|
}
|
|
100
137
|
|
|
101
|
-
export function blockFromPattern(
|
|
102
|
-
|
|
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(
|
|
107
|
-
|
|
108
|
-
|
|
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 = {
|
|
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 = [
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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("@")
|
|
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(
|
|
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(
|
|
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
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
.
|
|
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(
|
|
234
|
-
|
|
235
|
-
|
|
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
|
|