@open-workspace/email 0.4.2 → 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/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.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 +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 +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 +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 +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 +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 +696 -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 +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 +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 +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 +200 -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 +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,73 @@
|
|
|
1
|
+
import { attachmentHeaders, base64Lines, emlBody, emlHeaders } from "./eml";
|
|
2
|
+
import type { EmlInput } from "./eml";
|
|
3
|
+
|
|
4
|
+
type StreamAttachment = Omit<EmlInput["attachments"][number], "content"> & {
|
|
5
|
+
open(): Promise<ReadableStream<Uint8Array>>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
|
|
10
|
+
// 57 input bytes make one 76-character MIME line. Flush whole lines in
|
|
11
|
+
// batches and retain the final short line until the source reaches EOF.
|
|
12
|
+
async function* encodedFile(file: StreamAttachment) {
|
|
13
|
+
const buffer = new Uint8Array(57 * 512);
|
|
14
|
+
let used = 0;
|
|
15
|
+
|
|
16
|
+
for await (const chunk of await file.open()) {
|
|
17
|
+
for (let offset = 0; offset < chunk.length;) {
|
|
18
|
+
const n = Math.min(chunk.length - offset, buffer.length - used);
|
|
19
|
+
buffer.set(chunk.subarray(offset, offset + n), used);
|
|
20
|
+
used += n;
|
|
21
|
+
offset += n;
|
|
22
|
+
|
|
23
|
+
if (used === buffer.length) {
|
|
24
|
+
yield encoder.encode(base64Lines(buffer));
|
|
25
|
+
used = 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (used) yield encoder.encode(base64Lines(buffer.subarray(0, used)));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function streamEml(
|
|
34
|
+
input: Omit<EmlInput, "attachments"> & { attachments: StreamAttachment[] }
|
|
35
|
+
) {
|
|
36
|
+
async function* parts() {
|
|
37
|
+
const headers = emlHeaders(input);
|
|
38
|
+
const body = emlBody(input);
|
|
39
|
+
|
|
40
|
+
if (!input.attachments.length) {
|
|
41
|
+
yield encoder.encode(headers + body);
|
|
42
|
+
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const boundary = `ow-mixed-${input.id}`;
|
|
47
|
+
yield encoder.encode(
|
|
48
|
+
`${headers}Content-Type: multipart/mixed; boundary="${boundary}"\r\n\r\n--${boundary}\r\n${body}\r\n`
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
for (const file of input.attachments) {
|
|
52
|
+
yield encoder.encode(`--${boundary}\r\n${attachmentHeaders(file)}`);
|
|
53
|
+
yield* encodedFile(file);
|
|
54
|
+
yield encoder.encode("\r\n");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
yield encoder.encode(`--${boundary}--\r\n`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const chunks = parts();
|
|
61
|
+
|
|
62
|
+
return new ReadableStream<Uint8Array>({
|
|
63
|
+
async pull(controller) {
|
|
64
|
+
const next = await chunks.next();
|
|
65
|
+
|
|
66
|
+
if (next.done) controller.close();
|
|
67
|
+
else controller.enqueue(next.value);
|
|
68
|
+
},
|
|
69
|
+
async cancel() {
|
|
70
|
+
await chunks.return(undefined);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
package/worker/db/schema.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { mailboxes, oidcGrants, users } from "@open-workspace/core/
|
|
1
|
+
import { mailboxes, oidcGrants, users } from "@open-workspace/core/schema";
|
|
2
2
|
import { sql } from "drizzle-orm";
|
|
3
3
|
import {
|
|
4
|
+
check,
|
|
4
5
|
foreignKey,
|
|
5
6
|
index,
|
|
6
7
|
integer,
|
|
@@ -10,6 +11,8 @@ import {
|
|
|
10
11
|
uniqueIndex,
|
|
11
12
|
} from "drizzle-orm/sqlite-core";
|
|
12
13
|
|
|
14
|
+
import type { RichImage } from "../../shared/rich-content";
|
|
15
|
+
|
|
13
16
|
/** D1 tables of the mail module; core's tables are in @open-workspace/core. */
|
|
14
17
|
|
|
15
18
|
export const mailboxNotificationPreferences = sqliteTable(
|
|
@@ -32,6 +35,45 @@ export const mailboxNotificationPreferences = sqliteTable(
|
|
|
32
35
|
]
|
|
33
36
|
);
|
|
34
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Signatures: a person's own, used from their own mailboxes, or a team
|
|
40
|
+
* address's, used by everyone sending from it. Exactly one owner is set.
|
|
41
|
+
* Images are in R2 under `signatures/<owner id>/<content id>`.
|
|
42
|
+
*/
|
|
43
|
+
export const signatures = sqliteTable(
|
|
44
|
+
"signatures",
|
|
45
|
+
{
|
|
46
|
+
userId: text("user_id").references(() => users.id, {
|
|
47
|
+
onDelete: "cascade",
|
|
48
|
+
}),
|
|
49
|
+
mailboxId: text("mailbox_id").references(() => mailboxes.id, {
|
|
50
|
+
onDelete: "cascade",
|
|
51
|
+
}),
|
|
52
|
+
/** As the editor wrote it; images are `cid:` references. */
|
|
53
|
+
html: text("html").notNull(),
|
|
54
|
+
/** The editor's plain-text version, for text bodies. */
|
|
55
|
+
text: text("text").notNull(),
|
|
56
|
+
images: text("images", { mode: "json" }).$type<RichImage[]>().notNull(),
|
|
57
|
+
useOnNew: integer("use_on_new", { mode: "boolean" })
|
|
58
|
+
.notNull()
|
|
59
|
+
.default(true),
|
|
60
|
+
useOnReply: integer("use_on_reply", { mode: "boolean" })
|
|
61
|
+
.notNull()
|
|
62
|
+
.default(true),
|
|
63
|
+
updatedAt: integer("updated_at", { mode: "timestamp_ms" })
|
|
64
|
+
.notNull()
|
|
65
|
+
.default(sql`(unixepoch() * 1000)`),
|
|
66
|
+
},
|
|
67
|
+
(table) => [
|
|
68
|
+
uniqueIndex("signatures_user_idx").on(table.userId),
|
|
69
|
+
uniqueIndex("signatures_mailbox_idx").on(table.mailboxId),
|
|
70
|
+
check(
|
|
71
|
+
"signatures_one_owner",
|
|
72
|
+
sql`(${table.userId} is null) <> (${table.mailboxId} is null)`
|
|
73
|
+
),
|
|
74
|
+
]
|
|
75
|
+
);
|
|
76
|
+
|
|
35
77
|
/**
|
|
36
78
|
* Browsers that get new-mail notifications, one row per device, bound to
|
|
37
79
|
* the session that last registered it. Rows from before sessions were
|
|
@@ -45,10 +87,10 @@ export const pushSubscriptions = sqliteTable(
|
|
|
45
87
|
.notNull()
|
|
46
88
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
47
89
|
/**
|
|
48
|
-
* Not a foreign key: expiry
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
90
|
+
* Not a foreign key: expiry must not take the device's notifications
|
|
91
|
+
* with them. Re-registering moves the row to the new session; a session
|
|
92
|
+
* that ends otherwise (signed out, revoked, pushed out) removes it (the
|
|
93
|
+
* `sessionsEnded` hook).
|
|
52
94
|
*/
|
|
53
95
|
sessionId: text("session_id"),
|
|
54
96
|
endpoint: text("endpoint").notNull(),
|
|
@@ -163,3 +205,45 @@ export const mailboxReadCounts = sqliteTable(
|
|
|
163
205
|
},
|
|
164
206
|
(table) => [primaryKey({ columns: [table.mailboxId, table.userId] })]
|
|
165
207
|
);
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Journal of the optional write-once mail archive (`Email({ archive })`,
|
|
211
|
+
* worker/mail/archive.ts): one row per received delivery or sent message.
|
|
212
|
+
* No foreign key, so the record outlives the mailbox. Open rows (pending,
|
|
213
|
+
* failed) are retried by the hourly cron.
|
|
214
|
+
*/
|
|
215
|
+
export const mailArchiveEntries = sqliteTable(
|
|
216
|
+
"mail_archive_entries",
|
|
217
|
+
{
|
|
218
|
+
/** The inbound delivery id, or the sent message's id. */
|
|
219
|
+
id: text("id").primaryKey(),
|
|
220
|
+
/** The mailbox, or for list mail the mailing list. */
|
|
221
|
+
mailboxId: text("mailbox_id").notNull(),
|
|
222
|
+
direction: text("direction", { enum: ["incoming", "outgoing"] }).notNull(),
|
|
223
|
+
/**
|
|
224
|
+
* Where a retry reads the message: received mail's raw copy in
|
|
225
|
+
* MAIL_STORAGE (kept until it is archived), or a staged copy under
|
|
226
|
+
* `archive-staging/`. Each stored outbound attempt has its own raw key.
|
|
227
|
+
*/
|
|
228
|
+
sourceKey: text("source_key"),
|
|
229
|
+
/** `abandoned`: given up (archive.ts), no longer retried. */
|
|
230
|
+
status: text("status", {
|
|
231
|
+
enum: ["pending", "archived", "failed", "abandoned"],
|
|
232
|
+
})
|
|
233
|
+
.notNull()
|
|
234
|
+
.default("pending"),
|
|
235
|
+
attempts: integer("attempts").notNull().default(0),
|
|
236
|
+
lastError: text("last_error"),
|
|
237
|
+
archiveKey: text("archive_key").notNull(),
|
|
238
|
+
sha256: text("sha256"),
|
|
239
|
+
size: integer("size"),
|
|
240
|
+
createdAt: integer("created_at", { mode: "timestamp_ms" }).notNull(),
|
|
241
|
+
updatedAt: integer("updated_at", { mode: "timestamp_ms" }).notNull(),
|
|
242
|
+
archivedAt: integer("archived_at", { mode: "timestamp_ms" }),
|
|
243
|
+
},
|
|
244
|
+
(table) => [
|
|
245
|
+
index("mail_archive_entries_status_idx").on(table.status, table.createdAt),
|
|
246
|
+
index("mail_archive_entries_source_idx").on(table.sourceKey),
|
|
247
|
+
index("mail_archive_entries_mailbox_idx").on(table.mailboxId, table.status),
|
|
248
|
+
]
|
|
249
|
+
);
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { createId } from "@open-workspace/core/module";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { MAIL_READ_SCOPE } from "../../shared/permissions";
|
|
5
|
+
import {
|
|
6
|
+
copyAttachmentToUpload,
|
|
7
|
+
describeComposerUpload,
|
|
8
|
+
UploadValidationError,
|
|
9
|
+
} from "../mail/uploads";
|
|
10
|
+
import { mailboxStub } from "../mailbox";
|
|
11
|
+
import { ATTACHMENT_BLOB, UPLOAD_BLOB } from "./method-core";
|
|
12
|
+
import type { JmapAccount, RequestState } from "./method-core";
|
|
13
|
+
|
|
14
|
+
export const draftAttachment = z
|
|
15
|
+
.object({
|
|
16
|
+
blobId: z
|
|
17
|
+
.string()
|
|
18
|
+
.refine(
|
|
19
|
+
(id) => UPLOAD_BLOB.test(id) || ATTACHMENT_BLOB.test(id),
|
|
20
|
+
"Expected an upload or message attachment blob"
|
|
21
|
+
),
|
|
22
|
+
name: z.string().nullish(),
|
|
23
|
+
type: z.string().nullish(),
|
|
24
|
+
disposition: z.string().nullish(),
|
|
25
|
+
cid: z
|
|
26
|
+
.string()
|
|
27
|
+
.nullish()
|
|
28
|
+
.transform((cid) => cid?.replaceAll(/^<|>$/gu, "") || undefined),
|
|
29
|
+
})
|
|
30
|
+
.superRefine((part, context) => {
|
|
31
|
+
if (part.disposition === "inline" && !part.cid) {
|
|
32
|
+
context.addIssue({
|
|
33
|
+
code: "custom",
|
|
34
|
+
path: ["cid"],
|
|
35
|
+
message: "Inline attachments need a cid",
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
.transform((part) => ({
|
|
40
|
+
blobId: part.blobId,
|
|
41
|
+
uploadId: UPLOAD_BLOB.exec(part.blobId)?.[1] ?? createId("upl"),
|
|
42
|
+
name: part.name,
|
|
43
|
+
type: part.type,
|
|
44
|
+
disposition:
|
|
45
|
+
part.disposition === "inline"
|
|
46
|
+
? ("inline" as const)
|
|
47
|
+
: ("attachment" as const),
|
|
48
|
+
contentId: part.disposition === "inline" ? part.cid : undefined,
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
/** Both saved and one-request drafts use uploads owned by their sender. */
|
|
52
|
+
export async function prepareDraftAttachments(
|
|
53
|
+
state: RequestState,
|
|
54
|
+
account: JmapAccount,
|
|
55
|
+
parts: z.output<typeof draftAttachment>[]
|
|
56
|
+
) {
|
|
57
|
+
if (
|
|
58
|
+
parts.some((part) => ATTACHMENT_BLOB.test(part.blobId)) &&
|
|
59
|
+
(!account.canSend || !state.context.scopes.includes(MAIL_READ_SCOPE))
|
|
60
|
+
) {
|
|
61
|
+
return {
|
|
62
|
+
type: "forbidden",
|
|
63
|
+
description: "Reusing a mailbox attachment requires read and send access",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const { env, user } = state.context;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await Promise.all(
|
|
71
|
+
parts.map(async (part) => {
|
|
72
|
+
const ref = {
|
|
73
|
+
env,
|
|
74
|
+
userId: user.id,
|
|
75
|
+
mailboxId: account.id,
|
|
76
|
+
uploadId: part.uploadId,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const source = ATTACHMENT_BLOB.exec(part.blobId);
|
|
80
|
+
|
|
81
|
+
if (!source) {
|
|
82
|
+
await describeComposerUpload({
|
|
83
|
+
...ref,
|
|
84
|
+
filename: part.name ?? "attachment",
|
|
85
|
+
contentType: part.type ?? "application/octet-stream",
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const file = await mailboxStub(env, account.id).getAttachment(
|
|
92
|
+
source[1],
|
|
93
|
+
source[2]
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
if (!file) throw new UploadValidationError("Attachment was not found");
|
|
97
|
+
await copyAttachmentToUpload({
|
|
98
|
+
...ref,
|
|
99
|
+
sourceKey: file.r2Key,
|
|
100
|
+
size: file.size,
|
|
101
|
+
filename: part.name ?? file.filename,
|
|
102
|
+
contentType: part.type ?? file.contentType,
|
|
103
|
+
});
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
return null;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (!(error instanceof UploadValidationError)) throw error;
|
|
110
|
+
|
|
111
|
+
return { type: "blobNotFound", description: error.message };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { required } from "@open-workspace/core/shared";
|
|
2
|
+
|
|
3
|
+
import { quotedContextHtml } from "../../shared/mail-html";
|
|
4
|
+
import { streamEml } from "../backups/stream-eml";
|
|
5
|
+
import { draftSenderResolver } from "../mail/draft-sender";
|
|
6
|
+
import { messageText, separateHtmlQuote } from "../mail/outbound-content";
|
|
7
|
+
import { loadComposerUpload } from "../mail/uploads";
|
|
8
|
+
import { mailboxStub } from "../mailbox";
|
|
9
|
+
import { draftContent } from "./draft-content";
|
|
10
|
+
import { draftHeaders, isStoredDraftId } from "./drafts";
|
|
11
|
+
import { ATTACHMENT_BLOB, UPLOAD_BLOB } from "./method-core";
|
|
12
|
+
import type { JmapAccount } from "./method-core";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One of the person's drafts as MIME (its `draft~` blob): what the message
|
|
16
|
+
* would be if sent now, with its files. Null when the draft is gone.
|
|
17
|
+
*/
|
|
18
|
+
async function draftEml(
|
|
19
|
+
env: Env,
|
|
20
|
+
account: JmapAccount,
|
|
21
|
+
userId: string,
|
|
22
|
+
draftId: string
|
|
23
|
+
) {
|
|
24
|
+
const {
|
|
25
|
+
drafts: [draft],
|
|
26
|
+
} = await mailboxStub(env, account.id).jmapEmails(userId, [draftId]);
|
|
27
|
+
|
|
28
|
+
if (!draft) return null;
|
|
29
|
+
|
|
30
|
+
const content = await draftContent(env.MAIL_STORAGE, draft);
|
|
31
|
+
|
|
32
|
+
return streamEml({
|
|
33
|
+
id: draft.id,
|
|
34
|
+
...draftHeaders(
|
|
35
|
+
await draftSenderResolver(env, account)(draft.replySenderSource),
|
|
36
|
+
draft
|
|
37
|
+
),
|
|
38
|
+
date: required(draft.updatedAt, "the full draft includes its date"),
|
|
39
|
+
text: content.bodyText,
|
|
40
|
+
html: content.bodyHtml,
|
|
41
|
+
attachments: content.attachments.map((file) => ({
|
|
42
|
+
filename: file.filename,
|
|
43
|
+
type: file.contentType,
|
|
44
|
+
disposition: file.disposition,
|
|
45
|
+
contentId: file.contentId ?? undefined,
|
|
46
|
+
async open() {
|
|
47
|
+
const object = await env.MAIL_STORAGE.get(file.r2Key);
|
|
48
|
+
|
|
49
|
+
if (!object) throw new Error("Draft attachment data is missing");
|
|
50
|
+
|
|
51
|
+
return object.body;
|
|
52
|
+
},
|
|
53
|
+
})),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function readBodyPart(
|
|
58
|
+
env: Env,
|
|
59
|
+
account: JmapAccount,
|
|
60
|
+
userId: string,
|
|
61
|
+
id: string,
|
|
62
|
+
html: boolean
|
|
63
|
+
) {
|
|
64
|
+
const stub = mailboxStub(env, account.id);
|
|
65
|
+
|
|
66
|
+
if (isStoredDraftId(id)) {
|
|
67
|
+
const {
|
|
68
|
+
drafts: [draft],
|
|
69
|
+
} = await stub.jmapEmails(userId, [id], ["bodyValues"]);
|
|
70
|
+
|
|
71
|
+
if (!draft) return null;
|
|
72
|
+
const content = await draftContent(env.MAIL_STORAGE, draft);
|
|
73
|
+
|
|
74
|
+
return html ? (content.bodyHtml ?? null) : content.bodyText;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const email = await stub.getEmail(id);
|
|
78
|
+
|
|
79
|
+
if (!email) return null;
|
|
80
|
+
|
|
81
|
+
if (!html) return messageText(email);
|
|
82
|
+
|
|
83
|
+
if (!email.bodyHtmlR2Key) return null;
|
|
84
|
+
const htmlPart = await env.MAIL_STORAGE.get(email.bodyHtmlR2Key);
|
|
85
|
+
|
|
86
|
+
if (!htmlPart) return null;
|
|
87
|
+
const quote = separateHtmlQuote(email);
|
|
88
|
+
|
|
89
|
+
return quote
|
|
90
|
+
? (await htmlPart.text()) + quotedContextHtml(quote)
|
|
91
|
+
: htmlPart.body;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Resolve the bytes advertised by Email/get inside the authorized account. */
|
|
95
|
+
export async function readJmapBlob(
|
|
96
|
+
env: Env,
|
|
97
|
+
account: JmapAccount,
|
|
98
|
+
userId: string,
|
|
99
|
+
blobId: string
|
|
100
|
+
) {
|
|
101
|
+
const part = /^(text|html)~([^~]+)$/u.exec(blobId);
|
|
102
|
+
|
|
103
|
+
if (part) {
|
|
104
|
+
const [, kind, id] = part;
|
|
105
|
+
const html = kind === "html";
|
|
106
|
+
const body = await readBodyPart(env, account, userId, id, html);
|
|
107
|
+
|
|
108
|
+
return body === null
|
|
109
|
+
? null
|
|
110
|
+
: {
|
|
111
|
+
body,
|
|
112
|
+
type: html ? "text/html; charset=utf-8" : "text/plain; charset=utf-8",
|
|
113
|
+
mutable: isStoredDraftId(id),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const draft = /^draft~(.+)$/u.exec(blobId);
|
|
118
|
+
|
|
119
|
+
if (draft) {
|
|
120
|
+
const body = await draftEml(env, account, userId, draft[1]);
|
|
121
|
+
|
|
122
|
+
return body ? { body, type: "message/rfc822", mutable: true } : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const upload = UPLOAD_BLOB.exec(blobId);
|
|
126
|
+
const raw = /^raw~([^~]+)$/u.exec(blobId);
|
|
127
|
+
const attachment = ATTACHMENT_BLOB.exec(blobId);
|
|
128
|
+
let key: string | null | undefined;
|
|
129
|
+
|
|
130
|
+
if (upload) {
|
|
131
|
+
key = await loadComposerUpload({
|
|
132
|
+
env,
|
|
133
|
+
mailboxId: account.id,
|
|
134
|
+
userId,
|
|
135
|
+
uploadId: upload[1],
|
|
136
|
+
}).then(
|
|
137
|
+
(found) => found.r2Key,
|
|
138
|
+
() => null
|
|
139
|
+
);
|
|
140
|
+
} else if (raw) {
|
|
141
|
+
key = await mailboxStub(env, account.id).getRawMimeKey(raw[1]);
|
|
142
|
+
} else if (attachment) {
|
|
143
|
+
key = (
|
|
144
|
+
await mailboxStub(env, account.id).getAttachment(
|
|
145
|
+
attachment[1],
|
|
146
|
+
attachment[2]
|
|
147
|
+
)
|
|
148
|
+
)?.r2Key;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const object = key ? await env.MAIL_STORAGE.get(key) : null;
|
|
152
|
+
|
|
153
|
+
if (!object) return null;
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
body: object.body,
|
|
157
|
+
type:
|
|
158
|
+
object.httpMetadata?.contentType ||
|
|
159
|
+
(raw ? "message/rfc822" : "application/octet-stream"),
|
|
160
|
+
mutable: false,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
appendForwardedMessage,
|
|
3
|
+
selectedForwardFiles,
|
|
4
|
+
} from "../mail/outbound-content";
|
|
5
|
+
import { plainBody, readableDraftText } from "../mail/plain-body";
|
|
6
|
+
import type { JmapDraftRow } from "../mailbox/jmap-email-rows";
|
|
7
|
+
|
|
8
|
+
/** Web drafts keep the note and source file selection separately until send. */
|
|
9
|
+
export async function draftContent(bucket: R2Bucket, draft: JmapDraftRow) {
|
|
10
|
+
const sourceFiles = draft.sourceFiles ?? [];
|
|
11
|
+
|
|
12
|
+
const uploads = (draft.attachments ?? []).map((file) => ({
|
|
13
|
+
...file,
|
|
14
|
+
partId: `att-${file.uploadId}`,
|
|
15
|
+
blobId: `upl~${file.uploadId}`,
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const forwarded = selectedForwardFiles(sourceFiles, draft.selectedIds).map(
|
|
19
|
+
(file) => ({
|
|
20
|
+
...file,
|
|
21
|
+
partId: `fwd-${file.id}`,
|
|
22
|
+
blobId: `att~${draft.forwardId}~${file.id}`,
|
|
23
|
+
disposition: "attachment" as const,
|
|
24
|
+
contentId: null,
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const bodyText = readableDraftText(draft.bodyText, draft.attachments);
|
|
29
|
+
|
|
30
|
+
const body = draft.forwardBody
|
|
31
|
+
? await appendForwardedMessage(
|
|
32
|
+
bucket,
|
|
33
|
+
plainBody(bodyText, draft.bodyHtml),
|
|
34
|
+
draft.bodyHtml ?? undefined,
|
|
35
|
+
{ ...draft.forwardBody, attachmentsJson: sourceFiles },
|
|
36
|
+
// The draft contains MIME parts. No outgoing download links exist yet.
|
|
37
|
+
new Map()
|
|
38
|
+
)
|
|
39
|
+
: { bodyText, bodyHtml: draft.bodyHtml ?? undefined };
|
|
40
|
+
|
|
41
|
+
return { ...body, attachments: [...uploads, ...forwarded] };
|
|
42
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** JMAP's view of kept drafts and their transition to Sent. */
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { DRAFTS_FOLDER_ID } from "../../shared/folder-order";
|
|
5
|
+
import { canReplyFrom } from "../mail/reply-plan";
|
|
6
|
+
import { mailboxStub } from "../mailbox";
|
|
7
|
+
import type { JmapDraftRow } from "../mailbox/jmap-email-rows";
|
|
8
|
+
import type { Draft, JmapAccount, RequestState } from "./method-core";
|
|
9
|
+
|
|
10
|
+
// The outgoing copy is already in Sent, seen and without $draft. Accept only
|
|
11
|
+
// changes it already satisfies before releasing the submitted draft version.
|
|
12
|
+
const sentKeywords = {
|
|
13
|
+
"keywords/$draft": z.null().optional(),
|
|
14
|
+
"keywords/$seen": z.literal(true).optional(),
|
|
15
|
+
keywords: z.strictObject({ $seen: z.literal(true) }).optional(),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const moveDraftToSent = z.union([
|
|
19
|
+
z.strictObject({
|
|
20
|
+
[`mailboxIds/${DRAFTS_FOLDER_ID}`]: z.null(),
|
|
21
|
+
"mailboxIds/sent": z.literal(true),
|
|
22
|
+
...sentKeywords,
|
|
23
|
+
}),
|
|
24
|
+
z.strictObject({
|
|
25
|
+
mailboxIds: z.strictObject({ sent: z.literal(true) }),
|
|
26
|
+
...sentKeywords,
|
|
27
|
+
}),
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
/** Message headers shared by Email/get and the downloadable draft. */
|
|
31
|
+
export function draftHeaders(
|
|
32
|
+
sender: Pick<JmapAccount, "address" | "displayName">,
|
|
33
|
+
draft: JmapDraftRow
|
|
34
|
+
) {
|
|
35
|
+
const list = (values: string[]) =>
|
|
36
|
+
values.map((address) => ({ address, name: null }));
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
from: [{ address: sender.address, name: sender.displayName || null }],
|
|
40
|
+
to: list(draft.to ?? []),
|
|
41
|
+
cc: list(draft.cc ?? []),
|
|
42
|
+
bcc: list(draft.bcc ?? []),
|
|
43
|
+
replyTo: draft.replyTo ? list([draft.replyTo]) : [],
|
|
44
|
+
date: draft.updatedAt,
|
|
45
|
+
messageId: null,
|
|
46
|
+
subject: draft.subject ?? "",
|
|
47
|
+
...draft.thread,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const isStoredDraftId = (id: string) => id.startsWith("drf_");
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The message a draft answers, in this account, if it can be answered: by
|
|
55
|
+
* a kept draft's source, else the first of its inReplyTo Message-IDs here.
|
|
56
|
+
*/
|
|
57
|
+
export async function replySource(
|
|
58
|
+
state: RequestState,
|
|
59
|
+
owner: JmapAccount,
|
|
60
|
+
answers: Draft["answers"]
|
|
61
|
+
) {
|
|
62
|
+
const stub = mailboxStub(state.context.env, owner.id);
|
|
63
|
+
|
|
64
|
+
const candidates = await Promise.all([
|
|
65
|
+
answers.emailId ? stub.getEmail(answers.emailId) : null,
|
|
66
|
+
...answers.messageIds.map((id) => stub.getEmailByMessageId(id)),
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
return candidates.find((email) => email && canReplyFrom(email)) ?? null;
|
|
70
|
+
}
|