@open-workspace/email 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +49 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +387 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +9 -10
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +754 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +108 -1815
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +682 -0
- package/worker/mail/auth-headers.ts +48 -40
- package/worker/mail/auto-reply-routes.ts +255 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +3 -3
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +696 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +201 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +301 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +450 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +240 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +156 -0
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { inJsonList } from "@open-workspace/core/module";
|
|
2
|
+
/** Deleting mail for good: conversations, the files they reference, and Trash and Spam retention. */
|
|
3
|
+
import { and, asc, eq, inArray, isNotNull, lte, sql } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import { officePreviewExtension } from "../../shared/office-preview";
|
|
6
|
+
import { keysAwaitingArchive } from "../mail/archive";
|
|
7
|
+
import { inboundMessageId } from "../mail/inbound";
|
|
8
|
+
import {
|
|
9
|
+
mailboxOwnsObject,
|
|
10
|
+
OFFICE_PREVIEW_SUFFIX,
|
|
11
|
+
officePreviewKey,
|
|
12
|
+
} from "../mail/storage-keys";
|
|
13
|
+
import { refreshConversation } from "./email-index";
|
|
14
|
+
import { retentionRules } from "./retention";
|
|
15
|
+
import type { RetentionFolder } from "./retention";
|
|
16
|
+
import {
|
|
17
|
+
conversations,
|
|
18
|
+
drafts,
|
|
19
|
+
emails,
|
|
20
|
+
pendingInbound,
|
|
21
|
+
pendingObjectDeletions,
|
|
22
|
+
} from "./schema";
|
|
23
|
+
import type { MailboxContext } from "./support";
|
|
24
|
+
import {
|
|
25
|
+
ALARM_WAKE_DELAY_MS,
|
|
26
|
+
ARCHIVE_WAIT_MS,
|
|
27
|
+
OBJECT_DELETION_ALARM_BATCH_SIZE,
|
|
28
|
+
RETENTION_BATCH_SIZE,
|
|
29
|
+
retryDelay,
|
|
30
|
+
stringListJson,
|
|
31
|
+
} from "./support";
|
|
32
|
+
|
|
33
|
+
/** Deletes conversations that are all in `fromState` (Trash unless emptying Spam). */
|
|
34
|
+
export function permanentlyDeleteConversations(
|
|
35
|
+
m: MailboxContext,
|
|
36
|
+
conversationIds: string[],
|
|
37
|
+
fromState: RetentionFolder = "trash"
|
|
38
|
+
) {
|
|
39
|
+
return deleteMail(m, conversationIds, "conversation", fromState);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function permanentlyDeleteEmails(m: MailboxContext, ids: string[]) {
|
|
43
|
+
return deleteMail(m, ids, "email", "trash");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function deleteMail(
|
|
47
|
+
m: MailboxContext,
|
|
48
|
+
ids: string[],
|
|
49
|
+
target: "conversation" | "email",
|
|
50
|
+
fromState: RetentionFolder
|
|
51
|
+
) {
|
|
52
|
+
if (!ids.length) return { deletedIds: [] };
|
|
53
|
+
// Arm cleanup before selecting rows. The selection, file checks and delete
|
|
54
|
+
// then run synchronously, so a concurrent move out of Trash wins cleanly.
|
|
55
|
+
const cleanupAt = new Date();
|
|
56
|
+
const column = target === "email" ? emails.id : emails.conversationId;
|
|
57
|
+
|
|
58
|
+
const matches = and(
|
|
59
|
+
eq(conversations.mailboxState, fromState),
|
|
60
|
+
inJsonList(column, ids)
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const mayNeedCleanup = m.db
|
|
64
|
+
.select({ id: emails.id })
|
|
65
|
+
.from(emails)
|
|
66
|
+
.innerJoin(conversations, eq(emails.conversationId, conversations.id))
|
|
67
|
+
.where(
|
|
68
|
+
and(
|
|
69
|
+
matches,
|
|
70
|
+
sql`(${emails.bodyHtmlR2Key} is not null or ${emails.rawMimeR2Key} is not null or json_array_length(${emails.attachmentsJson}) > 0)`
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
.limit(1)
|
|
74
|
+
.get();
|
|
75
|
+
|
|
76
|
+
if (mayNeedCleanup) {
|
|
77
|
+
await m.scheduleAlarm(cleanupAt.getTime() + ALARM_WAKE_DELAY_MS);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const storedEmails = m.db
|
|
81
|
+
.select({
|
|
82
|
+
id: emails.id,
|
|
83
|
+
conversationId: emails.conversationId,
|
|
84
|
+
direction: emails.direction,
|
|
85
|
+
bodyHtmlR2Key: emails.bodyHtmlR2Key,
|
|
86
|
+
rawMimeR2Key: emails.rawMimeR2Key,
|
|
87
|
+
attachmentsJson: emails.attachmentsJson,
|
|
88
|
+
})
|
|
89
|
+
.from(emails)
|
|
90
|
+
.innerJoin(conversations, eq(emails.conversationId, conversations.id))
|
|
91
|
+
.where(matches)
|
|
92
|
+
.all();
|
|
93
|
+
|
|
94
|
+
if (!storedEmails.length) return { deletedIds: [] };
|
|
95
|
+
const selected = storedEmails.map((email) => email.id);
|
|
96
|
+
|
|
97
|
+
const candidates = [
|
|
98
|
+
...new Set(storedEmails.map((email) => email.conversationId)),
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
const selectedIds = sql`(select value from json_each(${stringListJson(selected)}))`;
|
|
102
|
+
const threadIds = sql`(select value from json_each(${stringListJson(candidates)}))`;
|
|
103
|
+
|
|
104
|
+
const incomingEmailIds = new Set(
|
|
105
|
+
storedEmails.flatMap((email) =>
|
|
106
|
+
email.direction === "incoming" ? [email.id] : []
|
|
107
|
+
)
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const pendingInboundIds = incomingEmailIds.size
|
|
111
|
+
? m.db
|
|
112
|
+
.select({ id: pendingInbound.id })
|
|
113
|
+
.from(pendingInbound)
|
|
114
|
+
.all()
|
|
115
|
+
.flatMap((job) =>
|
|
116
|
+
incomingEmailIds.has(inboundMessageId(job.id)) ? [job.id] : []
|
|
117
|
+
)
|
|
118
|
+
: [];
|
|
119
|
+
|
|
120
|
+
const pendingInboundIdsJson = stringListJson(pendingInboundIds);
|
|
121
|
+
|
|
122
|
+
const objectKeys = [
|
|
123
|
+
...new Set(
|
|
124
|
+
storedEmails.flatMap((email) => [
|
|
125
|
+
...(email.bodyHtmlR2Key ? [email.bodyHtmlR2Key] : []),
|
|
126
|
+
...(email.rawMimeR2Key ? [email.rawMimeR2Key] : []),
|
|
127
|
+
...email.attachmentsJson.flatMap((attachment) => [
|
|
128
|
+
attachment.r2Key,
|
|
129
|
+
...(officePreviewExtension(attachment.filename)
|
|
130
|
+
? [officePreviewKey(attachment.r2Key)]
|
|
131
|
+
: []),
|
|
132
|
+
]),
|
|
133
|
+
])
|
|
134
|
+
),
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
// Deleted with them: their conversations, and their deliveries still queued.
|
|
138
|
+
const referenced = referencedObjectKeys(
|
|
139
|
+
m,
|
|
140
|
+
objectKeys,
|
|
141
|
+
selected,
|
|
142
|
+
pendingInboundIds
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const mailboxId = m.state.id.name ?? "";
|
|
146
|
+
|
|
147
|
+
const ownedObjectKeys = objectKeys.filter(
|
|
148
|
+
(objectKey) =>
|
|
149
|
+
mailboxOwnsObject(mailboxId, objectKey) && !referenced.has(objectKey)
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
m.db.transaction((tx) => {
|
|
153
|
+
tx.run(
|
|
154
|
+
sql`delete from pending_office_previews where email_id in ${selectedIds}`
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
for (const objectKey of ownedObjectKeys) {
|
|
158
|
+
tx.insert(pendingObjectDeletions)
|
|
159
|
+
.values({ objectKey, nextAttemptAt: cleanupAt })
|
|
160
|
+
.onConflictDoNothing()
|
|
161
|
+
.run();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (pendingInboundIds.length) {
|
|
165
|
+
tx.delete(pendingInbound)
|
|
166
|
+
.where(sql`${pendingInbound.id} in (
|
|
167
|
+
select cast(value as text) from json_each(${pendingInboundIdsJson})
|
|
168
|
+
)`)
|
|
169
|
+
.run();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// A saved reply or forward must still open after its source is gone.
|
|
173
|
+
tx.update(drafts)
|
|
174
|
+
.set({
|
|
175
|
+
kind: "compose",
|
|
176
|
+
sourceEmailId: null,
|
|
177
|
+
conversationId: null,
|
|
178
|
+
replyMode: null,
|
|
179
|
+
version: sql`${drafts.version} + 1`,
|
|
180
|
+
})
|
|
181
|
+
.where(sql`${drafts.sourceEmailId} in ${selectedIds}`)
|
|
182
|
+
.run();
|
|
183
|
+
|
|
184
|
+
tx.run(sql`delete from email_search where email_id in ${selectedIds}`);
|
|
185
|
+
tx.run(sql`delete from email_read_states where email_id in ${selectedIds}`);
|
|
186
|
+
|
|
187
|
+
if (target === "email") {
|
|
188
|
+
// Move the latest-email foreign key to a survivor before deleting the
|
|
189
|
+
// messages. A thread with no survivors still points at a selected id.
|
|
190
|
+
for (const id of candidates) {
|
|
191
|
+
refreshConversation(m.state.storage.sql, id, selected);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
tx.run(
|
|
196
|
+
sql`delete from conversations where id in ${threadIds} and latest_email_id in ${selectedIds}`
|
|
197
|
+
);
|
|
198
|
+
tx.delete(emails)
|
|
199
|
+
.where(sql`${emails.id} in ${selectedIds}`)
|
|
200
|
+
.run();
|
|
201
|
+
});
|
|
202
|
+
m.forgetParticipants();
|
|
203
|
+
void m.announce();
|
|
204
|
+
|
|
205
|
+
return { deletedIds: target === "email" ? selected : candidates };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function referencedObjectKeys(
|
|
209
|
+
m: MailboxContext,
|
|
210
|
+
objectKeys: string[],
|
|
211
|
+
excludedEmailIds: string[],
|
|
212
|
+
excludedDeliveryIds: string[] = []
|
|
213
|
+
) {
|
|
214
|
+
if (!objectKeys.length) return new Set<string>();
|
|
215
|
+
|
|
216
|
+
// One pass over what still points at files, each branch narrowed to the
|
|
217
|
+
// candidates: a key per message would scan every message per key.
|
|
218
|
+
const rows = m.state.storage.sql
|
|
219
|
+
.exec<{ object_key: string }>(
|
|
220
|
+
`
|
|
221
|
+
with
|
|
222
|
+
candidate_keys(object_key) as (
|
|
223
|
+
select cast(value as text) from json_each(?1)
|
|
224
|
+
),
|
|
225
|
+
excluded_emails(id) as (
|
|
226
|
+
select cast(value as text) from json_each(?2)
|
|
227
|
+
),
|
|
228
|
+
excluded_deliveries(id) as (
|
|
229
|
+
select cast(value as text) from json_each(?3)
|
|
230
|
+
),
|
|
231
|
+
kept_messages as (
|
|
232
|
+
select body_html_r2_key, raw_mime_r2_key, attachments_json
|
|
233
|
+
from emails
|
|
234
|
+
where id not in (select id from excluded_emails)
|
|
235
|
+
),
|
|
236
|
+
-- Few terms: a Durable Object's SQLite allows only a short compound select.
|
|
237
|
+
used(object_key) as (
|
|
238
|
+
select message_key.value
|
|
239
|
+
from kept_messages,
|
|
240
|
+
json_each(json_array(body_html_r2_key, raw_mime_r2_key)) message_key
|
|
241
|
+
union all
|
|
242
|
+
-- Each file, and the PDF preview of an Office one.
|
|
243
|
+
select file_key.value
|
|
244
|
+
from kept_messages,
|
|
245
|
+
json_each(kept_messages.attachments_json) attachment,
|
|
246
|
+
json_each(json_array(
|
|
247
|
+
json_extract(attachment.value, '$.r2Key'),
|
|
248
|
+
json_extract(attachment.value, '$.r2Key') || ?4
|
|
249
|
+
)) file_key
|
|
250
|
+
union all
|
|
251
|
+
select json_extract(attachment.value, '$.r2Key')
|
|
252
|
+
from drafts, json_each(drafts.attachments_json) attachment
|
|
253
|
+
union all
|
|
254
|
+
-- A delivery still queued reads its raw copy.
|
|
255
|
+
select raw_object_key from pending_inbound
|
|
256
|
+
where id not in (select id from excluded_deliveries)
|
|
257
|
+
)
|
|
258
|
+
select distinct object_key from used
|
|
259
|
+
where object_key in (select object_key from candidate_keys)
|
|
260
|
+
`,
|
|
261
|
+
stringListJson(objectKeys),
|
|
262
|
+
stringListJson(excludedEmailIds),
|
|
263
|
+
stringListJson(excludedDeliveryIds),
|
|
264
|
+
OFFICE_PREVIEW_SUFFIX
|
|
265
|
+
)
|
|
266
|
+
.toArray();
|
|
267
|
+
|
|
268
|
+
return new Set(rows.map((row) => row.object_key));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export async function processObjectDeletionJobs(
|
|
272
|
+
m: MailboxContext,
|
|
273
|
+
now: number
|
|
274
|
+
) {
|
|
275
|
+
const jobs = m.db
|
|
276
|
+
.select()
|
|
277
|
+
.from(pendingObjectDeletions)
|
|
278
|
+
.where(lte(pendingObjectDeletions.nextAttemptAt, new Date(now)))
|
|
279
|
+
.orderBy(asc(pendingObjectDeletions.nextAttemptAt))
|
|
280
|
+
.limit(OBJECT_DELETION_ALARM_BATCH_SIZE)
|
|
281
|
+
.all();
|
|
282
|
+
|
|
283
|
+
if (!jobs.length) return;
|
|
284
|
+
const objectKeys = jobs.map((job) => job.objectKey);
|
|
285
|
+
const referenced = referencedObjectKeys(m, objectKeys, []);
|
|
286
|
+
|
|
287
|
+
const retainedKeys = objectKeys.filter((key) => referenced.has(key));
|
|
288
|
+
|
|
289
|
+
if (retainedKeys.length) {
|
|
290
|
+
m.db
|
|
291
|
+
.delete(pendingObjectDeletions)
|
|
292
|
+
.where(inArray(pendingObjectDeletions.objectKey, retainedKeys))
|
|
293
|
+
.run();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Raw copies the mail archive doesn't have yet stay until it does.
|
|
297
|
+
const awaitingArchive = await keysAwaitingArchive(
|
|
298
|
+
m.env,
|
|
299
|
+
objectKeys.filter((key) => !referenced.has(key))
|
|
300
|
+
).catch(() => new Set(objectKeys));
|
|
301
|
+
|
|
302
|
+
for (const key of awaitingArchive) {
|
|
303
|
+
m.db
|
|
304
|
+
.update(pendingObjectDeletions)
|
|
305
|
+
.set({ nextAttemptAt: new Date(now + ARCHIVE_WAIT_MS) })
|
|
306
|
+
.where(eq(pendingObjectDeletions.objectKey, key))
|
|
307
|
+
.run();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const deletableJobs = jobs.filter(
|
|
311
|
+
(job) =>
|
|
312
|
+
!referenced.has(job.objectKey) && !awaitingArchive.has(job.objectKey)
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
if (!deletableJobs.length) return;
|
|
316
|
+
const deletableKeys = deletableJobs.map((job) => job.objectKey);
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
await m.env.MAIL_STORAGE.delete(deletableKeys);
|
|
320
|
+
m.db
|
|
321
|
+
.delete(pendingObjectDeletions)
|
|
322
|
+
.where(inArray(pendingObjectDeletions.objectKey, deletableKeys))
|
|
323
|
+
.run();
|
|
324
|
+
} catch (error) {
|
|
325
|
+
for (const job of deletableJobs) {
|
|
326
|
+
const attempts = job.attempts + 1;
|
|
327
|
+
m.db
|
|
328
|
+
.update(pendingObjectDeletions)
|
|
329
|
+
.set({
|
|
330
|
+
attempts,
|
|
331
|
+
nextAttemptAt: new Date(now + retryDelay(attempts)),
|
|
332
|
+
})
|
|
333
|
+
.where(eq(pendingObjectDeletions.objectKey, job.objectKey))
|
|
334
|
+
.run();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
console.error("Could not delete permanent message objects", error);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Permanently deletes one batch from Trash or Spam; callers repeat while `remaining > 0`. */
|
|
342
|
+
export async function emptyFolder(m: MailboxContext, state: RetentionFolder) {
|
|
343
|
+
const ids = m.db
|
|
344
|
+
.select({ id: conversations.id })
|
|
345
|
+
.from(conversations)
|
|
346
|
+
.where(eq(conversations.mailboxState, state))
|
|
347
|
+
.orderBy(asc(conversations.timelineAt))
|
|
348
|
+
.limit(RETENTION_BATCH_SIZE)
|
|
349
|
+
.all()
|
|
350
|
+
.map((row) => row.id);
|
|
351
|
+
|
|
352
|
+
const result = ids.length
|
|
353
|
+
? await permanentlyDeleteConversations(m, ids, state)
|
|
354
|
+
: { deletedIds: [] };
|
|
355
|
+
|
|
356
|
+
const remaining =
|
|
357
|
+
m.db
|
|
358
|
+
.select({ count: sql<number>`count(*)` })
|
|
359
|
+
.from(conversations)
|
|
360
|
+
.where(eq(conversations.mailboxState, state))
|
|
361
|
+
.all()[0]?.count ?? 0;
|
|
362
|
+
|
|
363
|
+
return { deletedIds: result.deletedIds, remaining };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export async function processRetention(m: MailboxContext, now: number) {
|
|
367
|
+
for (const { state, ms } of retentionRules(m.env)) {
|
|
368
|
+
const expired = m.db
|
|
369
|
+
.select({ id: conversations.id })
|
|
370
|
+
.from(conversations)
|
|
371
|
+
.where(
|
|
372
|
+
and(
|
|
373
|
+
eq(conversations.mailboxState, state),
|
|
374
|
+
lte(conversations.stateChangedAt, new Date(now - ms))
|
|
375
|
+
)
|
|
376
|
+
)
|
|
377
|
+
.orderBy(asc(conversations.stateChangedAt))
|
|
378
|
+
.limit(RETENTION_BATCH_SIZE)
|
|
379
|
+
.all()
|
|
380
|
+
.map((row) => row.id);
|
|
381
|
+
|
|
382
|
+
if (expired.length) {
|
|
383
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- Trash then Spam, one bounded batch each
|
|
384
|
+
await permanentlyDeleteConversations(m, expired, state);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** When the oldest Trash or Spam conversation reaches its retention limit. */
|
|
390
|
+
export function nextRetentionAt(m: MailboxContext) {
|
|
391
|
+
const due = retentionRules(m.env).flatMap(({ state, ms }) => {
|
|
392
|
+
const oldest = m.db
|
|
393
|
+
.select({ at: conversations.stateChangedAt })
|
|
394
|
+
.from(conversations)
|
|
395
|
+
.where(
|
|
396
|
+
and(
|
|
397
|
+
eq(conversations.mailboxState, state),
|
|
398
|
+
isNotNull(conversations.stateChangedAt)
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
.orderBy(asc(conversations.stateChangedAt))
|
|
402
|
+
.limit(1)
|
|
403
|
+
.all()[0]?.at;
|
|
404
|
+
|
|
405
|
+
return oldest ? [oldest.getTime() + ms] : [];
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
return due.length > 0 ? Math.min(...due) : null;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Queues this mailbox's objects for deletion. The queue skips a key a
|
|
413
|
+
* message or draft still holds, and one the archive doesn't have yet.
|
|
414
|
+
*/
|
|
415
|
+
export function queueObjectDeletions(m: MailboxContext, objectKeys: string[]) {
|
|
416
|
+
const mailboxId = m.state.id.name ?? "";
|
|
417
|
+
|
|
418
|
+
const owned = objectKeys.filter((key) => mailboxOwnsObject(mailboxId, key));
|
|
419
|
+
|
|
420
|
+
if (!owned.length) return false;
|
|
421
|
+
const cleanupAt = new Date();
|
|
422
|
+
|
|
423
|
+
for (const objectKey of new Set(owned)) {
|
|
424
|
+
m.db
|
|
425
|
+
.insert(pendingObjectDeletions)
|
|
426
|
+
.values({ objectKey, nextAttemptAt: cleanupAt })
|
|
427
|
+
.onConflictDoNothing()
|
|
428
|
+
.run();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export async function forgetObjects(m: MailboxContext, objectKeys: string[]) {
|
|
435
|
+
if (queueObjectDeletions(m, objectKeys)) {
|
|
436
|
+
await m.scheduleAlarm(Date.now() + ALARM_WAKE_DELAY_MS);
|
|
437
|
+
}
|
|
438
|
+
}
|