@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
package/worker/mail/uploads.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { AppEnv } from "@open-workspace/core/module";
|
|
2
2
|
import { createId } from "@open-workspace/core/module";
|
|
3
|
+
import { waitUntil } from "cloudflare:workers";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
|
|
5
6
|
import { MAX_COMPOSER_ATTACHMENT_BYTES } from "../../shared/mail";
|
|
6
7
|
import { signR2PutUrl } from "./r2-presigned-urls";
|
|
8
|
+
import { mailboxPrefix } from "./storage-keys";
|
|
7
9
|
|
|
8
10
|
type Bindings = AppEnv["Bindings"];
|
|
9
11
|
|
|
@@ -76,6 +78,76 @@ export function composerUploadMetaKey(
|
|
|
76
78
|
return `${composerUploadPrefix(mailboxId, userId, uploadId)}/metadata.json`;
|
|
77
79
|
}
|
|
78
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Where a draft keeps an upload's bytes: under the mailbox's own prefix (so
|
|
83
|
+
* deleting the mailbox deletes it), outside `composer-uploads/` and its 7-day
|
|
84
|
+
* expiry. Keyed by the upload's id so the composer gets the same upload back.
|
|
85
|
+
*/
|
|
86
|
+
export function draftFileKey(
|
|
87
|
+
mailboxId: string,
|
|
88
|
+
userId: string,
|
|
89
|
+
uploadId: string
|
|
90
|
+
) {
|
|
91
|
+
return `${mailboxPrefix(mailboxId)}drafts/${userId}/${uploadId}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const draftFileMetaSchema = z.object({
|
|
95
|
+
filename: z.string().min(1).max(255),
|
|
96
|
+
contentType: z.string().min(1).max(255),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/** The upload as its draft holds it, or null if no draft holds it. */
|
|
100
|
+
async function loadDraftFile(input: {
|
|
101
|
+
env: Bindings;
|
|
102
|
+
mailboxId: string;
|
|
103
|
+
userId: string;
|
|
104
|
+
uploadId: string;
|
|
105
|
+
}) {
|
|
106
|
+
const r2Key = draftFileKey(input.mailboxId, input.userId, input.uploadId);
|
|
107
|
+
const object = await input.env.MAIL_STORAGE.head(r2Key);
|
|
108
|
+
const meta = draftFileMetaSchema.safeParse(object?.customMetadata);
|
|
109
|
+
|
|
110
|
+
if (!object || !meta.success) return null;
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
id: input.uploadId,
|
|
114
|
+
r2Key,
|
|
115
|
+
filename: meta.data.filename,
|
|
116
|
+
contentType: meta.data.contentType,
|
|
117
|
+
size: object.size,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Copies an object to a draft file key, with what loading it back needs. */
|
|
122
|
+
export async function copyDraftFile(
|
|
123
|
+
env: Bindings,
|
|
124
|
+
sourceKey: string,
|
|
125
|
+
file: { r2Key: string; filename: string; contentType: string }
|
|
126
|
+
) {
|
|
127
|
+
const source = await env.MAIL_STORAGE.get(sourceKey);
|
|
128
|
+
|
|
129
|
+
if (!source) {
|
|
130
|
+
throw new UploadValidationError("Attachment upload was not found");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
await env.MAIL_STORAGE.put(file.r2Key, source.body, {
|
|
134
|
+
httpMetadata: { contentType: file.contentType },
|
|
135
|
+
customMetadata: { filename: file.filename, contentType: file.contentType },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Reads a file a draft holds, for showing its inline image in the composer. */
|
|
140
|
+
export function readDraftFile(input: {
|
|
141
|
+
env: Bindings;
|
|
142
|
+
mailboxId: string;
|
|
143
|
+
userId: string;
|
|
144
|
+
uploadId: string;
|
|
145
|
+
}) {
|
|
146
|
+
return input.env.MAIL_STORAGE.get(
|
|
147
|
+
draftFileKey(input.mailboxId, input.userId, input.uploadId)
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
79
151
|
function composerUploadKeys(
|
|
80
152
|
mailboxId: string,
|
|
81
153
|
userId: string,
|
|
@@ -114,6 +186,50 @@ async function readUploadMeta(env: Bindings, metadataKey: string) {
|
|
|
114
186
|
}
|
|
115
187
|
}
|
|
116
188
|
|
|
189
|
+
/** An attachment already held by this mailbox becomes a private, expiring composer upload. */
|
|
190
|
+
export async function copyAttachmentToUpload(
|
|
191
|
+
input: UploadRef & {
|
|
192
|
+
sourceKey: string;
|
|
193
|
+
filename: string;
|
|
194
|
+
contentType: string;
|
|
195
|
+
size: number;
|
|
196
|
+
}
|
|
197
|
+
) {
|
|
198
|
+
if (input.size <= 0 || input.size > MAX_COMPOSER_ATTACHMENT_BYTES) {
|
|
199
|
+
throw new UploadValidationError(
|
|
200
|
+
"Attachment size is outside the composer limits"
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const keys = composerUploadKeys(
|
|
205
|
+
input.mailboxId,
|
|
206
|
+
input.userId,
|
|
207
|
+
input.uploadId
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
const contentType =
|
|
211
|
+
input.contentType.trim().slice(0, 255) || "application/octet-stream";
|
|
212
|
+
|
|
213
|
+
await copyStoredAttachmentToMessage({
|
|
214
|
+
env: input.env,
|
|
215
|
+
sourceKey: input.sourceKey,
|
|
216
|
+
destinationKey: keys.immutable,
|
|
217
|
+
contentType,
|
|
218
|
+
});
|
|
219
|
+
await putUploadMeta({
|
|
220
|
+
env: input.env,
|
|
221
|
+
metadataKey: keys.metadata,
|
|
222
|
+
meta: {
|
|
223
|
+
finalizedAt: Date.now(),
|
|
224
|
+
uploadedBy: input.userId,
|
|
225
|
+
mailboxId: input.mailboxId,
|
|
226
|
+
size: input.size,
|
|
227
|
+
filename: input.filename.trim().slice(0, 255) || "attachment",
|
|
228
|
+
contentType,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
117
233
|
export async function createComposerUploadIntent(input: {
|
|
118
234
|
env: Bindings;
|
|
119
235
|
mailboxId: string;
|
|
@@ -121,7 +237,7 @@ export async function createComposerUploadIntent(input: {
|
|
|
121
237
|
filename: string;
|
|
122
238
|
contentType: string;
|
|
123
239
|
size: number;
|
|
124
|
-
})
|
|
240
|
+
}) {
|
|
125
241
|
if (input.size <= 0) {
|
|
126
242
|
throw new UploadValidationError("Attachment file is empty");
|
|
127
243
|
}
|
|
@@ -158,6 +274,7 @@ export async function createComposerUploadIntent(input: {
|
|
|
158
274
|
env: input.env,
|
|
159
275
|
r2Key: keys.staging,
|
|
160
276
|
contentType,
|
|
277
|
+
size: input.size,
|
|
161
278
|
}),
|
|
162
279
|
headers: { "content-type": contentType },
|
|
163
280
|
filename,
|
|
@@ -322,40 +439,12 @@ function assertUploadOwner(
|
|
|
322
439
|
return meta;
|
|
323
440
|
}
|
|
324
441
|
|
|
325
|
-
export async function loadComposerUploadMetadata(input: {
|
|
326
|
-
env: Bindings;
|
|
327
|
-
mailboxId: string;
|
|
328
|
-
userId: string;
|
|
329
|
-
uploadId: string;
|
|
330
|
-
}): Promise<UploadedAttachment> {
|
|
331
|
-
const keys = composerUploadKeys(
|
|
332
|
-
input.mailboxId,
|
|
333
|
-
input.userId,
|
|
334
|
-
input.uploadId
|
|
335
|
-
);
|
|
336
|
-
|
|
337
|
-
const meta = assertUploadOwner(
|
|
338
|
-
await readUploadMeta(input.env, keys.metadata),
|
|
339
|
-
input.userId,
|
|
340
|
-
input.mailboxId
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
id: input.uploadId,
|
|
345
|
-
r2Key: meta.finalizedAt === null ? keys.staging : keys.immutable,
|
|
346
|
-
filename: meta.filename,
|
|
347
|
-
contentType: meta.contentType,
|
|
348
|
-
size: meta.size,
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
442
|
export async function finalizeComposerUpload(input: {
|
|
353
443
|
env: Bindings;
|
|
354
444
|
mailboxId: string;
|
|
355
445
|
userId: string;
|
|
356
446
|
uploadId: string;
|
|
357
|
-
|
|
358
|
-
}): Promise<UploadedAttachment> {
|
|
447
|
+
}) {
|
|
359
448
|
const keys = composerUploadKeys(
|
|
360
449
|
input.mailboxId,
|
|
361
450
|
input.userId,
|
|
@@ -370,7 +459,7 @@ export async function finalizeComposerUpload(input: {
|
|
|
370
459
|
|
|
371
460
|
if (meta.finalizedAt !== null) {
|
|
372
461
|
const upload = await loadComposerUpload(input);
|
|
373
|
-
|
|
462
|
+
cleanupStagingAfterFinalization(input.env, keys.staging);
|
|
374
463
|
|
|
375
464
|
return upload;
|
|
376
465
|
}
|
|
@@ -425,11 +514,10 @@ async function completeComposerUploadFinalization(
|
|
|
425
514
|
mailboxId: string;
|
|
426
515
|
userId: string;
|
|
427
516
|
uploadId: string;
|
|
428
|
-
defer?: (task: Promise<unknown>) => void;
|
|
429
517
|
},
|
|
430
518
|
keys: ReturnType<typeof composerUploadKeys>,
|
|
431
519
|
meta: UploadMeta
|
|
432
|
-
)
|
|
520
|
+
) {
|
|
433
521
|
await putUploadMeta({
|
|
434
522
|
env: input.env,
|
|
435
523
|
metadataKey: keys.metadata,
|
|
@@ -438,7 +526,7 @@ async function completeComposerUploadFinalization(
|
|
|
438
526
|
finalizedAt: Date.now(),
|
|
439
527
|
},
|
|
440
528
|
});
|
|
441
|
-
|
|
529
|
+
cleanupStagingAfterFinalization(input.env, keys.staging);
|
|
442
530
|
|
|
443
531
|
return {
|
|
444
532
|
id: input.uploadId,
|
|
@@ -449,17 +537,9 @@ async function completeComposerUploadFinalization(
|
|
|
449
537
|
};
|
|
450
538
|
}
|
|
451
539
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
defer?: (task: Promise<unknown>) => void;
|
|
456
|
-
},
|
|
457
|
-
key: string
|
|
458
|
-
) {
|
|
459
|
-
const cleanup = deleteStagingObject(input.env, key);
|
|
460
|
-
|
|
461
|
-
if (input.defer) input.defer(cleanup);
|
|
462
|
-
else return cleanup;
|
|
540
|
+
/** The staging copy goes after the response (`waitUntil`). */
|
|
541
|
+
function cleanupStagingAfterFinalization(env: Bindings, key: string) {
|
|
542
|
+
waitUntil(deleteStagingObject(env, key));
|
|
463
543
|
}
|
|
464
544
|
|
|
465
545
|
async function deleteStagingObject(env: Bindings, key: string) {
|
|
@@ -471,72 +551,105 @@ async function deleteStagingObject(env: Bindings, key: string) {
|
|
|
471
551
|
}
|
|
472
552
|
}
|
|
473
553
|
|
|
474
|
-
|
|
554
|
+
type UploadRef = {
|
|
475
555
|
env: Bindings;
|
|
476
556
|
mailboxId: string;
|
|
477
557
|
userId: string;
|
|
478
558
|
uploadId: string;
|
|
479
|
-
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* The one way to find an upload's bytes: the uploaded file, else the copy
|
|
563
|
+
* a draft keeps of it (a reopened or restored draft whose upload expired).
|
|
564
|
+
* Found by the file, not its metadata: metadata outliving its file goes to
|
|
565
|
+
* the draft's copy, and a draft's copy needs no metadata. `meta` is the
|
|
566
|
+
* upload's own metadata when the bytes are the upload's.
|
|
567
|
+
*/
|
|
568
|
+
async function locateComposerUpload(
|
|
569
|
+
input: UploadRef,
|
|
570
|
+
options: { unfinished: boolean }
|
|
571
|
+
) {
|
|
480
572
|
const keys = composerUploadKeys(
|
|
481
573
|
input.mailboxId,
|
|
482
574
|
input.userId,
|
|
483
575
|
input.uploadId
|
|
484
576
|
);
|
|
485
577
|
|
|
486
|
-
const [object,
|
|
578
|
+
const [object, stored] = await Promise.all([
|
|
487
579
|
input.env.MAIL_STORAGE.head(keys.immutable),
|
|
488
580
|
readUploadMeta(input.env, keys.metadata),
|
|
489
581
|
]);
|
|
490
582
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
583
|
+
const meta =
|
|
584
|
+
stored?.uploadedBy === input.userId && stored.mailboxId === input.mailboxId
|
|
585
|
+
? stored
|
|
586
|
+
: null;
|
|
587
|
+
|
|
588
|
+
const own = (found: UploadMeta, r2Key: string, size: number) => ({
|
|
589
|
+
upload: {
|
|
590
|
+
id: input.uploadId,
|
|
591
|
+
r2Key,
|
|
592
|
+
filename: found.filename,
|
|
593
|
+
contentType: found.contentType,
|
|
594
|
+
size,
|
|
595
|
+
},
|
|
596
|
+
meta: found,
|
|
597
|
+
});
|
|
494
598
|
|
|
495
|
-
if (meta
|
|
496
|
-
|
|
497
|
-
|
|
599
|
+
if (meta && object && meta.finalizedAt !== null) {
|
|
600
|
+
if (object.size <= 0 || object.size !== meta.size) {
|
|
601
|
+
throw new UploadValidationError("Attachment upload is incomplete");
|
|
602
|
+
}
|
|
498
603
|
|
|
499
|
-
|
|
500
|
-
|
|
604
|
+
if (object.size > MAX_COMPOSER_ATTACHMENT_BYTES) {
|
|
605
|
+
throw new UploadValidationError("Attachment upload is invalid");
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
return own(meta, keys.immutable, object.size);
|
|
501
609
|
}
|
|
502
610
|
|
|
503
|
-
|
|
504
|
-
|
|
611
|
+
// Not sealed yet: its metadata says what it will be (preflight).
|
|
612
|
+
if (meta && meta.finalizedAt === null && options.unfinished) {
|
|
613
|
+
return own(meta, keys.staging, meta.size);
|
|
505
614
|
}
|
|
506
615
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
616
|
+
const held = await loadDraftFile(input);
|
|
617
|
+
|
|
618
|
+
if (held) return { upload: held, meta: null };
|
|
619
|
+
throw new UploadValidationError("Attachment upload was not found");
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* A finished upload's bytes (see `locateComposerUpload`); `unfinished` also
|
|
624
|
+
* takes one whose bytes aren't sealed yet, at its staging key.
|
|
625
|
+
*/
|
|
626
|
+
export async function loadComposerUpload(
|
|
627
|
+
input: UploadRef,
|
|
628
|
+
options?: { unfinished: boolean }
|
|
629
|
+
) {
|
|
630
|
+
const unfinished = options?.unfinished ?? false;
|
|
631
|
+
|
|
632
|
+
return (await locateComposerUpload(input, { unfinished })).upload;
|
|
514
633
|
}
|
|
515
634
|
|
|
516
635
|
/**
|
|
517
636
|
* Sets the name and type a finished upload is sent with, for clients that
|
|
518
|
-
* upload bytes first and describe them when composing (JMAP).
|
|
637
|
+
* upload bytes first and describe them when composing (JMAP). A draft's
|
|
638
|
+
* copy keeps the name it has.
|
|
519
639
|
*/
|
|
520
|
-
export async function describeComposerUpload(
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}) {
|
|
640
|
+
export async function describeComposerUpload(
|
|
641
|
+
input: UploadRef & { filename: string; contentType: string }
|
|
642
|
+
) {
|
|
643
|
+
const { meta } = await locateComposerUpload(input, { unfinished: false });
|
|
644
|
+
|
|
645
|
+
if (!meta) return;
|
|
646
|
+
|
|
528
647
|
const keys = composerUploadKeys(
|
|
529
648
|
input.mailboxId,
|
|
530
649
|
input.userId,
|
|
531
650
|
input.uploadId
|
|
532
651
|
);
|
|
533
652
|
|
|
534
|
-
const meta = assertUploadOwner(
|
|
535
|
-
await readUploadMeta(input.env, keys.metadata),
|
|
536
|
-
input.userId,
|
|
537
|
-
input.mailboxId
|
|
538
|
-
);
|
|
539
|
-
|
|
540
653
|
await putUploadMeta({
|
|
541
654
|
env: input.env,
|
|
542
655
|
metadataKey: keys.metadata,
|
|
@@ -2,15 +2,16 @@ import {
|
|
|
2
2
|
createDb,
|
|
3
3
|
mailboxAddressSql,
|
|
4
4
|
mailboxKind,
|
|
5
|
-
mailboxes,
|
|
6
5
|
} from "@open-workspace/core/module";
|
|
7
6
|
import type { WorkspaceModule } from "@open-workspace/core/module";
|
|
7
|
+
import { mailboxes } from "@open-workspace/core/schema";
|
|
8
8
|
import { eq } from "drizzle-orm";
|
|
9
9
|
import { z } from "zod";
|
|
10
10
|
|
|
11
11
|
import { mailboxStub } from "../mailbox";
|
|
12
12
|
import type { Email } from "../mailbox/schema";
|
|
13
13
|
import { headerAuthentication } from "./header-metadata";
|
|
14
|
+
import { messageText } from "./outbound-content";
|
|
14
15
|
|
|
15
16
|
function publicEmail(email: Email, bodyHtml: string | null) {
|
|
16
17
|
return {
|
|
@@ -24,7 +25,7 @@ function publicEmail(email: Email, bodyHtml: string | null) {
|
|
|
24
25
|
replyTo: email.replyToJson,
|
|
25
26
|
subject: email.subject,
|
|
26
27
|
body: {
|
|
27
|
-
text: email
|
|
28
|
+
text: messageText(email),
|
|
28
29
|
html: bodyHtml,
|
|
29
30
|
},
|
|
30
31
|
attachments: email.attachmentsJson.map((attachment) => ({
|