@open-workspace/email 0.3.0 → 0.4.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 +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the conversation projection and the search index in step with one
|
|
3
|
+
* stored message. Call it inside the transaction that inserted the row, right
|
|
4
|
+
* after the insert: it reads the row as stored (column defaults included), so
|
|
5
|
+
* the conversation and the `email_search` row commit or roll back with it.
|
|
6
|
+
*/
|
|
7
|
+
export function indexInsertedEmail(sql: SqlStorage, emailId: string) {
|
|
8
|
+
// A new message moves its conversation's latest message and timeline forward,
|
|
9
|
+
// brings it back from Archive or Trash (and from Spam when it is a reply sent
|
|
10
|
+
// from here), and marks which directions it has seen.
|
|
11
|
+
sql.exec(
|
|
12
|
+
`insert into conversations (
|
|
13
|
+
id, latest_email_id, timeline_at, mailbox_state, folder_id, has_incoming, has_outgoing
|
|
14
|
+
)
|
|
15
|
+
select conversation_id, id, timeline_at, 'active', null,
|
|
16
|
+
direction = 'incoming', direction = 'outgoing'
|
|
17
|
+
from emails
|
|
18
|
+
where id = ?
|
|
19
|
+
on conflict (id) do update set
|
|
20
|
+
latest_email_id = case
|
|
21
|
+
when excluded.timeline_at > conversations.timeline_at
|
|
22
|
+
or (
|
|
23
|
+
excluded.timeline_at = conversations.timeline_at
|
|
24
|
+
and excluded.latest_email_id > conversations.latest_email_id
|
|
25
|
+
)
|
|
26
|
+
then excluded.latest_email_id
|
|
27
|
+
else conversations.latest_email_id
|
|
28
|
+
end,
|
|
29
|
+
timeline_at = max(conversations.timeline_at, excluded.timeline_at),
|
|
30
|
+
mailbox_state = case
|
|
31
|
+
when conversations.mailbox_state in ('archive', 'trash')
|
|
32
|
+
or (conversations.mailbox_state = 'spam' and excluded.has_outgoing)
|
|
33
|
+
then 'active'
|
|
34
|
+
else conversations.mailbox_state
|
|
35
|
+
end,
|
|
36
|
+
has_incoming = conversations.has_incoming or excluded.has_incoming,
|
|
37
|
+
has_outgoing = conversations.has_outgoing or excluded.has_outgoing`,
|
|
38
|
+
emailId
|
|
39
|
+
);
|
|
40
|
+
sql.exec(
|
|
41
|
+
`insert into email_search (email_id, conversation_id, subject, addresses, body)
|
|
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 = ?`,
|
|
51
|
+
emailId
|
|
52
|
+
);
|
|
53
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "../../shared/mail";
|
|
1
|
+
import { DEFAULT_SYSTEM_FOLDER_ORDER } from "../../shared/folder-order";
|
|
2
|
+
|
|
3
|
+
export { mailboxStates, type MailboxState } from "../../shared/mail";
|
|
5
4
|
|
|
6
5
|
export const systemFolderTypes = [
|
|
7
6
|
"inbox",
|
|
@@ -10,8 +9,14 @@ export const systemFolderTypes = [
|
|
|
10
9
|
"spam",
|
|
11
10
|
"trash",
|
|
12
11
|
] as const;
|
|
12
|
+
|
|
13
13
|
export type SystemFolderType = (typeof systemFolderTypes)[number];
|
|
14
14
|
|
|
15
|
+
/** Where a system folder sits until someone moves it; every one has a default. */
|
|
16
|
+
export function defaultSystemFolderOrder(systemType: SystemFolderType) {
|
|
17
|
+
return DEFAULT_SYSTEM_FOLDER_ORDER[systemType];
|
|
18
|
+
}
|
|
19
|
+
|
|
15
20
|
function inboxDistributionPredicate(conversationAlias: string) {
|
|
16
21
|
return `${conversationAlias}.has_incoming = 1 and ${conversationAlias}.mailbox_state = 'active'`;
|
|
17
22
|
}
|
|
@@ -21,7 +26,8 @@ const systemFolderPredicateBuilders: Record<
|
|
|
21
26
|
(conversationAlias: string) => string
|
|
22
27
|
> = {
|
|
23
28
|
inbox: inboxDistributionPredicate,
|
|
24
|
-
sent: (alias) =>
|
|
29
|
+
sent: (alias) =>
|
|
30
|
+
`${alias}.has_outgoing = 1 and ${alias}.mailbox_state <> 'trash'`,
|
|
25
31
|
archive: (alias) => `${alias}.mailbox_state = 'archive'`,
|
|
26
32
|
spam: (alias) => `${alias}.mailbox_state = 'spam'`,
|
|
27
33
|
trash: (alias) => `${alias}.mailbox_state = 'trash'`,
|
|
@@ -29,7 +35,7 @@ const systemFolderPredicateBuilders: Record<
|
|
|
29
35
|
|
|
30
36
|
export function systemFolderPredicate(
|
|
31
37
|
systemType: SystemFolderType,
|
|
32
|
-
conversationAlias: string
|
|
38
|
+
conversationAlias: string
|
|
33
39
|
) {
|
|
34
40
|
return systemFolderPredicateBuilders[systemType](conversationAlias);
|
|
35
41
|
}
|
|
@@ -40,33 +46,63 @@ export function customFolderPredicate(conversationAlias: string) {
|
|
|
40
46
|
|
|
41
47
|
export function folderAggregateJoinPredicate(
|
|
42
48
|
folderAlias: string,
|
|
43
|
-
conversationAlias: string
|
|
49
|
+
conversationAlias: string
|
|
44
50
|
) {
|
|
45
51
|
const custom = `(
|
|
46
52
|
${folderAlias}.kind = 'custom'
|
|
47
53
|
and ${conversationAlias}.folder_id = ${folderAlias}.id
|
|
48
54
|
and ${customFolderPredicate(conversationAlias)}
|
|
49
55
|
)`;
|
|
50
|
-
|
|
56
|
+
|
|
57
|
+
const system = systemFolderTypes.map(
|
|
58
|
+
(systemType) => `(
|
|
51
59
|
${folderAlias}.system_type = '${systemType}'
|
|
52
60
|
and ${systemFolderPredicate(systemType, conversationAlias)}
|
|
53
|
-
)`
|
|
61
|
+
)`
|
|
62
|
+
);
|
|
63
|
+
|
|
54
64
|
return [custom, ...system].join("\n or ");
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
/**
|
|
58
68
|
* System folders are views over conversations, not stored folder records.
|
|
59
|
-
* Only user-defined classification folders live in SQLite
|
|
69
|
+
* Only user-defined classification folders live in SQLite, with the places
|
|
70
|
+
* system folders were moved to in the folder order. These are the defaults.
|
|
60
71
|
*/
|
|
61
72
|
export const systemFolderDefinitions = [
|
|
62
|
-
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
{
|
|
74
|
+
id: "inbox",
|
|
75
|
+
name: "Inbox",
|
|
76
|
+
systemType: "inbox",
|
|
77
|
+
sortOrder: defaultSystemFolderOrder("inbox"),
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "sent",
|
|
81
|
+
name: "Sent",
|
|
82
|
+
systemType: "sent",
|
|
83
|
+
sortOrder: defaultSystemFolderOrder("sent"),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: "archive",
|
|
87
|
+
name: "Archive",
|
|
88
|
+
systemType: "archive",
|
|
89
|
+
sortOrder: defaultSystemFolderOrder("archive"),
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "spam",
|
|
93
|
+
name: "Spam",
|
|
94
|
+
systemType: "spam",
|
|
95
|
+
sortOrder: defaultSystemFolderOrder("spam"),
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: "trash",
|
|
99
|
+
name: "Trash",
|
|
100
|
+
systemType: "trash",
|
|
101
|
+
sortOrder: defaultSystemFolderOrder("trash"),
|
|
102
|
+
},
|
|
103
|
+
] as const satisfies readonly {
|
|
68
104
|
id: string;
|
|
69
105
|
name: string;
|
|
70
106
|
systemType: SystemFolderType;
|
|
71
107
|
sortOrder: number;
|
|
72
|
-
}
|
|
108
|
+
}[];
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ImportPlacement } from "../../shared/mail-import";
|
|
2
|
+
import type { MailboxState } from "./folder-model";
|
|
3
|
+
|
|
4
|
+
/** A conversation's place: its state, and its custom folder while it is in the Inbox. */
|
|
5
|
+
export type ConversationPlacement = {
|
|
6
|
+
mailboxState: MailboxState;
|
|
7
|
+
folderId: string | null;
|
|
8
|
+
};
|
|
9
|
+
|
|
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
|
+
*/
|
|
16
|
+
function rank(placement: ConversationPlacement | null) {
|
|
17
|
+
if (!placement) return 0;
|
|
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
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The place an imported message asks for, or null for none (`sent`). */
|
|
44
|
+
export function requestedPlacement(
|
|
45
|
+
placement: ImportPlacement,
|
|
46
|
+
folderId: string | null
|
|
47
|
+
): ConversationPlacement | null {
|
|
48
|
+
switch (placement) {
|
|
49
|
+
case "inbox": {
|
|
50
|
+
return { mailboxState: "active", folderId };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
case "archive": {
|
|
54
|
+
return { mailboxState: "archive", folderId: null };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
case "spam": {
|
|
58
|
+
return { mailboxState: "spam", folderId: null };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
case "trash": {
|
|
62
|
+
return { mailboxState: "trash", folderId: null };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
case "sent": {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
default: {
|
|
70
|
+
const unexpected: never = placement;
|
|
71
|
+
throw new Error(`Unknown import placement ${String(unexpected)}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Where a conversation goes once an imported message joins it: the stronger
|
|
78
|
+
* of where it was before (null when it is new) and where the message asks
|
|
79
|
+
* for; on a tie, where it was. Null leaves the conversation as indexing put it.
|
|
80
|
+
*/
|
|
81
|
+
export function mergedPlacement(
|
|
82
|
+
before: ConversationPlacement | null,
|
|
83
|
+
requested: ConversationPlacement | null
|
|
84
|
+
): ConversationPlacement | null {
|
|
85
|
+
if (!before) return requested;
|
|
86
|
+
|
|
87
|
+
return rank(requested) > rank(before) ? requested : before;
|
|
88
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { systemFolderPredicate } from "./folder-model";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A mailbox's inbox counts as D1 keeps them (see `mailboxCounts` in
|
|
5
|
+
* worker/db/schema.ts): incoming inbox messages, and how many of them each
|
|
6
|
+
* person has read. A person's unread count is the difference.
|
|
7
|
+
*/
|
|
8
|
+
export type InboxCounts = {
|
|
9
|
+
incomingCount: number;
|
|
10
|
+
readCounts: Map<string, number>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const INBOX = systemFolderPredicate("inbox", "c");
|
|
14
|
+
|
|
15
|
+
/** The inbox counts from the mailbox's own SQLite. */
|
|
16
|
+
export function readInboxCounts(sql: SqlStorage): InboxCounts {
|
|
17
|
+
const incoming = sql
|
|
18
|
+
.exec<{ incoming_count: number }>(`
|
|
19
|
+
select count(*) as incoming_count
|
|
20
|
+
from emails message
|
|
21
|
+
join conversations c on c.id = message.conversation_id
|
|
22
|
+
where message.direction = 'incoming' and ${INBOX}
|
|
23
|
+
`)
|
|
24
|
+
.one();
|
|
25
|
+
|
|
26
|
+
const reads = sql
|
|
27
|
+
.exec<{ user_id: string; read_count: number }>(`
|
|
28
|
+
select read_state.user_id, count(*) as read_count
|
|
29
|
+
from email_read_states read_state
|
|
30
|
+
join emails message on message.id = read_state.email_id
|
|
31
|
+
join conversations c on c.id = message.conversation_id
|
|
32
|
+
where message.direction = 'incoming' and ${INBOX}
|
|
33
|
+
group by read_state.user_id
|
|
34
|
+
`)
|
|
35
|
+
.toArray();
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
incomingCount: Number(incoming.incoming_count),
|
|
39
|
+
readCounts: new Map(
|
|
40
|
+
reads.map((row) => [row.user_id, Number(row.read_count)])
|
|
41
|
+
),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Writes the counts to D1 in one batch on the primary and returns the
|
|
47
|
+
* session bookmark after it (null where D1 has none, e.g. locally). A
|
|
48
|
+
* mailbox that is not (or no longer) in D1 has nothing to write.
|
|
49
|
+
* With `previous` (what the last successful write stored), only people
|
|
50
|
+
* whose read count changed are written; without it, the mailbox's rows are
|
|
51
|
+
* replaced.
|
|
52
|
+
*/
|
|
53
|
+
export async function writeInboxCounts(
|
|
54
|
+
binding: D1Database,
|
|
55
|
+
input: {
|
|
56
|
+
mailboxId: string;
|
|
57
|
+
version: number;
|
|
58
|
+
counts: InboxCounts;
|
|
59
|
+
previous: InboxCounts | null;
|
|
60
|
+
}
|
|
61
|
+
) {
|
|
62
|
+
const { mailboxId, version, counts, previous } = input;
|
|
63
|
+
const session = binding.withSession("first-primary");
|
|
64
|
+
|
|
65
|
+
const statements = [
|
|
66
|
+
session
|
|
67
|
+
.prepare(`
|
|
68
|
+
insert into mailbox_counts (mailbox_id, incoming_count, version, updated_at)
|
|
69
|
+
select id, ?, ?, ? from mailboxes where id = ?
|
|
70
|
+
on conflict (mailbox_id) do update set
|
|
71
|
+
incoming_count = excluded.incoming_count,
|
|
72
|
+
version = excluded.version,
|
|
73
|
+
updated_at = excluded.updated_at
|
|
74
|
+
`)
|
|
75
|
+
.bind(counts.incomingCount, version, Date.now(), mailboxId),
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
if (!previous) {
|
|
79
|
+
statements.push(
|
|
80
|
+
session
|
|
81
|
+
.prepare("delete from mailbox_read_counts where mailbox_id = ?")
|
|
82
|
+
.bind(mailboxId)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const upsert = `
|
|
87
|
+
insert into mailbox_read_counts (mailbox_id, user_id, read_count)
|
|
88
|
+
select id, ?, ? from mailboxes where id = ?
|
|
89
|
+
on conflict (mailbox_id, user_id) do update set read_count = excluded.read_count
|
|
90
|
+
`;
|
|
91
|
+
|
|
92
|
+
for (const [userId, readCount] of counts.readCounts) {
|
|
93
|
+
if (previous && previous.readCounts.get(userId) === readCount) continue;
|
|
94
|
+
statements.push(session.prepare(upsert).bind(userId, readCount, mailboxId));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
for (const userId of previous?.readCounts.keys() ?? []) {
|
|
98
|
+
if (!counts.readCounts.has(userId)) {
|
|
99
|
+
statements.push(session.prepare(upsert).bind(userId, 0, mailboxId));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await session.batch(statements);
|
|
104
|
+
|
|
105
|
+
return session.getBookmark();
|
|
106
|
+
}
|