@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,473 @@
|
|
|
1
|
+
import {
|
|
2
|
+
apiError,
|
|
3
|
+
createId,
|
|
4
|
+
oauthScope,
|
|
5
|
+
zValidator,
|
|
6
|
+
} from "@open-workspace/core/module";
|
|
7
|
+
import type { AppEnv } from "@open-workspace/core/module";
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import type { Context } from "hono";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
composerAttachmentLimitError,
|
|
14
|
+
isComposerInlineImageContentType,
|
|
15
|
+
} from "../../shared/mail";
|
|
16
|
+
import { MAIL_SEND_SCOPE } from "../../shared/permissions";
|
|
17
|
+
import type { StoredImageRef } from "../../shared/rich-content";
|
|
18
|
+
import { mailboxStub } from "../mailbox";
|
|
19
|
+
import type { Draft, DraftAttachment } from "../mailbox/schema";
|
|
20
|
+
import { MAX_DRAFTS_PER_USER } from "../mailbox/support";
|
|
21
|
+
import type { SaveDraftResult } from "../mailbox/support";
|
|
22
|
+
import { accessibleMailbox } from "./access";
|
|
23
|
+
import { draftSenderResolver } from "./draft-sender";
|
|
24
|
+
import {
|
|
25
|
+
draftIdSchema,
|
|
26
|
+
mailboxQuerySchema,
|
|
27
|
+
saveDraftSchema,
|
|
28
|
+
uploadIdSchema,
|
|
29
|
+
} from "./schemas";
|
|
30
|
+
import { signatureImagePrefix, withSignature } from "./signatures";
|
|
31
|
+
import {
|
|
32
|
+
copyDraftFile,
|
|
33
|
+
draftFileKey,
|
|
34
|
+
loadComposerUpload,
|
|
35
|
+
readDraftFile,
|
|
36
|
+
UploadValidationError,
|
|
37
|
+
} from "./uploads";
|
|
38
|
+
|
|
39
|
+
const draftParamSchema = z.object({ id: draftIdSchema });
|
|
40
|
+
|
|
41
|
+
/** A draft as the API returns it; the owner is always the caller. */
|
|
42
|
+
export function draftView(
|
|
43
|
+
{ userId: _, attachments, ...draft }: Draft,
|
|
44
|
+
fromAddress?: string
|
|
45
|
+
) {
|
|
46
|
+
return {
|
|
47
|
+
...draft,
|
|
48
|
+
...(fromAddress && { fromAddress }),
|
|
49
|
+
attachments: attachments.map(({ r2Key: _key, ...file }) => file),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type DraftSave = z.output<typeof saveDraftSchema>;
|
|
54
|
+
|
|
55
|
+
type DraftOwner = {
|
|
56
|
+
env: Env;
|
|
57
|
+
user: { id: string; name: string };
|
|
58
|
+
mailbox: { id: string; ownerUserId: string | null };
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** A file of a save: one the draft holds already, or one to copy in from `copyFrom`. */
|
|
62
|
+
type ResolvedFile = { file: DraftAttachment; copyFrom: string | null };
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The uploads a save names, with what the draft already holds reused and new
|
|
66
|
+
* uploads checked against the composer's limits. Copies nothing yet.
|
|
67
|
+
*/
|
|
68
|
+
async function resolveUploads(
|
|
69
|
+
owner: DraftOwner,
|
|
70
|
+
files: DraftSave["attachments"],
|
|
71
|
+
currentFiles: DraftAttachment[]
|
|
72
|
+
) {
|
|
73
|
+
const { env, user, mailbox } = owner;
|
|
74
|
+
|
|
75
|
+
const held = new Map(currentFiles.map((file) => [file.uploadId, file]));
|
|
76
|
+
|
|
77
|
+
const resolved = await Promise.all(
|
|
78
|
+
files.map(async (file) => {
|
|
79
|
+
const known = held.get(file.uploadId);
|
|
80
|
+
|
|
81
|
+
const upload =
|
|
82
|
+
known ??
|
|
83
|
+
(await loadComposerUpload({
|
|
84
|
+
env,
|
|
85
|
+
mailboxId: mailbox.id,
|
|
86
|
+
userId: user.id,
|
|
87
|
+
uploadId: file.uploadId,
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
file.disposition === "inline" &&
|
|
92
|
+
!isComposerInlineImageContentType(upload.contentType)
|
|
93
|
+
) {
|
|
94
|
+
throw new UploadValidationError(
|
|
95
|
+
"Inline images must be PNG, JPEG, GIF, or WebP"
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const r2Key = draftFileKey(mailbox.id, user.id, file.uploadId);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
copyFrom: known || upload.r2Key === r2Key ? null : upload.r2Key,
|
|
103
|
+
file: {
|
|
104
|
+
assetId: file.assetId,
|
|
105
|
+
uploadId: file.uploadId,
|
|
106
|
+
disposition: file.disposition,
|
|
107
|
+
contentId: file.contentId ?? null,
|
|
108
|
+
filename: upload.filename,
|
|
109
|
+
contentType: upload.contentType,
|
|
110
|
+
size: upload.size,
|
|
111
|
+
r2Key,
|
|
112
|
+
},
|
|
113
|
+
} satisfies ResolvedFile;
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const limitError = composerAttachmentLimitError(
|
|
118
|
+
resolved.map((entry) => entry.file)
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
if (limitError) throw new UploadValidationError(limitError);
|
|
122
|
+
|
|
123
|
+
return resolved;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const storedKey = (ref: StoredImageRef) => `${ref.ownerId}/${ref.contentId}`;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The signature images a save shows, each as the draft's own copy, so a
|
|
130
|
+
* later change to the signature leaves the draft as it was. Copies are made
|
|
131
|
+
* once; the sender may use only the signatures a message may (withSignature).
|
|
132
|
+
*/
|
|
133
|
+
async function resolveStoredImages(
|
|
134
|
+
owner: DraftOwner,
|
|
135
|
+
refs: readonly StoredImageRef[],
|
|
136
|
+
currentFiles: DraftAttachment[]
|
|
137
|
+
) {
|
|
138
|
+
const held = new Map(
|
|
139
|
+
currentFiles.flatMap((file) =>
|
|
140
|
+
file.storedImage ? [[storedKey(file.storedImage), file] as const] : []
|
|
141
|
+
)
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const unique = [
|
|
145
|
+
...new Map(refs.map((ref) => [storedKey(ref), ref])).values(),
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
const missing = unique.filter((ref) => !held.has(storedKey(ref)));
|
|
149
|
+
|
|
150
|
+
const { inlineImages } = missing.length
|
|
151
|
+
? await withSignature({
|
|
152
|
+
env: owner.env,
|
|
153
|
+
sender: owner.user,
|
|
154
|
+
mailbox: owner.mailbox,
|
|
155
|
+
kind: "new",
|
|
156
|
+
message: { bodyText: "", storedImages: missing },
|
|
157
|
+
})
|
|
158
|
+
: { inlineImages: [] };
|
|
159
|
+
|
|
160
|
+
// Keep the editor's order on the first save and on replay, even when some
|
|
161
|
+
// images already belong to the draft or have disappeared from the signature.
|
|
162
|
+
const sources = new Map(inlineImages.map((image) => [image.r2Key, image]));
|
|
163
|
+
|
|
164
|
+
return unique.flatMap<ResolvedFile>((ref) => {
|
|
165
|
+
const file = held.get(storedKey(ref));
|
|
166
|
+
|
|
167
|
+
if (file) return [{ file, copyFrom: null }];
|
|
168
|
+
|
|
169
|
+
const image = sources.get(
|
|
170
|
+
`${signatureImagePrefix(ref.ownerId)}${ref.contentId}`
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (!image) return [];
|
|
174
|
+
const uploadId = createId("upl");
|
|
175
|
+
|
|
176
|
+
return [
|
|
177
|
+
{
|
|
178
|
+
copyFrom: image.r2Key,
|
|
179
|
+
file: {
|
|
180
|
+
assetId: crypto.randomUUID(),
|
|
181
|
+
uploadId,
|
|
182
|
+
disposition: "inline",
|
|
183
|
+
contentId: image.contentId,
|
|
184
|
+
filename: image.filename,
|
|
185
|
+
contentType: image.contentType,
|
|
186
|
+
size: image.size,
|
|
187
|
+
r2Key: draftFileKey(owner.mailbox.id, owner.user.id, uploadId),
|
|
188
|
+
storedImage: ref,
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
];
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export type StoreDraftResult =
|
|
196
|
+
| SaveDraftResult
|
|
197
|
+
| { status: "invalid"; message: string };
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Saves the caller's draft `id` in their mailbox: its files are copied out
|
|
201
|
+
* of the expiring upload prefix (only those it doesn't hold yet, so
|
|
202
|
+
* autosaving while typing copies nothing), then the row is written if it is
|
|
203
|
+
* still at `expectedVersion`. Used by the drafts API and JMAP Email/set.
|
|
204
|
+
*/
|
|
205
|
+
export async function storeDraft(
|
|
206
|
+
owner: DraftOwner,
|
|
207
|
+
id: string,
|
|
208
|
+
save: DraftSave
|
|
209
|
+
) {
|
|
210
|
+
const { expectedVersion, attachments, storedImages, ...input } = save;
|
|
211
|
+
const stub = mailboxStub(owner.env, owner.mailbox.id);
|
|
212
|
+
|
|
213
|
+
const currentFiles =
|
|
214
|
+
attachments.length || storedImages.length
|
|
215
|
+
? await stub.getDraftAttachments(owner.user.id, id)
|
|
216
|
+
: [];
|
|
217
|
+
|
|
218
|
+
let files: ResolvedFile[];
|
|
219
|
+
|
|
220
|
+
try {
|
|
221
|
+
files = [
|
|
222
|
+
...(await resolveUploads(owner, attachments, currentFiles)),
|
|
223
|
+
...(await resolveStoredImages(owner, storedImages, currentFiles)),
|
|
224
|
+
];
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (!(error instanceof UploadValidationError)) throw error;
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
status: "invalid",
|
|
230
|
+
message: error.message,
|
|
231
|
+
} satisfies StoreDraftResult;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const copied = files.filter((entry) => entry.copyFrom !== null);
|
|
235
|
+
|
|
236
|
+
const copies = await Promise.allSettled(
|
|
237
|
+
copied.map((entry) =>
|
|
238
|
+
copyDraftFile(owner.env, entry.copyFrom ?? "", entry.file)
|
|
239
|
+
)
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const copiedKeys = copied.map((entry) => entry.file.r2Key);
|
|
243
|
+
|
|
244
|
+
const failed = copies.find(
|
|
245
|
+
(copy): copy is PromiseRejectedResult => copy.status === "rejected"
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
if (failed) {
|
|
249
|
+
await stub.forgetDraftFiles(copiedKeys);
|
|
250
|
+
|
|
251
|
+
if (failed.reason instanceof UploadValidationError) {
|
|
252
|
+
return {
|
|
253
|
+
status: "invalid",
|
|
254
|
+
message: failed.reason.message,
|
|
255
|
+
} satisfies StoreDraftResult;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
throw failed.reason;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const saved: StoreDraftResult = await stub.saveDraft(
|
|
262
|
+
owner.user.id,
|
|
263
|
+
{ ...input, id, attachments: files.map((entry) => entry.file) },
|
|
264
|
+
expectedVersion,
|
|
265
|
+
copiedKeys
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
return saved;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function forbidden(c: Context<AppEnv>) {
|
|
272
|
+
return apiError(c, 403, "FORBIDDEN", "Send access is required");
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Autosaved drafts, private to their author in any mailbox. Saving one needs
|
|
277
|
+
* send access, like the message it becomes.
|
|
278
|
+
*/
|
|
279
|
+
export const mailDraftRoutes = new Hono<AppEnv>()
|
|
280
|
+
.get(
|
|
281
|
+
"/",
|
|
282
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
283
|
+
zValidator("query", mailboxQuerySchema),
|
|
284
|
+
async (c) => {
|
|
285
|
+
const { mailboxId } = c.req.valid("query");
|
|
286
|
+
|
|
287
|
+
if (!(await accessibleMailbox(c, mailboxId, "send"))) {
|
|
288
|
+
return forbidden(c);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const { drafts, version } = await mailboxStub(
|
|
292
|
+
c.env,
|
|
293
|
+
mailboxId
|
|
294
|
+
).versionedDrafts(c.get("user").id);
|
|
295
|
+
|
|
296
|
+
return c.json({
|
|
297
|
+
ok: true as const,
|
|
298
|
+
drafts: drafts.map((draft) => draftView(draft)),
|
|
299
|
+
version,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
)
|
|
303
|
+
.get(
|
|
304
|
+
"/files/:uploadId",
|
|
305
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
306
|
+
zValidator("param", z.object({ uploadId: uploadIdSchema })),
|
|
307
|
+
zValidator("query", mailboxQuerySchema),
|
|
308
|
+
async (c) => {
|
|
309
|
+
const { mailboxId } = c.req.valid("query");
|
|
310
|
+
|
|
311
|
+
if (!(await accessibleMailbox(c, mailboxId, "send"))) {
|
|
312
|
+
return forbidden(c);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const object = await readDraftFile({
|
|
316
|
+
env: c.env,
|
|
317
|
+
mailboxId,
|
|
318
|
+
userId: c.get("user").id,
|
|
319
|
+
uploadId: c.req.valid("param").uploadId,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const contentType = object?.httpMetadata?.contentType ?? "";
|
|
323
|
+
|
|
324
|
+
// Only inline images are shown in the composer; nothing else is served.
|
|
325
|
+
if (!object || !isComposerInlineImageContentType(contentType)) {
|
|
326
|
+
return apiError(c, 404, "NOT_FOUND", "Draft file was not found");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return c.body(object.body, 200, {
|
|
330
|
+
"content-type": contentType,
|
|
331
|
+
"cache-control": "private, max-age=3600",
|
|
332
|
+
"x-content-type-options": "nosniff",
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
)
|
|
336
|
+
.get(
|
|
337
|
+
"/:id",
|
|
338
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
339
|
+
zValidator("param", draftParamSchema),
|
|
340
|
+
zValidator("query", mailboxQuerySchema),
|
|
341
|
+
async (c) => {
|
|
342
|
+
const { mailboxId } = c.req.valid("query");
|
|
343
|
+
|
|
344
|
+
const access = await accessibleMailbox(c, mailboxId, "send");
|
|
345
|
+
|
|
346
|
+
if (!access) return forbidden(c);
|
|
347
|
+
|
|
348
|
+
const row = await mailboxStub(c.env, mailboxId).draftForEditor(
|
|
349
|
+
c.get("user").id,
|
|
350
|
+
c.req.valid("param").id
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
if (!row) return apiError(c, 404, "NOT_FOUND", "Draft was not found");
|
|
354
|
+
|
|
355
|
+
const sender = await draftSenderResolver(c.env, access)(row.parent);
|
|
356
|
+
|
|
357
|
+
return c.json({
|
|
358
|
+
ok: true as const,
|
|
359
|
+
draft: draftView(row.draft, sender.address),
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
)
|
|
363
|
+
.put(
|
|
364
|
+
"/:id",
|
|
365
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
366
|
+
zValidator("param", draftParamSchema),
|
|
367
|
+
zValidator("query", mailboxQuerySchema),
|
|
368
|
+
zValidator("json", saveDraftSchema),
|
|
369
|
+
async (c) => {
|
|
370
|
+
const { mailboxId } = c.req.valid("query");
|
|
371
|
+
const access = await accessibleMailbox(c, mailboxId, "send");
|
|
372
|
+
|
|
373
|
+
if (!access) return forbidden(c);
|
|
374
|
+
|
|
375
|
+
const result = await storeDraft(
|
|
376
|
+
{ env: c.env, user: c.get("user"), mailbox: access },
|
|
377
|
+
c.req.valid("param").id,
|
|
378
|
+
c.req.valid("json")
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
if (result.status === "invalid") {
|
|
382
|
+
return apiError(c, 400, "BAD_REQUEST", result.message);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (result.status === "limit") {
|
|
386
|
+
return apiError(
|
|
387
|
+
c,
|
|
388
|
+
409,
|
|
389
|
+
"CONFLICT",
|
|
390
|
+
"Too many drafts: send or discard some first"
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (result.status === "conflict") {
|
|
395
|
+
return c.json(
|
|
396
|
+
{
|
|
397
|
+
ok: false as const,
|
|
398
|
+
error: {
|
|
399
|
+
code: "DRAFT_CHANGED" as const,
|
|
400
|
+
message: "This draft was changed in another window",
|
|
401
|
+
},
|
|
402
|
+
draft: result.draft ? draftView(result.draft) : null,
|
|
403
|
+
},
|
|
404
|
+
409
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return c.json({ ok: true as const, draft: draftView(result.draft) });
|
|
409
|
+
}
|
|
410
|
+
)
|
|
411
|
+
.post(
|
|
412
|
+
"/discard",
|
|
413
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
414
|
+
zValidator("query", mailboxQuerySchema),
|
|
415
|
+
zValidator(
|
|
416
|
+
"json",
|
|
417
|
+
z.object({
|
|
418
|
+
drafts: z
|
|
419
|
+
.array(
|
|
420
|
+
draftParamSchema.extend({ version: z.number().int().positive() })
|
|
421
|
+
)
|
|
422
|
+
.min(1)
|
|
423
|
+
.max(MAX_DRAFTS_PER_USER),
|
|
424
|
+
})
|
|
425
|
+
),
|
|
426
|
+
async (c) => {
|
|
427
|
+
const { mailboxId } = c.req.valid("query");
|
|
428
|
+
|
|
429
|
+
if (!(await accessibleMailbox(c, mailboxId, "send"))) return forbidden(c);
|
|
430
|
+
|
|
431
|
+
const discarded = await mailboxStub(c.env, mailboxId).discardDrafts(
|
|
432
|
+
c.get("user").id,
|
|
433
|
+
c.req.valid("json").drafts
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
return c.json({ ok: true as const, discarded });
|
|
437
|
+
}
|
|
438
|
+
)
|
|
439
|
+
.delete(
|
|
440
|
+
"/:id",
|
|
441
|
+
oauthScope(MAIL_SEND_SCOPE),
|
|
442
|
+
zValidator("param", draftParamSchema),
|
|
443
|
+
zValidator(
|
|
444
|
+
"query",
|
|
445
|
+
mailboxQuerySchema.extend({
|
|
446
|
+
version: z.coerce.number().int().positive().optional(),
|
|
447
|
+
})
|
|
448
|
+
),
|
|
449
|
+
async (c) => {
|
|
450
|
+
const { mailboxId, version } = c.req.valid("query");
|
|
451
|
+
|
|
452
|
+
if (!(await accessibleMailbox(c, mailboxId, "send"))) {
|
|
453
|
+
return forbidden(c);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const deleted = await mailboxStub(c.env, mailboxId).deleteDraft(
|
|
457
|
+
c.get("user").id,
|
|
458
|
+
c.req.valid("param").id,
|
|
459
|
+
version
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
if (!deleted && version !== undefined) {
|
|
463
|
+
return apiError(
|
|
464
|
+
c,
|
|
465
|
+
409,
|
|
466
|
+
"CONFLICT",
|
|
467
|
+
"This draft changed or was removed. Reopen it before discarding."
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return c.json({ ok: true as const });
|
|
472
|
+
}
|
|
473
|
+
);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Email } from "../mailbox/schema";
|
|
2
|
+
import { mailboxAliases } from "./outgoing-context";
|
|
3
|
+
import { canReplyFrom, replyAlias } from "./reply-plan";
|
|
4
|
+
|
|
5
|
+
type Sender = { address: string; displayName: string };
|
|
6
|
+
|
|
7
|
+
type Mailbox = Sender & { id: string };
|
|
8
|
+
|
|
9
|
+
type Parent = Pick<
|
|
10
|
+
Email,
|
|
11
|
+
| "direction"
|
|
12
|
+
| "envelopeTo"
|
|
13
|
+
| "fromJson"
|
|
14
|
+
| "toJson"
|
|
15
|
+
| "ccJson"
|
|
16
|
+
| "transportState"
|
|
17
|
+
| "messageIdHeader"
|
|
18
|
+
>;
|
|
19
|
+
|
|
20
|
+
export type DraftSenders = { primary: Sender; aliases: Sender[] };
|
|
21
|
+
|
|
22
|
+
export async function mailboxDraftSenders(db: D1Database, owner: Mailbox) {
|
|
23
|
+
const { address, displayName } = owner;
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
primary: { address, displayName },
|
|
27
|
+
aliases: await mailboxAliases(db, owner.id),
|
|
28
|
+
} satisfies DraftSenders;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A forward or a new message has no reply parent and uses the mailbox address. */
|
|
32
|
+
export function draftSender(
|
|
33
|
+
senders: DraftSenders,
|
|
34
|
+
parent: Parent | null | undefined
|
|
35
|
+
) {
|
|
36
|
+
return (
|
|
37
|
+
(parent &&
|
|
38
|
+
canReplyFrom(parent) &&
|
|
39
|
+
replyAlias(parent, senders.primary.address, senders.aliases)) ||
|
|
40
|
+
senders.primary
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A batch of drafts without reply parents needs no alias lookup. */
|
|
45
|
+
export function draftSenderResolver(env: Env, owner: Mailbox) {
|
|
46
|
+
let senders: Promise<DraftSenders> | undefined;
|
|
47
|
+
|
|
48
|
+
return async (parent: Parent | null | undefined) => {
|
|
49
|
+
if (!parent) return owner;
|
|
50
|
+
senders ??= mailboxDraftSenders(env.DB, owner);
|
|
51
|
+
|
|
52
|
+
return draftSender(await senders, parent);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AppEnv } from "@open-workspace/core/module";
|
|
2
2
|
import { timed } from "@open-workspace/core/module";
|
|
3
|
+
import { waitUntil } from "cloudflare:workers";
|
|
3
4
|
import type { Context } from "hono";
|
|
4
5
|
|
|
5
6
|
import { mailS3 } from "./r2-presigned-urls";
|
|
@@ -21,24 +22,6 @@ const MAX_CACHED_BODY_BYTES = 2 * 1024 * 1024;
|
|
|
21
22
|
/** Remote images are shared by everyone who opens the same newsletter. */
|
|
22
23
|
const REMOTE_TTL_SECONDS = 24 * 60 * 60;
|
|
23
24
|
|
|
24
|
-
async function openCache(name: string) {
|
|
25
|
-
try {
|
|
26
|
-
return await caches.open(name);
|
|
27
|
-
} catch {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function background(c: Context<AppEnv>, work: Promise<unknown>) {
|
|
33
|
-
const guarded = work.catch(() => undefined);
|
|
34
|
-
|
|
35
|
-
try {
|
|
36
|
-
c.executionCtx.waitUntil(guarded);
|
|
37
|
-
} catch {
|
|
38
|
-
// No execution context (in-process calls): the promise still runs.
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
25
|
async function sha256Hex(value: string) {
|
|
43
26
|
const digest = await crypto.subtle.digest(
|
|
44
27
|
"SHA-256",
|
|
@@ -55,18 +38,16 @@ async function sha256Hex(value: string) {
|
|
|
55
38
|
* earlier request put it there, else from R2, then cached.
|
|
56
39
|
*/
|
|
57
40
|
export async function readStoredBody(c: Context<AppEnv>, key: string) {
|
|
58
|
-
const cache = await
|
|
41
|
+
const cache = await caches.open("openworkspace:mail-bodies");
|
|
59
42
|
|
|
60
43
|
// The bucket name keeps workspaces that share a zone apart.
|
|
61
44
|
const request = new Request(
|
|
62
45
|
`https://mail-bodies.openworkspace.invalid/v1/${encodeURIComponent(mailS3(c.env).bucketName)}/${encodeURIComponent(key)}`
|
|
63
46
|
);
|
|
64
47
|
|
|
65
|
-
const hit = cache
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
)
|
|
69
|
-
: undefined;
|
|
48
|
+
const hit = await timed(c, "cache", "body", () =>
|
|
49
|
+
cache.match(request).catch(() => undefined)
|
|
50
|
+
);
|
|
70
51
|
|
|
71
52
|
if (hit) return await hit.text();
|
|
72
53
|
|
|
@@ -77,24 +58,29 @@ export async function readStoredBody(c: Context<AppEnv>, key: string) {
|
|
|
77
58
|
if (!object) return null;
|
|
78
59
|
const html = await object.text();
|
|
79
60
|
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
61
|
+
if (object.size <= MAX_CACHED_BODY_BYTES) {
|
|
62
|
+
waitUntil(
|
|
63
|
+
cache
|
|
64
|
+
.put(
|
|
65
|
+
request,
|
|
66
|
+
new Response(html, {
|
|
67
|
+
headers: {
|
|
68
|
+
"content-type": "text/html; charset=utf-8",
|
|
69
|
+
"cache-control": `max-age=${BODY_TTL_SECONDS}`,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
)
|
|
73
|
+
.catch(() => undefined)
|
|
92
74
|
);
|
|
93
75
|
}
|
|
94
76
|
|
|
95
77
|
return html;
|
|
96
78
|
}
|
|
97
79
|
|
|
80
|
+
type ProxiedRemoteImage =
|
|
81
|
+
| { outcome: "ok"; body: ArrayBuffer | ReadableStream; contentType: string }
|
|
82
|
+
| { outcome: "limited" };
|
|
83
|
+
|
|
98
84
|
/**
|
|
99
85
|
* A remote image through the proxy's guards (remote-proxy.ts): from this
|
|
100
86
|
* colo's cache, keyed by the URL alone, or fetched, then cached. `beforeFetch`
|
|
@@ -105,21 +91,16 @@ export async function proxiedRemoteImage(
|
|
|
105
91
|
c: Context<AppEnv>,
|
|
106
92
|
url: string,
|
|
107
93
|
beforeFetch: () => Promise<boolean>
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
| { outcome: "limited" }
|
|
111
|
-
> {
|
|
112
|
-
const cache = await openCache("openworkspace:mail-remote");
|
|
94
|
+
) {
|
|
95
|
+
const cache = await caches.open("openworkspace:mail-remote");
|
|
113
96
|
|
|
114
97
|
const request = new Request(
|
|
115
98
|
`https://mail-remote.openworkspace.invalid/v1/${await sha256Hex(url)}`
|
|
116
99
|
);
|
|
117
100
|
|
|
118
|
-
const hit = cache
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
)
|
|
122
|
-
: undefined;
|
|
101
|
+
const hit = await timed(c, "cache", "remote", () =>
|
|
102
|
+
cache.match(request).catch(() => undefined)
|
|
103
|
+
);
|
|
123
104
|
|
|
124
105
|
if (hit?.body) {
|
|
125
106
|
return {
|
|
@@ -127,19 +108,20 @@ export async function proxiedRemoteImage(
|
|
|
127
108
|
body: hit.body,
|
|
128
109
|
contentType:
|
|
129
110
|
hit.headers.get("content-type") ?? "application/octet-stream",
|
|
130
|
-
};
|
|
111
|
+
} satisfies ProxiedRemoteImage;
|
|
131
112
|
}
|
|
132
113
|
|
|
133
|
-
if (!(await beforeFetch()))
|
|
114
|
+
if (!(await beforeFetch())) {
|
|
115
|
+
return { outcome: "limited" } satisfies ProxiedRemoteImage;
|
|
116
|
+
}
|
|
134
117
|
|
|
135
118
|
const remote = await timed(c, "fetch", "origin", () =>
|
|
136
119
|
fetchProxiedRemoteMedia(url)
|
|
137
120
|
);
|
|
138
121
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
cache.put(
|
|
122
|
+
waitUntil(
|
|
123
|
+
cache
|
|
124
|
+
.put(
|
|
143
125
|
request,
|
|
144
126
|
new Response(remote.body.slice(0), {
|
|
145
127
|
headers: {
|
|
@@ -148,8 +130,12 @@ export async function proxiedRemoteImage(
|
|
|
148
130
|
},
|
|
149
131
|
})
|
|
150
132
|
)
|
|
151
|
-
|
|
152
|
-
|
|
133
|
+
.catch(() => undefined)
|
|
134
|
+
);
|
|
153
135
|
|
|
154
|
-
return {
|
|
136
|
+
return {
|
|
137
|
+
outcome: "ok",
|
|
138
|
+
body: remote.body,
|
|
139
|
+
contentType: remote.contentType,
|
|
140
|
+
} satisfies ProxiedRemoteImage;
|
|
155
141
|
}
|