@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,891 @@
|
|
|
1
|
+
import { inJsonList } from "@open-workspace/core/module";
|
|
2
|
+
import {
|
|
3
|
+
and,
|
|
4
|
+
asc,
|
|
5
|
+
count,
|
|
6
|
+
countDistinct,
|
|
7
|
+
desc,
|
|
8
|
+
eq,
|
|
9
|
+
exists,
|
|
10
|
+
gt,
|
|
11
|
+
notExists,
|
|
12
|
+
inArray,
|
|
13
|
+
lt,
|
|
14
|
+
or,
|
|
15
|
+
sql,
|
|
16
|
+
} from "drizzle-orm";
|
|
17
|
+
import { alias } from "drizzle-orm/sqlite-core";
|
|
18
|
+
|
|
19
|
+
import { DRAFTS_FOLDER_ID, sortFolders } from "../../shared/folder-order";
|
|
20
|
+
/** What the app reads: conversation lists and views, folders and counts, attachments, read state. */
|
|
21
|
+
import type { DraftSenders } from "../mail/draft-sender";
|
|
22
|
+
import { emailListKey } from "../mail/message-view";
|
|
23
|
+
import { buildEmailSearchQuery } from "../mail/search";
|
|
24
|
+
import {
|
|
25
|
+
customFolderPredicate,
|
|
26
|
+
systemFolderPredicate,
|
|
27
|
+
systemFolderTypes,
|
|
28
|
+
} from "./folder-model";
|
|
29
|
+
import * as mailboxDrafts from "./mailbox-drafts";
|
|
30
|
+
import * as mailboxFolders from "./mailbox-folders";
|
|
31
|
+
import * as senderSafety from "./mailbox-senders";
|
|
32
|
+
import { retentionDays } from "./retention";
|
|
33
|
+
import { conversations, drafts, emailReadStates, emails } from "./schema";
|
|
34
|
+
import type {
|
|
35
|
+
ConversationCursorPosition,
|
|
36
|
+
ConversationListItem,
|
|
37
|
+
ConversationPage,
|
|
38
|
+
FolderListItem,
|
|
39
|
+
FolderListRow,
|
|
40
|
+
MailFolder,
|
|
41
|
+
MailboxContext,
|
|
42
|
+
} from "./support";
|
|
43
|
+
import { folderMembership, listEmailColumns, stringListJson } from "./support";
|
|
44
|
+
|
|
45
|
+
export function listConversations(
|
|
46
|
+
m: MailboxContext,
|
|
47
|
+
userId: string,
|
|
48
|
+
folderId: string,
|
|
49
|
+
limit: number,
|
|
50
|
+
cursor: ConversationCursorPosition | null,
|
|
51
|
+
search: string | undefined,
|
|
52
|
+
unreadOnly = false,
|
|
53
|
+
draftSenders?: DraftSenders
|
|
54
|
+
) {
|
|
55
|
+
if (folderId === DRAFTS_FOLDER_ID) {
|
|
56
|
+
return {
|
|
57
|
+
items: [],
|
|
58
|
+
...(unreadOnly
|
|
59
|
+
? { drafts: [], next: null }
|
|
60
|
+
: mailboxDrafts.searchDrafts(
|
|
61
|
+
m,
|
|
62
|
+
userId,
|
|
63
|
+
limit,
|
|
64
|
+
cursor,
|
|
65
|
+
search,
|
|
66
|
+
draftSenders
|
|
67
|
+
)),
|
|
68
|
+
version: m.version(),
|
|
69
|
+
} satisfies ConversationPage;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const folder = mailboxFolders.getFolder(m, folderId);
|
|
73
|
+
|
|
74
|
+
if (!folder) return null;
|
|
75
|
+
const membership = folderMembership(folder);
|
|
76
|
+
const searchQuery = search ? buildEmailSearchQuery(search) : undefined;
|
|
77
|
+
|
|
78
|
+
if (search && !searchQuery) {
|
|
79
|
+
return {
|
|
80
|
+
items: [],
|
|
81
|
+
drafts: [],
|
|
82
|
+
next: null,
|
|
83
|
+
version: m.version(),
|
|
84
|
+
} satisfies ConversationPage;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const inThread = eq(emails.conversationId, conversations.id);
|
|
88
|
+
|
|
89
|
+
const unread = and(
|
|
90
|
+
inThread,
|
|
91
|
+
eq(emails.direction, "incoming"),
|
|
92
|
+
notExists(
|
|
93
|
+
m.db
|
|
94
|
+
.select({ id: emailReadStates.emailId })
|
|
95
|
+
.from(emailReadStates)
|
|
96
|
+
.where(
|
|
97
|
+
and(
|
|
98
|
+
eq(emailReadStates.emailId, emails.id),
|
|
99
|
+
eq(emailReadStates.userId, userId)
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const cursorDate = cursor && new Date(cursor.timelineAt);
|
|
106
|
+
|
|
107
|
+
// A later reply often repeats the original verbatim. Prefer a match that
|
|
108
|
+
// satisfies the whole search without its quote, with the usual rowid tie-break.
|
|
109
|
+
const directQuery = `-quoted : (${searchQuery})`;
|
|
110
|
+
|
|
111
|
+
const where = and(
|
|
112
|
+
membership,
|
|
113
|
+
unreadOnly
|
|
114
|
+
? exists(m.db.select({ id: emails.id }).from(emails).where(unread))
|
|
115
|
+
: undefined,
|
|
116
|
+
cursor && cursorDate
|
|
117
|
+
? or(
|
|
118
|
+
lt(conversations.timelineAt, cursorDate),
|
|
119
|
+
and(
|
|
120
|
+
eq(conversations.timelineAt, cursorDate),
|
|
121
|
+
lt(conversations.latestEmailId, cursor.emailId)
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
: undefined
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const order = [
|
|
128
|
+
desc(conversations.timelineAt),
|
|
129
|
+
desc(conversations.latestEmailId),
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
// Restrict matching emails to the folder and cursor before grouping them.
|
|
133
|
+
// Only the visible conversations need snippets.
|
|
134
|
+
const searchPage = searchQuery
|
|
135
|
+
? m.db
|
|
136
|
+
.select({
|
|
137
|
+
conversationId: conversations.id,
|
|
138
|
+
anyHit: sql<number>`max(email_search.rowid)`.as("any_hit"),
|
|
139
|
+
directHit: sql<
|
|
140
|
+
number | null
|
|
141
|
+
>`max(email_search.rowid) filter (where email_search.rowid in (
|
|
142
|
+
select rowid from email_search where email_search match ${directQuery}
|
|
143
|
+
))`.as("direct_hit"),
|
|
144
|
+
})
|
|
145
|
+
.from(sql`email_search`)
|
|
146
|
+
.innerJoin(
|
|
147
|
+
conversations,
|
|
148
|
+
sql`${conversations.id} = email_search.conversation_id`
|
|
149
|
+
)
|
|
150
|
+
.where(and(sql`email_search match ${searchQuery}`, where))
|
|
151
|
+
// The latest email belongs to just one thread. Group in page order so
|
|
152
|
+
// SQLite can stop aggregating once it has a full page.
|
|
153
|
+
.groupBy(conversations.timelineAt, conversations.latestEmailId)
|
|
154
|
+
.orderBy(...order)
|
|
155
|
+
.limit(limit + 1)
|
|
156
|
+
.as("search_page")
|
|
157
|
+
: null;
|
|
158
|
+
|
|
159
|
+
const listing = m.db
|
|
160
|
+
.select({
|
|
161
|
+
emailId: conversations.latestEmailId,
|
|
162
|
+
...(searchPage && {
|
|
163
|
+
matchId: sql<string>`email_search.email_id`,
|
|
164
|
+
needsQuote: sql<number>`${searchPage.directHit} is null`,
|
|
165
|
+
snippet: sql<string>`snippet(email_search, case when ${searchPage.directHit} is null then 7 else -1 end, '', '', '…', 18)`,
|
|
166
|
+
}),
|
|
167
|
+
timelineAt: conversations.timelineAt,
|
|
168
|
+
hasIncoming: conversations.hasIncoming,
|
|
169
|
+
hasOutgoing: conversations.hasOutgoing,
|
|
170
|
+
mailboxState: conversations.mailboxState,
|
|
171
|
+
folderId: conversations.folderId,
|
|
172
|
+
messageCount: m.db.$count(emails, inThread),
|
|
173
|
+
unreadCount: m.db.$count(emails, unread),
|
|
174
|
+
})
|
|
175
|
+
.from(conversations);
|
|
176
|
+
|
|
177
|
+
const matched = searchPage
|
|
178
|
+
? listing
|
|
179
|
+
.innerJoin(searchPage, eq(searchPage.conversationId, conversations.id))
|
|
180
|
+
.innerJoin(
|
|
181
|
+
sql`email_search`,
|
|
182
|
+
sql`email_search.rowid = coalesce(${searchPage.directHit}, ${searchPage.anyHit})
|
|
183
|
+
and email_search match case when ${searchPage.directHit} is null then ${searchQuery} else ${directQuery} end`
|
|
184
|
+
)
|
|
185
|
+
: listing.where(where);
|
|
186
|
+
|
|
187
|
+
const rows = matched
|
|
188
|
+
.orderBy(...order)
|
|
189
|
+
.limit(limit + 1)
|
|
190
|
+
.all();
|
|
191
|
+
|
|
192
|
+
const pageRows = rows.slice(0, limit);
|
|
193
|
+
const emailIds = pageRows.map((row) => row.emailId);
|
|
194
|
+
|
|
195
|
+
const emailRows = emailIds.length
|
|
196
|
+
? m.db
|
|
197
|
+
.select(listEmailColumns)
|
|
198
|
+
.from(emails)
|
|
199
|
+
.where(inArray(emails.id, emailIds))
|
|
200
|
+
.all()
|
|
201
|
+
: [];
|
|
202
|
+
|
|
203
|
+
const emailsById = new Map(emailRows.map((email) => [email.id, email]));
|
|
204
|
+
|
|
205
|
+
const replyDrafts = new Set(
|
|
206
|
+
emailRows.length
|
|
207
|
+
? m.db
|
|
208
|
+
.select({ conversationId: drafts.conversationId })
|
|
209
|
+
.from(drafts)
|
|
210
|
+
.where(
|
|
211
|
+
and(
|
|
212
|
+
eq(drafts.userId, userId),
|
|
213
|
+
eq(drafts.kind, "reply"),
|
|
214
|
+
inArray(
|
|
215
|
+
drafts.conversationId,
|
|
216
|
+
emailRows.map((email) => email.conversationId)
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
)
|
|
220
|
+
.all()
|
|
221
|
+
.map((row) => row.conversationId)
|
|
222
|
+
: []
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// Names for the rows your own message is the latest of: its recipients
|
|
226
|
+
// are addresses, so they take the names their own mail gave.
|
|
227
|
+
const named = emailRows.filter((email) => email.direction === "outgoing");
|
|
228
|
+
const senderNames = new Map<string, Record<string, string>>();
|
|
229
|
+
|
|
230
|
+
if (named.length) {
|
|
231
|
+
const address = sql<string>`lower(json_extract(${emails.fromJson}, '$[0].address'))`;
|
|
232
|
+
const name = sql<string>`trim(json_extract(${emails.fromJson}, '$[0].name'))`;
|
|
233
|
+
|
|
234
|
+
const nameRows = m.db
|
|
235
|
+
.select({ conversationId: emails.conversationId, address, name })
|
|
236
|
+
.from(emails)
|
|
237
|
+
.where(
|
|
238
|
+
and(
|
|
239
|
+
inJsonList(
|
|
240
|
+
emails.conversationId,
|
|
241
|
+
named.map((email) => email.conversationId)
|
|
242
|
+
),
|
|
243
|
+
eq(emails.direction, "incoming"),
|
|
244
|
+
sql`${name} <> ''`
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
.groupBy(emails.conversationId, address)
|
|
248
|
+
.all();
|
|
249
|
+
|
|
250
|
+
for (const row of nameRows) {
|
|
251
|
+
const names = senderNames.get(row.conversationId) ?? {};
|
|
252
|
+
names[row.address] = row.name;
|
|
253
|
+
senderNames.set(row.conversationId, names);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const items = pageRows.flatMap((row) => {
|
|
258
|
+
const email = emailsById.get(row.emailId);
|
|
259
|
+
|
|
260
|
+
return email
|
|
261
|
+
? [
|
|
262
|
+
{
|
|
263
|
+
email,
|
|
264
|
+
searchMatch: row.matchId
|
|
265
|
+
? {
|
|
266
|
+
id: row.matchId,
|
|
267
|
+
snippet: row.snippet ?? "",
|
|
268
|
+
quoted: Boolean(row.needsQuote),
|
|
269
|
+
}
|
|
270
|
+
: null,
|
|
271
|
+
messageCount: row.messageCount,
|
|
272
|
+
unreadCount: row.unreadCount,
|
|
273
|
+
hasIncoming: row.hasIncoming,
|
|
274
|
+
hasOutgoing: row.hasOutgoing,
|
|
275
|
+
mailboxState: row.mailboxState,
|
|
276
|
+
folderId: row.folderId,
|
|
277
|
+
hasDraft: replyDrafts.has(email.conversationId),
|
|
278
|
+
senderNames: senderNames.get(email.conversationId) ?? {},
|
|
279
|
+
} satisfies ConversationListItem,
|
|
280
|
+
]
|
|
281
|
+
: [];
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const last = pageRows.at(-1);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
items,
|
|
288
|
+
drafts: [],
|
|
289
|
+
next:
|
|
290
|
+
rows.length > limit && last
|
|
291
|
+
? { timelineAt: last.timelineAt.getTime(), emailId: last.emailId }
|
|
292
|
+
: null,
|
|
293
|
+
version: m.version(),
|
|
294
|
+
} satisfies ConversationPage;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function getConversation(m: MailboxContext, conversationId: string) {
|
|
298
|
+
return m.db
|
|
299
|
+
.select()
|
|
300
|
+
.from(emails)
|
|
301
|
+
.where(eq(emails.conversationId, conversationId))
|
|
302
|
+
.orderBy(asc(emails.timelineAt), asc(emails.id))
|
|
303
|
+
.all();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function getConversationSnapshot(
|
|
307
|
+
m: MailboxContext,
|
|
308
|
+
conversationId: string
|
|
309
|
+
) {
|
|
310
|
+
const conversation = m.db
|
|
311
|
+
.select({
|
|
312
|
+
mailboxState: conversations.mailboxState,
|
|
313
|
+
folderId: conversations.folderId,
|
|
314
|
+
})
|
|
315
|
+
.from(conversations)
|
|
316
|
+
.where(eq(conversations.id, conversationId))
|
|
317
|
+
.all()[0];
|
|
318
|
+
|
|
319
|
+
if (!conversation) return null;
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
mailboxState: conversation.mailboxState,
|
|
323
|
+
folderId: conversation.folderId,
|
|
324
|
+
messages: getConversation(m, conversationId),
|
|
325
|
+
readStates: m.db
|
|
326
|
+
.select({
|
|
327
|
+
emailId: emailReadStates.emailId,
|
|
328
|
+
userId: emailReadStates.userId,
|
|
329
|
+
readAt: emailReadStates.readAt,
|
|
330
|
+
})
|
|
331
|
+
.from(emailReadStates)
|
|
332
|
+
.innerJoin(emails, eq(emailReadStates.emailId, emails.id))
|
|
333
|
+
.where(eq(emails.conversationId, conversationId))
|
|
334
|
+
.orderBy(asc(emailReadStates.readAt))
|
|
335
|
+
.all(),
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function getConversationPageSnapshot(
|
|
340
|
+
m: MailboxContext,
|
|
341
|
+
conversationId: string,
|
|
342
|
+
userId: string,
|
|
343
|
+
limit: number,
|
|
344
|
+
cursor: ConversationCursorPosition | null,
|
|
345
|
+
matchId?: string
|
|
346
|
+
) {
|
|
347
|
+
const conversation = m.db
|
|
348
|
+
.select({
|
|
349
|
+
mailboxState: conversations.mailboxState,
|
|
350
|
+
folderId: conversations.folderId,
|
|
351
|
+
hasIncoming: conversations.hasIncoming,
|
|
352
|
+
hasOutgoing: conversations.hasOutgoing,
|
|
353
|
+
})
|
|
354
|
+
.from(conversations)
|
|
355
|
+
.where(eq(conversations.id, conversationId))
|
|
356
|
+
.all()[0];
|
|
357
|
+
|
|
358
|
+
if (!conversation) return null;
|
|
359
|
+
|
|
360
|
+
const pageLimit = Math.max(1, Math.min(limit, 25));
|
|
361
|
+
const cursorDate = cursor ? new Date(cursor.timelineAt) : null;
|
|
362
|
+
|
|
363
|
+
const predicate =
|
|
364
|
+
cursor && cursorDate
|
|
365
|
+
? and(
|
|
366
|
+
eq(emails.conversationId, conversationId),
|
|
367
|
+
or(
|
|
368
|
+
lt(emails.timelineAt, cursorDate),
|
|
369
|
+
and(
|
|
370
|
+
eq(emails.timelineAt, cursorDate),
|
|
371
|
+
lt(emails.id, cursor.emailId)
|
|
372
|
+
)
|
|
373
|
+
)
|
|
374
|
+
)
|
|
375
|
+
: eq(emails.conversationId, conversationId);
|
|
376
|
+
|
|
377
|
+
const descending = m.db
|
|
378
|
+
.select()
|
|
379
|
+
.from(emails)
|
|
380
|
+
.where(predicate)
|
|
381
|
+
.orderBy(desc(emails.timelineAt), desc(emails.id))
|
|
382
|
+
.limit(pageLimit + 1)
|
|
383
|
+
.all();
|
|
384
|
+
|
|
385
|
+
const page = descending.slice(0, pageLimit);
|
|
386
|
+
const oldest = page.at(-1);
|
|
387
|
+
|
|
388
|
+
// A search hit can predate the first page. Include just that message in
|
|
389
|
+
// the same read, leaving the page boundary at its original position.
|
|
390
|
+
const olderMatch =
|
|
391
|
+
matchId &&
|
|
392
|
+
!cursor &&
|
|
393
|
+
descending.length > pageLimit &&
|
|
394
|
+
!page.some((email) => email.id === matchId)
|
|
395
|
+
? m.db
|
|
396
|
+
.select()
|
|
397
|
+
.from(emails)
|
|
398
|
+
.where(
|
|
399
|
+
and(
|
|
400
|
+
eq(emails.id, matchId),
|
|
401
|
+
eq(emails.conversationId, conversationId)
|
|
402
|
+
)
|
|
403
|
+
)
|
|
404
|
+
.get()
|
|
405
|
+
: undefined;
|
|
406
|
+
|
|
407
|
+
if (olderMatch) page.push(olderMatch);
|
|
408
|
+
const messageIds = page.map((email) => email.id);
|
|
409
|
+
|
|
410
|
+
const [counts] = m.db
|
|
411
|
+
.select({
|
|
412
|
+
messageCount: count(),
|
|
413
|
+
unreadCount: sql<number>`count(*) filter (where ${emails.direction} = 'incoming' and ${emailReadStates.emailId} is null)`,
|
|
414
|
+
})
|
|
415
|
+
.from(emails)
|
|
416
|
+
.leftJoin(
|
|
417
|
+
emailReadStates,
|
|
418
|
+
and(
|
|
419
|
+
eq(emailReadStates.emailId, emails.id),
|
|
420
|
+
eq(emailReadStates.userId, userId)
|
|
421
|
+
)
|
|
422
|
+
)
|
|
423
|
+
.where(eq(emails.conversationId, conversationId))
|
|
424
|
+
.all();
|
|
425
|
+
|
|
426
|
+
return {
|
|
427
|
+
mailboxState: conversation.mailboxState,
|
|
428
|
+
folderId: conversation.folderId,
|
|
429
|
+
hasIncoming: conversation.hasIncoming,
|
|
430
|
+
hasOutgoing: conversation.hasOutgoing,
|
|
431
|
+
messageCount: counts.messageCount,
|
|
432
|
+
unreadCount: counts.unreadCount,
|
|
433
|
+
pinnedMessageId: olderMatch?.id ?? null,
|
|
434
|
+
messages: page.reverse(),
|
|
435
|
+
next:
|
|
436
|
+
descending.length > pageLimit && oldest
|
|
437
|
+
? { timelineAt: oldest.timelineAt.getTime(), emailId: oldest.id }
|
|
438
|
+
: null,
|
|
439
|
+
readStates: messageIds.length
|
|
440
|
+
? m.db
|
|
441
|
+
.select({
|
|
442
|
+
emailId: emailReadStates.emailId,
|
|
443
|
+
userId: emailReadStates.userId,
|
|
444
|
+
readAt: emailReadStates.readAt,
|
|
445
|
+
})
|
|
446
|
+
.from(emailReadStates)
|
|
447
|
+
.where(inArray(emailReadStates.emailId, messageIds))
|
|
448
|
+
.orderBy(asc(emailReadStates.readAt))
|
|
449
|
+
.all()
|
|
450
|
+
: [],
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Everything the reader needs from this mailbox in one call: a page of the
|
|
456
|
+
* conversation with read states, plus sender history and block, trust and
|
|
457
|
+
* unsubscribe state for its incoming senders.
|
|
458
|
+
*/
|
|
459
|
+
export function getConversationView(
|
|
460
|
+
m: MailboxContext,
|
|
461
|
+
conversationId: string,
|
|
462
|
+
userId: string,
|
|
463
|
+
limit: number,
|
|
464
|
+
cursor: ConversationCursorPosition | null,
|
|
465
|
+
includeReplyDraft: boolean,
|
|
466
|
+
matchId?: string
|
|
467
|
+
) {
|
|
468
|
+
const snapshot = getConversationPageSnapshot(
|
|
469
|
+
m,
|
|
470
|
+
conversationId,
|
|
471
|
+
userId,
|
|
472
|
+
limit,
|
|
473
|
+
cursor,
|
|
474
|
+
matchId
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
if (!snapshot) return null;
|
|
478
|
+
|
|
479
|
+
const replyDraft =
|
|
480
|
+
includeReplyDraft && !cursor
|
|
481
|
+
? (m.db
|
|
482
|
+
.select()
|
|
483
|
+
.from(drafts)
|
|
484
|
+
.where(
|
|
485
|
+
and(
|
|
486
|
+
eq(drafts.userId, userId),
|
|
487
|
+
eq(drafts.kind, "reply"),
|
|
488
|
+
eq(drafts.conversationId, conversationId)
|
|
489
|
+
)
|
|
490
|
+
)
|
|
491
|
+
.get() ?? null)
|
|
492
|
+
: null;
|
|
493
|
+
|
|
494
|
+
// Older pages stay lazy. Only the saved reply's parent is needed by the editor.
|
|
495
|
+
const sourceId = replyDraft?.sourceEmailId;
|
|
496
|
+
|
|
497
|
+
const source =
|
|
498
|
+
sourceId && !snapshot.messages.some((message) => message.id === sourceId)
|
|
499
|
+
? m.getEmail(sourceId)
|
|
500
|
+
: null;
|
|
501
|
+
|
|
502
|
+
const replyDraftSource =
|
|
503
|
+
source?.conversationId === conversationId ? source : null;
|
|
504
|
+
|
|
505
|
+
const incoming = (
|
|
506
|
+
replyDraftSource
|
|
507
|
+
? [...snapshot.messages, replyDraftSource]
|
|
508
|
+
: snapshot.messages
|
|
509
|
+
).filter((message) => message.direction === "incoming");
|
|
510
|
+
|
|
511
|
+
const senderAddresses = incoming.flatMap((message) =>
|
|
512
|
+
message.fromJson[0] ? [message.fromJson[0].address] : []
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
return {
|
|
516
|
+
...snapshot,
|
|
517
|
+
replyDraft,
|
|
518
|
+
replyDraftSource,
|
|
519
|
+
history: senderSafety.getSenderHistory(m, senderAddresses),
|
|
520
|
+
safety: senderSafety.getMessageSafetyContext(
|
|
521
|
+
m,
|
|
522
|
+
// Envelope senders too: a block on either holds a message back.
|
|
523
|
+
[
|
|
524
|
+
...senderAddresses,
|
|
525
|
+
...incoming.flatMap((message) => message.envelopeFrom ?? []),
|
|
526
|
+
],
|
|
527
|
+
incoming.flatMap((message) => emailListKey(message) ?? [])
|
|
528
|
+
),
|
|
529
|
+
version: m.version(),
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** One message with its sender's history and safety state, for re-deriving its security summary. */
|
|
534
|
+
export function getMessageSenderView(m: MailboxContext, emailId: string) {
|
|
535
|
+
const email = m.getEmail(emailId);
|
|
536
|
+
|
|
537
|
+
if (!email) return null;
|
|
538
|
+
|
|
539
|
+
const sender =
|
|
540
|
+
email.direction === "incoming" ? email.fromJson[0]?.address : undefined;
|
|
541
|
+
|
|
542
|
+
const senderAddresses = sender ? [sender] : [];
|
|
543
|
+
const listKey = emailListKey(email);
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
email,
|
|
547
|
+
history: senderSafety.getSenderHistory(m, senderAddresses),
|
|
548
|
+
safety: senderSafety.getMessageSafetyContext(
|
|
549
|
+
m,
|
|
550
|
+
// Its envelope sender too: a block on either holds it back.
|
|
551
|
+
sender && email.envelopeFrom
|
|
552
|
+
? [sender, email.envelopeFrom]
|
|
553
|
+
: senderAddresses,
|
|
554
|
+
listKey ? [listKey] : []
|
|
555
|
+
),
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function getAttachment(
|
|
560
|
+
m: MailboxContext,
|
|
561
|
+
emailId: string,
|
|
562
|
+
attachmentId: string
|
|
563
|
+
) {
|
|
564
|
+
return (
|
|
565
|
+
emailAttachments(m, emailId).find((file) => file.id === attachmentId) ??
|
|
566
|
+
null
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export function emailAttachments(m: MailboxContext, emailId: string) {
|
|
571
|
+
return (
|
|
572
|
+
m.db
|
|
573
|
+
.select({ files: emails.attachmentsJson })
|
|
574
|
+
.from(emails)
|
|
575
|
+
.where(eq(emails.id, emailId))
|
|
576
|
+
.get()?.files ?? []
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// Each placement group can count towards several folders (e.g. Inbox, Sent
|
|
581
|
+
// and a custom folder). Apply the same membership rules as the message list.
|
|
582
|
+
const systemMembership = systemFolderTypes
|
|
583
|
+
.map(
|
|
584
|
+
(type) => `(folder.id = '${type}' and ${systemFolderPredicate(type, "p")})`
|
|
585
|
+
)
|
|
586
|
+
.join(" or ");
|
|
587
|
+
|
|
588
|
+
export function listFolders(m: MailboxContext, userId: string) {
|
|
589
|
+
const system = mailboxFolders.systemFolders(m);
|
|
590
|
+
|
|
591
|
+
const rows = m.state.storage.sql
|
|
592
|
+
.exec<FolderListRow>(
|
|
593
|
+
`
|
|
594
|
+
with folder_rows(id, name, kind, system_type, sort_order) as (
|
|
595
|
+
select
|
|
596
|
+
json_extract(value, '$.id'),
|
|
597
|
+
json_extract(value, '$.name'),
|
|
598
|
+
'system',
|
|
599
|
+
json_extract(value, '$.systemType'),
|
|
600
|
+
json_extract(value, '$.sortOrder')
|
|
601
|
+
from json_each(?)
|
|
602
|
+
union all
|
|
603
|
+
select id, name, 'custom', null, sort_order
|
|
604
|
+
from folders
|
|
605
|
+
),
|
|
606
|
+
placements as (
|
|
607
|
+
select c.mailbox_state, c.folder_id, c.has_incoming, c.has_outgoing,
|
|
608
|
+
count(distinct c.id) as total_count,
|
|
609
|
+
count(message.id) - count(read_state.email_id) as unread_count
|
|
610
|
+
from conversations c
|
|
611
|
+
left join emails message
|
|
612
|
+
on message.conversation_id = c.id and message.direction = 'incoming'
|
|
613
|
+
left join email_read_states read_state
|
|
614
|
+
on read_state.user_id = ? and read_state.email_id = message.id
|
|
615
|
+
group by c.mailbox_state, c.folder_id, c.has_incoming, c.has_outgoing
|
|
616
|
+
),
|
|
617
|
+
counts as (
|
|
618
|
+
select folder.id, folder.kind, sum(total_count) as total_count, sum(unread_count) as unread_count
|
|
619
|
+
from folder_rows folder join placements p on ${systemMembership}
|
|
620
|
+
where folder.kind = 'system'
|
|
621
|
+
group by folder.id
|
|
622
|
+
union all
|
|
623
|
+
select p.folder_id, 'custom', sum(total_count), sum(unread_count)
|
|
624
|
+
from placements p where p.folder_id is not null and ${customFolderPredicate("p")}
|
|
625
|
+
group by p.folder_id
|
|
626
|
+
)
|
|
627
|
+
select folder.*, coalesce(counts.total_count, 0) as total_count,
|
|
628
|
+
coalesce(counts.unread_count, 0) as unread_count
|
|
629
|
+
from folder_rows folder left join counts using (id, kind)
|
|
630
|
+
`,
|
|
631
|
+
JSON.stringify(system),
|
|
632
|
+
userId
|
|
633
|
+
)
|
|
634
|
+
.toArray();
|
|
635
|
+
|
|
636
|
+
const sent = system.find((folder) => folder.id === "sent");
|
|
637
|
+
|
|
638
|
+
const [draftCount] = m.db
|
|
639
|
+
.select({ value: sql<number>`count(*)` })
|
|
640
|
+
.from(drafts)
|
|
641
|
+
.where(eq(drafts.userId, userId))
|
|
642
|
+
.all();
|
|
643
|
+
|
|
644
|
+
return sortFolders([
|
|
645
|
+
...rows.map(
|
|
646
|
+
(row) =>
|
|
647
|
+
({
|
|
648
|
+
id: row.id,
|
|
649
|
+
name: row.name,
|
|
650
|
+
kind: row.kind,
|
|
651
|
+
systemType: row.system_type,
|
|
652
|
+
sortOrder: Number(row.sort_order),
|
|
653
|
+
totalCount: Number(row.total_count),
|
|
654
|
+
unreadCount: Number(row.unread_count),
|
|
655
|
+
}) satisfies FolderListItem
|
|
656
|
+
),
|
|
657
|
+
{
|
|
658
|
+
id: DRAFTS_FOLDER_ID,
|
|
659
|
+
name: "Drafts",
|
|
660
|
+
kind: "system",
|
|
661
|
+
systemType: DRAFTS_FOLDER_ID,
|
|
662
|
+
// Folder positions are steps of 10 apart, so this is right after Sent.
|
|
663
|
+
sortOrder: (sent?.sortOrder ?? 0) + 1,
|
|
664
|
+
totalCount: Number(draftCount?.value ?? 0),
|
|
665
|
+
unreadCount: 0,
|
|
666
|
+
} satisfies FolderListItem,
|
|
667
|
+
]);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* What the mail screen shows first, in one call: the folders and the first
|
|
672
|
+
* page of `folderId` (null for no such folder).
|
|
673
|
+
*/
|
|
674
|
+
export function startupView(
|
|
675
|
+
m: MailboxContext,
|
|
676
|
+
userId: string,
|
|
677
|
+
folderId: string,
|
|
678
|
+
limit: number,
|
|
679
|
+
draftSenders?: DraftSenders
|
|
680
|
+
) {
|
|
681
|
+
return {
|
|
682
|
+
folders: folderView(m, userId),
|
|
683
|
+
conversations: listConversations(
|
|
684
|
+
m,
|
|
685
|
+
userId,
|
|
686
|
+
folderId,
|
|
687
|
+
limit,
|
|
688
|
+
null,
|
|
689
|
+
undefined,
|
|
690
|
+
false,
|
|
691
|
+
draftSenders
|
|
692
|
+
),
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** `listFolders` with the change counter it reflects. */
|
|
697
|
+
export function folderView(m: MailboxContext, userId: string) {
|
|
698
|
+
return {
|
|
699
|
+
folders: listFolders(m, userId),
|
|
700
|
+
version: m.version(),
|
|
701
|
+
retention: retentionDays(m.env),
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export function getFolderCounts(
|
|
706
|
+
m: MailboxContext,
|
|
707
|
+
userId: string,
|
|
708
|
+
folderId: string
|
|
709
|
+
) {
|
|
710
|
+
const folder = mailboxFolders.getFolder(m, folderId);
|
|
711
|
+
|
|
712
|
+
return folder ? countFolder(m, userId, folder) : null;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
export function countFolder(
|
|
716
|
+
m: MailboxContext,
|
|
717
|
+
userId: string,
|
|
718
|
+
folder: MailFolder
|
|
719
|
+
) {
|
|
720
|
+
const [counts] = m.db
|
|
721
|
+
.select({
|
|
722
|
+
totalCount: countDistinct(conversations.id),
|
|
723
|
+
unreadCount: sql<number>`count(${emails.id}) - count(${emailReadStates.emailId})`,
|
|
724
|
+
})
|
|
725
|
+
.from(conversations)
|
|
726
|
+
.leftJoin(
|
|
727
|
+
emails,
|
|
728
|
+
and(
|
|
729
|
+
eq(emails.conversationId, conversations.id),
|
|
730
|
+
eq(emails.direction, "incoming")
|
|
731
|
+
)
|
|
732
|
+
)
|
|
733
|
+
.leftJoin(
|
|
734
|
+
emailReadStates,
|
|
735
|
+
and(
|
|
736
|
+
eq(emailReadStates.emailId, emails.id),
|
|
737
|
+
eq(emailReadStates.userId, userId)
|
|
738
|
+
)
|
|
739
|
+
)
|
|
740
|
+
.where(folderMembership(folder))
|
|
741
|
+
.all();
|
|
742
|
+
|
|
743
|
+
return counts;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function writeReadStates(
|
|
747
|
+
m: MailboxContext,
|
|
748
|
+
userId: string,
|
|
749
|
+
read: string[],
|
|
750
|
+
unread: string[]
|
|
751
|
+
) {
|
|
752
|
+
if (!read.length && !unread.length) return;
|
|
753
|
+
const { sql: storage } = m.state.storage;
|
|
754
|
+
m.state.storage.transactionSync(() => {
|
|
755
|
+
if (read.length) {
|
|
756
|
+
storage.exec(
|
|
757
|
+
`insert into email_read_states (user_id, email_id, read_at)
|
|
758
|
+
select ?, cast(value as text), ? from json_each(?)
|
|
759
|
+
where true
|
|
760
|
+
on conflict (user_id, email_id) do update set read_at = excluded.read_at`,
|
|
761
|
+
userId,
|
|
762
|
+
Date.now(),
|
|
763
|
+
stringListJson(read)
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (unread.length) {
|
|
768
|
+
storage.exec(
|
|
769
|
+
`delete from email_read_states
|
|
770
|
+
where user_id = ? and email_id in (select cast(value as text) from json_each(?))`,
|
|
771
|
+
userId,
|
|
772
|
+
stringListJson(unread)
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
void m.announce();
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* The one way people's read state changes: each message read or unread
|
|
781
|
+
* for `userId`, then one announcement. The ids not here, and the version
|
|
782
|
+
* the change started from and the one after it (a change counts more than
|
|
783
|
+
* one step, so the client can't work them out).
|
|
784
|
+
*/
|
|
785
|
+
export function setReadStates(
|
|
786
|
+
m: MailboxContext,
|
|
787
|
+
userId: string,
|
|
788
|
+
changes: readonly { emailId: string; isRead: boolean }[]
|
|
789
|
+
) {
|
|
790
|
+
const previousVersion = m.version();
|
|
791
|
+
|
|
792
|
+
const here = new Set(
|
|
793
|
+
changes.length
|
|
794
|
+
? m.db
|
|
795
|
+
.select({ id: emails.id })
|
|
796
|
+
.from(emails)
|
|
797
|
+
.where(
|
|
798
|
+
inJsonList(
|
|
799
|
+
emails.id,
|
|
800
|
+
changes.map((change) => change.emailId)
|
|
801
|
+
)
|
|
802
|
+
)
|
|
803
|
+
.all()
|
|
804
|
+
.map((row) => row.id)
|
|
805
|
+
: []
|
|
806
|
+
);
|
|
807
|
+
|
|
808
|
+
const read: string[] = [];
|
|
809
|
+
const unread: string[] = [];
|
|
810
|
+
const notFound: string[] = [];
|
|
811
|
+
|
|
812
|
+
for (const { emailId, isRead } of changes) {
|
|
813
|
+
if (!here.has(emailId)) notFound.push(emailId);
|
|
814
|
+
else if (isRead) read.push(emailId);
|
|
815
|
+
else unread.push(emailId);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
writeReadStates(m, userId, read, unread);
|
|
819
|
+
|
|
820
|
+
return { previousVersion, version: m.version(), notFound };
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** Sets one person's read state; the change counter after it, or false for no such message. */
|
|
824
|
+
export function setMessageRead(
|
|
825
|
+
m: MailboxContext,
|
|
826
|
+
userId: string,
|
|
827
|
+
emailId: string,
|
|
828
|
+
isRead: boolean
|
|
829
|
+
) {
|
|
830
|
+
const { previousVersion, version, notFound } = setReadStates(m, userId, [
|
|
831
|
+
{ emailId, isRead },
|
|
832
|
+
]);
|
|
833
|
+
|
|
834
|
+
return notFound.length ? false : { previousVersion, version };
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* Marks conversations read (every received message) or unread (the newest
|
|
839
|
+
* received one) for one person; how many it marked.
|
|
840
|
+
*/
|
|
841
|
+
export function bulkSetConversationRead(
|
|
842
|
+
m: MailboxContext,
|
|
843
|
+
userId: string,
|
|
844
|
+
conversationIds: string[],
|
|
845
|
+
isRead: boolean
|
|
846
|
+
) {
|
|
847
|
+
const candidates = [...new Set(conversationIds)];
|
|
848
|
+
|
|
849
|
+
if (!candidates.length) return 0;
|
|
850
|
+
|
|
851
|
+
const newer = alias(emails, "newer");
|
|
852
|
+
|
|
853
|
+
const rows = m.db
|
|
854
|
+
.select({ id: emails.id, conversationId: emails.conversationId })
|
|
855
|
+
.from(emails)
|
|
856
|
+
.innerJoin(conversations, eq(conversations.id, emails.conversationId))
|
|
857
|
+
.where(
|
|
858
|
+
and(
|
|
859
|
+
inJsonList(conversations.id, candidates),
|
|
860
|
+
eq(conversations.hasIncoming, true),
|
|
861
|
+
eq(emails.direction, "incoming"),
|
|
862
|
+
isRead
|
|
863
|
+
? undefined
|
|
864
|
+
: notExists(
|
|
865
|
+
m.db
|
|
866
|
+
.select({ id: newer.id })
|
|
867
|
+
.from(newer)
|
|
868
|
+
.where(
|
|
869
|
+
and(
|
|
870
|
+
eq(newer.conversationId, emails.conversationId),
|
|
871
|
+
eq(newer.direction, "incoming"),
|
|
872
|
+
or(
|
|
873
|
+
gt(newer.timelineAt, emails.timelineAt),
|
|
874
|
+
and(
|
|
875
|
+
eq(newer.timelineAt, emails.timelineAt),
|
|
876
|
+
gt(newer.id, emails.id)
|
|
877
|
+
)
|
|
878
|
+
)
|
|
879
|
+
)
|
|
880
|
+
)
|
|
881
|
+
)
|
|
882
|
+
)
|
|
883
|
+
)
|
|
884
|
+
.all();
|
|
885
|
+
|
|
886
|
+
if (!rows.length) return 0;
|
|
887
|
+
const ids = rows.map((row) => row.id);
|
|
888
|
+
writeReadStates(m, userId, isRead ? ids : [], isRead ? [] : ids);
|
|
889
|
+
|
|
890
|
+
return new Set(rows.map((row) => row.conversationId)).size;
|
|
891
|
+
}
|