@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,933 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where conversations are filed, changed by a person or an app: one move
|
|
3
|
+
* for the app, JMAP and MCP, and taking messages to Spam without the rest
|
|
4
|
+
* of their conversation. What a move into or out of Spam teaches the
|
|
5
|
+
* classifier comes with it.
|
|
6
|
+
*/
|
|
7
|
+
import { createId, inJsonList } from "@open-workspace/core/module";
|
|
8
|
+
import {
|
|
9
|
+
and,
|
|
10
|
+
eq,
|
|
11
|
+
inArray,
|
|
12
|
+
isNotNull,
|
|
13
|
+
isNull,
|
|
14
|
+
sql as sqlTemplate,
|
|
15
|
+
} from "drizzle-orm";
|
|
16
|
+
import type { SQL } from "drizzle-orm";
|
|
17
|
+
|
|
18
|
+
import { conversationFolder } from "../../shared/folder-order";
|
|
19
|
+
import type { ConversationPlace } from "../../shared/mail";
|
|
20
|
+
import type { FeedbackLabel } from "../../shared/mail-feedback";
|
|
21
|
+
import { workspaceBlocksForVerdicts } from "../mail/workspace-safety";
|
|
22
|
+
import { mergeConversation, refreshConversation } from "./email-index";
|
|
23
|
+
import type { MailboxState } from "./inbound-decision";
|
|
24
|
+
import { isRetentionFolder } from "./retention";
|
|
25
|
+
import { conversations, drafts, emails } from "./schema";
|
|
26
|
+
import {
|
|
27
|
+
mailboxBlockFor,
|
|
28
|
+
recordSenderFeedback,
|
|
29
|
+
senderBlockFor,
|
|
30
|
+
setSenderTrusted,
|
|
31
|
+
} from "./senders";
|
|
32
|
+
import type { MailboxContext } from "./support";
|
|
33
|
+
import { stringListJson } from "./support";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Where a move puts conversations: a state, and in the Inbox a custom
|
|
37
|
+
* folder (null takes them out of theirs); what is undefined stays. A
|
|
38
|
+
* folder only counts in the Inbox: elsewhere the conversation keeps its
|
|
39
|
+
* own, to return to.
|
|
40
|
+
*/
|
|
41
|
+
export type Placement = {
|
|
42
|
+
mailboxState?: MailboxState;
|
|
43
|
+
folderId?: string | null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Read final placement after edits and merges, including moves larger than SQLite's bind limit. */
|
|
47
|
+
function currentFolders(m: MailboxContext, ids: string[]) {
|
|
48
|
+
const rows = ids.length
|
|
49
|
+
? m.db
|
|
50
|
+
.select({
|
|
51
|
+
id: conversations.id,
|
|
52
|
+
mailboxState: conversations.mailboxState,
|
|
53
|
+
folderId: conversations.folderId,
|
|
54
|
+
hasIncoming: conversations.hasIncoming,
|
|
55
|
+
})
|
|
56
|
+
.from(conversations)
|
|
57
|
+
.where(inJsonList(conversations.id, ids))
|
|
58
|
+
.all()
|
|
59
|
+
: [];
|
|
60
|
+
|
|
61
|
+
return new Map(rows.map((row) => [row.id, conversationFolder(row)]));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The one way a conversation's place is written: its state and, when
|
|
66
|
+
* given, its folder (a label in the Inbox, kept while it is elsewhere so it
|
|
67
|
+
* returns there). Retention counts from entering Trash or Spam, never from
|
|
68
|
+
* a move within; `stored` says a message was just stored into the
|
|
69
|
+
* conversation, which restarts it there, so new mail keeps its full time.
|
|
70
|
+
* `from` limits it to conversations in that state. The ids it changed.
|
|
71
|
+
*/
|
|
72
|
+
export function fileConversations(
|
|
73
|
+
sql: SqlStorage,
|
|
74
|
+
ids: readonly string[],
|
|
75
|
+
place: Placement,
|
|
76
|
+
options: { stored?: boolean; from?: MailboxState } = {}
|
|
77
|
+
) {
|
|
78
|
+
if (!ids.length) return [];
|
|
79
|
+
|
|
80
|
+
const retention = isRetentionFolder(place.mailboxState);
|
|
81
|
+
|
|
82
|
+
// Out of the Inbox, its folder stays for when it comes back.
|
|
83
|
+
const folderId =
|
|
84
|
+
place.mailboxState && place.mailboxState !== "active"
|
|
85
|
+
? undefined
|
|
86
|
+
: place.folderId;
|
|
87
|
+
|
|
88
|
+
return sql
|
|
89
|
+
.exec<{ id: string }>(
|
|
90
|
+
`update conversations set
|
|
91
|
+
mailbox_state = coalesce(?, mailbox_state),
|
|
92
|
+
folder_id = ${folderId === undefined ? "folder_id" : "?"},
|
|
93
|
+
state_changed_at = case
|
|
94
|
+
when ? and (? or mailbox_state <> ?) then ? else state_changed_at
|
|
95
|
+
end
|
|
96
|
+
where id in (select cast(value as text) from json_each(?))
|
|
97
|
+
${options.from ? "and mailbox_state = ?" : ""}
|
|
98
|
+
returning id`,
|
|
99
|
+
place.mailboxState ?? null,
|
|
100
|
+
...(folderId === undefined ? [] : [folderId]),
|
|
101
|
+
retention ? 1 : 0,
|
|
102
|
+
options.stored ? 1 : 0,
|
|
103
|
+
place.mailboxState ?? "",
|
|
104
|
+
Date.now(),
|
|
105
|
+
stringListJson([...ids]),
|
|
106
|
+
...(options.from ? [options.from] : [])
|
|
107
|
+
)
|
|
108
|
+
.toArray()
|
|
109
|
+
.map((row) => row.id);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* How strongly a place holds a conversation when its messages ask for
|
|
114
|
+
* different ones (Gmail labels each message, a conversation here has one
|
|
115
|
+
* place): Trash, then Spam, then a custom folder, then the Inbox, then
|
|
116
|
+
* Archive. One order for JMAP moves and imports, and for importers.
|
|
117
|
+
*/
|
|
118
|
+
export function placementRank(placement: Placement) {
|
|
119
|
+
switch (placement.mailboxState) {
|
|
120
|
+
case "trash": {
|
|
121
|
+
return 5;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
case "spam": {
|
|
125
|
+
return 4;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
case "archive": {
|
|
129
|
+
return 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
default: {
|
|
133
|
+
return placement.folderId ? 3 : 2;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** What a person said about a sender, for the workspace to learn from too. */
|
|
139
|
+
export type SpamVerdict = {
|
|
140
|
+
emailId: string;
|
|
141
|
+
address: string;
|
|
142
|
+
label: FeedbackLabel;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const LEFT_SPAM: ReadonlySet<MailboxState | undefined> = new Set([
|
|
146
|
+
"active",
|
|
147
|
+
"archive",
|
|
148
|
+
]);
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Received messages in `conversationIds`, or (with `emailIds`) those
|
|
152
|
+
* messages and their senders' other received ones in the same
|
|
153
|
+
* conversations: what Spam concerns. A report is about a sender, so their
|
|
154
|
+
* earlier messages in the thread go with the one named.
|
|
155
|
+
*/
|
|
156
|
+
function spamMessages(
|
|
157
|
+
m: MailboxContext,
|
|
158
|
+
scope:
|
|
159
|
+
| { conversationIds: readonly string[] }
|
|
160
|
+
| { emailIds: readonly string[] }
|
|
161
|
+
) {
|
|
162
|
+
if ("conversationIds" in scope) {
|
|
163
|
+
return m.db
|
|
164
|
+
.select({ id: emails.id })
|
|
165
|
+
.from(emails)
|
|
166
|
+
.where(
|
|
167
|
+
and(
|
|
168
|
+
eq(emails.direction, "incoming"),
|
|
169
|
+
inJsonList(emails.conversationId, [...scope.conversationIds])
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
.all();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const sender = sqlTemplate<
|
|
176
|
+
string | null
|
|
177
|
+
>`lower(json_extract(${emails.fromJson}, '$[0].address'))`;
|
|
178
|
+
|
|
179
|
+
const named = m.db
|
|
180
|
+
.selectDistinct({
|
|
181
|
+
conversationId: emails.conversationId,
|
|
182
|
+
sender: sender.as("sender"),
|
|
183
|
+
})
|
|
184
|
+
.from(emails)
|
|
185
|
+
.where(
|
|
186
|
+
and(
|
|
187
|
+
eq(emails.direction, "incoming"),
|
|
188
|
+
inJsonList(emails.id, [...scope.emailIds])
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
.as("named");
|
|
192
|
+
|
|
193
|
+
return m.db
|
|
194
|
+
.select({ id: emails.id })
|
|
195
|
+
.from(emails)
|
|
196
|
+
.innerJoin(
|
|
197
|
+
named,
|
|
198
|
+
and(
|
|
199
|
+
eq(named.conversationId, emails.conversationId),
|
|
200
|
+
sqlTemplate`${named.sender} is ${sender}`
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
.where(eq(emails.direction, "incoming"))
|
|
204
|
+
.all();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* One person's verdict on received messages: each sender among them gets
|
|
209
|
+
* one (on their newest message), as Report spam and Not spam record it.
|
|
210
|
+
*/
|
|
211
|
+
function recordSpamVerdicts(
|
|
212
|
+
m: MailboxContext,
|
|
213
|
+
emailIds: readonly string[],
|
|
214
|
+
label: FeedbackLabel,
|
|
215
|
+
userId: string
|
|
216
|
+
) {
|
|
217
|
+
const newest = m.state.storage.sql
|
|
218
|
+
.exec<{ emailId: string; address: string | null }>(
|
|
219
|
+
`select emailId, address from (
|
|
220
|
+
select e.id as emailId,
|
|
221
|
+
json_extract(e.from_json, '$[0].address') as address, row_number() over (
|
|
222
|
+
partition by lower(json_extract(e.from_json, '$[0].address'))
|
|
223
|
+
order by e.timeline_at desc, e.id desc
|
|
224
|
+
) as position
|
|
225
|
+
from emails e
|
|
226
|
+
where e.direction = 'incoming'
|
|
227
|
+
and e.id in (select cast(value as text) from json_each(?))
|
|
228
|
+
) where position = 1`,
|
|
229
|
+
stringListJson([...emailIds])
|
|
230
|
+
)
|
|
231
|
+
.toArray();
|
|
232
|
+
|
|
233
|
+
return newest.flatMap(({ address, emailId }) => {
|
|
234
|
+
if (!address?.includes("@")) return [];
|
|
235
|
+
const verdict = { address, emailId, label };
|
|
236
|
+
recordSenderFeedback(m.db, {
|
|
237
|
+
...verdict,
|
|
238
|
+
id: createId("fbk"),
|
|
239
|
+
userId,
|
|
240
|
+
now: Date.now(),
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
return [verdict];
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The one way mail goes to Spam because someone said so (Report spam, a
|
|
249
|
+
* block, a move into Spam in the app or over JMAP): the messages Spam
|
|
250
|
+
* concerns (`spamMessages`) leave their conversations for Spam
|
|
251
|
+
* (`moveMessagesToSpam`), and with `userId` each sender gets that
|
|
252
|
+
* person's verdict. Returns the selected messages, their placement and verdicts.
|
|
253
|
+
*/
|
|
254
|
+
export function fileSpam(
|
|
255
|
+
m: MailboxContext,
|
|
256
|
+
scope: Parameters<typeof spamMessages>[1],
|
|
257
|
+
userId: string | null
|
|
258
|
+
) {
|
|
259
|
+
const messages = spamMessages(m, scope);
|
|
260
|
+
const ids = messages.map((row) => row.id);
|
|
261
|
+
|
|
262
|
+
// Filter on the manual label: a first report still counts when delivery
|
|
263
|
+
// already filed the message in Spam, but replaying it casts no new vote.
|
|
264
|
+
const labeled =
|
|
265
|
+
userId && ids.length
|
|
266
|
+
? m.db
|
|
267
|
+
.update(emails)
|
|
268
|
+
.set({ reportLabel: "spam" })
|
|
269
|
+
.where(and(isNull(emails.reportLabel), inJsonList(emails.id, ids)))
|
|
270
|
+
.returning({ id: emails.id, conversationId: emails.conversationId })
|
|
271
|
+
.all()
|
|
272
|
+
: [];
|
|
273
|
+
|
|
274
|
+
const places = moveMessagesToSpam(m, ids);
|
|
275
|
+
const stationary = new Set(labeled.map((row) => row.conversationId));
|
|
276
|
+
|
|
277
|
+
for (const place of places) stationary.delete(place.from);
|
|
278
|
+
|
|
279
|
+
for (const id of stationary) {
|
|
280
|
+
places.push({ from: id, conversationId: id, folder: "spam", left: false });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
emailIds: ids,
|
|
285
|
+
places,
|
|
286
|
+
verdicts:
|
|
287
|
+
userId && labeled.length
|
|
288
|
+
? recordSpamVerdicts(
|
|
289
|
+
m,
|
|
290
|
+
labeled.map((row) => row.id),
|
|
291
|
+
"spam",
|
|
292
|
+
userId
|
|
293
|
+
)
|
|
294
|
+
: [],
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The workspace's blocks for moves of conversations to a state
|
|
300
|
+
* (`leaveSpam`'s `workspaceBlocks`), read fresh only when one of them
|
|
301
|
+
* leaves Spam by its move: null when they can't be read.
|
|
302
|
+
*/
|
|
303
|
+
export function blocksForLeavingSpam(
|
|
304
|
+
m: MailboxContext,
|
|
305
|
+
moves: readonly {
|
|
306
|
+
conversationIds: readonly string[];
|
|
307
|
+
mailboxState: MailboxState | undefined;
|
|
308
|
+
}[]
|
|
309
|
+
) {
|
|
310
|
+
const leaving = moves.flatMap((move) =>
|
|
311
|
+
LEFT_SPAM.has(move.mailboxState) ? move.conversationIds : []
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const leaves =
|
|
315
|
+
leaving.length > 0 &&
|
|
316
|
+
m.db
|
|
317
|
+
.select({ id: conversations.id })
|
|
318
|
+
.from(conversations)
|
|
319
|
+
.where(
|
|
320
|
+
and(
|
|
321
|
+
eq(conversations.mailboxState, "spam"),
|
|
322
|
+
inJsonList(conversations.id, leaving)
|
|
323
|
+
)
|
|
324
|
+
)
|
|
325
|
+
.limit(1)
|
|
326
|
+
.get();
|
|
327
|
+
|
|
328
|
+
return leaves
|
|
329
|
+
? workspaceBlocksForVerdicts(m.env.DB)
|
|
330
|
+
: Promise.resolve(new Map<string, number>());
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The one way conversations leave Spam on someone's word (Not spam, a move
|
|
335
|
+
* out of Spam in the app or over JMAP, a reply from there): the received
|
|
336
|
+
* mail is read first, the conversations go to `target`, mail kept apart
|
|
337
|
+
* rejoins its threads, and with `userId` each sender is that person's
|
|
338
|
+
* verdict of not spam. Mail held back by a block (`senderBlockFor`, on its
|
|
339
|
+
* From or envelope sender) is no such verdict: replying "stop" or taking
|
|
340
|
+
* it out of Spam mustn't let a blocked sender back in; Unblock does that.
|
|
341
|
+
* `workspaceBlocks` are the workspace's (null, unknown: no verdicts).
|
|
342
|
+
* The verdict trusts each sender only with `trustSenders`: trusting
|
|
343
|
+
* changes how the mailbox sorts their mail for everyone in it, which only
|
|
344
|
+
* those who may organize it decide. Without `userId` (an app, or an
|
|
345
|
+
* Undo) it is no verdict and trusts nobody. Inside the caller's
|
|
346
|
+
* transaction; where each conversation is now.
|
|
347
|
+
*/
|
|
348
|
+
export function leaveSpam(
|
|
349
|
+
m: MailboxContext,
|
|
350
|
+
conversationIds: readonly string[],
|
|
351
|
+
target: Placement,
|
|
352
|
+
options: {
|
|
353
|
+
userId: string | null;
|
|
354
|
+
trustSenders: boolean;
|
|
355
|
+
workspaceBlocks: ReadonlyMap<string, number> | null;
|
|
356
|
+
beforeMerge?: (target: string) => void;
|
|
357
|
+
}
|
|
358
|
+
) {
|
|
359
|
+
const { sql } = m.state.storage;
|
|
360
|
+
|
|
361
|
+
const spamRows = m.db
|
|
362
|
+
.select({ id: conversations.id, folderId: conversations.folderId })
|
|
363
|
+
.from(conversations)
|
|
364
|
+
.where(
|
|
365
|
+
and(
|
|
366
|
+
eq(conversations.mailboxState, "spam"),
|
|
367
|
+
inJsonList(conversations.id, [...conversationIds])
|
|
368
|
+
)
|
|
369
|
+
)
|
|
370
|
+
.all();
|
|
371
|
+
|
|
372
|
+
const inSpam = spamRows.map((row) => row.id);
|
|
373
|
+
|
|
374
|
+
if (!inSpam.length) return { places: [], verdicts: [] };
|
|
375
|
+
|
|
376
|
+
const received = spamMessages(m, { conversationIds: inSpam }).map(
|
|
377
|
+
(row) => row.id
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
// No verdict on blocked mail (`senderBlockFor`, as delivery checks it).
|
|
381
|
+
const { workspaceBlocks } = options;
|
|
382
|
+
|
|
383
|
+
const judged = workspaceBlocks
|
|
384
|
+
? m.db
|
|
385
|
+
.select({
|
|
386
|
+
id: emails.id,
|
|
387
|
+
address: sqlTemplate<
|
|
388
|
+
string | null
|
|
389
|
+
>`json_extract(${emails.fromJson}, '$[0].address')`,
|
|
390
|
+
envelopeFrom: emails.envelopeFrom,
|
|
391
|
+
})
|
|
392
|
+
.from(emails)
|
|
393
|
+
.where(inJsonList(emails.id, received))
|
|
394
|
+
.all()
|
|
395
|
+
.flatMap((row) =>
|
|
396
|
+
senderBlockFor(
|
|
397
|
+
{ from: row.address, envelopeFrom: row.envelopeFrom },
|
|
398
|
+
workspaceBlocks,
|
|
399
|
+
(address) => mailboxBlockFor(m.db, address)
|
|
400
|
+
)
|
|
401
|
+
? []
|
|
402
|
+
: [row.id]
|
|
403
|
+
)
|
|
404
|
+
: [];
|
|
405
|
+
|
|
406
|
+
fileConversations(sql, inSpam, target);
|
|
407
|
+
|
|
408
|
+
// Out of Spam, it no longer says "Reported as spam", verdict or not (an
|
|
409
|
+
// Undo puts reported mail back without one).
|
|
410
|
+
sql.exec(
|
|
411
|
+
`update emails set report_label = null
|
|
412
|
+
where id in (select cast(value as text) from json_each(?))`,
|
|
413
|
+
stringListJson(received)
|
|
414
|
+
);
|
|
415
|
+
const rejoined = rejoinSplitConversations(m, inSpam, options.beforeMerge);
|
|
416
|
+
|
|
417
|
+
const verdicts = options.userId
|
|
418
|
+
? recordSpamVerdicts(m, judged, "not_spam", options.userId)
|
|
419
|
+
: [];
|
|
420
|
+
|
|
421
|
+
if (options.trustSenders) {
|
|
422
|
+
const now = Date.now();
|
|
423
|
+
|
|
424
|
+
for (const verdict of verdicts) {
|
|
425
|
+
setSenderTrusted(m.db, verdict.address, true, now);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const foldersAfter = currentFolders(
|
|
430
|
+
m,
|
|
431
|
+
inSpam.map((id) => rejoined.get(id) ?? id)
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
const places = spamRows.map(({ id: from, folderId }) => {
|
|
435
|
+
const now = rejoined.get(from) ?? from;
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
from,
|
|
439
|
+
conversationId: now,
|
|
440
|
+
previous: { mailboxState: "spam" as const, folderId },
|
|
441
|
+
folder: foldersAfter.get(now) ?? "inbox",
|
|
442
|
+
left: true,
|
|
443
|
+
};
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
return { places, verdicts };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Moves conversations, as a person or an app asks: those of `ids` that
|
|
451
|
+
* `only` (a condition on `c`, the conversation) allows. Filing in the
|
|
452
|
+
* Inbox (into a folder or out of one) takes only conversations with
|
|
453
|
+
* received mail; sent-only ones stay in Sent. Retention counts from entering Trash or Spam, not from a move
|
|
454
|
+
* within it. Moved out of Spam, mail kept apart rejoins its conversation.
|
|
455
|
+
*
|
|
456
|
+
* With `userId`, the move is that person's verdict on each sender of the
|
|
457
|
+
* received mail (`recordSpamVerdicts`): into Spam reports them, out of it
|
|
458
|
+
* (to Inbox or Archive) says they are not spam, as Report spam and Not
|
|
459
|
+
* spam do (`leaveSpam`: blocked mail, by `workspaceBlocks` and the
|
|
460
|
+
* mailbox's, is no verdict; it trusts them only with `trustSenders`).
|
|
461
|
+
* `emailIds` (JMAP moves name messages) limits a move into Spam to those
|
|
462
|
+
* messages and their senders' (`spamMessages`).
|
|
463
|
+
*/
|
|
464
|
+
export function placeConversations(
|
|
465
|
+
m: MailboxContext,
|
|
466
|
+
ids: readonly string[],
|
|
467
|
+
target: Placement,
|
|
468
|
+
options: {
|
|
469
|
+
undo?: boolean;
|
|
470
|
+
only?: SQL;
|
|
471
|
+
userId: string | null;
|
|
472
|
+
trustSenders: boolean;
|
|
473
|
+
workspaceBlocks: ReadonlyMap<string, number> | null;
|
|
474
|
+
beforeMerge?: (target: string) => void;
|
|
475
|
+
emailIds?: readonly string[];
|
|
476
|
+
}
|
|
477
|
+
) {
|
|
478
|
+
const candidates = [...new Set(ids)];
|
|
479
|
+
|
|
480
|
+
if (!candidates.length) return { moved: [], places: [], verdicts: [] };
|
|
481
|
+
const { sql } = m.state.storage;
|
|
482
|
+
const { mailboxState } = target;
|
|
483
|
+
|
|
484
|
+
// Filed in the Inbox distribution (a folder, or none) or Spam: received
|
|
485
|
+
// mail only; sent-only threads stay in Sent.
|
|
486
|
+
const toInbox =
|
|
487
|
+
target.folderId !== undefined &&
|
|
488
|
+
(mailboxState === undefined || mailboxState === "active");
|
|
489
|
+
|
|
490
|
+
const result = m.db.transaction(() => {
|
|
491
|
+
const found = m.db
|
|
492
|
+
.select({
|
|
493
|
+
id: conversations.id,
|
|
494
|
+
mailboxState: conversations.mailboxState,
|
|
495
|
+
folderId: conversations.folderId,
|
|
496
|
+
hasIncoming: conversations.hasIncoming,
|
|
497
|
+
})
|
|
498
|
+
.from(conversations)
|
|
499
|
+
.where(
|
|
500
|
+
and(
|
|
501
|
+
inJsonList(conversations.id, candidates),
|
|
502
|
+
options.only,
|
|
503
|
+
toInbox || mailboxState === "spam"
|
|
504
|
+
? eq(conversations.hasIncoming, true)
|
|
505
|
+
: undefined
|
|
506
|
+
)
|
|
507
|
+
)
|
|
508
|
+
.all();
|
|
509
|
+
|
|
510
|
+
if (!found.length) return { moved: [], places: [], verdicts: [] };
|
|
511
|
+
|
|
512
|
+
const { userId } = options;
|
|
513
|
+
const verdicts: SpamVerdict[] = [];
|
|
514
|
+
const places = new Map<string, ConversationPlace>();
|
|
515
|
+
const filed = new Set<string>();
|
|
516
|
+
|
|
517
|
+
// Into Spam goes as Report spam does: the received mail (the emails
|
|
518
|
+
// named, with their senders'), not your replies in the thread (`fileSpam`).
|
|
519
|
+
const intoSpam = found.filter(
|
|
520
|
+
(row) => mailboxState === "spam" && row.mailboxState !== "spam"
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
if (intoSpam.length) {
|
|
524
|
+
const reported = fileSpam(
|
|
525
|
+
m,
|
|
526
|
+
options.emailIds
|
|
527
|
+
? { emailIds: options.emailIds }
|
|
528
|
+
: { conversationIds: intoSpam.map((row) => row.id) },
|
|
529
|
+
userId
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
verdicts.push(...reported.verdicts);
|
|
533
|
+
|
|
534
|
+
for (const place of reported.places) places.set(place.from, place);
|
|
535
|
+
|
|
536
|
+
for (const row of intoSpam) filed.add(row.id);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Out of Spam into Inbox or Archive: Not spam (`leaveSpam`).
|
|
540
|
+
const out = LEFT_SPAM.has(mailboxState)
|
|
541
|
+
? found.filter((row) => row.mailboxState === "spam")
|
|
542
|
+
: [];
|
|
543
|
+
|
|
544
|
+
if (out.length) {
|
|
545
|
+
const left = leaveSpam(
|
|
546
|
+
m,
|
|
547
|
+
out.map((row) => row.id),
|
|
548
|
+
target,
|
|
549
|
+
{
|
|
550
|
+
userId,
|
|
551
|
+
trustSenders: options.trustSenders,
|
|
552
|
+
workspaceBlocks: options.workspaceBlocks,
|
|
553
|
+
beforeMerge: options.beforeMerge,
|
|
554
|
+
}
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
verdicts.push(...left.verdicts);
|
|
558
|
+
|
|
559
|
+
for (const place of left.places) places.set(place.from, place);
|
|
560
|
+
|
|
561
|
+
for (const row of out) filed.add(row.id);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
fileConversations(
|
|
565
|
+
sql,
|
|
566
|
+
found.filter((row) => !filed.has(row.id)).map((row) => row.id),
|
|
567
|
+
target
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
// Mail kept apart rejoins a thread moved into the Inbox or Archive
|
|
571
|
+
// (from Trash, say): the conversation is merged away into it.
|
|
572
|
+
const rejoined = rejoinSplitConversations(
|
|
573
|
+
m,
|
|
574
|
+
found.map((row) => row.id),
|
|
575
|
+
options.beforeMerge
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
// Where the rest are now, and whether they left where they were listed.
|
|
579
|
+
const remaining = found.filter((row) => !places.has(row.id));
|
|
580
|
+
|
|
581
|
+
const foldersAfter = currentFolders(
|
|
582
|
+
m,
|
|
583
|
+
remaining.map((row) => rejoined.get(row.id) ?? row.id)
|
|
584
|
+
);
|
|
585
|
+
|
|
586
|
+
for (const row of remaining) {
|
|
587
|
+
const conversationId = rejoined.get(row.id) ?? row.id;
|
|
588
|
+
|
|
589
|
+
const before = conversationFolder(row);
|
|
590
|
+
const after = foldersAfter.get(conversationId) ?? before;
|
|
591
|
+
|
|
592
|
+
places.set(row.id, {
|
|
593
|
+
from: row.id,
|
|
594
|
+
conversationId,
|
|
595
|
+
folder: after,
|
|
596
|
+
previous: {
|
|
597
|
+
mailboxState: row.mailboxState,
|
|
598
|
+
folderId: row.hasIncoming ? row.folderId : undefined,
|
|
599
|
+
},
|
|
600
|
+
left: after !== before || conversationId !== row.id,
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Archive, Trash and Spam remember a custom folder for the next restore.
|
|
605
|
+
// Undo restores that folder alongside the state in this transaction.
|
|
606
|
+
if (
|
|
607
|
+
options.undo &&
|
|
608
|
+
mailboxState &&
|
|
609
|
+
mailboxState !== "active" &&
|
|
610
|
+
target.folderId !== undefined
|
|
611
|
+
) {
|
|
612
|
+
fileConversations(
|
|
613
|
+
sql,
|
|
614
|
+
[...places.values()].map((place) => place.conversationId),
|
|
615
|
+
{ folderId: target.folderId }
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
moved: found.map((row) => row.id),
|
|
621
|
+
places: [...places.values()],
|
|
622
|
+
verdicts,
|
|
623
|
+
};
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
if (result.moved.length) {
|
|
627
|
+
if (result.verdicts.length) m.forgetParticipants();
|
|
628
|
+
void m.announce();
|
|
629
|
+
|
|
630
|
+
if (isRetentionFolder(mailboxState)) m.scheduleRetention();
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
return result;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Moves received messages to Spam without the rest of their conversations
|
|
638
|
+
* (sent mail, other people's messages), inside the caller's transaction.
|
|
639
|
+
* A conversation in Inbox or Archive that holds only these messages goes
|
|
640
|
+
* to Spam whole; otherwise they leave for a conversation of their own in
|
|
641
|
+
* Spam that remembers the one they came from, which they rejoin once they
|
|
642
|
+
* are not spam (rejoinSplitConversations), as delivery keeps spam apart.
|
|
643
|
+
* Returns, for each conversation that lost messages to Spam, where they
|
|
644
|
+
* are now (`ConversationPlace`).
|
|
645
|
+
*/
|
|
646
|
+
export function moveMessagesToSpam(
|
|
647
|
+
m: MailboxContext,
|
|
648
|
+
emailIds: readonly string[]
|
|
649
|
+
) {
|
|
650
|
+
const { sql } = m.state.storage;
|
|
651
|
+
|
|
652
|
+
const idsQuery = sqlTemplate`(select value from json_each(${stringListJson([...emailIds])}))`;
|
|
653
|
+
|
|
654
|
+
const selected = m.db.$with("selected").as(
|
|
655
|
+
m.db
|
|
656
|
+
.select({
|
|
657
|
+
id: emails.id,
|
|
658
|
+
conversationId: emails.conversationId,
|
|
659
|
+
mailboxState: conversations.mailboxState,
|
|
660
|
+
folderId: conversations.folderId,
|
|
661
|
+
})
|
|
662
|
+
.from(emails)
|
|
663
|
+
.innerJoin(conversations, eq(conversations.id, emails.conversationId))
|
|
664
|
+
.where(
|
|
665
|
+
and(
|
|
666
|
+
sqlTemplate`${emails.id} in ${idsQuery}`,
|
|
667
|
+
eq(emails.direction, "incoming"),
|
|
668
|
+
inArray(conversations.mailboxState, ["active", "archive", "trash"])
|
|
669
|
+
)
|
|
670
|
+
)
|
|
671
|
+
);
|
|
672
|
+
|
|
673
|
+
// Count the mail left behind once per affected conversation, even for a bulk move.
|
|
674
|
+
const remaining = m.db.$with("remaining").as(
|
|
675
|
+
m.db
|
|
676
|
+
.select({
|
|
677
|
+
conversationId: emails.conversationId,
|
|
678
|
+
hasIncoming:
|
|
679
|
+
sqlTemplate<number>`max(${emails.direction} = 'incoming')`.as(
|
|
680
|
+
"remaining_has_incoming"
|
|
681
|
+
),
|
|
682
|
+
})
|
|
683
|
+
.from(emails)
|
|
684
|
+
.where(
|
|
685
|
+
and(
|
|
686
|
+
inArray(
|
|
687
|
+
emails.conversationId,
|
|
688
|
+
m.db.select({ id: selected.conversationId }).from(selected)
|
|
689
|
+
),
|
|
690
|
+
sqlTemplate`${emails.id} not in ${idsQuery}`
|
|
691
|
+
)
|
|
692
|
+
)
|
|
693
|
+
.groupBy(emails.conversationId)
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
const rows = m.db
|
|
697
|
+
.with(selected, remaining)
|
|
698
|
+
.select({
|
|
699
|
+
id: selected.id,
|
|
700
|
+
conversationId: selected.conversationId,
|
|
701
|
+
mailboxState: selected.mailboxState,
|
|
702
|
+
folderId: selected.folderId,
|
|
703
|
+
remainingHasIncoming: remaining.hasIncoming,
|
|
704
|
+
})
|
|
705
|
+
.from(selected)
|
|
706
|
+
.leftJoin(remaining, eq(remaining.conversationId, selected.conversationId))
|
|
707
|
+
.all();
|
|
708
|
+
|
|
709
|
+
const byConversation = new Map<string, typeof rows>();
|
|
710
|
+
|
|
711
|
+
for (const row of rows) {
|
|
712
|
+
const group = byConversation.get(row.conversationId);
|
|
713
|
+
|
|
714
|
+
if (group) group.push(row);
|
|
715
|
+
else byConversation.set(row.conversationId, [row]);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const now = Date.now();
|
|
719
|
+
const places: ConversationPlace[] = [];
|
|
720
|
+
|
|
721
|
+
for (const [conversationId, messages] of byConversation) {
|
|
722
|
+
const [first] = messages;
|
|
723
|
+
|
|
724
|
+
if (!first) continue;
|
|
725
|
+
|
|
726
|
+
const previous = {
|
|
727
|
+
mailboxState: first.mailboxState,
|
|
728
|
+
folderId: first.folderId,
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
if (first.remainingHasIncoming === null) {
|
|
732
|
+
// Its folder stays for when it comes back.
|
|
733
|
+
fileConversations(sql, [conversationId], { mailboxState: "spam" });
|
|
734
|
+
places.push({
|
|
735
|
+
from: conversationId,
|
|
736
|
+
conversationId,
|
|
737
|
+
previous,
|
|
738
|
+
folder: "spam",
|
|
739
|
+
left: true,
|
|
740
|
+
});
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const spamId = createId("conv");
|
|
745
|
+
const ids = stringListJson(messages.map((message) => message.id));
|
|
746
|
+
|
|
747
|
+
sql.exec(
|
|
748
|
+
`insert into conversations (
|
|
749
|
+
id, latest_email_id, timeline_at, mailbox_state, folder_id, state_changed_at
|
|
750
|
+
) values (?, ?, 0, 'spam', null, ?)`,
|
|
751
|
+
spamId,
|
|
752
|
+
first.id,
|
|
753
|
+
now
|
|
754
|
+
);
|
|
755
|
+
sql.exec(
|
|
756
|
+
`update emails set conversation_id = ?, split_from_conversation_id = ?
|
|
757
|
+
where id in (select cast(value as text) from json_each(?))`,
|
|
758
|
+
spamId,
|
|
759
|
+
conversationId,
|
|
760
|
+
ids
|
|
761
|
+
);
|
|
762
|
+
sql.exec(
|
|
763
|
+
`update email_search set conversation_id = ?
|
|
764
|
+
where email_id in (select cast(value as text) from json_each(?))`,
|
|
765
|
+
spamId,
|
|
766
|
+
ids
|
|
767
|
+
);
|
|
768
|
+
|
|
769
|
+
// The editor reopens a draft in its source message's thread. An already
|
|
770
|
+
// open editor must reload before saving the old thread back over this one.
|
|
771
|
+
m.db
|
|
772
|
+
.update(drafts)
|
|
773
|
+
.set({
|
|
774
|
+
conversationId: spamId,
|
|
775
|
+
version: sqlTemplate`${drafts.version} + 1`,
|
|
776
|
+
})
|
|
777
|
+
.where(
|
|
778
|
+
inJsonList(
|
|
779
|
+
drafts.sourceEmailId,
|
|
780
|
+
messages.map((message) => message.id)
|
|
781
|
+
)
|
|
782
|
+
)
|
|
783
|
+
.run();
|
|
784
|
+
|
|
785
|
+
refreshConversation(sql, spamId);
|
|
786
|
+
refreshConversation(sql, conversationId);
|
|
787
|
+
|
|
788
|
+
places.push({
|
|
789
|
+
from: conversationId,
|
|
790
|
+
conversationId: spamId,
|
|
791
|
+
previous,
|
|
792
|
+
folder: "spam",
|
|
793
|
+
// An active thread that now holds only outgoing mail moves to Sent.
|
|
794
|
+
left:
|
|
795
|
+
conversationFolder({
|
|
796
|
+
...previous,
|
|
797
|
+
hasIncoming: Boolean(first.remainingHasIncoming),
|
|
798
|
+
}) !== conversationFolder(previous),
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
return places;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Puts mail kept out of a conversation as spam (#25) back into it, once
|
|
807
|
+
* its own conversation has left Spam: by Not spam, a move in the app or
|
|
808
|
+
* over JMAP, or a reply sent from here. Call it inside the transaction
|
|
809
|
+
* that moved `conversationIds`; only those now in Inbox or Archive that
|
|
810
|
+
* hold a message marked with `split_from_conversation_id` are touched, so
|
|
811
|
+
* conversations threaded elsewhere (an import's, say) are never merged.
|
|
812
|
+
*
|
|
813
|
+
* The marked message names the conversation it was kept out of. When that
|
|
814
|
+
* conversation is gone, the message's own threading headers are resolved
|
|
815
|
+
* again. The conversation rejoins its target when the target is in Inbox
|
|
816
|
+
* or Archive; a target in Trash or Spam is not brought back by it, and the
|
|
817
|
+
* conversation stays where it was moved. Either way the decision is final
|
|
818
|
+
* and the mark is cleared.
|
|
819
|
+
*
|
|
820
|
+
* The rescued message is placed as a legitimate reply would be: moved to
|
|
821
|
+
* Inbox (or a custom folder), the target becomes active and takes that
|
|
822
|
+
* folder (else keeps its own). Moved to Archive, the target stays where it
|
|
823
|
+
* is. Returns, for each merged conversation, the one its messages are in.
|
|
824
|
+
*/
|
|
825
|
+
export function rejoinSplitConversations(
|
|
826
|
+
m: MailboxContext,
|
|
827
|
+
conversationIds: string[],
|
|
828
|
+
beforeMerge?: (target: string) => void
|
|
829
|
+
) {
|
|
830
|
+
const merged = new Map<string, string>();
|
|
831
|
+
|
|
832
|
+
if (!conversationIds.length) return merged;
|
|
833
|
+
|
|
834
|
+
const marked = m.db
|
|
835
|
+
.select({
|
|
836
|
+
emailId: emails.id,
|
|
837
|
+
conversationId: conversations.id,
|
|
838
|
+
mailboxState: conversations.mailboxState,
|
|
839
|
+
folderId: conversations.folderId,
|
|
840
|
+
splitFrom: emails.splitFromConversationId,
|
|
841
|
+
inReplyTo: emails.inReplyToJson,
|
|
842
|
+
references: emails.referencesJson,
|
|
843
|
+
})
|
|
844
|
+
.from(emails)
|
|
845
|
+
.innerJoin(conversations, eq(conversations.id, emails.conversationId))
|
|
846
|
+
.where(
|
|
847
|
+
and(
|
|
848
|
+
inJsonList(conversations.id, conversationIds),
|
|
849
|
+
inArray(conversations.mailboxState, ["active", "archive"]),
|
|
850
|
+
isNotNull(emails.splitFromConversationId)
|
|
851
|
+
)
|
|
852
|
+
)
|
|
853
|
+
.all();
|
|
854
|
+
|
|
855
|
+
const groups = new Map<string, typeof marked>();
|
|
856
|
+
|
|
857
|
+
for (const row of marked) {
|
|
858
|
+
groups.set(row.conversationId, [
|
|
859
|
+
...(groups.get(row.conversationId) ?? []),
|
|
860
|
+
row,
|
|
861
|
+
]);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
for (const [conversationId, rows] of groups) {
|
|
865
|
+
const targets = new Set(
|
|
866
|
+
rows.map((row) => splitTarget(m, conversationId, row))
|
|
867
|
+
);
|
|
868
|
+
|
|
869
|
+
const [target, ...others] = targets;
|
|
870
|
+
|
|
871
|
+
m.db
|
|
872
|
+
.update(emails)
|
|
873
|
+
.set({ splitFromConversationId: null })
|
|
874
|
+
.where(
|
|
875
|
+
and(
|
|
876
|
+
eq(emails.conversationId, conversationId),
|
|
877
|
+
isNotNull(emails.splitFromConversationId)
|
|
878
|
+
)
|
|
879
|
+
)
|
|
880
|
+
.run();
|
|
881
|
+
|
|
882
|
+
if (!target || others.length) continue;
|
|
883
|
+
const [moved] = rows;
|
|
884
|
+
|
|
885
|
+
if (!moved) continue;
|
|
886
|
+
beforeMerge?.(target);
|
|
887
|
+
mergeConversation(m.state.storage.sql, conversationId, target);
|
|
888
|
+
|
|
889
|
+
if (moved.mailboxState === "active") {
|
|
890
|
+
fileConversations(m.state.storage.sql, [target], {
|
|
891
|
+
mailboxState: "active",
|
|
892
|
+
folderId: moved.folderId ?? undefined,
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
merged.set(conversationId, target);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
return merged;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* The conversation a marked message rejoins, or null: the one it was kept
|
|
904
|
+
* out of, else (when that one is gone) its parent's, while that
|
|
905
|
+
* conversation is in Inbox or Archive.
|
|
906
|
+
*/
|
|
907
|
+
function splitTarget(
|
|
908
|
+
m: MailboxContext,
|
|
909
|
+
conversationId: string,
|
|
910
|
+
row: { splitFrom: string | null; inReplyTo: string[]; references: string[] }
|
|
911
|
+
) {
|
|
912
|
+
const placeOf = (id: string) =>
|
|
913
|
+
m.db
|
|
914
|
+
.select({ mailboxState: conversations.mailboxState })
|
|
915
|
+
.from(conversations)
|
|
916
|
+
.where(eq(conversations.id, id))
|
|
917
|
+
.all()[0];
|
|
918
|
+
|
|
919
|
+
let targetId = row.splitFrom;
|
|
920
|
+
let target = targetId ? placeOf(targetId) : undefined;
|
|
921
|
+
|
|
922
|
+
if (!target) {
|
|
923
|
+
const parent = m.resolveParent(row.inReplyTo, row.references);
|
|
924
|
+
targetId = parent?.conversationId ?? null;
|
|
925
|
+
target = targetId ? placeOf(targetId) : undefined;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
return targetId &&
|
|
929
|
+
targetId !== conversationId &&
|
|
930
|
+
(target?.mailboxState === "active" || target?.mailboxState === "archive")
|
|
931
|
+
? targetId
|
|
932
|
+
: null;
|
|
933
|
+
}
|