@open-workspace/email 0.4.1 → 0.5.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 +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0002_good_komodo.sql +2 -0
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +14 -0
- package/drizzle/mailbox/migrations.js +5 -1
- package/migrations/0001_overrated_ricochet.sql +2 -0
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0001_snapshot.json +564 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +14 -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.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 +49 -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 +387 -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 +320 -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 -102
- 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 +57 -33
- package/src/mail/sender-actions.tsx +49 -39
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +9 -10
- 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 +754 -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 +98 -2
- 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 +108 -1815
- 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 +682 -0
- package/worker/mail/auth-headers.ts +48 -40
- package/worker/mail/auto-reply-routes.ts +255 -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.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 +3 -3
- 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 +87 -43
- package/worker/mail/quote-parser.ts +107 -25
- package/worker/mail/r2-presigned-urls.ts +7 -0
- 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 +696 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -80
- 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 +201 -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 +193 -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 +92 -13
- 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 +843 -2888
- 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 +301 -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 +450 -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 +211 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +240 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +156 -0
- package/worker/mcp/mail-compose-tools.ts +8 -2
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -2
- 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,450 @@
|
|
|
1
|
+
/** Imported mail: stored as history, placed as the importer says, announced once per batch. */
|
|
2
|
+
import { createId, normalizeMailboxAddress } from "@open-workspace/core/module";
|
|
3
|
+
import { and, desc, eq, or } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
inboundMessageId,
|
|
7
|
+
MissingRawMimeError,
|
|
8
|
+
prepareInboundEmail,
|
|
9
|
+
rawMessageId,
|
|
10
|
+
UnprocessableInboundEmailError,
|
|
11
|
+
} from "../mail/inbound";
|
|
12
|
+
import { sentQuote } from "../mail/quote-parser";
|
|
13
|
+
import { storeEmail } from "./email-index";
|
|
14
|
+
import { mergedPlacement, requestedPlacement } from "./import-placement";
|
|
15
|
+
import * as mailboxFolders from "./mailbox-folders";
|
|
16
|
+
import { forgetObjects } from "./mailbox-storage";
|
|
17
|
+
import {
|
|
18
|
+
hasPendingOfficePreviews,
|
|
19
|
+
prepareOfficePreviews,
|
|
20
|
+
} from "./office-preview-jobs";
|
|
21
|
+
import { fileConversations } from "./placement";
|
|
22
|
+
import { restoredThread, rememberRestoredThread } from "./restore-thread";
|
|
23
|
+
import { conversations, emailReadStates, emails, folders } from "./schema";
|
|
24
|
+
import type { NewEmail } from "./schema";
|
|
25
|
+
import type {
|
|
26
|
+
ImportMessageInput,
|
|
27
|
+
ImportOriginal,
|
|
28
|
+
ImportMessageResult,
|
|
29
|
+
MailboxContext,
|
|
30
|
+
} from "./support";
|
|
31
|
+
import {
|
|
32
|
+
ALARM_WAKE_DELAY_MS,
|
|
33
|
+
IMPORTED_FOLDERS_KEY,
|
|
34
|
+
IMPORT_ANNOUNCE_DELAY_MS,
|
|
35
|
+
IMPORT_PENDING_KEY,
|
|
36
|
+
headerDate,
|
|
37
|
+
} from "./support";
|
|
38
|
+
|
|
39
|
+
export type ImportCopyKey = {
|
|
40
|
+
messageId: string;
|
|
41
|
+
direction: "incoming" | "outgoing";
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function sameMessage(copy: ImportCopyKey) {
|
|
45
|
+
return and(
|
|
46
|
+
eq(emails.messageIdHeader, copy.messageId),
|
|
47
|
+
eq(emails.direction, copy.direction)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Prefer the source ID, then the backup's original ID, then a copy from another source. */
|
|
52
|
+
export function findImported(
|
|
53
|
+
m: MailboxContext,
|
|
54
|
+
emailId: string,
|
|
55
|
+
original: Pick<ImportOriginal, "emailId" | "conversationId"> | null,
|
|
56
|
+
conversationId: string | null,
|
|
57
|
+
copy?: ImportCopyKey | null
|
|
58
|
+
) {
|
|
59
|
+
const sourceId = eq(emails.id, emailId);
|
|
60
|
+
const originalId = original && eq(emails.id, original.emailId);
|
|
61
|
+
|
|
62
|
+
const existing = m.db
|
|
63
|
+
.select({ id: emails.id, conversationId: emails.conversationId })
|
|
64
|
+
.from(emails)
|
|
65
|
+
.where(
|
|
66
|
+
or(
|
|
67
|
+
sourceId,
|
|
68
|
+
originalId ?? undefined,
|
|
69
|
+
copy ? sameMessage(copy) : undefined
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
.orderBy(desc(sourceId), ...(originalId ? [desc(originalId)] : []))
|
|
73
|
+
.limit(1)
|
|
74
|
+
.get();
|
|
75
|
+
|
|
76
|
+
if (existing && original && conversationId) {
|
|
77
|
+
rememberRestoredThread(
|
|
78
|
+
m,
|
|
79
|
+
original.conversationId,
|
|
80
|
+
conversationId,
|
|
81
|
+
existing.conversationId
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return existing ?? null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Stores one existing message (raw MIME already in R2) as delivery would,
|
|
90
|
+
* with its place, read state, date and thread given by the importer. No
|
|
91
|
+
* AI, sender checks, webhooks or push: imported mail is history. The
|
|
92
|
+
* conversation and search index are kept as for any message; counts,
|
|
93
|
+
* realtime and JMAP state are announced once for the batch
|
|
94
|
+
* (`finishImport`, or the alarm when the importer never says it finished).
|
|
95
|
+
* Idempotent by `importId` and by Message-ID.
|
|
96
|
+
*/
|
|
97
|
+
export async function importMessage(
|
|
98
|
+
m: MailboxContext,
|
|
99
|
+
input: ImportMessageInput
|
|
100
|
+
) {
|
|
101
|
+
const id = inboundMessageId(input.importId);
|
|
102
|
+
|
|
103
|
+
const known = findImported(m, id, input.original, input.conversationId);
|
|
104
|
+
|
|
105
|
+
if (known) {
|
|
106
|
+
return {
|
|
107
|
+
status: "duplicate",
|
|
108
|
+
id: known.id,
|
|
109
|
+
conversationId: known.conversationId,
|
|
110
|
+
} satisfies ImportMessageResult;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const object = await m.env.MAIL_STORAGE.get(input.rawObjectKey);
|
|
114
|
+
|
|
115
|
+
if (!object) throw new MissingRawMimeError();
|
|
116
|
+
const rawMime = await object.arrayBuffer();
|
|
117
|
+
const messageIdHeader = rawMessageId(rawMime);
|
|
118
|
+
|
|
119
|
+
const receivedAt = new Date(
|
|
120
|
+
input.receivedAt ?? headerDate(rawMime) ?? Date.now()
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
let parsed: NewEmail;
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
parsed = (
|
|
127
|
+
await prepareInboundEmail({
|
|
128
|
+
env: m.env,
|
|
129
|
+
mailboxId: m.state.id.name ?? "unknown-mailbox",
|
|
130
|
+
job: {
|
|
131
|
+
id: input.importId,
|
|
132
|
+
rawObjectKey: input.rawObjectKey,
|
|
133
|
+
envelopeFrom: "",
|
|
134
|
+
envelopeTo: input.mailboxAddress,
|
|
135
|
+
listAddress: null,
|
|
136
|
+
receivedAt,
|
|
137
|
+
},
|
|
138
|
+
rawMime,
|
|
139
|
+
fallbackConversationId: input.conversationId,
|
|
140
|
+
resolveParent: (inReplyTo, references) =>
|
|
141
|
+
m.resolveParent(inReplyTo, references),
|
|
142
|
+
})
|
|
143
|
+
).email;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (!(error instanceof UnprocessableInboundEmailError)) throw error;
|
|
146
|
+
await forgetObjects(m, [input.rawObjectKey]);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
status: "unprocessable",
|
|
150
|
+
reason: error.message,
|
|
151
|
+
} satisfies ImportMessageResult;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const sender = parsed.fromJson[0];
|
|
155
|
+
|
|
156
|
+
const fromAddress = sender && normalizeMailboxAddress(sender.address);
|
|
157
|
+
|
|
158
|
+
const fromMailbox =
|
|
159
|
+
fromAddress &&
|
|
160
|
+
(fromAddress === normalizeMailboxAddress(input.mailboxAddress) ||
|
|
161
|
+
input.aliases.some(
|
|
162
|
+
(address) => normalizeMailboxAddress(address) === fromAddress
|
|
163
|
+
));
|
|
164
|
+
|
|
165
|
+
const direction =
|
|
166
|
+
input.direction ??
|
|
167
|
+
(input.placement === "sent" || fromMailbox ? "outgoing" : "incoming");
|
|
168
|
+
|
|
169
|
+
// The same message from another source (IMAP after Takeout) is a copy;
|
|
170
|
+
// mail sent to yourself is two, the sent one and the received one, as
|
|
171
|
+
// delivery keeps them.
|
|
172
|
+
const copy = messageIdHeader
|
|
173
|
+
? m.db
|
|
174
|
+
.select({ id: emails.id, conversationId: emails.conversationId })
|
|
175
|
+
.from(emails)
|
|
176
|
+
.where(sameMessage({ messageId: messageIdHeader, direction }))
|
|
177
|
+
.limit(1)
|
|
178
|
+
.get()
|
|
179
|
+
: undefined;
|
|
180
|
+
|
|
181
|
+
// A skipped or duplicate import doesn't own these objects. The deletion
|
|
182
|
+
// queue keeps any key that a concurrent import has already committed.
|
|
183
|
+
const discardParsed = () =>
|
|
184
|
+
forgetObjects(m, [
|
|
185
|
+
input.rawObjectKey,
|
|
186
|
+
...(parsed.bodyHtmlR2Key ? [parsed.bodyHtmlR2Key] : []),
|
|
187
|
+
...(parsed.attachmentsJson ?? []).map((attachment) => attachment.r2Key),
|
|
188
|
+
]);
|
|
189
|
+
|
|
190
|
+
if (copy) {
|
|
191
|
+
if (input.original && input.conversationId) {
|
|
192
|
+
rememberRestoredThread(
|
|
193
|
+
m,
|
|
194
|
+
input.original.conversationId,
|
|
195
|
+
input.conversationId,
|
|
196
|
+
copy.conversationId
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
await discardParsed();
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
status: "duplicate",
|
|
204
|
+
id: copy.id,
|
|
205
|
+
conversationId: copy.conversationId,
|
|
206
|
+
} satisfies ImportMessageResult;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// A snapshot preserves its recorded grouping, including on a fresh mailbox.
|
|
210
|
+
const restoredConversation =
|
|
211
|
+
input.original &&
|
|
212
|
+
(restoredThread(m, input.original.conversationId) ?? input.conversationId);
|
|
213
|
+
|
|
214
|
+
const imported: NewEmail = {
|
|
215
|
+
...parsed,
|
|
216
|
+
attachmentsJson: [
|
|
217
|
+
...(parsed.attachmentsJson ?? []),
|
|
218
|
+
...(input.linkedFiles ?? []),
|
|
219
|
+
],
|
|
220
|
+
conversationId: restoredConversation ?? parsed.conversationId,
|
|
221
|
+
direction,
|
|
222
|
+
envelopeFrom: null,
|
|
223
|
+
// Only a mailbox snapshot knows the SMTP recipient, including a Bcc alias.
|
|
224
|
+
envelopeTo: input.original?.envelopeTo ?? null,
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// Mail imported from another system has no local authentication verdict.
|
|
228
|
+
// Its quote is kept as a sent message's, without the `>` it went out with.
|
|
229
|
+
if (direction === "outgoing") {
|
|
230
|
+
imported.quotedText = parsed.quotedText && sentQuote(parsed.quotedText);
|
|
231
|
+
imported.authenticationResultsJson = null;
|
|
232
|
+
imported.listUnsubscribeJson = null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// A migration imports historical mail. A snapshot also records the result
|
|
236
|
+
// of the original send, including unconfirmed attempts awaiting a manual retry.
|
|
237
|
+
if (input.original) {
|
|
238
|
+
// Workspace list delivery is recorded by the router, outside the raw MIME.
|
|
239
|
+
imported.listId = input.original.listId;
|
|
240
|
+
imported.listPostAddress = input.original.listPostAddress;
|
|
241
|
+
imported.transportState = input.original.transportState;
|
|
242
|
+
imported.transportError = input.original.transportError;
|
|
243
|
+
imported.deliveryStatusJson = input.original.deliveryStatusJson;
|
|
244
|
+
} else if (direction === "outgoing") {
|
|
245
|
+
imported.transportState = "submitted";
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const email = prepareOfficePreviews(imported);
|
|
249
|
+
|
|
250
|
+
const stored = m.db.transaction((tx) => {
|
|
251
|
+
const before =
|
|
252
|
+
tx
|
|
253
|
+
.select({
|
|
254
|
+
mailboxState: conversations.mailboxState,
|
|
255
|
+
folderId: conversations.folderId,
|
|
256
|
+
hasIncoming: conversations.hasIncoming,
|
|
257
|
+
})
|
|
258
|
+
.from(conversations)
|
|
259
|
+
.where(eq(conversations.id, email.conversationId))
|
|
260
|
+
.all()[0] ?? null;
|
|
261
|
+
|
|
262
|
+
let folderId: string | null = null;
|
|
263
|
+
let folderSkipped: "limit" | "invalid" | null = null;
|
|
264
|
+
|
|
265
|
+
const keepPlacement =
|
|
266
|
+
before?.hasIncoming && email.conversationId !== input.conversationId;
|
|
267
|
+
|
|
268
|
+
// A newly restored conversation remembers its folder even while archived,
|
|
269
|
+
// trashed or only in Sent. Existing conversations keep their current folder.
|
|
270
|
+
const restoreHiddenFolder =
|
|
271
|
+
input.original && !before && input.placement !== "inbox";
|
|
272
|
+
|
|
273
|
+
if (
|
|
274
|
+
input.folderName &&
|
|
275
|
+
!(input.original && keepPlacement) &&
|
|
276
|
+
(input.placement === "inbox" || restoreHiddenFolder)
|
|
277
|
+
) {
|
|
278
|
+
// The settings line restores folder positions after the messages.
|
|
279
|
+
const folder = importFolder(
|
|
280
|
+
m,
|
|
281
|
+
input.folderName,
|
|
282
|
+
input.original ? "end" : "after-inbox"
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
if ("id" in folder) folderId = folder.id;
|
|
286
|
+
else if (input.original) {
|
|
287
|
+
// Leave the message retryable until it can be filed as recorded.
|
|
288
|
+
return {
|
|
289
|
+
status: "unprocessable",
|
|
290
|
+
reason: importFolderError(input.folderName, folder.skipped),
|
|
291
|
+
} as const;
|
|
292
|
+
} else folderSkipped = folder.skipped;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (!storeEmail(tx, m.state.storage.sql, email)) return null;
|
|
296
|
+
|
|
297
|
+
if (restoreHiddenFolder && folderId) {
|
|
298
|
+
fileConversations(m.state.storage.sql, [email.conversationId], {
|
|
299
|
+
folderId,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Imported messages that share the importer's thread settle where
|
|
304
|
+
// its conversation goes; one joining a conversation that was here
|
|
305
|
+
// already (by its headers, or a restore's own) leaves it in place,
|
|
306
|
+
// unless that holds only sent mail, which asked for no place.
|
|
307
|
+
const placement = keepPlacement
|
|
308
|
+
? before
|
|
309
|
+
: mergedPlacement(
|
|
310
|
+
// Sent mail alone asked for no place unless it was put somewhere
|
|
311
|
+
// (Trash, Spam, Archive, a folder): the next message's decides.
|
|
312
|
+
before &&
|
|
313
|
+
(before.hasIncoming ||
|
|
314
|
+
before.mailboxState !== "active" ||
|
|
315
|
+
before.folderId)
|
|
316
|
+
? before
|
|
317
|
+
: null,
|
|
318
|
+
requestedPlacement(input.placement, folderId)
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
if (placement) {
|
|
322
|
+
fileConversations(
|
|
323
|
+
m.state.storage.sql,
|
|
324
|
+
[email.conversationId],
|
|
325
|
+
placement,
|
|
326
|
+
{ stored: true }
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Snapshots record who viewed sent mail too. A migration's seen flag
|
|
331
|
+
// only describes received mail; it cannot identify viewers of sent mail.
|
|
332
|
+
if (direction === "incoming" || input.original) {
|
|
333
|
+
for (const userId of new Set(input.seenBy)) {
|
|
334
|
+
tx.insert(emailReadStates)
|
|
335
|
+
.values({ userId, emailId: email.id, readAt: receivedAt })
|
|
336
|
+
.onConflictDoNothing()
|
|
337
|
+
.run();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return { status: "imported", folderSkipped } as const;
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
if (stored?.status === "unprocessable") {
|
|
345
|
+
await discardParsed();
|
|
346
|
+
|
|
347
|
+
return stored;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (!stored) {
|
|
351
|
+
const existing = m.getEmail(id);
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
status: "duplicate",
|
|
355
|
+
id,
|
|
356
|
+
conversationId: existing?.conversationId ?? email.conversationId,
|
|
357
|
+
} satisfies ImportMessageResult;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
m.forgetParticipants();
|
|
361
|
+
m.state.storage.kv.put(IMPORT_PENDING_KEY, true);
|
|
362
|
+
await m.scheduleAlarm(
|
|
363
|
+
Date.now() +
|
|
364
|
+
(hasPendingOfficePreviews(email)
|
|
365
|
+
? ALARM_WAKE_DELAY_MS
|
|
366
|
+
: IMPORT_ANNOUNCE_DELAY_MS)
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
status: "imported",
|
|
371
|
+
id,
|
|
372
|
+
conversationId: email.conversationId,
|
|
373
|
+
folderSkipped: stored.folderSkipped,
|
|
374
|
+
} satisfies ImportMessageResult;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function importFolderError(name: string, reason: "limit" | "invalid") {
|
|
378
|
+
return reason === "limit" ? "folders limit" : `invalid folder name: ${name}`;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
type ImportFolder = { id: string } | { skipped: "limit" | "invalid" };
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* The custom folder with this name (any case), created when missing (placed
|
|
385
|
+
* as `insertFolder` places it); why not, when it cannot be.
|
|
386
|
+
*/
|
|
387
|
+
export function importFolder(
|
|
388
|
+
m: MailboxContext,
|
|
389
|
+
name: string,
|
|
390
|
+
place?: "after-inbox" | "end"
|
|
391
|
+
) {
|
|
392
|
+
const trimmed = name.trim();
|
|
393
|
+
|
|
394
|
+
const exact = m.db
|
|
395
|
+
.select({ id: folders.id })
|
|
396
|
+
.from(folders)
|
|
397
|
+
.where(eq(folders.name, trimmed))
|
|
398
|
+
.get();
|
|
399
|
+
|
|
400
|
+
if (exact) return exact;
|
|
401
|
+
|
|
402
|
+
// SQLite's built-in NOCASE only folds ASCII. Preserve Unicode matching on a miss.
|
|
403
|
+
const normalized = trimmed.toLocaleLowerCase("en-US");
|
|
404
|
+
|
|
405
|
+
const existing = m.db
|
|
406
|
+
.select({ id: folders.id, name: folders.name })
|
|
407
|
+
.from(folders)
|
|
408
|
+
.all()
|
|
409
|
+
.find((folder) => folder.name.toLocaleLowerCase("en-US") === normalized);
|
|
410
|
+
|
|
411
|
+
if (existing) return { id: existing.id };
|
|
412
|
+
|
|
413
|
+
const created = mailboxFolders.insertFolder(m, createId("fld"), name, {
|
|
414
|
+
place,
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
if (created.status === "ok") {
|
|
418
|
+
// Placed by an import, not by a person: a restored settings line may
|
|
419
|
+
// still put it where it was (restoreSettings).
|
|
420
|
+
m.state.storage.kv.put(IMPORTED_FOLDERS_KEY, [
|
|
421
|
+
...(m.state.storage.kv.get<string[]>(IMPORTED_FOLDERS_KEY) ?? []),
|
|
422
|
+
created.folder.id,
|
|
423
|
+
]);
|
|
424
|
+
|
|
425
|
+
const placed: ImportFolder = { id: created.folder.id };
|
|
426
|
+
|
|
427
|
+
return placed;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// A system folder's name ("Sent", "Archive") is no custom folder.
|
|
431
|
+
const skipped: ImportFolder = {
|
|
432
|
+
skipped: created.status === "limit" ? "limit" : "invalid",
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
return skipped;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Announces what imports stored since the last time: writes the inbox
|
|
440
|
+
* counts to D1 once, bumps the JMAP state, and tells open apps to refresh.
|
|
441
|
+
* Push subscribers are not told; imported mail is not new mail.
|
|
442
|
+
*/
|
|
443
|
+
export async function finishImport(m: MailboxContext) {
|
|
444
|
+
if (m.state.storage.kv.get<boolean>(IMPORT_PENDING_KEY)) {
|
|
445
|
+
m.state.storage.kv.delete(IMPORT_PENDING_KEY);
|
|
446
|
+
await m.announce({ push: false });
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return { version: m.version() };
|
|
450
|
+
}
|