@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,1223 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { DRAFTS_FOLDER_ID } from "../../shared/folder-order";
|
|
4
|
+
import { MAX_COMPOSER_ATTACHMENT_COUNT } from "../../shared/mail";
|
|
5
|
+
import { MAIL_ORGANIZE_SCOPE, MAIL_SEND_SCOPE } from "../../shared/permissions";
|
|
6
|
+
import { storeDraft } from "../mail/draft-routes";
|
|
7
|
+
/** Email/get, Email/query, Email/queryChanges and Email/set: reading, filing and keeping drafts. */
|
|
8
|
+
import { draftSenderResolver, mailboxDraftSenders } from "../mail/draft-sender";
|
|
9
|
+
import { messageText, separateHtmlQuote } from "../mail/outbound-content";
|
|
10
|
+
import { plainBody } from "../mail/plain-body";
|
|
11
|
+
import { composeSchema, saveDraftSchema } from "../mail/schemas";
|
|
12
|
+
import { shareSpamVerdicts } from "../mail/workspace-safety";
|
|
13
|
+
import { mailboxStub } from "../mailbox";
|
|
14
|
+
import type { JmapEmailRow, JmapDraftRow } from "../mailbox/jmap-email-rows";
|
|
15
|
+
import type { MailboxChange } from "../mailbox/jmap-placement";
|
|
16
|
+
import type { JmapEmailFilter } from "../mailbox/jmap-store";
|
|
17
|
+
import { draftAttachment, prepareDraftAttachments } from "./attachment-blobs";
|
|
18
|
+
import { draftContent } from "./draft-content";
|
|
19
|
+
import {
|
|
20
|
+
draftHeaders,
|
|
21
|
+
isStoredDraftId,
|
|
22
|
+
moveDraftToSent,
|
|
23
|
+
replySource,
|
|
24
|
+
} from "./drafts";
|
|
25
|
+
import { draftHtmlPart, emailBody, storedHtmlPart } from "./email-body";
|
|
26
|
+
import type {
|
|
27
|
+
JmapAccount,
|
|
28
|
+
Json,
|
|
29
|
+
JsonObject,
|
|
30
|
+
RequestState,
|
|
31
|
+
} from "./method-core";
|
|
32
|
+
import {
|
|
33
|
+
MAX_OBJECTS,
|
|
34
|
+
MethodError,
|
|
35
|
+
account,
|
|
36
|
+
accountId,
|
|
37
|
+
addresses,
|
|
38
|
+
draftIssue,
|
|
39
|
+
idList,
|
|
40
|
+
invalidProperties,
|
|
41
|
+
setError,
|
|
42
|
+
isJsonObject,
|
|
43
|
+
methodIssue,
|
|
44
|
+
pick,
|
|
45
|
+
properties,
|
|
46
|
+
setObjects,
|
|
47
|
+
setResult,
|
|
48
|
+
utcDate,
|
|
49
|
+
} from "./method-core";
|
|
50
|
+
|
|
51
|
+
// -------------------------------------------------------------------- email
|
|
52
|
+
|
|
53
|
+
/** The FilterCondition properties (RFC 8621 §4.4.1) Email/query supports. */
|
|
54
|
+
const emailCondition = {
|
|
55
|
+
inMailbox: z.string().optional(),
|
|
56
|
+
inMailboxOtherThan: z.array(z.string()).optional(),
|
|
57
|
+
before: utcDate.transform((date) => Date.parse(date)).optional(),
|
|
58
|
+
after: utcDate.transform((date) => Date.parse(date)).optional(),
|
|
59
|
+
text: z.string().optional(),
|
|
60
|
+
from: z.string().optional(),
|
|
61
|
+
to: z.string().optional(),
|
|
62
|
+
subject: z.string().optional(),
|
|
63
|
+
hasKeyword: z.string().optional(),
|
|
64
|
+
notKeyword: z.string().optional(),
|
|
65
|
+
hasAttachment: z.boolean().optional(),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const emailConditionKeys = Object.keys(emailCondition);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* An Email/query filter, flattened into one condition. Other conditions,
|
|
72
|
+
* operators than AND, and keywords than $seen are valid JMAP the store
|
|
73
|
+
* cannot answer: unsupportedFilter.
|
|
74
|
+
*/
|
|
75
|
+
const emailFilter: z.ZodType<JmapEmailFilter> = z.lazy(() =>
|
|
76
|
+
z
|
|
77
|
+
.record(z.string(), z.unknown())
|
|
78
|
+
.superRefine((filter, context) => {
|
|
79
|
+
const allowed = Object.hasOwn(filter, "operator")
|
|
80
|
+
? ["operator", "conditions"]
|
|
81
|
+
: emailConditionKeys;
|
|
82
|
+
|
|
83
|
+
const unsupported = Object.keys(filter).filter(
|
|
84
|
+
(key) => !allowed.includes(key)
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (unsupported.length) {
|
|
88
|
+
methodIssue(
|
|
89
|
+
context,
|
|
90
|
+
"unsupportedFilter",
|
|
91
|
+
`Unsupported filter: ${unsupported.join(", ")}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
.pipe(
|
|
96
|
+
z.object({
|
|
97
|
+
operator: z.enum(["AND", "OR", "NOT"]).optional(),
|
|
98
|
+
conditions: z.array(emailFilter).optional(),
|
|
99
|
+
...emailCondition,
|
|
100
|
+
})
|
|
101
|
+
)
|
|
102
|
+
.superRefine((filter, context) => {
|
|
103
|
+
if (filter.operator && filter.operator !== "AND") {
|
|
104
|
+
methodIssue(
|
|
105
|
+
context,
|
|
106
|
+
"unsupportedFilter",
|
|
107
|
+
"Only AND filters are supported"
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (filter.operator && !filter.conditions) {
|
|
112
|
+
context.addIssue({
|
|
113
|
+
code: "custom",
|
|
114
|
+
path: ["conditions"],
|
|
115
|
+
message: "A filter operator needs conditions",
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const keyword of [filter.hasKeyword, filter.notKeyword]) {
|
|
120
|
+
if (
|
|
121
|
+
keyword !== undefined &&
|
|
122
|
+
keyword !== "$seen" &&
|
|
123
|
+
keyword !== "$draft"
|
|
124
|
+
) {
|
|
125
|
+
methodIssue(
|
|
126
|
+
context,
|
|
127
|
+
"unsupportedFilter",
|
|
128
|
+
"Only the $seen and $draft keywords are supported"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
.transform(({ operator, conditions, ...condition }, context) => {
|
|
134
|
+
if (!operator) return condition;
|
|
135
|
+
// AND: every condition holds. They become one filter, which holds one
|
|
136
|
+
// value per property: two that differ can't both be asked.
|
|
137
|
+
const merged: JmapEmailFilter = {};
|
|
138
|
+
|
|
139
|
+
for (const part of conditions ?? []) {
|
|
140
|
+
const had = new Map(Object.entries(merged));
|
|
141
|
+
|
|
142
|
+
const clash = Object.entries(part).find(
|
|
143
|
+
([key, value]) =>
|
|
144
|
+
had.has(key) &&
|
|
145
|
+
JSON.stringify(had.get(key)) !== JSON.stringify(value)
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
if (clash) {
|
|
149
|
+
methodIssue(
|
|
150
|
+
context,
|
|
151
|
+
"unsupportedFilter",
|
|
152
|
+
`AND with two different ${clash[0]} conditions is not supported`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
Object.assign(merged, part);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return merged;
|
|
160
|
+
})
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const emailComparator = z
|
|
164
|
+
.object({
|
|
165
|
+
property: z.string(),
|
|
166
|
+
isAscending: z.boolean().default(true),
|
|
167
|
+
collation: z.string().optional(),
|
|
168
|
+
})
|
|
169
|
+
.superRefine((comparator, context) => {
|
|
170
|
+
if (!["receivedAt", "sentAt"].includes(comparator.property)) {
|
|
171
|
+
methodIssue(
|
|
172
|
+
context,
|
|
173
|
+
"unsupportedSort",
|
|
174
|
+
"Emails sort by receivedAt or sentAt"
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
export const emailQueryArguments = z.object({
|
|
180
|
+
accountId,
|
|
181
|
+
filter: emailFilter.nullish(),
|
|
182
|
+
sort: z.array(emailComparator).nullish(),
|
|
183
|
+
calculateTotal: z.boolean().nullish(),
|
|
184
|
+
position: z.number().int().nullish(),
|
|
185
|
+
anchor: z.string().nullish(),
|
|
186
|
+
anchorOffset: z.number().int().nullish(),
|
|
187
|
+
limit: z.number().int().min(0).nullish(),
|
|
188
|
+
collapseThreads: z.boolean().nullish(),
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
function queryDraftSenders(
|
|
192
|
+
env: Env,
|
|
193
|
+
owner: JmapAccount,
|
|
194
|
+
filter: JmapEmailFilter | null | undefined
|
|
195
|
+
) {
|
|
196
|
+
return (filter?.from || filter?.text) &&
|
|
197
|
+
(!filter.inMailbox || filter.inMailbox === DRAFTS_FOLDER_ID)
|
|
198
|
+
? mailboxDraftSenders(env.DB, owner)
|
|
199
|
+
: undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function emailQuery(
|
|
203
|
+
state: RequestState,
|
|
204
|
+
args: z.output<typeof emailQueryArguments>
|
|
205
|
+
) {
|
|
206
|
+
const owner = account(state, args.accountId);
|
|
207
|
+
const limit = Math.min(MAX_OBJECTS, args.limit ?? 100);
|
|
208
|
+
|
|
209
|
+
const result = await mailboxStub(state.context.env, owner.id).jmapQueryEmails(
|
|
210
|
+
{
|
|
211
|
+
userId: state.context.user.id,
|
|
212
|
+
draftSenders: await queryDraftSenders(
|
|
213
|
+
state.context.env,
|
|
214
|
+
owner,
|
|
215
|
+
args.filter
|
|
216
|
+
),
|
|
217
|
+
filter: args.filter ?? {},
|
|
218
|
+
sort: args.sort ?? [],
|
|
219
|
+
calculateTotal: args.calculateTotal === true,
|
|
220
|
+
position: args.position ?? 0,
|
|
221
|
+
anchor: args.anchor ?? null,
|
|
222
|
+
anchorOffset: args.anchorOffset ?? 0,
|
|
223
|
+
limit,
|
|
224
|
+
collapseThreads: args.collapseThreads === true,
|
|
225
|
+
}
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
if (!result) throw new MethodError("anchorNotFound");
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
accountId: owner.id,
|
|
232
|
+
queryState: result.state,
|
|
233
|
+
canCalculateChanges: true,
|
|
234
|
+
position: result.position,
|
|
235
|
+
ids: result.ids,
|
|
236
|
+
...(result.total !== null && { total: result.total }),
|
|
237
|
+
limit,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export const emailQueryChangesArguments = z.object({
|
|
242
|
+
accountId,
|
|
243
|
+
filter: emailFilter.nullish(),
|
|
244
|
+
sort: z.array(emailComparator).nullish(),
|
|
245
|
+
sinceQueryState: z.string(),
|
|
246
|
+
maxChanges: z.number().int().positive().nullish(),
|
|
247
|
+
// A hint the server may ignore (RFC 8620 §5.6); every change is listed.
|
|
248
|
+
upToId: z.string().nullish(),
|
|
249
|
+
calculateTotal: z.boolean().nullish(),
|
|
250
|
+
collapseThreads: z.boolean().nullish(),
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Email/queryChanges: what to remove from and add to a query's results to
|
|
255
|
+
* bring them from `sinceQueryState` to now (mailbox/index.ts,
|
|
256
|
+
* jmapQueryChanges).
|
|
257
|
+
*/
|
|
258
|
+
export async function emailQueryChanges(
|
|
259
|
+
state: RequestState,
|
|
260
|
+
args: z.output<typeof emailQueryChangesArguments>
|
|
261
|
+
) {
|
|
262
|
+
const owner = account(state, args.accountId);
|
|
263
|
+
|
|
264
|
+
const changes = await mailboxStub(
|
|
265
|
+
state.context.env,
|
|
266
|
+
owner.id
|
|
267
|
+
).jmapQueryChanges({
|
|
268
|
+
userId: state.context.user.id,
|
|
269
|
+
draftSenders: await queryDraftSenders(
|
|
270
|
+
state.context.env,
|
|
271
|
+
owner,
|
|
272
|
+
args.filter
|
|
273
|
+
),
|
|
274
|
+
filter: args.filter ?? {},
|
|
275
|
+
sort: args.sort ?? [],
|
|
276
|
+
collapseThreads: args.collapseThreads === true,
|
|
277
|
+
sinceQueryState: args.sinceQueryState,
|
|
278
|
+
calculateTotal: args.calculateTotal === true,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
if (!changes) throw new MethodError("cannotCalculateChanges");
|
|
282
|
+
|
|
283
|
+
if (
|
|
284
|
+
args.maxChanges &&
|
|
285
|
+
changes.removed.length + changes.added.length > args.maxChanges
|
|
286
|
+
) {
|
|
287
|
+
throw new MethodError("tooManyChanges");
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const response: JsonObject = {
|
|
291
|
+
accountId: owner.id,
|
|
292
|
+
oldQueryState: args.sinceQueryState,
|
|
293
|
+
newQueryState: changes.state,
|
|
294
|
+
removed: changes.removed,
|
|
295
|
+
added: changes.added,
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
if (changes.total !== null) response.total = changes.total;
|
|
299
|
+
|
|
300
|
+
return response;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const defaultEmailProperties = [
|
|
304
|
+
"id",
|
|
305
|
+
"blobId",
|
|
306
|
+
"threadId",
|
|
307
|
+
"mailboxIds",
|
|
308
|
+
"keywords",
|
|
309
|
+
"size",
|
|
310
|
+
"receivedAt",
|
|
311
|
+
"messageId",
|
|
312
|
+
"inReplyTo",
|
|
313
|
+
"references",
|
|
314
|
+
"sender",
|
|
315
|
+
"from",
|
|
316
|
+
"to",
|
|
317
|
+
"cc",
|
|
318
|
+
"bcc",
|
|
319
|
+
"replyTo",
|
|
320
|
+
"subject",
|
|
321
|
+
"sentAt",
|
|
322
|
+
"hasAttachment",
|
|
323
|
+
"preview",
|
|
324
|
+
"bodyValues",
|
|
325
|
+
"textBody",
|
|
326
|
+
"htmlBody",
|
|
327
|
+
"attachments",
|
|
328
|
+
];
|
|
329
|
+
|
|
330
|
+
export const emailGetArguments = z.object({
|
|
331
|
+
accountId,
|
|
332
|
+
ids: idList.nullish(),
|
|
333
|
+
properties,
|
|
334
|
+
fetchTextBodyValues: z.boolean().nullish(),
|
|
335
|
+
fetchHTMLBodyValues: z.boolean().nullish(),
|
|
336
|
+
fetchAllBodyValues: z.boolean().nullish(),
|
|
337
|
+
maxBodyValueBytes: z.number().int().min(0).nullish(),
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
type EmailGetArguments = z.output<typeof emailGetArguments>;
|
|
341
|
+
|
|
342
|
+
/** Message-IDs as JMAP gives them: without the angle brackets they are stored with (RFC 8621 §4.1.3). */
|
|
343
|
+
function jmapMessageIds(ids: readonly string[]) {
|
|
344
|
+
return ids.map((id) => id.replaceAll(/^<|>$/gu, ""));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* One of the person's drafts as an Email in the drafts mailbox, with the
|
|
349
|
+
* $draft keyword.
|
|
350
|
+
*/
|
|
351
|
+
async function draftEmailObject(
|
|
352
|
+
env: Env,
|
|
353
|
+
sender: Pick<JmapAccount, "address" | "displayName">,
|
|
354
|
+
draft: JmapDraftRow,
|
|
355
|
+
args: EmailGetArguments
|
|
356
|
+
) {
|
|
357
|
+
const content = await draftContent(env.MAIL_STORAGE, draft);
|
|
358
|
+
const text = content.bodyText;
|
|
359
|
+
const html = draftHtmlPart(content.bodyHtml ?? null, args);
|
|
360
|
+
|
|
361
|
+
const attachments = content.attachments.map((file) => ({
|
|
362
|
+
partId: file.partId,
|
|
363
|
+
blobId: file.blobId,
|
|
364
|
+
size: file.size,
|
|
365
|
+
name: file.filename,
|
|
366
|
+
type: file.contentType,
|
|
367
|
+
charset: null,
|
|
368
|
+
disposition: file.disposition,
|
|
369
|
+
cid: file.contentId,
|
|
370
|
+
}));
|
|
371
|
+
|
|
372
|
+
const wanted = args.properties ?? defaultEmailProperties;
|
|
373
|
+
const headers = draftHeaders(sender, draft);
|
|
374
|
+
const timestamp = headers.date?.toISOString() ?? null;
|
|
375
|
+
|
|
376
|
+
return pick(
|
|
377
|
+
{
|
|
378
|
+
id: draft.id,
|
|
379
|
+
blobId: `draft~${draft.id}`,
|
|
380
|
+
// A thread of its own: Thread/get of its conversation lists only
|
|
381
|
+
// what everyone with the mailbox sees.
|
|
382
|
+
threadId: draft.id,
|
|
383
|
+
mailboxIds: { [DRAFTS_FOLDER_ID]: true },
|
|
384
|
+
keywords: { $draft: true, $seen: true },
|
|
385
|
+
size:
|
|
386
|
+
text.length + attachments.reduce((total, file) => total + file.size, 0),
|
|
387
|
+
receivedAt: timestamp,
|
|
388
|
+
messageId: headers.messageId,
|
|
389
|
+
inReplyTo: headers.inReplyTo.length
|
|
390
|
+
? jmapMessageIds(headers.inReplyTo)
|
|
391
|
+
: null,
|
|
392
|
+
references: headers.references.length
|
|
393
|
+
? jmapMessageIds(headers.references)
|
|
394
|
+
: null,
|
|
395
|
+
sender: null,
|
|
396
|
+
from: addresses(headers.from),
|
|
397
|
+
to: addresses(headers.to),
|
|
398
|
+
cc: addresses(headers.cc),
|
|
399
|
+
bcc: addresses(headers.bcc),
|
|
400
|
+
replyTo: addresses(headers.replyTo),
|
|
401
|
+
subject: headers.subject,
|
|
402
|
+
sentAt: timestamp,
|
|
403
|
+
hasAttachment: attachments.some(
|
|
404
|
+
(file) => file.disposition === "attachment"
|
|
405
|
+
),
|
|
406
|
+
preview: wanted.includes("preview")
|
|
407
|
+
? plainBody(text, content.bodyHtml)
|
|
408
|
+
.replaceAll(/\s+/gu, " ")
|
|
409
|
+
.trim()
|
|
410
|
+
.slice(0, 256)
|
|
411
|
+
: "",
|
|
412
|
+
...emailBody(draft.id, text, html, attachments, args),
|
|
413
|
+
},
|
|
414
|
+
wanted
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async function emailObject(
|
|
419
|
+
env: Env,
|
|
420
|
+
row: JmapEmailRow,
|
|
421
|
+
args: EmailGetArguments
|
|
422
|
+
) {
|
|
423
|
+
const { email } = row;
|
|
424
|
+
const raw = `raw~${email.id}`;
|
|
425
|
+
|
|
426
|
+
const text = email.direction
|
|
427
|
+
? messageText({
|
|
428
|
+
direction: email.direction,
|
|
429
|
+
bodyText: email.bodyText ?? null,
|
|
430
|
+
quotedText: email.quotedText ?? null,
|
|
431
|
+
})
|
|
432
|
+
: "";
|
|
433
|
+
|
|
434
|
+
const wanted = args.properties ?? defaultEmailProperties;
|
|
435
|
+
|
|
436
|
+
const html = email.bodyHtmlR2Key
|
|
437
|
+
? await storedHtmlPart(
|
|
438
|
+
env.MAIL_STORAGE,
|
|
439
|
+
email.bodyHtmlR2Key,
|
|
440
|
+
args,
|
|
441
|
+
separateHtmlQuote(email)
|
|
442
|
+
)
|
|
443
|
+
: null;
|
|
444
|
+
|
|
445
|
+
const attachments = (email.attachmentsJson ?? []).map((attachment) => ({
|
|
446
|
+
partId: `att-${attachment.id}`,
|
|
447
|
+
blobId: `att~${email.id}~${attachment.id}`,
|
|
448
|
+
size: attachment.size,
|
|
449
|
+
name: attachment.filename,
|
|
450
|
+
type: attachment.contentType,
|
|
451
|
+
charset: null,
|
|
452
|
+
disposition: attachment.disposition,
|
|
453
|
+
cid: attachment.contentId,
|
|
454
|
+
}));
|
|
455
|
+
|
|
456
|
+
const timestamp = email.timelineAt?.toISOString() ?? null;
|
|
457
|
+
const keywords: Record<string, true> = row.seen ? { $seen: true } : {};
|
|
458
|
+
|
|
459
|
+
const object = {
|
|
460
|
+
id: email.id,
|
|
461
|
+
blobId: raw,
|
|
462
|
+
threadId: email.conversationId ?? null,
|
|
463
|
+
mailboxIds: Object.fromEntries(
|
|
464
|
+
(row.mailboxIds ?? []).map((id) => [id, true])
|
|
465
|
+
),
|
|
466
|
+
keywords,
|
|
467
|
+
size: email.rawSize ?? 0,
|
|
468
|
+
receivedAt: timestamp,
|
|
469
|
+
messageId: email.messageIdHeader
|
|
470
|
+
? jmapMessageIds([email.messageIdHeader])
|
|
471
|
+
: null,
|
|
472
|
+
inReplyTo: email.inReplyToJson?.length
|
|
473
|
+
? jmapMessageIds(email.inReplyToJson)
|
|
474
|
+
: null,
|
|
475
|
+
references: email.referencesJson?.length
|
|
476
|
+
? jmapMessageIds(email.referencesJson)
|
|
477
|
+
: null,
|
|
478
|
+
sender: null,
|
|
479
|
+
from: addresses(email.fromJson),
|
|
480
|
+
to: addresses(email.toJson),
|
|
481
|
+
cc: addresses(email.ccJson),
|
|
482
|
+
bcc: addresses(email.bccJson),
|
|
483
|
+
replyTo: addresses(email.replyToJson),
|
|
484
|
+
subject: email.subject ?? null,
|
|
485
|
+
sentAt: email.sentAt?.toISOString() ?? null,
|
|
486
|
+
hasAttachment: attachments.some(
|
|
487
|
+
(attachment) => attachment.disposition === "attachment"
|
|
488
|
+
),
|
|
489
|
+
preview: email.preview ?? null,
|
|
490
|
+
...emailBody(email.id, text, html, attachments, args),
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
return pick(object, wanted);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export async function emailGet(state: RequestState, args: EmailGetArguments) {
|
|
497
|
+
const owner = account(state, args.accountId);
|
|
498
|
+
|
|
499
|
+
if (!args.ids) {
|
|
500
|
+
throw new MethodError("requestTooLarge", "Ask for emails by id");
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const { ids } = args;
|
|
504
|
+
|
|
505
|
+
const result = await mailboxStub(state.context.env, owner.id).jmapEmails(
|
|
506
|
+
state.context.user.id,
|
|
507
|
+
ids,
|
|
508
|
+
args.properties ?? defaultEmailProperties
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
const senderFor = draftSenderResolver(state.context.env, owner);
|
|
512
|
+
|
|
513
|
+
const found = new Set([
|
|
514
|
+
...result.emails.map((row) => row.email.id),
|
|
515
|
+
...result.drafts.map((draft) => draft.id),
|
|
516
|
+
]);
|
|
517
|
+
|
|
518
|
+
return {
|
|
519
|
+
accountId: owner.id,
|
|
520
|
+
state: result.state,
|
|
521
|
+
list: [
|
|
522
|
+
...(await Promise.all(
|
|
523
|
+
result.emails.map((row) => emailObject(state.context.env, row, args))
|
|
524
|
+
)),
|
|
525
|
+
...(await Promise.all(
|
|
526
|
+
result.drafts.map(async (draft) =>
|
|
527
|
+
draftEmailObject(
|
|
528
|
+
state.context.env,
|
|
529
|
+
await senderFor(draft.replySenderSource),
|
|
530
|
+
draft,
|
|
531
|
+
args
|
|
532
|
+
)
|
|
533
|
+
)
|
|
534
|
+
)),
|
|
535
|
+
],
|
|
536
|
+
notFound: ids.filter((id) => !found.has(id)),
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** A patch key is a JSON Pointer into the email (RFC 8620 §5.3). */
|
|
541
|
+
function patchPath(key: string) {
|
|
542
|
+
return key
|
|
543
|
+
.split("/")
|
|
544
|
+
.map((part) => part.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** Keywords and mailboxIds map their names to true (RFC 8621 §4.1.1). */
|
|
548
|
+
export const trueSet = z.record(z.string(), z.literal(true));
|
|
549
|
+
|
|
550
|
+
/** One entry of a set: true adds it, null removes it. */
|
|
551
|
+
const setMember = z.literal(true).nullable();
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* An Email/set update patch: the $seen keyword and the Mailboxes an email
|
|
555
|
+
* is in are all that change.
|
|
556
|
+
*/
|
|
557
|
+
const emailPatch = z
|
|
558
|
+
.record(z.string(), z.json())
|
|
559
|
+
.transform((patch, context) => {
|
|
560
|
+
let seen: boolean | undefined;
|
|
561
|
+
let mailboxes: MailboxChange | undefined;
|
|
562
|
+
|
|
563
|
+
const change = () => {
|
|
564
|
+
mailboxes ??= { replace: null, add: [], remove: [] };
|
|
565
|
+
|
|
566
|
+
return mailboxes;
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
570
|
+
const path = patchPath(key);
|
|
571
|
+
|
|
572
|
+
const fail = (message: string) =>
|
|
573
|
+
context.addIssue({ code: "custom", path, message, input: value });
|
|
574
|
+
|
|
575
|
+
if (key === "keywords") {
|
|
576
|
+
const keywords = trueSet.safeParse(value);
|
|
577
|
+
|
|
578
|
+
if (!keywords.success) fail("keywords map keywords to true");
|
|
579
|
+
else if (Object.keys(keywords.data).some((name) => name !== "$seen")) {
|
|
580
|
+
fail("Only the $seen keyword is stored");
|
|
581
|
+
} else seen = Object.hasOwn(keywords.data, "$seen");
|
|
582
|
+
} else if (key === "keywords/$seen") {
|
|
583
|
+
const member = setMember.safeParse(value);
|
|
584
|
+
|
|
585
|
+
if (member.success) seen = member.data === true;
|
|
586
|
+
else fail("A keyword is set to true or null");
|
|
587
|
+
} else if (key.startsWith("keywords/")) {
|
|
588
|
+
fail("Only the $seen keyword is stored");
|
|
589
|
+
} else if (key === "mailboxIds") {
|
|
590
|
+
const mailboxIds = trueSet.safeParse(value);
|
|
591
|
+
|
|
592
|
+
if (mailboxIds.success) {
|
|
593
|
+
change().replace = Object.keys(mailboxIds.data);
|
|
594
|
+
} else fail("mailboxIds map Mailbox ids to true");
|
|
595
|
+
} else if (key.startsWith("mailboxIds/")) {
|
|
596
|
+
const member = setMember.safeParse(value);
|
|
597
|
+
const id = key.slice("mailboxIds/".length);
|
|
598
|
+
|
|
599
|
+
if (!member.success) fail("A mailbox is set to true or null");
|
|
600
|
+
else if (member.data) change().add.push(id);
|
|
601
|
+
else change().remove.push(id);
|
|
602
|
+
} else {
|
|
603
|
+
fail(`${key} cannot be changed`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
return { seen, mailboxes };
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
const draftAddress = z.object({
|
|
611
|
+
name: z.string().nullish(),
|
|
612
|
+
email: z.string(),
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
const draftAddresses = z
|
|
616
|
+
.array(draftAddress)
|
|
617
|
+
.nullish()
|
|
618
|
+
.transform((list) => list?.map((item) => item.email) ?? []);
|
|
619
|
+
|
|
620
|
+
/** A body part of a draft names its value in bodyValues. */
|
|
621
|
+
const draftBodyPart = z
|
|
622
|
+
.array(z.object({ partId: z.string() }))
|
|
623
|
+
.max(1, "Only one body part per format is supported")
|
|
624
|
+
.nullish();
|
|
625
|
+
|
|
626
|
+
/** An Email/set create object (RFC 8621 §4.6): a message to send in this request. */
|
|
627
|
+
const draftSchema = z
|
|
628
|
+
.object({
|
|
629
|
+
mailboxIds: trueSet
|
|
630
|
+
.refine(
|
|
631
|
+
(ids) =>
|
|
632
|
+
Object.keys(ids).length === 1 && ids[DRAFTS_FOLDER_ID] === true,
|
|
633
|
+
"Create in Drafts or use Email/import for another mailbox"
|
|
634
|
+
)
|
|
635
|
+
.optional(),
|
|
636
|
+
from: z.array(draftAddress).length(1).nullish(),
|
|
637
|
+
to: draftAddresses,
|
|
638
|
+
cc: draftAddresses,
|
|
639
|
+
bcc: draftAddresses,
|
|
640
|
+
replyTo: z
|
|
641
|
+
.array(draftAddress)
|
|
642
|
+
.max(1, "Only one Reply-To address is supported")
|
|
643
|
+
.nullish(),
|
|
644
|
+
subject: z.string().nullish(),
|
|
645
|
+
bodyValues: z.record(z.string(), z.object({ value: z.string() })).nullish(),
|
|
646
|
+
textBody: draftBodyPart,
|
|
647
|
+
htmlBody: draftBodyPart,
|
|
648
|
+
inReplyTo: z.array(z.string().trim().min(1).max(998)).max(20).nullish(),
|
|
649
|
+
attachments: z
|
|
650
|
+
.array(draftAttachment)
|
|
651
|
+
.max(
|
|
652
|
+
MAX_COMPOSER_ATTACHMENT_COUNT,
|
|
653
|
+
`At most ${MAX_COMPOSER_ATTACHMENT_COUNT} attachments per message`
|
|
654
|
+
)
|
|
655
|
+
.nullish()
|
|
656
|
+
.transform((list) => list ?? [])
|
|
657
|
+
.refine(
|
|
658
|
+
(list) => new Set(list.map((item) => item.blobId)).size === list.length,
|
|
659
|
+
"Each blob can be attached once"
|
|
660
|
+
),
|
|
661
|
+
})
|
|
662
|
+
.transform((input, context) => {
|
|
663
|
+
const values = new Map(Object.entries(input.bodyValues ?? {}));
|
|
664
|
+
|
|
665
|
+
const partText = (field: "textBody" | "htmlBody") => {
|
|
666
|
+
const [part] = input[field] ?? [];
|
|
667
|
+
|
|
668
|
+
if (!part) return undefined;
|
|
669
|
+
const value = values.get(part.partId);
|
|
670
|
+
|
|
671
|
+
if (!value) {
|
|
672
|
+
context.addIssue({
|
|
673
|
+
code: "custom",
|
|
674
|
+
path: [field],
|
|
675
|
+
message: "Missing bodyValues entry for the referenced part",
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return value?.value;
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
const bodyText = partText("textBody");
|
|
683
|
+
const bodyHtml = partText("htmlBody");
|
|
684
|
+
|
|
685
|
+
if (bodyText === undefined && bodyHtml === undefined) {
|
|
686
|
+
context.addIssue({
|
|
687
|
+
code: "custom",
|
|
688
|
+
path: ["textBody"],
|
|
689
|
+
message: "Give the message a textBody or htmlBody with bodyValues",
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
return {
|
|
694
|
+
from: input.from?.[0]?.email.trim().toLowerCase(),
|
|
695
|
+
to: input.to,
|
|
696
|
+
cc: input.cc,
|
|
697
|
+
bcc: input.bcc,
|
|
698
|
+
replyTo: input.replyTo?.[0]?.email,
|
|
699
|
+
subject: input.subject ?? "",
|
|
700
|
+
bodyText: bodyText ?? "",
|
|
701
|
+
bodyHtml,
|
|
702
|
+
attachments: input.attachments,
|
|
703
|
+
// Stored with angle brackets; JMAP gives them without.
|
|
704
|
+
inReplyTo: (input.inReplyTo ?? []).map((id) =>
|
|
705
|
+
id.startsWith("<") ? id : `<${id}>`
|
|
706
|
+
),
|
|
707
|
+
};
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
/** Whether an Email/set create puts the email in the drafts mailbox, to keep it. */
|
|
711
|
+
function inDraftsMailbox(input: Json) {
|
|
712
|
+
return (
|
|
713
|
+
isJsonObject(input) &&
|
|
714
|
+
isJsonObject(input.mailboxIds ?? null) &&
|
|
715
|
+
isJsonObject(input.mailboxIds) &&
|
|
716
|
+
input.mailboxIds[DRAFTS_FOLDER_ID] === true
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
type ParsedDraft = {
|
|
721
|
+
message: Omit<z.output<typeof draftSchema>, "from" | "inReplyTo">;
|
|
722
|
+
sourceId: string | null;
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
/** Both stored and send-now drafts use the sender the transport can actually use. */
|
|
726
|
+
async function parseDraft(
|
|
727
|
+
state: RequestState,
|
|
728
|
+
owner: JmapAccount,
|
|
729
|
+
input: Json,
|
|
730
|
+
senderFor: ReturnType<typeof draftSenderResolver>
|
|
731
|
+
) {
|
|
732
|
+
const parsed = draftSchema.safeParse(input);
|
|
733
|
+
|
|
734
|
+
if (!parsed.success) {
|
|
735
|
+
return setError(invalidProperties(parsed.error.issues, { topLevel: true }));
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
const { from, inReplyTo, ...message } = parsed.data;
|
|
739
|
+
|
|
740
|
+
const source = inReplyTo.length
|
|
741
|
+
? await replySource(state, owner, { messageIds: inReplyTo, emailId: null })
|
|
742
|
+
: null;
|
|
743
|
+
|
|
744
|
+
if (
|
|
745
|
+
from !== undefined &&
|
|
746
|
+
from !== (await senderFor(source)).address.toLowerCase()
|
|
747
|
+
) {
|
|
748
|
+
return setError({
|
|
749
|
+
type: "invalidProperties",
|
|
750
|
+
properties: ["from"],
|
|
751
|
+
description: "From must match the sending address for this message",
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
return { message, sourceId: source?.id ?? null };
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Keeps a mail app's draft (Email/set create in the drafts mailbox) as one
|
|
760
|
+
* of the person's drafts, which the web app lists too. Returns its id, or
|
|
761
|
+
* the SetError it was not created with.
|
|
762
|
+
*/
|
|
763
|
+
async function createStoredDraft(
|
|
764
|
+
state: RequestState,
|
|
765
|
+
owner: JmapAccount,
|
|
766
|
+
draft: ParsedDraft
|
|
767
|
+
) {
|
|
768
|
+
const { attachments, ...message } = draft.message;
|
|
769
|
+
|
|
770
|
+
const save = saveDraftSchema.safeParse({
|
|
771
|
+
...message,
|
|
772
|
+
expectedVersion: 0,
|
|
773
|
+
kind: "compose",
|
|
774
|
+
// The message it answers, so sending it threads the reply.
|
|
775
|
+
sourceEmailId: draft.sourceId,
|
|
776
|
+
bodyHtml: message.bodyHtml ?? null,
|
|
777
|
+
attachments: attachments.map((attachment) => ({
|
|
778
|
+
assetId: crypto.randomUUID(),
|
|
779
|
+
uploadId: attachment.uploadId,
|
|
780
|
+
disposition: attachment.disposition,
|
|
781
|
+
contentId: attachment.contentId,
|
|
782
|
+
})),
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
if (!save.success) {
|
|
786
|
+
return setError(
|
|
787
|
+
invalidProperties(save.error.issues.map(draftIssue), {
|
|
788
|
+
topLevel: true,
|
|
789
|
+
})
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
const attachmentError = await prepareDraftAttachments(
|
|
794
|
+
state,
|
|
795
|
+
owner,
|
|
796
|
+
attachments
|
|
797
|
+
);
|
|
798
|
+
|
|
799
|
+
if (attachmentError) return setError(attachmentError);
|
|
800
|
+
|
|
801
|
+
const id = `drf_${crypto.randomUUID()}`;
|
|
802
|
+
|
|
803
|
+
const result = await storeDraft(
|
|
804
|
+
{
|
|
805
|
+
env: state.context.env,
|
|
806
|
+
user: state.context.user,
|
|
807
|
+
mailbox: { id: owner.id, ownerUserId: owner.ownerUserId },
|
|
808
|
+
},
|
|
809
|
+
id,
|
|
810
|
+
save.data
|
|
811
|
+
);
|
|
812
|
+
|
|
813
|
+
if (result.status === "saved") {
|
|
814
|
+
return { id, size: result.draft.bodyText.length };
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
if (result.status === "limit") {
|
|
818
|
+
return setError({
|
|
819
|
+
type: "overQuota",
|
|
820
|
+
description: "Too many drafts: send or discard some first",
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
return setError({
|
|
825
|
+
type: "invalidProperties",
|
|
826
|
+
properties: ["attachments"],
|
|
827
|
+
description:
|
|
828
|
+
result.status === "invalid" ? result.message : "Draft was not saved",
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* Why an Email/set update of a draft can't apply, or null: a draft's
|
|
834
|
+
* content can't change (a mail app replaces it with a new one), and it stays
|
|
835
|
+
* in the drafts mailbox with $draft until it is sent.
|
|
836
|
+
*/
|
|
837
|
+
function draftPatchError(patch: Json) {
|
|
838
|
+
if (!isJsonObject(patch)) {
|
|
839
|
+
const invalid: JsonObject = {
|
|
840
|
+
type: "invalidPatch",
|
|
841
|
+
description: "A patch is an object",
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
return invalid;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
848
|
+
const keeps =
|
|
849
|
+
(key === "keywords" &&
|
|
850
|
+
isJsonObject(value) &&
|
|
851
|
+
value.$draft === true &&
|
|
852
|
+
value.$seen === true &&
|
|
853
|
+
Object.keys(value).length === 2) ||
|
|
854
|
+
(key === "keywords/$draft" && value === true) ||
|
|
855
|
+
(key === "keywords/$seen" && value === true) ||
|
|
856
|
+
(key === "mailboxIds" &&
|
|
857
|
+
isJsonObject(value) &&
|
|
858
|
+
Object.keys(value).length === 1 &&
|
|
859
|
+
value[DRAFTS_FOLDER_ID] === true) ||
|
|
860
|
+
(key === `mailboxIds/${DRAFTS_FOLDER_ID}` && value === true);
|
|
861
|
+
|
|
862
|
+
if (!keeps) {
|
|
863
|
+
const stays: JsonObject = {
|
|
864
|
+
type: "invalidProperties",
|
|
865
|
+
properties: [patchPath(key)[0] ?? key],
|
|
866
|
+
description:
|
|
867
|
+
"A draft stays in Drafts until it is sent; save a new draft to change it",
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
return stays;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return null;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
export const emailSetArguments = z.object({
|
|
878
|
+
accountId,
|
|
879
|
+
ifInState: z.string().nullish(),
|
|
880
|
+
create: setObjects,
|
|
881
|
+
update: setObjects,
|
|
882
|
+
destroy: idList.nullish(),
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
type EmailSetArguments = z.output<typeof emailSetArguments>;
|
|
886
|
+
|
|
887
|
+
/** Email/set creates drafts only to send them; changing or destroying mail is organizing. */
|
|
888
|
+
export function emailSetScopes(args: EmailSetArguments) {
|
|
889
|
+
const scopes: string[] = [];
|
|
890
|
+
|
|
891
|
+
if (args.create && Object.keys(args.create).length) {
|
|
892
|
+
scopes.push(MAIL_SEND_SCOPE);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
if (
|
|
896
|
+
(args.update && Object.keys(args.update).length) ||
|
|
897
|
+
args.destroy?.length
|
|
898
|
+
) {
|
|
899
|
+
scopes.push(MAIL_ORGANIZE_SCOPE);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
return scopes;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export async function emailSet(
|
|
906
|
+
state: RequestState,
|
|
907
|
+
args: EmailSetArguments,
|
|
908
|
+
submittedVersions?: ReadonlyMap<string, number>
|
|
909
|
+
) {
|
|
910
|
+
const owner = account(state, args.accountId);
|
|
911
|
+
const stub = mailboxStub(state.context.env, owner.id);
|
|
912
|
+
const oldState = await stub.jmapState();
|
|
913
|
+
|
|
914
|
+
if (args.ifInState != null && args.ifInState !== oldState) {
|
|
915
|
+
throw new MethodError("stateMismatch");
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
const created = new Map<string, JsonObject>();
|
|
919
|
+
const notCreated = new Map<string, JsonObject>();
|
|
920
|
+
|
|
921
|
+
const senderFor = draftSenderResolver(state.context.env, owner);
|
|
922
|
+
const toSubmit: (ParsedDraft & { creationId: string })[] = [];
|
|
923
|
+
|
|
924
|
+
for (const [creationId, input] of Object.entries(args.create ?? {})) {
|
|
925
|
+
const keep = inDraftsMailbox(input);
|
|
926
|
+
|
|
927
|
+
if (keep && !owner.canSend) {
|
|
928
|
+
notCreated.set(creationId, {
|
|
929
|
+
type: "forbidden",
|
|
930
|
+
description: "This account cannot send",
|
|
931
|
+
});
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- resolve reply parents one creation at a time
|
|
936
|
+
const draft = await parseDraft(state, owner, input, senderFor);
|
|
937
|
+
|
|
938
|
+
if ("error" in draft) {
|
|
939
|
+
notCreated.set(creationId, draft.error);
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
if (!keep) {
|
|
944
|
+
toSubmit.push({ creationId, ...draft });
|
|
945
|
+
continue;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- drafts are saved in the order the client gave them
|
|
949
|
+
const stored = await createStoredDraft(state, owner, draft);
|
|
950
|
+
|
|
951
|
+
if ("error" in stored) {
|
|
952
|
+
notCreated.set(creationId, stored.error);
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
state.createdIds.set(creationId, stored.id);
|
|
957
|
+
created.set(creationId, {
|
|
958
|
+
id: stored.id,
|
|
959
|
+
blobId: `draft~${stored.id}`,
|
|
960
|
+
threadId: stored.id,
|
|
961
|
+
size: stored.size,
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// Without mailboxIds, clients can compose and submit in a single request.
|
|
966
|
+
const drafts = toSubmit.flatMap(
|
|
967
|
+
({ creationId, message: input, sourceId }) => {
|
|
968
|
+
const { attachments, ...message } = input;
|
|
969
|
+
|
|
970
|
+
// The rules the mail app's composer follows: valid, normalized and
|
|
971
|
+
// deduplicated recipients within the limits, a body, content ids.
|
|
972
|
+
const compose = composeSchema.safeParse({
|
|
973
|
+
...message,
|
|
974
|
+
requestId: crypto.randomUUID(),
|
|
975
|
+
mailboxId: owner.id,
|
|
976
|
+
attachments: attachments.map((attachment) => ({
|
|
977
|
+
uploadId: attachment.uploadId,
|
|
978
|
+
disposition: attachment.disposition,
|
|
979
|
+
contentId: attachment.contentId,
|
|
980
|
+
})),
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
if (!compose.success) {
|
|
984
|
+
notCreated.set(
|
|
985
|
+
creationId,
|
|
986
|
+
invalidProperties(compose.error.issues.map(draftIssue), {
|
|
987
|
+
topLevel: true,
|
|
988
|
+
})
|
|
989
|
+
);
|
|
990
|
+
|
|
991
|
+
return [];
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
return [
|
|
995
|
+
{
|
|
996
|
+
creationId,
|
|
997
|
+
attachments,
|
|
998
|
+
draft: {
|
|
999
|
+
accountId: owner.id,
|
|
1000
|
+
compose: compose.data,
|
|
1001
|
+
answers: { messageIds: [], emailId: sourceId },
|
|
1002
|
+
},
|
|
1003
|
+
},
|
|
1004
|
+
];
|
|
1005
|
+
}
|
|
1006
|
+
);
|
|
1007
|
+
|
|
1008
|
+
const attachmentErrors = await Promise.all(
|
|
1009
|
+
drafts.map(({ attachments }) =>
|
|
1010
|
+
prepareDraftAttachments(state, owner, attachments)
|
|
1011
|
+
)
|
|
1012
|
+
);
|
|
1013
|
+
|
|
1014
|
+
for (const [index, { creationId, draft }] of drafts.entries()) {
|
|
1015
|
+
const error = attachmentErrors[index];
|
|
1016
|
+
|
|
1017
|
+
if (error) {
|
|
1018
|
+
notCreated.set(creationId, error);
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// Drafts are not stored: one is sent by EmailSubmission/set in the same request.
|
|
1023
|
+
const id = `draft-${crypto.randomUUID()}`;
|
|
1024
|
+
state.drafts.set(id, draft);
|
|
1025
|
+
state.createdIds.set(creationId, id);
|
|
1026
|
+
created.set(creationId, {
|
|
1027
|
+
id,
|
|
1028
|
+
blobId: `draft~${id}`,
|
|
1029
|
+
threadId: id,
|
|
1030
|
+
size: draft.compose.bodyText.length,
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
const updated = new Map<string, null>();
|
|
1035
|
+
const notUpdated = new Map<string, JsonObject>();
|
|
1036
|
+
const reads: [string, boolean][] = [];
|
|
1037
|
+
const mailboxChanges = new Map<string, MailboxChange>();
|
|
1038
|
+
|
|
1039
|
+
const updates = Object.entries(args.update ?? {});
|
|
1040
|
+
|
|
1041
|
+
const temporaryDraft = (id: string) =>
|
|
1042
|
+
state.drafts.get(id)?.accountId === owner.id;
|
|
1043
|
+
|
|
1044
|
+
const isDraft = (id: string) => isStoredDraftId(id) || temporaryDraft(id);
|
|
1045
|
+
const draftUpdates = updates.filter(([id]) => isDraft(id));
|
|
1046
|
+
|
|
1047
|
+
const deleteDraft = (id: string) =>
|
|
1048
|
+
temporaryDraft(id)
|
|
1049
|
+
? state.drafts.delete(id)
|
|
1050
|
+
: stub.deleteDraft(state.context.user.id, id, submittedVersions?.get(id));
|
|
1051
|
+
|
|
1052
|
+
const missingDraft = (id: string) => {
|
|
1053
|
+
const error: JsonObject = submittedVersions?.has(id)
|
|
1054
|
+
? {
|
|
1055
|
+
type: "notFound",
|
|
1056
|
+
description: "The submitted draft version no longer exists",
|
|
1057
|
+
}
|
|
1058
|
+
: { type: "notFound" };
|
|
1059
|
+
|
|
1060
|
+
return error;
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
const draftErrors = await Promise.all(
|
|
1064
|
+
draftUpdates.map(async ([id, patch]) => {
|
|
1065
|
+
if (
|
|
1066
|
+
submittedVersions?.has(id) &&
|
|
1067
|
+
moveDraftToSent.safeParse(patch).success
|
|
1068
|
+
) {
|
|
1069
|
+
return (await deleteDraft(id)) ? null : missingDraft(id);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
const found =
|
|
1073
|
+
temporaryDraft(id) || (await stub.getDraft(state.context.user.id, id));
|
|
1074
|
+
|
|
1075
|
+
return found ? draftPatchError(patch) : missingDraft(id);
|
|
1076
|
+
})
|
|
1077
|
+
);
|
|
1078
|
+
|
|
1079
|
+
for (const [index, [id]] of draftUpdates.entries()) {
|
|
1080
|
+
const error = draftErrors[index];
|
|
1081
|
+
|
|
1082
|
+
if (error) notUpdated.set(id, error);
|
|
1083
|
+
else updated.set(id, null);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
for (const [id, patch] of updates.filter(([emailId]) => !isDraft(emailId))) {
|
|
1087
|
+
const change = emailPatch.safeParse(patch);
|
|
1088
|
+
|
|
1089
|
+
if (!change.success) {
|
|
1090
|
+
notUpdated.set(
|
|
1091
|
+
id,
|
|
1092
|
+
change.error.issues.some((issue) => issue.path.length === 0)
|
|
1093
|
+
? { type: "invalidPatch", description: "A patch is an object" }
|
|
1094
|
+
: invalidProperties(change.error.issues)
|
|
1095
|
+
);
|
|
1096
|
+
continue;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
const { seen, mailboxes } = change.data;
|
|
1100
|
+
|
|
1101
|
+
if (seen !== undefined) reads.push([id, seen]);
|
|
1102
|
+
|
|
1103
|
+
if (mailboxes) mailboxChanges.set(id, mailboxes);
|
|
1104
|
+
updated.set(id, null);
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
// Mailboxes first: an email whose move is refused isn't updated at all,
|
|
1108
|
+
// its $seen change included.
|
|
1109
|
+
const filed = mailboxChanges.size
|
|
1110
|
+
? await stub.jmapChangeMailboxes(
|
|
1111
|
+
[...mailboxChanges].map(([emailId, change]) => ({ emailId, change })),
|
|
1112
|
+
{
|
|
1113
|
+
userId: state.context.user.id,
|
|
1114
|
+
// Out of Junk trusts the senders in a person's mailbox; in a
|
|
1115
|
+
// shared inbox only those who manage teams may, and JMAP doesn't
|
|
1116
|
+
// know the workspace role, so there it doesn't.
|
|
1117
|
+
trustSenders: owner.ownerUserId !== null,
|
|
1118
|
+
}
|
|
1119
|
+
)
|
|
1120
|
+
: { verdicts: [], refused: [], notFound: [] };
|
|
1121
|
+
|
|
1122
|
+
// Into or out of Junk: what the person said, for the workspace too.
|
|
1123
|
+
await shareSpamVerdicts(state.context.env.DB, owner.id, filed.verdicts);
|
|
1124
|
+
|
|
1125
|
+
for (const id of filed.notFound) {
|
|
1126
|
+
updated.delete(id);
|
|
1127
|
+
notUpdated.set(id, { type: "notFound" });
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
for (const id of filed.refused) {
|
|
1131
|
+
updated.delete(id);
|
|
1132
|
+
notUpdated.set(id, {
|
|
1133
|
+
type: "invalidProperties",
|
|
1134
|
+
properties: ["mailboxIds"],
|
|
1135
|
+
description: "The email can't be in those Mailboxes",
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
const allowedReads = reads.filter(([id]) => !notUpdated.has(id));
|
|
1140
|
+
|
|
1141
|
+
// One write and one announcement for all of them.
|
|
1142
|
+
const { notFound } = allowedReads.length
|
|
1143
|
+
? await stub.setReadStates(
|
|
1144
|
+
state.context.user.id,
|
|
1145
|
+
allowedReads.map(([emailId, isRead]) => ({ emailId, isRead }))
|
|
1146
|
+
)
|
|
1147
|
+
: { notFound: [] };
|
|
1148
|
+
|
|
1149
|
+
for (const id of notFound) {
|
|
1150
|
+
updated.delete(id);
|
|
1151
|
+
notUpdated.set(id, { type: "notFound" });
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
const destroyed: string[] = [];
|
|
1155
|
+
const notDestroyed = new Map<string, JsonObject>();
|
|
1156
|
+
const destroyIds = (args.destroy ?? []).filter((id) => !isDraft(id));
|
|
1157
|
+
const draftDestroyIds = (args.destroy ?? []).filter(isDraft);
|
|
1158
|
+
|
|
1159
|
+
const draftsDeleted = await Promise.all(draftDestroyIds.map(deleteDraft));
|
|
1160
|
+
|
|
1161
|
+
for (const [index, id] of draftDestroyIds.entries()) {
|
|
1162
|
+
if (draftsDeleted[index]) destroyed.push(id);
|
|
1163
|
+
else notDestroyed.set(id, missingDraft(id));
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
if (destroyIds.length) {
|
|
1167
|
+
const { emails } = await stub.jmapEmails(
|
|
1168
|
+
state.context.user.id,
|
|
1169
|
+
destroyIds,
|
|
1170
|
+
["mailboxIds"]
|
|
1171
|
+
);
|
|
1172
|
+
|
|
1173
|
+
const inTrash = emails.filter((row) => row.mailboxIds?.includes("trash"));
|
|
1174
|
+
|
|
1175
|
+
// Mail is deleted for good from Trash only, as in the app. Moving it
|
|
1176
|
+
// there instead would answer "destroyed" for mail that still exists.
|
|
1177
|
+
const elsewhere = new Set(
|
|
1178
|
+
emails
|
|
1179
|
+
.filter((row) => !row.mailboxIds?.includes("trash"))
|
|
1180
|
+
.map((row) => row.email.id)
|
|
1181
|
+
);
|
|
1182
|
+
|
|
1183
|
+
// Deleting for good needs send access, as in the app.
|
|
1184
|
+
const removed =
|
|
1185
|
+
inTrash.length && owner.canSend
|
|
1186
|
+
? await stub.permanentlyDeleteEmails(inTrash.map((row) => row.email.id))
|
|
1187
|
+
: { deletedIds: [] };
|
|
1188
|
+
|
|
1189
|
+
const deleted = new Set(removed.deletedIds);
|
|
1190
|
+
|
|
1191
|
+
const refused = new Set(
|
|
1192
|
+
owner.canSend ? [] : inTrash.map((row) => row.email.id)
|
|
1193
|
+
);
|
|
1194
|
+
|
|
1195
|
+
for (const id of destroyIds) {
|
|
1196
|
+
if (elsewhere.has(id)) {
|
|
1197
|
+
notDestroyed.set(id, {
|
|
1198
|
+
type: "forbidden",
|
|
1199
|
+
description:
|
|
1200
|
+
"Only mail in Trash is deleted for good; move it to Trash first",
|
|
1201
|
+
});
|
|
1202
|
+
} else if (refused.has(id)) {
|
|
1203
|
+
notDestroyed.set(id, {
|
|
1204
|
+
type: "forbidden",
|
|
1205
|
+
description: "Send access is required to delete mail for good",
|
|
1206
|
+
});
|
|
1207
|
+
} else if (deleted.has(id)) destroyed.push(id);
|
|
1208
|
+
else notDestroyed.set(id, { type: "notFound" });
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
return {
|
|
1213
|
+
accountId: owner.id,
|
|
1214
|
+
oldState,
|
|
1215
|
+
newState: await stub.jmapState(),
|
|
1216
|
+
created: setResult(created),
|
|
1217
|
+
updated: setResult(updated),
|
|
1218
|
+
destroyed: destroyed.length ? destroyed : null,
|
|
1219
|
+
notCreated: setResult(notCreated),
|
|
1220
|
+
notUpdated: setResult(notUpdated),
|
|
1221
|
+
notDestroyed: setResult(notDestroyed),
|
|
1222
|
+
};
|
|
1223
|
+
}
|