@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.
- package/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail-sender-intel.ts +5 -1
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +52 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +391 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +12 -11
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +758 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +119 -1816
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +700 -0
- package/worker/mail/auth-headers.ts +57 -43
- package/worker/mail/auto-reply-routes.ts +260 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound-threading.ts +15 -2
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +19 -8
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +698 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +206 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +316 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +455 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +253 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +161 -1
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { inJsonList } from "@open-workspace/core/module";
|
|
2
|
+
/** The mailbox as JMAP sees it: state, changes, queries and moves. */
|
|
3
|
+
import { eq } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import { DRAFTS_FOLDER_ID } from "../../shared/folder-order";
|
|
6
|
+
import {
|
|
7
|
+
changedSince,
|
|
8
|
+
currentSeq,
|
|
9
|
+
jmapChanges as jmapChangeLog,
|
|
10
|
+
jmapState as jmapStateOf,
|
|
11
|
+
stateOf,
|
|
12
|
+
stateSeq,
|
|
13
|
+
} from "./jmap-change-log";
|
|
14
|
+
import { mailboxIds, mailboxes } from "./jmap-mailboxes";
|
|
15
|
+
import { placementForChange } from "./jmap-placement";
|
|
16
|
+
import type { MailboxChange } from "./jmap-placement";
|
|
17
|
+
import {
|
|
18
|
+
countQueryEmails,
|
|
19
|
+
emailPlacement as jmapEmailPlacement,
|
|
20
|
+
queryChangedEmails,
|
|
21
|
+
queryEmails as queryJmapEmails,
|
|
22
|
+
threadEmails as jmapThreadEmails,
|
|
23
|
+
} from "./jmap-store";
|
|
24
|
+
import {
|
|
25
|
+
blocksForLeavingSpam,
|
|
26
|
+
placeConversations,
|
|
27
|
+
placementRank,
|
|
28
|
+
} from "./placement";
|
|
29
|
+
import type { Placement, SpamVerdict } from "./placement";
|
|
30
|
+
import { emails, folders } from "./schema";
|
|
31
|
+
import type { MailboxContext } from "./support";
|
|
32
|
+
|
|
33
|
+
export function jmapState(m: MailboxContext) {
|
|
34
|
+
return jmapStateOf(m.db);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Foo/changes for this person; null: resync instead. */
|
|
38
|
+
export function jmapChanges(
|
|
39
|
+
m: MailboxContext,
|
|
40
|
+
input: Parameters<typeof jmapChangeLog>[1]
|
|
41
|
+
) {
|
|
42
|
+
const changes = jmapChangeLog(m, input);
|
|
43
|
+
|
|
44
|
+
if (!changes || input.type !== "Mailbox" || !changes.emailsChanged) {
|
|
45
|
+
return changes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Mail changed, so any mailbox's counts may have: all of them updated.
|
|
49
|
+
const updated = mailboxIds(m).filter(
|
|
50
|
+
(id) => !changes.created.includes(id) && !changes.destroyed.includes(id)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// More than the client takes at once: it reads the mailboxes again.
|
|
54
|
+
if (
|
|
55
|
+
input.maxChanges !== null &&
|
|
56
|
+
changes.created.length + changes.destroyed.length + updated.length >
|
|
57
|
+
input.maxChanges
|
|
58
|
+
) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ...changes, updated };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function jmapMailboxes(
|
|
66
|
+
m: MailboxContext,
|
|
67
|
+
userId: string,
|
|
68
|
+
withCounts: boolean
|
|
69
|
+
) {
|
|
70
|
+
return {
|
|
71
|
+
state: jmapStateOf(m.db),
|
|
72
|
+
mailboxes: mailboxes(m, userId, withCounts),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function jmapQueryEmails(
|
|
77
|
+
m: MailboxContext,
|
|
78
|
+
input: Parameters<typeof queryJmapEmails>[1]
|
|
79
|
+
) {
|
|
80
|
+
const page = queryJmapEmails(m, input);
|
|
81
|
+
|
|
82
|
+
return page && { ...page, state: jmapStateOf(m.db) };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Email/queryChanges (RFC 8620 §5.6): every email that changed since the
|
|
87
|
+
* state is removed and, where the query lists it now, added back at its
|
|
88
|
+
* index. Emails that didn't change can't have joined, left or moved in
|
|
89
|
+
* the list, so this turns the old list into the current one. With
|
|
90
|
+
* collapseThreads a changed email can change its thread's representative,
|
|
91
|
+
* so every email of a changed thread counts as changed. Null: resync.
|
|
92
|
+
*/
|
|
93
|
+
export function jmapQueryChanges(
|
|
94
|
+
m: MailboxContext,
|
|
95
|
+
input: Parameters<typeof queryChangedEmails>[1] & {
|
|
96
|
+
sinceQueryState: string;
|
|
97
|
+
}
|
|
98
|
+
) {
|
|
99
|
+
const current = currentSeq(m.db);
|
|
100
|
+
const state = stateOf(current);
|
|
101
|
+
|
|
102
|
+
const unchanged = () => ({
|
|
103
|
+
state,
|
|
104
|
+
removed: [],
|
|
105
|
+
added: [],
|
|
106
|
+
total: input.calculateTotal ? countQueryEmails(m, input) : null,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (input.sinceQueryState === state) return unchanged();
|
|
110
|
+
const since = stateSeq(m.db, input.sinceQueryState, current);
|
|
111
|
+
|
|
112
|
+
if (since === null) return null;
|
|
113
|
+
const changed = changedSince(m.db, "Email", since, input.userId);
|
|
114
|
+
const removed = new Set(changed);
|
|
115
|
+
|
|
116
|
+
const collapse =
|
|
117
|
+
input.collapseThreads && input.filter.inMailbox !== DRAFTS_FOLDER_ID;
|
|
118
|
+
|
|
119
|
+
const threads = collapse
|
|
120
|
+
? changedSince(m.db, "Thread", since, input.userId)
|
|
121
|
+
: new Set<string>();
|
|
122
|
+
|
|
123
|
+
if (!changed.size && !threads.size) return unchanged();
|
|
124
|
+
|
|
125
|
+
if (collapse) {
|
|
126
|
+
for (const row of m.db
|
|
127
|
+
.select({ conversationId: emails.conversationId })
|
|
128
|
+
.from(emails)
|
|
129
|
+
.where(inJsonList(emails.id, [...changed]))
|
|
130
|
+
.all()) {
|
|
131
|
+
threads.add(row.conversationId);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const row of m.db
|
|
135
|
+
.select({ id: emails.id })
|
|
136
|
+
.from(emails)
|
|
137
|
+
.where(inJsonList(emails.conversationId, [...threads]))
|
|
138
|
+
.all()) {
|
|
139
|
+
removed.add(row.id);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const { added, total } = queryChangedEmails(m, input, removed, threads);
|
|
144
|
+
|
|
145
|
+
for (const { id } of added) removed.add(id);
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
state,
|
|
149
|
+
removed: [...removed],
|
|
150
|
+
added,
|
|
151
|
+
total,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function jmapThreads(m: MailboxContext, ids: string[], userId: string) {
|
|
156
|
+
return {
|
|
157
|
+
state: jmapStateOf(m.db),
|
|
158
|
+
threads: [...jmapThreadEmails(m, ids, userId)].map(([id, emailIds]) => ({
|
|
159
|
+
id,
|
|
160
|
+
emailIds,
|
|
161
|
+
})),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Applies Email/set changes to emails' Mailboxes, as `by.userId` asks
|
|
167
|
+
* (`placeConversations`: into or out of Junk is their verdict, trusting
|
|
168
|
+
* senders only with `by.trustSenders`). Each
|
|
169
|
+
* conversation is filed once, by the strongest placement its emails ask
|
|
170
|
+
* for (`placementForChange`). `refused` names emails whose change can't
|
|
171
|
+
* apply, or whose conversation can't go there (sent-only mail stays out of
|
|
172
|
+
* the Inbox, as in the app); `notFound` those that aren't here.
|
|
173
|
+
*/
|
|
174
|
+
export async function jmapChangeMailboxes(
|
|
175
|
+
m: MailboxContext,
|
|
176
|
+
changes: { emailId: string; change: MailboxChange }[],
|
|
177
|
+
by: { userId: string; trustSenders: boolean }
|
|
178
|
+
) {
|
|
179
|
+
const current = jmapEmailPlacement(
|
|
180
|
+
m.db,
|
|
181
|
+
by.userId,
|
|
182
|
+
changes.map((item) => item.emailId)
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const refused: string[] = [];
|
|
186
|
+
const notFound: string[] = [];
|
|
187
|
+
const asked = new Map<string, { placement: Placement; emailIds: string[] }>();
|
|
188
|
+
|
|
189
|
+
// A bulk move usually names the same destination for every message.
|
|
190
|
+
const knownFolders = new Map<string, boolean>();
|
|
191
|
+
|
|
192
|
+
const folderExists = (id: string) => {
|
|
193
|
+
const known = knownFolders.get(id);
|
|
194
|
+
|
|
195
|
+
if (known !== undefined) return known;
|
|
196
|
+
|
|
197
|
+
const exists = Boolean(
|
|
198
|
+
m.db
|
|
199
|
+
.select({ id: folders.id })
|
|
200
|
+
.from(folders)
|
|
201
|
+
.where(eq(folders.id, id))
|
|
202
|
+
.get()
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
knownFolders.set(id, exists);
|
|
206
|
+
|
|
207
|
+
return exists;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
for (const { emailId, change } of changes) {
|
|
211
|
+
const place = current.get(emailId);
|
|
212
|
+
|
|
213
|
+
if (!place) {
|
|
214
|
+
notFound.push(emailId);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const { conversationId } = place;
|
|
219
|
+
const placement = placementForChange(folderExists, place, change);
|
|
220
|
+
|
|
221
|
+
if (placement === "unchanged") continue;
|
|
222
|
+
|
|
223
|
+
if (!placement) {
|
|
224
|
+
refused.push(emailId);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const known = asked.get(conversationId);
|
|
229
|
+
|
|
230
|
+
if (known) {
|
|
231
|
+
known.emailIds.push(emailId);
|
|
232
|
+
|
|
233
|
+
if (placementRank(placement) > placementRank(known.placement)) {
|
|
234
|
+
known.placement = placement;
|
|
235
|
+
}
|
|
236
|
+
} else asked.set(conversationId, { placement, emailIds: [emailId] });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Conversations bound for the same place move together.
|
|
240
|
+
const groups = new Map<
|
|
241
|
+
string,
|
|
242
|
+
{ placement: Placement; conversationIds: string[]; emailIds: string[] }
|
|
243
|
+
>();
|
|
244
|
+
|
|
245
|
+
for (const [conversationId, { placement, emailIds }] of asked) {
|
|
246
|
+
const key = JSON.stringify(placement);
|
|
247
|
+
|
|
248
|
+
const group = groups.get(key) ?? {
|
|
249
|
+
placement,
|
|
250
|
+
conversationIds: [],
|
|
251
|
+
emailIds: [],
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
group.conversationIds.push(conversationId);
|
|
255
|
+
group.emailIds.push(...emailIds);
|
|
256
|
+
groups.set(key, group);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const verdicts: SpamVerdict[] = [];
|
|
260
|
+
|
|
261
|
+
const workspaceBlocks = await blocksForLeavingSpam(
|
|
262
|
+
m,
|
|
263
|
+
[...groups.values()].map((group) => ({
|
|
264
|
+
conversationIds: group.conversationIds,
|
|
265
|
+
mailboxState: group.placement.mailboxState,
|
|
266
|
+
}))
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
for (const group of groups.values()) {
|
|
270
|
+
// Into Junk go the emails named (and their senders'), not the thread.
|
|
271
|
+
const placed = placeConversations(
|
|
272
|
+
m,
|
|
273
|
+
group.conversationIds,
|
|
274
|
+
group.placement,
|
|
275
|
+
{
|
|
276
|
+
...by,
|
|
277
|
+
workspaceBlocks,
|
|
278
|
+
emailIds:
|
|
279
|
+
group.placement.mailboxState === "spam" ? group.emailIds : undefined,
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
verdicts.push(...placed.verdicts);
|
|
284
|
+
const moved = new Set(placed.moved);
|
|
285
|
+
|
|
286
|
+
refused.push(
|
|
287
|
+
...group.emailIds.filter(
|
|
288
|
+
(emailId) => !moved.has(current.get(emailId)?.conversationId ?? "")
|
|
289
|
+
)
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return { verdicts, refused, notFound };
|
|
294
|
+
}
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/** Sent mail: storing it, handing it to Email Sending once at a time, and its delivery statuses. */
|
|
2
|
+
import { and, eq } from "drizzle-orm";
|
|
3
|
+
|
|
4
|
+
import { sendArchived } from "../mail/archive";
|
|
5
|
+
import { refreshDownloadLinks } from "../mail/download-links";
|
|
6
|
+
import { prepareOutboundDelivery } from "../mail/outbound-delivery";
|
|
7
|
+
import { storedMessageId } from "../mail/rfc";
|
|
8
|
+
import { rawMessageKey } from "../mail/storage-keys";
|
|
9
|
+
import { storeEmail } from "./email-index";
|
|
10
|
+
import * as mailboxStorage from "./mailbox-storage";
|
|
11
|
+
import type { RecipientDeliveryStatus } from "./model";
|
|
12
|
+
import {
|
|
13
|
+
hasPendingOfficePreviews,
|
|
14
|
+
prepareOfficePreviews,
|
|
15
|
+
} from "./office-preview-jobs";
|
|
16
|
+
import { blocksForLeavingSpam, leaveSpam } from "./placement";
|
|
17
|
+
import { drafts, emails } from "./schema";
|
|
18
|
+
import type { Email, NewEmail } from "./schema";
|
|
19
|
+
import type {
|
|
20
|
+
SendingDraft,
|
|
21
|
+
SubmitOutgoingResult,
|
|
22
|
+
TransportUpdate,
|
|
23
|
+
MailboxContext,
|
|
24
|
+
} from "./support";
|
|
25
|
+
import { ALARM_WAKE_DELAY_MS, errorMessage } from "./support";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Stores a message to send and sends it, once per id: the same request
|
|
29
|
+
* again gets the stored message. The send runs outside any block, so a
|
|
30
|
+
* long one holds up neither the mailbox nor itself (a block is reset
|
|
31
|
+
* after 30 seconds); `sending` keeps one send per message at a time.
|
|
32
|
+
*/
|
|
33
|
+
export async function submitOutgoing(
|
|
34
|
+
m: MailboxContext,
|
|
35
|
+
input: NewEmail,
|
|
36
|
+
userId: string,
|
|
37
|
+
draft?: SendingDraft
|
|
38
|
+
) {
|
|
39
|
+
if (
|
|
40
|
+
input.direction !== "outgoing" ||
|
|
41
|
+
input.transportState !== "unconfirmed"
|
|
42
|
+
) {
|
|
43
|
+
throw new Error("Only unconfirmed outgoing messages can be submitted");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!input.requestFingerprint) {
|
|
47
|
+
throw new Error("An outgoing request fingerprint is required");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const email = prepareOfficePreviews(input);
|
|
51
|
+
|
|
52
|
+
// A reply from Spam judges its senders, but not blocked ones: the
|
|
53
|
+
// workspace's blocks are read first, only then. Unreadable, the reply
|
|
54
|
+
// judges nobody rather than fail to send.
|
|
55
|
+
const workspaceBlocks = await blocksForLeavingSpam(m, [
|
|
56
|
+
{ conversationIds: [email.conversationId], mailboxState: "active" },
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
if (draft || hasPendingOfficePreviews(email)) {
|
|
60
|
+
await m.scheduleAlarm(Date.now() + ALARM_WAKE_DELAY_MS);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const submission = m.db.transaction((tx) => {
|
|
64
|
+
const existing = tx
|
|
65
|
+
.select()
|
|
66
|
+
.from(emails)
|
|
67
|
+
.where(eq(emails.id, email.id))
|
|
68
|
+
.all()[0];
|
|
69
|
+
|
|
70
|
+
if (existing) {
|
|
71
|
+
const sameRequest =
|
|
72
|
+
existing.direction === "outgoing" &&
|
|
73
|
+
existing.requestFingerprint === email.requestFingerprint;
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
outcome: sameRequest ? "existing" : "conflict",
|
|
77
|
+
email: existing,
|
|
78
|
+
} satisfies SubmitOutgoingResult;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Consume exactly the version being sent in the transaction that queues delivery.
|
|
82
|
+
if (draft) {
|
|
83
|
+
const match = and(
|
|
84
|
+
eq(drafts.userId, userId),
|
|
85
|
+
eq(drafts.id, draft.id),
|
|
86
|
+
eq(drafts.version, draft.version)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const current = tx
|
|
90
|
+
.select({ attachments: drafts.attachments })
|
|
91
|
+
.from(drafts)
|
|
92
|
+
.where(match)
|
|
93
|
+
.get();
|
|
94
|
+
|
|
95
|
+
if (!current) return { outcome: "draftChanged" as const, email: null };
|
|
96
|
+
|
|
97
|
+
if (!draft.keep) {
|
|
98
|
+
tx.delete(drafts).where(match).run();
|
|
99
|
+
mailboxStorage.queueObjectDeletions(
|
|
100
|
+
m,
|
|
101
|
+
current.attachments.map((file) => file.r2Key)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A reply sent from Spam brings its conversation out, as Not spam does
|
|
107
|
+
// (`leaveSpam`), and goes where that mail now is: the thread it was
|
|
108
|
+
// kept out of, when it rejoined one.
|
|
109
|
+
const { places, verdicts } = leaveSpam(
|
|
110
|
+
m,
|
|
111
|
+
[email.conversationId],
|
|
112
|
+
{ mailboxState: "active" },
|
|
113
|
+
{ userId, trustSenders: true, workspaceBlocks }
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
storeEmail(tx, m.state.storage.sql, {
|
|
117
|
+
...email,
|
|
118
|
+
conversationId: places[0]?.conversationId ?? email.conversationId,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const inserted = tx
|
|
122
|
+
.select()
|
|
123
|
+
.from(emails)
|
|
124
|
+
.where(eq(emails.id, email.id))
|
|
125
|
+
.all()[0];
|
|
126
|
+
|
|
127
|
+
if (!inserted) throw new Error("Outgoing message was not persisted");
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
outcome: "inserted",
|
|
131
|
+
email: inserted,
|
|
132
|
+
verdicts,
|
|
133
|
+
} satisfies SubmitOutgoingResult;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (submission.outcome !== "inserted") return submission;
|
|
137
|
+
m.forgetParticipants();
|
|
138
|
+
|
|
139
|
+
const result = {
|
|
140
|
+
...submission,
|
|
141
|
+
email: await deliverOutgoing(m, submission.email.id),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
void m.announce();
|
|
145
|
+
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Sends a failed or unconfirmed message again; null when it is neither, or is being sent. */
|
|
150
|
+
export async function resendOutgoing(
|
|
151
|
+
m: MailboxContext,
|
|
152
|
+
id: string,
|
|
153
|
+
origin: string
|
|
154
|
+
) {
|
|
155
|
+
if (m.sending.has(id)) return null;
|
|
156
|
+
const email = m.getEmail(id);
|
|
157
|
+
|
|
158
|
+
if (
|
|
159
|
+
!email ||
|
|
160
|
+
email.direction !== "outgoing" ||
|
|
161
|
+
!["failed", "unconfirmed"].includes(email.transportState)
|
|
162
|
+
) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
m.sending.add(id);
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
const links = await refreshDownloadLinks(
|
|
170
|
+
m.env,
|
|
171
|
+
m.state.id.name ?? "",
|
|
172
|
+
email,
|
|
173
|
+
origin
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const current = m.getEmail(id);
|
|
177
|
+
|
|
178
|
+
if (!current) {
|
|
179
|
+
if (links?.bodyHtmlR2Key) {
|
|
180
|
+
await mailboxStorage.forgetObjects(m, [links.bodyHtmlR2Key]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const [pending] = m.db
|
|
187
|
+
.update(emails)
|
|
188
|
+
.set({
|
|
189
|
+
...(links && {
|
|
190
|
+
rawMimeR2Key: null,
|
|
191
|
+
rawSize: null,
|
|
192
|
+
sentAt: null,
|
|
193
|
+
bodyText: links.bodyText,
|
|
194
|
+
preview: links.bodyText.replaceAll(/\s+/gu, " ").trim().slice(0, 220),
|
|
195
|
+
bodyHtmlR2Key: links.bodyHtmlR2Key,
|
|
196
|
+
attachmentsJson: current.attachmentsJson.map((file) => ({
|
|
197
|
+
...file,
|
|
198
|
+
...links.replacements.get(file.id),
|
|
199
|
+
})),
|
|
200
|
+
}),
|
|
201
|
+
transportState: "unconfirmed",
|
|
202
|
+
messageIdHeader: null,
|
|
203
|
+
transportError: null,
|
|
204
|
+
deliveryStatusJson: [],
|
|
205
|
+
backupSha256: null,
|
|
206
|
+
})
|
|
207
|
+
.where(eq(emails.id, id))
|
|
208
|
+
.returning()
|
|
209
|
+
.all();
|
|
210
|
+
|
|
211
|
+
if (!pending) return null;
|
|
212
|
+
|
|
213
|
+
const obsolete = [
|
|
214
|
+
links && current.rawMimeR2Key,
|
|
215
|
+
links && current.bodyHtmlR2Key,
|
|
216
|
+
].filter((key): key is string => Boolean(key));
|
|
217
|
+
|
|
218
|
+
await mailboxStorage.forgetObjects(m, obsolete);
|
|
219
|
+
|
|
220
|
+
const result = await sendUnconfirmed(
|
|
221
|
+
m,
|
|
222
|
+
pending,
|
|
223
|
+
`${id}~${crypto.randomUUID()}`
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
void m.announce();
|
|
227
|
+
|
|
228
|
+
return result;
|
|
229
|
+
} finally {
|
|
230
|
+
m.sending.delete(id);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function updateTransport(
|
|
235
|
+
m: MailboxContext,
|
|
236
|
+
id: string,
|
|
237
|
+
update: TransportUpdate
|
|
238
|
+
) {
|
|
239
|
+
const result = m.db
|
|
240
|
+
.update(emails)
|
|
241
|
+
.set({
|
|
242
|
+
backupSha256: null,
|
|
243
|
+
transportState: update.state,
|
|
244
|
+
messageIdHeader: storedMessageId(update.messageIdHeader),
|
|
245
|
+
transportError: update.error,
|
|
246
|
+
})
|
|
247
|
+
.where(and(eq(emails.id, id), eq(emails.direction, "outgoing")))
|
|
248
|
+
.returning({ id: emails.id })
|
|
249
|
+
.all();
|
|
250
|
+
|
|
251
|
+
return result.length > 0;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function recordDeliveryStatus(
|
|
255
|
+
m: MailboxContext,
|
|
256
|
+
messageIdHeader: string,
|
|
257
|
+
status: RecipientDeliveryStatus
|
|
258
|
+
) {
|
|
259
|
+
// Sent here: a copy that came back (Cc to itself) has the same Message-ID.
|
|
260
|
+
const email =
|
|
261
|
+
m.db
|
|
262
|
+
.select()
|
|
263
|
+
.from(emails)
|
|
264
|
+
.where(
|
|
265
|
+
and(
|
|
266
|
+
eq(emails.messageIdHeader, storedMessageId(messageIdHeader) ?? ""),
|
|
267
|
+
eq(emails.direction, "outgoing")
|
|
268
|
+
)
|
|
269
|
+
)
|
|
270
|
+
.all()[0] ?? null;
|
|
271
|
+
|
|
272
|
+
if (!email) return "not_found" as const;
|
|
273
|
+
|
|
274
|
+
const existing = email.deliveryStatusJson.find(
|
|
275
|
+
(item) => item.recipient === status.recipient
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
if (existing?.eventId === status.eventId) return "duplicate" as const;
|
|
279
|
+
|
|
280
|
+
if (existing && existing.eventAt > status.eventAt) return "stale" as const;
|
|
281
|
+
|
|
282
|
+
const next = [
|
|
283
|
+
...email.deliveryStatusJson.filter(
|
|
284
|
+
(item) => item.recipient !== status.recipient
|
|
285
|
+
),
|
|
286
|
+
status,
|
|
287
|
+
];
|
|
288
|
+
|
|
289
|
+
m.db
|
|
290
|
+
.update(emails)
|
|
291
|
+
.set({ deliveryStatusJson: next })
|
|
292
|
+
.where(eq(emails.id, email.id))
|
|
293
|
+
.run();
|
|
294
|
+
void m.announce();
|
|
295
|
+
|
|
296
|
+
return "updated" as const;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export async function deliverOutgoing(m: MailboxContext, id: string) {
|
|
300
|
+
const message = m.getEmail(id);
|
|
301
|
+
|
|
302
|
+
if (
|
|
303
|
+
!message ||
|
|
304
|
+
message.direction !== "outgoing" ||
|
|
305
|
+
message.transportState !== "unconfirmed"
|
|
306
|
+
) {
|
|
307
|
+
throw new Error("Unconfirmed outgoing message was not found");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (m.sending.has(id)) return message;
|
|
311
|
+
m.sending.add(id);
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
return await sendUnconfirmed(m, message, id);
|
|
315
|
+
} finally {
|
|
316
|
+
m.sending.delete(id);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async function sendUnconfirmed(
|
|
321
|
+
m: MailboxContext,
|
|
322
|
+
message: Email,
|
|
323
|
+
attemptId: string
|
|
324
|
+
) {
|
|
325
|
+
const { id } = message;
|
|
326
|
+
|
|
327
|
+
let delivery: Awaited<ReturnType<typeof prepareOutboundDelivery>>;
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
delivery = await prepareOutboundDelivery(m.env, message);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
updateTransport(m, message.id, {
|
|
333
|
+
state: "failed",
|
|
334
|
+
error: errorMessage(error),
|
|
335
|
+
});
|
|
336
|
+
const failed = m.getEmail(id);
|
|
337
|
+
|
|
338
|
+
if (!failed) {
|
|
339
|
+
throw new Error("Failed message was not persisted", { cause: error });
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return failed;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
let sent = false;
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
const mailboxId = m.state.id.name ?? "unknown-mailbox";
|
|
349
|
+
|
|
350
|
+
const result = await sendArchived(
|
|
351
|
+
m.env,
|
|
352
|
+
{
|
|
353
|
+
id: attemptId,
|
|
354
|
+
mailboxId,
|
|
355
|
+
rawKey: rawMessageKey(mailboxId, attemptId),
|
|
356
|
+
},
|
|
357
|
+
delivery,
|
|
358
|
+
// Recorded the moment Email Sending took it: a copy that comes back
|
|
359
|
+
// (a list's, Cc to itself) is then known as this message, and a
|
|
360
|
+
// restart can't leave it looking unsent.
|
|
361
|
+
({ messageId }) => {
|
|
362
|
+
sent = true;
|
|
363
|
+
updateTransport(m, message.id, {
|
|
364
|
+
state: "submitted",
|
|
365
|
+
messageIdHeader: messageId,
|
|
366
|
+
error: null,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
if (result.rawKey) {
|
|
372
|
+
// The message as sent: what backups, JMAP and the archive read. Its
|
|
373
|
+
// row deleted meanwhile (deleted for good while sending), the copy
|
|
374
|
+
// has nothing to keep it: delete it.
|
|
375
|
+
const kept = m.db
|
|
376
|
+
.update(emails)
|
|
377
|
+
.set({
|
|
378
|
+
rawMimeR2Key: result.rawKey,
|
|
379
|
+
rawSize: result.rawSize,
|
|
380
|
+
sentAt: result.sentAt,
|
|
381
|
+
backupSha256: null,
|
|
382
|
+
})
|
|
383
|
+
.where(eq(emails.id, message.id))
|
|
384
|
+
.returning({ id: emails.id })
|
|
385
|
+
.all();
|
|
386
|
+
|
|
387
|
+
if (!kept.length) {
|
|
388
|
+
await mailboxStorage.forgetObjects(m, [result.rawKey]);
|
|
389
|
+
} else if (
|
|
390
|
+
message.rawMimeR2Key &&
|
|
391
|
+
message.rawMimeR2Key !== result.rawKey
|
|
392
|
+
) {
|
|
393
|
+
await mailboxStorage.forgetObjects(m, [message.rawMimeR2Key]);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
} catch (error) {
|
|
397
|
+
// Sent, then a copy failed: it stays sent.
|
|
398
|
+
if (!sent) {
|
|
399
|
+
updateTransport(m, message.id, {
|
|
400
|
+
state: "unconfirmed",
|
|
401
|
+
error: errorMessage(error),
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Deleted for good while it was being sent: answer with it as sent.
|
|
407
|
+
return (
|
|
408
|
+
m.getEmail(id) ?? {
|
|
409
|
+
...message,
|
|
410
|
+
transportState: sent ? "submitted" : message.transportState,
|
|
411
|
+
}
|
|
412
|
+
);
|
|
413
|
+
}
|