@open-workspace/email 0.4.2 → 0.5.1
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-sender-intel.ts +5 -1
- 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 +52 -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 +391 -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 +12 -11
- 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 +758 -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 +119 -1816
- 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 +700 -0
- package/worker/mail/auth-headers.ts +57 -43
- package/worker/mail/auto-reply-routes.ts +260 -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-threading.ts +15 -2
- 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 +19 -8
- 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 +698 -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 +206 -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 +316 -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 +455 -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 +253 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +161 -1
- 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
package/worker/mailbox/demo.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
hirelineHtml,
|
|
15
15
|
} from "./demo-html";
|
|
16
16
|
import type { EmailAiClassification, MailAddress } from "./model";
|
|
17
|
-
import type { NewEmail
|
|
17
|
+
import type { NewEmail } from "./schema";
|
|
18
18
|
|
|
19
19
|
type DemoInput = {
|
|
20
20
|
id: string;
|
|
@@ -336,10 +336,7 @@ function demoRawMime(input: DemoInput, now: number) {
|
|
|
336
336
|
].join("\r\n");
|
|
337
337
|
}
|
|
338
338
|
|
|
339
|
-
function demoSenderIntel(
|
|
340
|
-
input: DemoInput,
|
|
341
|
-
checkedAt: number
|
|
342
|
-
): SenderIntel | null {
|
|
339
|
+
function demoSenderIntel(input: DemoInput, checkedAt: number) {
|
|
343
340
|
const { checks } = input;
|
|
344
341
|
const intel = checks?.intel;
|
|
345
342
|
|
|
@@ -403,7 +400,7 @@ function demoSenderIntel(
|
|
|
403
400
|
bimi: intel.bimi
|
|
404
401
|
? { domain: fromDomain, status: "ok", reason: null }
|
|
405
402
|
: null,
|
|
406
|
-
};
|
|
403
|
+
} satisfies SenderIntel;
|
|
407
404
|
}
|
|
408
405
|
|
|
409
406
|
/** The domain of a demo address; every one has one. */
|
|
@@ -411,7 +408,7 @@ function demoDomain(address: string) {
|
|
|
411
408
|
return required(address.split("@")[1], `${address} has a domain`);
|
|
412
409
|
}
|
|
413
410
|
|
|
414
|
-
function email(input: DemoInput, now: number)
|
|
411
|
+
function email(input: DemoInput, now: number) {
|
|
415
412
|
const timelineAt = new Date(now - input.ageMs);
|
|
416
413
|
const outgoing = input.direction === "outgoing";
|
|
417
414
|
|
|
@@ -476,9 +473,9 @@ function email(input: DemoInput, now: number): NewEmail {
|
|
|
476
473
|
const sam = { address: "sam@demo.example", name: "Sam Brooks" };
|
|
477
474
|
const support = { address: "support@demo.example", name: "Customer care" };
|
|
478
475
|
|
|
479
|
-
function demoInputs(mailboxId: string)
|
|
476
|
+
function demoInputs(mailboxId: string) {
|
|
480
477
|
if (mailboxId === "mbx_demo_personal") {
|
|
481
|
-
|
|
478
|
+
const personalInputs: DemoInput[] = [
|
|
482
479
|
{
|
|
483
480
|
id: "msg_demo_01",
|
|
484
481
|
conversationId: "conv_demo_launch",
|
|
@@ -1030,10 +1027,12 @@ function demoInputs(mailboxId: string): DemoInput[] {
|
|
|
1030
1027
|
},
|
|
1031
1028
|
},
|
|
1032
1029
|
];
|
|
1030
|
+
|
|
1031
|
+
return personalInputs;
|
|
1033
1032
|
}
|
|
1034
1033
|
|
|
1035
1034
|
if (mailboxId === "mbx_demo_support") {
|
|
1036
|
-
|
|
1035
|
+
const supportInputs: DemoInput[] = [
|
|
1037
1036
|
{
|
|
1038
1037
|
id: "msg_demo_08",
|
|
1039
1038
|
conversationId: "conv_demo_customer1",
|
|
@@ -1198,6 +1197,8 @@ function demoInputs(mailboxId: string): DemoInput[] {
|
|
|
1198
1197
|
},
|
|
1199
1198
|
},
|
|
1200
1199
|
];
|
|
1200
|
+
|
|
1201
|
+
return supportInputs;
|
|
1201
1202
|
}
|
|
1202
1203
|
|
|
1203
1204
|
return [];
|
|
@@ -1253,10 +1254,7 @@ export function demoMailboxSenders(mailboxId: string, now = Date.now()) {
|
|
|
1253
1254
|
return [];
|
|
1254
1255
|
}
|
|
1255
1256
|
|
|
1256
|
-
export function demoMailboxEmails(
|
|
1257
|
-
mailboxId: string,
|
|
1258
|
-
now = Date.now()
|
|
1259
|
-
): NewEmail[] {
|
|
1257
|
+
export function demoMailboxEmails(mailboxId: string, now = Date.now()) {
|
|
1260
1258
|
return demoInputs(mailboxId).map((input) => email(input, now));
|
|
1261
1259
|
}
|
|
1262
1260
|
|
|
@@ -1298,7 +1296,7 @@ export function demoMailboxObjects(mailboxId: string, now = Date.now()) {
|
|
|
1298
1296
|
]);
|
|
1299
1297
|
}
|
|
1300
1298
|
|
|
1301
|
-
export function demoMailboxFolders(mailboxId: string)
|
|
1299
|
+
export function demoMailboxFolders(mailboxId: string) {
|
|
1302
1300
|
if (mailboxId !== "mbx_demo_personal") return [];
|
|
1303
1301
|
|
|
1304
1302
|
return [
|
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
import { insertOfficePreviewJobs } from "./office-preview-jobs";
|
|
2
|
+
import { emails } from "./schema";
|
|
3
|
+
import type { NewEmail } from "./schema";
|
|
4
|
+
import { recordCorrespondents } from "./senders";
|
|
5
|
+
import type { MailboxDatabase } from "./support";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Stores one message, the one way every path does (delivery, sending,
|
|
9
|
+
* import, tests): its row, its conversation and search rows, its Office
|
|
10
|
+
* preview jobs and its correspondents. Call it inside a transaction. Null
|
|
11
|
+
* when a message with its id is stored already.
|
|
12
|
+
*/
|
|
13
|
+
export function storeEmail(
|
|
14
|
+
db: Pick<MailboxDatabase, "insert">,
|
|
15
|
+
sql: SqlStorage,
|
|
16
|
+
email: NewEmail
|
|
17
|
+
) {
|
|
18
|
+
const inserted = db
|
|
19
|
+
.insert(emails)
|
|
20
|
+
.values(email)
|
|
21
|
+
.onConflictDoNothing()
|
|
22
|
+
.returning()
|
|
23
|
+
.all()[0];
|
|
24
|
+
|
|
25
|
+
if (!inserted) return null;
|
|
26
|
+
indexInsertedEmail(sql, inserted.id);
|
|
27
|
+
insertOfficePreviewJobs(db, email);
|
|
28
|
+
recordCorrespondents(db, email);
|
|
29
|
+
|
|
30
|
+
return inserted;
|
|
31
|
+
}
|
|
32
|
+
|
|
1
33
|
/**
|
|
2
34
|
* Keeps the conversation projection and the search index in step with one
|
|
3
35
|
* stored message. Call it inside the transaction that inserted the row, right
|
|
@@ -28,9 +60,7 @@ export function indexInsertedEmail(sql: SqlStorage, emailId: string) {
|
|
|
28
60
|
end,
|
|
29
61
|
timeline_at = max(conversations.timeline_at, excluded.timeline_at),
|
|
30
62
|
mailbox_state = case
|
|
31
|
-
when conversations.mailbox_state in ('archive', 'trash')
|
|
32
|
-
or (conversations.mailbox_state = 'spam' and excluded.has_outgoing)
|
|
33
|
-
then 'active'
|
|
63
|
+
when conversations.mailbox_state in ('archive', 'trash') then 'active'
|
|
34
64
|
else conversations.mailbox_state
|
|
35
65
|
end,
|
|
36
66
|
has_incoming = conversations.has_incoming or excluded.has_incoming,
|
|
@@ -38,16 +68,7 @@ export function indexInsertedEmail(sql: SqlStorage, emailId: string) {
|
|
|
38
68
|
emailId
|
|
39
69
|
);
|
|
40
70
|
sql.exec(
|
|
41
|
-
|
|
42
|
-
select id, conversation_id, subject,
|
|
43
|
-
coalesce(from_json, '') || ' '
|
|
44
|
-
|| coalesce(reply_to_json, '') || ' '
|
|
45
|
-
|| coalesce(to_json, '') || ' '
|
|
46
|
-
|| coalesce(cc_json, '') || ' '
|
|
47
|
-
|| coalesce(bcc_json, ''),
|
|
48
|
-
coalesce(body_text, '') || ' ' || coalesce(preview, '')
|
|
49
|
-
from emails
|
|
50
|
-
where id = ?`,
|
|
71
|
+
"insert into email_search select * from email_search_document where email_id = ?",
|
|
51
72
|
emailId
|
|
52
73
|
);
|
|
53
74
|
}
|
|
@@ -74,29 +95,38 @@ export function mergeConversation(
|
|
|
74
95
|
intoId,
|
|
75
96
|
fromId
|
|
76
97
|
);
|
|
98
|
+
// Reply drafts follow their conversation, or they'd name one that's gone.
|
|
77
99
|
sql.exec(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
where message.conversation_id = conversations.id
|
|
82
|
-
order by message.timeline_at desc, message.id desc limit 1
|
|
83
|
-
),
|
|
84
|
-
timeline_at = (
|
|
85
|
-
select max(message.timeline_at) from emails message
|
|
86
|
-
where message.conversation_id = conversations.id
|
|
87
|
-
),
|
|
88
|
-
has_incoming = exists (
|
|
89
|
-
select 1 from emails message
|
|
90
|
-
where message.conversation_id = conversations.id
|
|
91
|
-
and message.direction = 'incoming'
|
|
92
|
-
),
|
|
93
|
-
has_outgoing = exists (
|
|
94
|
-
select 1 from emails message
|
|
95
|
-
where message.conversation_id = conversations.id
|
|
96
|
-
and message.direction = 'outgoing'
|
|
97
|
-
)
|
|
98
|
-
where id = ?`,
|
|
99
|
-
intoId
|
|
100
|
+
"update drafts set conversation_id = ?, version = version + 1 where conversation_id = ?",
|
|
101
|
+
intoId,
|
|
102
|
+
fromId
|
|
100
103
|
);
|
|
104
|
+
refreshConversation(sql, intoId);
|
|
101
105
|
sql.exec("delete from conversations where id = ?", fromId);
|
|
102
106
|
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Recomputes a conversation's latest message, timeline and directions from
|
|
110
|
+
* the messages it holds, after messages moved in or out of it.
|
|
111
|
+
*/
|
|
112
|
+
export function refreshConversation(
|
|
113
|
+
sql: SqlStorage,
|
|
114
|
+
conversationId: string,
|
|
115
|
+
excluding: string[] = []
|
|
116
|
+
) {
|
|
117
|
+
sql.exec(
|
|
118
|
+
`with remaining as (
|
|
119
|
+
select id, timeline_at, direction from emails
|
|
120
|
+
where conversation_id = ? and id not in (select value from json_each(?))
|
|
121
|
+
)
|
|
122
|
+
update conversations set
|
|
123
|
+
latest_email_id = (select id from remaining order by timeline_at desc, id desc limit 1),
|
|
124
|
+
timeline_at = (select max(timeline_at) from remaining),
|
|
125
|
+
has_incoming = exists (select 1 from remaining where direction = 'incoming'),
|
|
126
|
+
has_outgoing = exists (select 1 from remaining where direction = 'outgoing')
|
|
127
|
+
where id = ? and exists (select 1 from remaining)`,
|
|
128
|
+
conversationId,
|
|
129
|
+
JSON.stringify(excluding),
|
|
130
|
+
conversationId
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -44,26 +44,6 @@ export function customFolderPredicate(conversationAlias: string) {
|
|
|
44
44
|
return inboxDistributionPredicate(conversationAlias);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export function folderAggregateJoinPredicate(
|
|
48
|
-
folderAlias: string,
|
|
49
|
-
conversationAlias: string
|
|
50
|
-
) {
|
|
51
|
-
const custom = `(
|
|
52
|
-
${folderAlias}.kind = 'custom'
|
|
53
|
-
and ${conversationAlias}.folder_id = ${folderAlias}.id
|
|
54
|
-
and ${customFolderPredicate(conversationAlias)}
|
|
55
|
-
)`;
|
|
56
|
-
|
|
57
|
-
const system = systemFolderTypes.map(
|
|
58
|
-
(systemType) => `(
|
|
59
|
-
${folderAlias}.system_type = '${systemType}'
|
|
60
|
-
and ${systemFolderPredicate(systemType, conversationAlias)}
|
|
61
|
-
)`
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
return [custom, ...system].join("\n or ");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
47
|
/**
|
|
68
48
|
* System folders are views over conversations, not stored folder records.
|
|
69
49
|
* Only user-defined classification folders live in SQLite, with the places
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ImportPlacement } from "../../shared/mail-import";
|
|
2
2
|
import type { MailboxState } from "./folder-model";
|
|
3
|
+
import { placementRank } from "./placement";
|
|
3
4
|
|
|
4
5
|
/** A conversation's place: its state, and its custom folder while it is in the Inbox. */
|
|
5
6
|
export type ConversationPlacement = {
|
|
@@ -7,59 +8,43 @@ export type ConversationPlacement = {
|
|
|
7
8
|
folderId: string | null;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
/**
|
|
11
|
-
* How strongly a place holds a conversation when imported messages disagree
|
|
12
|
-
* (Gmail labels each message, a conversation here has one place): Trash,
|
|
13
|
-
* then Spam, then a custom folder, then the Inbox, then Archive. A sent
|
|
14
|
-
* message on its own has no say.
|
|
15
|
-
*/
|
|
11
|
+
/** A place's strength (`placementRank`); a sent message on its own has no say. */
|
|
16
12
|
function rank(placement: ConversationPlacement | null) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
switch (placement.mailboxState) {
|
|
20
|
-
case "trash": {
|
|
21
|
-
return 5;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
case "spam": {
|
|
25
|
-
return 4;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
case "active": {
|
|
29
|
-
return placement.folderId ? 3 : 2;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
case "archive": {
|
|
33
|
-
return 1;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
default: {
|
|
37
|
-
const unexpected: never = placement.mailboxState;
|
|
38
|
-
throw new Error(`Unknown mailbox state ${String(unexpected)}`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
13
|
+
return placement ? placementRank(placement) : 0;
|
|
41
14
|
}
|
|
42
15
|
|
|
43
16
|
/** The place an imported message asks for, or null for none (`sent`). */
|
|
44
17
|
export function requestedPlacement(
|
|
45
18
|
placement: ImportPlacement,
|
|
46
19
|
folderId: string | null
|
|
47
|
-
)
|
|
20
|
+
) {
|
|
48
21
|
switch (placement) {
|
|
49
22
|
case "inbox": {
|
|
50
|
-
return {
|
|
23
|
+
return {
|
|
24
|
+
mailboxState: "active",
|
|
25
|
+
folderId,
|
|
26
|
+
} satisfies ConversationPlacement;
|
|
51
27
|
}
|
|
52
28
|
|
|
53
29
|
case "archive": {
|
|
54
|
-
return {
|
|
30
|
+
return {
|
|
31
|
+
mailboxState: "archive",
|
|
32
|
+
folderId: null,
|
|
33
|
+
} satisfies ConversationPlacement;
|
|
55
34
|
}
|
|
56
35
|
|
|
57
36
|
case "spam": {
|
|
58
|
-
return {
|
|
37
|
+
return {
|
|
38
|
+
mailboxState: "spam",
|
|
39
|
+
folderId: null,
|
|
40
|
+
} satisfies ConversationPlacement;
|
|
59
41
|
}
|
|
60
42
|
|
|
61
43
|
case "trash": {
|
|
62
|
-
return {
|
|
44
|
+
return {
|
|
45
|
+
mailboxState: "trash",
|
|
46
|
+
folderId: null,
|
|
47
|
+
} satisfies ConversationPlacement;
|
|
63
48
|
}
|
|
64
49
|
|
|
65
50
|
case "sent": {
|
|
@@ -81,7 +66,7 @@ export function requestedPlacement(
|
|
|
81
66
|
export function mergedPlacement(
|
|
82
67
|
before: ConversationPlacement | null,
|
|
83
68
|
requested: ConversationPlacement | null
|
|
84
|
-
)
|
|
69
|
+
) {
|
|
85
70
|
if (!before) return requested;
|
|
86
71
|
|
|
87
72
|
return rank(requested) > rank(before) ? requested : before;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where an incoming message goes: one decision from the sender's screening,
|
|
3
|
+
* the mailbox's rules, AI sorting and the conversation it names, which the
|
|
4
|
+
* mailbox then applies as it stores the message.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type MailboxState = "active" | "archive" | "spam" | "trash";
|
|
8
|
+
|
|
9
|
+
export type DeliveryInput = {
|
|
10
|
+
/** A block (the mailbox's or the workspace's) on the sender. */
|
|
11
|
+
blocked: boolean;
|
|
12
|
+
/** The sender is marked trusted here. */
|
|
13
|
+
trusted: boolean;
|
|
14
|
+
/** DMARC verified the From address, so it can't be forged. */
|
|
15
|
+
authenticated: boolean;
|
|
16
|
+
/** The last applicable placement, and the rules' other actions. */
|
|
17
|
+
rules: {
|
|
18
|
+
placement:
|
|
19
|
+
| { type: "spam" | "trash" | "archive" }
|
|
20
|
+
| { type: "folder"; folderId: string }
|
|
21
|
+
| null;
|
|
22
|
+
notSpam: boolean;
|
|
23
|
+
markRead: boolean;
|
|
24
|
+
} | null;
|
|
25
|
+
/** AI sorting's verdict; its folder only when that folder still exists. */
|
|
26
|
+
classification: { spam: boolean; folderId: string | null } | null;
|
|
27
|
+
/** The conversation the message's threading headers name, if it exists. */
|
|
28
|
+
parent: { state: MailboxState; folderId: string | null } | null;
|
|
29
|
+
/**
|
|
30
|
+
* The message would be the conversation's first incoming one, e.g. a
|
|
31
|
+
* reply to a thread this mailbox started: it is filed like new mail.
|
|
32
|
+
*/
|
|
33
|
+
startsConversation: boolean;
|
|
34
|
+
/** Recheck at insert time in case the folder was deleted while awaiting AI. */
|
|
35
|
+
folderExists: (folderId: string) => boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type DeliveryDecision = {
|
|
39
|
+
/**
|
|
40
|
+
* The message starts its own conversation instead of joining the one it
|
|
41
|
+
* names: that one is somewhere this message must not take it (or pull it
|
|
42
|
+
* from), e.g. spam naming a thread in Inbox.
|
|
43
|
+
*/
|
|
44
|
+
split: boolean;
|
|
45
|
+
state: MailboxState;
|
|
46
|
+
folderId: string | null;
|
|
47
|
+
/** Read for everyone with access (a rule's "Mark read"). */
|
|
48
|
+
markRead: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Blocks win over rules and AI. A rule's Spam wins over AI; an AI spam
|
|
53
|
+
* verdict holds unless the sender is trusted (and authenticated, since the
|
|
54
|
+
* From address is the sender's to choose) or a rule says "never Spam".
|
|
55
|
+
*
|
|
56
|
+
* A message joins the conversation it names unless that would move it
|
|
57
|
+
* between Spam or Trash and the rest; then it starts its own (recording
|
|
58
|
+
* the one it named, to rejoin when it leaves Spam). Joining a received
|
|
59
|
+
* thread leaves it where it is, except that new mail brings a thread back
|
|
60
|
+
* from Archive or Trash, unless an Archive rule matches (a notification
|
|
61
|
+
* thread stays archived). A thread's first incoming message is filed like
|
|
62
|
+
* new mail.
|
|
63
|
+
*/
|
|
64
|
+
export function decideDelivery(input: DeliveryInput) {
|
|
65
|
+
const rule = input.rules?.placement ?? null;
|
|
66
|
+
const trusted = input.trusted && input.authenticated && !input.blocked;
|
|
67
|
+
|
|
68
|
+
const spam =
|
|
69
|
+
input.blocked ||
|
|
70
|
+
rule?.type === "spam" ||
|
|
71
|
+
(Boolean(input.classification?.spam) && !trusted && !input.rules?.notSpam);
|
|
72
|
+
|
|
73
|
+
const { parent } = input;
|
|
74
|
+
const markRead = Boolean(input.rules?.markRead) && !spam;
|
|
75
|
+
|
|
76
|
+
const decided = (
|
|
77
|
+
state: MailboxState,
|
|
78
|
+
folderId: string | null,
|
|
79
|
+
split: boolean
|
|
80
|
+
) => ({ split, state, folderId, markRead });
|
|
81
|
+
|
|
82
|
+
if (spam || rule?.type === "trash") {
|
|
83
|
+
const state = spam ? "spam" : "trash";
|
|
84
|
+
|
|
85
|
+
// Spam may join spam, and Trash Trash; anything else stays apart.
|
|
86
|
+
return decided(state, null, Boolean(parent && parent.state !== state));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Not spam: a thread in Spam isn't brought into Inbox by it, nor is this
|
|
90
|
+
// message hidden in Spam with it.
|
|
91
|
+
if (parent?.state === "spam") return decided("active", null, true);
|
|
92
|
+
|
|
93
|
+
// A rule's folder, else AI sorting's (asked only when no rule placed it).
|
|
94
|
+
const folder =
|
|
95
|
+
rule?.type === "folder"
|
|
96
|
+
? input.folderExists(rule.folderId)
|
|
97
|
+
? rule.folderId
|
|
98
|
+
: null
|
|
99
|
+
: (input.classification?.folderId ?? null);
|
|
100
|
+
|
|
101
|
+
if (!parent || input.startsConversation) {
|
|
102
|
+
if (rule?.type === "archive") return decided("archive", null, false);
|
|
103
|
+
|
|
104
|
+
// Filed only if the thread isn't in a folder already.
|
|
105
|
+
return decided("active", parent?.folderId ?? folder, false);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (parent.state === "archive" && rule?.type === "archive") {
|
|
109
|
+
return decided("archive", null, false);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Where the thread is, brought back from Archive or Trash.
|
|
113
|
+
return decided("active", parent.folderId, false);
|
|
114
|
+
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
import { createDb } from "@open-workspace/core/module";
|
|
2
|
+
import { and, count, eq, sql } from "drizzle-orm";
|
|
3
|
+
|
|
4
|
+
import { mailboxCounts } from "../db/schema";
|
|
1
5
|
import { systemFolderPredicate } from "./folder-model";
|
|
6
|
+
import { emails, conversations, emailReadStates } from "./schema";
|
|
7
|
+
import type { MailboxDatabase } from "./support";
|
|
2
8
|
|
|
3
9
|
/**
|
|
4
10
|
* A mailbox's inbox counts as D1 keeps them (see `mailboxCounts` in
|
|
@@ -10,38 +16,52 @@ export type InboxCounts = {
|
|
|
10
16
|
readCounts: Map<string, number>;
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
const
|
|
19
|
+
const incomingInbox = and(
|
|
20
|
+
eq(emails.direction, "incoming"),
|
|
21
|
+
sql.raw(systemFolderPredicate("inbox", "conversations"))
|
|
22
|
+
);
|
|
14
23
|
|
|
15
24
|
/** The inbox counts from the mailbox's own SQLite. */
|
|
16
|
-
export function readInboxCounts(
|
|
17
|
-
const incoming =
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
where message.direction = 'incoming' and ${INBOX}
|
|
33
|
-
group by read_state.user_id
|
|
34
|
-
`)
|
|
35
|
-
.toArray();
|
|
25
|
+
export function readInboxCounts(db: MailboxDatabase) {
|
|
26
|
+
const [incoming] = db
|
|
27
|
+
.select({ total: count() })
|
|
28
|
+
.from(emails)
|
|
29
|
+
.innerJoin(conversations, eq(conversations.id, emails.conversationId))
|
|
30
|
+
.where(incomingInbox)
|
|
31
|
+
.all();
|
|
32
|
+
|
|
33
|
+
const reads = db
|
|
34
|
+
.select({ userId: emailReadStates.userId, total: count() })
|
|
35
|
+
.from(emailReadStates)
|
|
36
|
+
.innerJoin(emails, eq(emails.id, emailReadStates.emailId))
|
|
37
|
+
.innerJoin(conversations, eq(conversations.id, emails.conversationId))
|
|
38
|
+
.where(incomingInbox)
|
|
39
|
+
.groupBy(emailReadStates.userId)
|
|
40
|
+
.all();
|
|
36
41
|
|
|
37
42
|
return {
|
|
38
|
-
incomingCount:
|
|
39
|
-
readCounts: new Map(
|
|
40
|
-
reads.map((row) => [row.user_id, Number(row.read_count)])
|
|
41
|
-
),
|
|
43
|
+
incomingCount: incoming.total,
|
|
44
|
+
readCounts: new Map(reads.map((row) => [row.userId, row.total])),
|
|
42
45
|
};
|
|
43
46
|
}
|
|
44
47
|
|
|
48
|
+
/** A web-visible change outside the inbox still advances the version clients compare. */
|
|
49
|
+
export async function advanceInboxCountsVersion(
|
|
50
|
+
binding: D1Database,
|
|
51
|
+
mailboxId: string,
|
|
52
|
+
version: number
|
|
53
|
+
) {
|
|
54
|
+
const session = binding.withSession("first-primary");
|
|
55
|
+
|
|
56
|
+
const result = await createDb(session)
|
|
57
|
+
.update(mailboxCounts)
|
|
58
|
+
.set({ version, updatedAt: new Date() })
|
|
59
|
+
.where(eq(mailboxCounts.mailboxId, mailboxId))
|
|
60
|
+
.run();
|
|
61
|
+
|
|
62
|
+
return result.meta.changes ? { bookmark: session.getBookmark() } : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
45
65
|
/**
|
|
46
66
|
* Writes the counts to D1 in one batch on the primary and returns the
|
|
47
67
|
* session bookmark after it (null where D1 has none, e.g. locally). A
|