@open-workspace/email 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +49 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +387 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +9 -10
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +754 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +108 -1815
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +682 -0
- package/worker/mail/auth-headers.ts +48 -40
- package/worker/mail/auto-reply-routes.ts +255 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +3 -3
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +696 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +201 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +301 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +450 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +240 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +156 -0
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/** Each person's drafts in a mailbox, and the files they keep. */
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
|
|
4
|
+
import { and, desc, eq, getTableColumns, lt, ne, or, sql } from "drizzle-orm";
|
|
5
|
+
import type { SQL } from "drizzle-orm";
|
|
6
|
+
import { alias } from "drizzle-orm/sqlite-core";
|
|
7
|
+
|
|
8
|
+
import { draftSender } from "../mail/draft-sender";
|
|
9
|
+
import type { DraftSenders } from "../mail/draft-sender";
|
|
10
|
+
import { plainBody, readableDraftText } from "../mail/plain-body";
|
|
11
|
+
import { draftSearchMatcher } from "../mail/search";
|
|
12
|
+
import * as mailboxStorage from "./mailbox-storage";
|
|
13
|
+
import { drafts, emails } from "./schema";
|
|
14
|
+
import type { Draft } from "./schema";
|
|
15
|
+
import type {
|
|
16
|
+
ConversationCursorPosition,
|
|
17
|
+
DraftInput,
|
|
18
|
+
SaveDraftResult,
|
|
19
|
+
MailboxContext,
|
|
20
|
+
MailboxDatabase,
|
|
21
|
+
} from "./support";
|
|
22
|
+
import { ALARM_WAKE_DELAY_MS, MAX_DRAFTS_PER_USER } from "./support";
|
|
23
|
+
|
|
24
|
+
// Read HTML for a listing only when the draft has no usable text alternative.
|
|
25
|
+
export const draftHtmlFallback = sql<
|
|
26
|
+
string | null
|
|
27
|
+
>`case when trim(${drafts.bodyText}, char(9,10,11,12,13,32)) = '' then ${drafts.bodyHtml} end`;
|
|
28
|
+
|
|
29
|
+
export const draftReplyParent = and(
|
|
30
|
+
eq(drafts.sourceEmailId, emails.id),
|
|
31
|
+
ne(drafts.kind, "forward")
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export const replySenderColumns = {
|
|
35
|
+
direction: emails.direction,
|
|
36
|
+
envelopeTo: emails.envelopeTo,
|
|
37
|
+
fromJson: emails.fromJson,
|
|
38
|
+
toJson: emails.toJson,
|
|
39
|
+
ccJson: emails.ccJson,
|
|
40
|
+
messageIdHeader: emails.messageIdHeader,
|
|
41
|
+
transportState: emails.transportState,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function draftForEditor(m: MailboxContext, userId: string, id: string) {
|
|
45
|
+
return m.db
|
|
46
|
+
.select({ draft: getTableColumns(drafts), parent: replySenderColumns })
|
|
47
|
+
.from(drafts)
|
|
48
|
+
.leftJoin(emails, draftReplyParent)
|
|
49
|
+
.where(and(eq(drafts.id, id), eq(drafts.userId, userId)))
|
|
50
|
+
.get();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The person's drafts in this mailbox, newest first. */
|
|
54
|
+
export function listDrafts(m: MailboxContext, userId: string) {
|
|
55
|
+
return m.db
|
|
56
|
+
.select()
|
|
57
|
+
.from(drafts)
|
|
58
|
+
.where(eq(drafts.userId, userId))
|
|
59
|
+
.orderBy(desc(drafts.updatedAt))
|
|
60
|
+
.all();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Both the web folder and JMAP search the same draft fields and reply parent. */
|
|
64
|
+
export function draftSearchRows(
|
|
65
|
+
db: MailboxDatabase,
|
|
66
|
+
userId: string,
|
|
67
|
+
body: "search" | "preview" | "none",
|
|
68
|
+
withParent: boolean,
|
|
69
|
+
before?: SQL
|
|
70
|
+
) {
|
|
71
|
+
const source = alias(emails, "forward_source");
|
|
72
|
+
|
|
73
|
+
const query = db
|
|
74
|
+
.select({
|
|
75
|
+
id: drafts.id,
|
|
76
|
+
kind: drafts.kind,
|
|
77
|
+
conversationId: drafts.conversationId,
|
|
78
|
+
to: drafts.to,
|
|
79
|
+
cc: drafts.cc,
|
|
80
|
+
bcc: drafts.bcc,
|
|
81
|
+
replyTo: drafts.replyTo,
|
|
82
|
+
subject: drafts.subject,
|
|
83
|
+
updatedAt: drafts.updatedAt,
|
|
84
|
+
version: drafts.version,
|
|
85
|
+
...(body !== "none" && {
|
|
86
|
+
bodyText: drafts.bodyText,
|
|
87
|
+
attachments: drafts.attachments,
|
|
88
|
+
bodyHtml: draftHtmlFallback,
|
|
89
|
+
}),
|
|
90
|
+
...(body === "search" && {
|
|
91
|
+
forwarded: {
|
|
92
|
+
preview: source.preview,
|
|
93
|
+
bodyText: source.bodyText,
|
|
94
|
+
quotedText: source.quotedText,
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
...(withParent && { parent: replySenderColumns }),
|
|
98
|
+
})
|
|
99
|
+
.from(drafts)
|
|
100
|
+
.where(and(eq(drafts.userId, userId), before))
|
|
101
|
+
.$dynamic();
|
|
102
|
+
|
|
103
|
+
if (withParent) query.leftJoin(emails, draftReplyParent);
|
|
104
|
+
|
|
105
|
+
if (body === "search") {
|
|
106
|
+
query.leftJoin(
|
|
107
|
+
source,
|
|
108
|
+
and(eq(drafts.kind, "forward"), eq(drafts.sourceEmailId, source.id))
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return query;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The same searchable fields in the web folder and JMAP. A forward's original
|
|
116
|
+
* already has searchable text in SQLite, including its separately stored quote. */
|
|
117
|
+
export function draftSearchFields(
|
|
118
|
+
row: Pick<Draft, "to" | "cc" | "bcc" | "replyTo" | "subject">,
|
|
119
|
+
sender: DraftSenders["primary"] | undefined,
|
|
120
|
+
body: string,
|
|
121
|
+
forwarded?: {
|
|
122
|
+
bodyText: string | null;
|
|
123
|
+
quotedText: string | null;
|
|
124
|
+
preview: string | null;
|
|
125
|
+
} | null
|
|
126
|
+
) {
|
|
127
|
+
const from = [sender?.displayName, sender?.address].join(" ");
|
|
128
|
+
const to = row.to.join(" ");
|
|
129
|
+
|
|
130
|
+
const text = [
|
|
131
|
+
from,
|
|
132
|
+
to,
|
|
133
|
+
...row.cc,
|
|
134
|
+
...row.bcc,
|
|
135
|
+
row.replyTo,
|
|
136
|
+
row.subject,
|
|
137
|
+
body,
|
|
138
|
+
forwarded?.bodyText || forwarded?.preview,
|
|
139
|
+
forwarded?.quotedText,
|
|
140
|
+
].join(" ");
|
|
141
|
+
|
|
142
|
+
return { from, to, subject: row.subject, text };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** One page of the person's Drafts folder, newest first. */
|
|
146
|
+
export function searchDrafts(
|
|
147
|
+
m: MailboxContext,
|
|
148
|
+
userId: string,
|
|
149
|
+
limit: number,
|
|
150
|
+
cursor: ConversationCursorPosition | null,
|
|
151
|
+
search: string | undefined,
|
|
152
|
+
senders?: DraftSenders
|
|
153
|
+
) {
|
|
154
|
+
const matches = search ? draftSearchMatcher(search) : null;
|
|
155
|
+
const before = cursor && new Date(cursor.timelineAt);
|
|
156
|
+
|
|
157
|
+
const query = draftSearchRows(
|
|
158
|
+
m.db,
|
|
159
|
+
userId,
|
|
160
|
+
matches ? "search" : "preview",
|
|
161
|
+
Boolean(senders?.aliases.length),
|
|
162
|
+
cursor && before
|
|
163
|
+
? or(
|
|
164
|
+
lt(drafts.updatedAt, before),
|
|
165
|
+
and(eq(drafts.updatedAt, before), lt(drafts.id, cursor.emailId))
|
|
166
|
+
)
|
|
167
|
+
: undefined
|
|
168
|
+
).orderBy(desc(drafts.updatedAt), desc(drafts.id));
|
|
169
|
+
|
|
170
|
+
// Search can skip drafts between two hits. Apply the page size after matching.
|
|
171
|
+
const rows = matches ? query.all() : query.limit(limit + 1).all();
|
|
172
|
+
const items = [];
|
|
173
|
+
|
|
174
|
+
for (const {
|
|
175
|
+
bodyText,
|
|
176
|
+
bodyHtml,
|
|
177
|
+
attachments,
|
|
178
|
+
parent,
|
|
179
|
+
forwarded,
|
|
180
|
+
...row
|
|
181
|
+
} of rows) {
|
|
182
|
+
const text = plainBody(readableDraftText(bodyText, attachments), bodyHtml);
|
|
183
|
+
const sender = senders && draftSender(senders, parent);
|
|
184
|
+
|
|
185
|
+
if (
|
|
186
|
+
matches &&
|
|
187
|
+
!matches(draftSearchFields(row, sender, text, forwarded).text)
|
|
188
|
+
) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
items.push({
|
|
193
|
+
...row,
|
|
194
|
+
sender,
|
|
195
|
+
preview: text.replaceAll(/\s+/gu, " ").trim().slice(0, 200),
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
if (items.length > limit) break;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const page = items.slice(0, limit);
|
|
202
|
+
const last = page.at(-1);
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
drafts: page,
|
|
206
|
+
next:
|
|
207
|
+
items.length > limit && last
|
|
208
|
+
? { timelineAt: last.updatedAt.getTime(), emailId: last.id }
|
|
209
|
+
: null,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function getDraft(m: MailboxContext, userId: string, id: string) {
|
|
214
|
+
return (
|
|
215
|
+
m.db
|
|
216
|
+
.select()
|
|
217
|
+
.from(drafts)
|
|
218
|
+
.where(and(eq(drafts.userId, userId), eq(drafts.id, id)))
|
|
219
|
+
.get() ?? null
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Saves a draft if it is still at `expectedVersion` (0 for a new one). The
|
|
225
|
+
* check and the write run without an await between them, so they can't
|
|
226
|
+
* interleave with another save. Release copies the stored draft does not use.
|
|
227
|
+
*/
|
|
228
|
+
export async function saveDraft(
|
|
229
|
+
m: MailboxContext,
|
|
230
|
+
userId: string,
|
|
231
|
+
input: DraftInput,
|
|
232
|
+
expectedVersion: number,
|
|
233
|
+
copiedKeys: string[] = []
|
|
234
|
+
) {
|
|
235
|
+
const current = getDraft(m, userId, input.id);
|
|
236
|
+
const result = writeDraft(m, userId, input, expectedVersion, current);
|
|
237
|
+
const kept = result.status === "saved" ? result.draft : current;
|
|
238
|
+
const keptKeys = new Set(kept?.attachments.map((file) => file.r2Key));
|
|
239
|
+
|
|
240
|
+
await forgetDraftFiles(
|
|
241
|
+
m,
|
|
242
|
+
[
|
|
243
|
+
...copiedKeys,
|
|
244
|
+
...(current?.attachments.map((file) => file.r2Key) ?? []),
|
|
245
|
+
].filter((key) => !keptKeys.has(key))
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
// The previous PUT may have committed before its response was lost.
|
|
249
|
+
// A replay acknowledges that exact revision without writing or announcing again.
|
|
250
|
+
if (
|
|
251
|
+
result.status === "conflict" &&
|
|
252
|
+
current?.version === expectedVersion + 1
|
|
253
|
+
) {
|
|
254
|
+
const {
|
|
255
|
+
version: _version,
|
|
256
|
+
updatedAt: _updatedAt,
|
|
257
|
+
userId: _userId,
|
|
258
|
+
...stored
|
|
259
|
+
} = current;
|
|
260
|
+
|
|
261
|
+
if (isDeepStrictEqual(input, stored)) {
|
|
262
|
+
return { status: "saved", draft: current } satisfies SaveDraftResult;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (result.status === "saved") {
|
|
267
|
+
announceDrafts(m, !current);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Tells clients the drafts changed. A new or deleted draft changes the
|
|
275
|
+
* Drafts count and list, so web clients refresh; an autosave only tells
|
|
276
|
+
* JMAP streams, without a push to phones every few seconds.
|
|
277
|
+
*/
|
|
278
|
+
export function announceDrafts(m: MailboxContext, listChanged: boolean) {
|
|
279
|
+
if (listChanged) {
|
|
280
|
+
void m.announce({ inboxChanged: false });
|
|
281
|
+
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
void m.announce({ web: false, push: false });
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function writeDraft(
|
|
289
|
+
m: MailboxContext,
|
|
290
|
+
userId: string,
|
|
291
|
+
input: DraftInput,
|
|
292
|
+
expectedVersion: number,
|
|
293
|
+
current: Draft | null
|
|
294
|
+
) {
|
|
295
|
+
if ((current?.version ?? 0) !== expectedVersion) {
|
|
296
|
+
return { status: "conflict", draft: current } satisfies SaveDraftResult;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Two reply boxes opened before the first save have different draft IDs.
|
|
300
|
+
// The editor explicitly discards the previous reply before replacing it.
|
|
301
|
+
if (input.kind === "reply" && input.conversationId) {
|
|
302
|
+
const other = m.db
|
|
303
|
+
.select()
|
|
304
|
+
.from(drafts)
|
|
305
|
+
.where(
|
|
306
|
+
and(
|
|
307
|
+
eq(drafts.userId, userId),
|
|
308
|
+
eq(drafts.kind, "reply"),
|
|
309
|
+
eq(drafts.conversationId, input.conversationId),
|
|
310
|
+
ne(drafts.id, input.id)
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
.get();
|
|
314
|
+
|
|
315
|
+
if (other) {
|
|
316
|
+
return { status: "conflict", draft: other } satisfies SaveDraftResult;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (!current) {
|
|
321
|
+
const [count] = m.db
|
|
322
|
+
.select({ value: sql<number>`count(*)` })
|
|
323
|
+
.from(drafts)
|
|
324
|
+
.where(eq(drafts.userId, userId))
|
|
325
|
+
.all();
|
|
326
|
+
|
|
327
|
+
if ((count?.value ?? 0) >= MAX_DRAFTS_PER_USER) {
|
|
328
|
+
return { status: "limit" } satisfies SaveDraftResult;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const draft: Draft = {
|
|
333
|
+
...input,
|
|
334
|
+
userId,
|
|
335
|
+
version: expectedVersion + 1,
|
|
336
|
+
updatedAt: new Date(),
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
m.db
|
|
340
|
+
.insert(drafts)
|
|
341
|
+
.values(draft)
|
|
342
|
+
.onConflictDoUpdate({ target: [drafts.userId, drafts.id], set: draft })
|
|
343
|
+
.run();
|
|
344
|
+
|
|
345
|
+
return { status: "saved", draft } satisfies SaveDraftResult;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Whether the person had a draft with this id; its files are queued for deletion. */
|
|
349
|
+
export async function deleteDraft(
|
|
350
|
+
m: MailboxContext,
|
|
351
|
+
userId: string,
|
|
352
|
+
id: string,
|
|
353
|
+
version?: number
|
|
354
|
+
) {
|
|
355
|
+
const deleted = await discardMatchingDrafts(
|
|
356
|
+
m,
|
|
357
|
+
userId,
|
|
358
|
+
eq(drafts.id, id),
|
|
359
|
+
version === undefined ? undefined : eq(drafts.version, version)
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
return deleted.length > 0;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function discardDrafts(
|
|
366
|
+
m: MailboxContext,
|
|
367
|
+
userId: string,
|
|
368
|
+
selected: Pick<Draft, "id" | "version">[]
|
|
369
|
+
) {
|
|
370
|
+
return discardMatchingDrafts(
|
|
371
|
+
m,
|
|
372
|
+
userId,
|
|
373
|
+
sql`(${drafts.id}, ${drafts.version}) in (
|
|
374
|
+
select json_extract(value, '$.id'), json_extract(value, '$.version') from json_each(${JSON.stringify(selected)})
|
|
375
|
+
)`
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function discardMatchingDrafts(
|
|
380
|
+
m: MailboxContext,
|
|
381
|
+
userId: string,
|
|
382
|
+
...conditions: (SQL | undefined)[]
|
|
383
|
+
) {
|
|
384
|
+
const { discarded, queued } = m.db.transaction(() => {
|
|
385
|
+
const deleted = m.db
|
|
386
|
+
.delete(drafts)
|
|
387
|
+
.where(and(eq(drafts.userId, userId), ...conditions))
|
|
388
|
+
.returning({ id: drafts.id, attachments: drafts.attachments })
|
|
389
|
+
.all();
|
|
390
|
+
|
|
391
|
+
const keys = deleted.flatMap((row) =>
|
|
392
|
+
row.attachments.map((file) => file.r2Key)
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
return {
|
|
396
|
+
discarded: deleted.map((row) => row.id),
|
|
397
|
+
queued: mailboxStorage.queueObjectDeletions(m, keys),
|
|
398
|
+
};
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
if (queued) await m.scheduleAlarm(Date.now() + ALARM_WAKE_DELAY_MS);
|
|
402
|
+
|
|
403
|
+
if (discarded.length) announceDrafts(m, true);
|
|
404
|
+
|
|
405
|
+
return discarded;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Queues draft files for deletion. The queue skips a key some draft still
|
|
410
|
+
* holds, so a save racing this one can't lose its file.
|
|
411
|
+
*/
|
|
412
|
+
export async function forgetDraftFiles(
|
|
413
|
+
m: MailboxContext,
|
|
414
|
+
objectKeys: string[]
|
|
415
|
+
) {
|
|
416
|
+
await mailboxStorage.forgetObjects(m, objectKeys);
|
|
417
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/** A mailbox's folders: the system ones at their places, and custom ones. */
|
|
2
|
+
import { required } from "@open-workspace/core/shared";
|
|
3
|
+
import { eq } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
DRAFTS_FOLDER_ID,
|
|
7
|
+
FOLDER_ORDER_STEP,
|
|
8
|
+
MOVABLE_SYSTEM_FOLDER_IDS,
|
|
9
|
+
folderOrderPositions,
|
|
10
|
+
isCompleteFolderOrder,
|
|
11
|
+
sortFolders,
|
|
12
|
+
} from "../../shared/folder-order";
|
|
13
|
+
import { MAX_CUSTOM_FOLDER_COUNT } from "../../shared/mail";
|
|
14
|
+
import { systemFolderDefinitions } from "./folder-model";
|
|
15
|
+
import { folders, systemFolderOrder } from "./schema";
|
|
16
|
+
import { IMPORTED_FOLDERS_KEY } from "./support";
|
|
17
|
+
import type { MailFolder, MailboxContext } from "./support";
|
|
18
|
+
|
|
19
|
+
/** The system folders at their places in the mailbox's folder order. */
|
|
20
|
+
export function systemFolders(m: MailboxContext) {
|
|
21
|
+
const stored = new Map(
|
|
22
|
+
m.db
|
|
23
|
+
.select()
|
|
24
|
+
.from(systemFolderOrder)
|
|
25
|
+
.all()
|
|
26
|
+
.map((row) => [row.folderId, row.sortOrder])
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return systemFolderDefinitions.map((folder) => ({
|
|
30
|
+
...folder,
|
|
31
|
+
sortOrder:
|
|
32
|
+
folder.id === "inbox" ? 0 : (stored.get(folder.id) ?? folder.sortOrder),
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getFolder(m: MailboxContext, id: string) {
|
|
37
|
+
const system = systemFolders(m).find((folder) => folder.id === id);
|
|
38
|
+
|
|
39
|
+
if (system) {
|
|
40
|
+
return {
|
|
41
|
+
...system,
|
|
42
|
+
kind: "system",
|
|
43
|
+
} satisfies MailFolder;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const custom = m.db.select().from(folders).where(eq(folders.id, id)).all()[0];
|
|
47
|
+
|
|
48
|
+
return custom
|
|
49
|
+
? ({ ...custom, kind: "custom", systemType: null } satisfies MailFolder)
|
|
50
|
+
: null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function folderNameTaken(
|
|
54
|
+
m: MailboxContext,
|
|
55
|
+
name: string,
|
|
56
|
+
excludingId?: string
|
|
57
|
+
) {
|
|
58
|
+
const normalized = name.toLocaleLowerCase("en-US");
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
normalized === DRAFTS_FOLDER_ID ||
|
|
62
|
+
systemFolderDefinitions.some(
|
|
63
|
+
(folder) => folder.name.toLocaleLowerCase("en-US") === normalized
|
|
64
|
+
) ||
|
|
65
|
+
m.db
|
|
66
|
+
.select({ id: folders.id, name: folders.name })
|
|
67
|
+
.from(folders)
|
|
68
|
+
.all()
|
|
69
|
+
.some(
|
|
70
|
+
(folder) =>
|
|
71
|
+
folder.id !== excludingId &&
|
|
72
|
+
folder.name.toLocaleLowerCase("en-US") === normalized
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createFolder(m: MailboxContext, id: string, name: string) {
|
|
78
|
+
const result = insertFolder(m, id, name);
|
|
79
|
+
|
|
80
|
+
if (result.status === "ok") void m.announce({ inboxChanged: false });
|
|
81
|
+
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Adds a custom folder. It goes after Inbox and the custom folders right
|
|
87
|
+
* after it, before the first system folder; `place: "end"` puts it after
|
|
88
|
+
* every folder instead, for a restore that then sets its saved place.
|
|
89
|
+
*/
|
|
90
|
+
export function insertFolder(
|
|
91
|
+
m: MailboxContext,
|
|
92
|
+
id: string,
|
|
93
|
+
name: string,
|
|
94
|
+
{ place = "after-inbox" }: { place?: "after-inbox" | "end" } = {}
|
|
95
|
+
) {
|
|
96
|
+
const normalizedName = name.trim();
|
|
97
|
+
|
|
98
|
+
if (!normalizedName || normalizedName.length > 80) {
|
|
99
|
+
return { status: "invalid" as const };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const existing = m.db
|
|
103
|
+
.select({
|
|
104
|
+
id: folders.id,
|
|
105
|
+
name: folders.name,
|
|
106
|
+
sortOrder: folders.sortOrder,
|
|
107
|
+
})
|
|
108
|
+
.from(folders)
|
|
109
|
+
.all();
|
|
110
|
+
|
|
111
|
+
if (existing.length >= MAX_CUSTOM_FOLDER_COUNT) {
|
|
112
|
+
return { status: "limit" as const };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (folderNameTaken(m, normalizedName)) {
|
|
116
|
+
return { status: "conflict" as const };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const system = systemFolders(m);
|
|
120
|
+
|
|
121
|
+
const last = Math.max(
|
|
122
|
+
...existing.map((item) => item.sortOrder),
|
|
123
|
+
...system.map((item) => item.sortOrder)
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const folder = m.db
|
|
127
|
+
.insert(folders)
|
|
128
|
+
.values({ id, name: normalizedName, sortOrder: last + FOLDER_ORDER_STEP })
|
|
129
|
+
.returning()
|
|
130
|
+
.get();
|
|
131
|
+
|
|
132
|
+
if (place === "after-inbox") {
|
|
133
|
+
const order = sortFolders([...system, ...existing])
|
|
134
|
+
.map((item) => item.id)
|
|
135
|
+
.filter((itemId) => itemId !== "inbox");
|
|
136
|
+
|
|
137
|
+
const firstSystem = order.findIndex((itemId) =>
|
|
138
|
+
MOVABLE_SYSTEM_FOLDER_IDS.some((systemId) => systemId === itemId)
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
order.splice(firstSystem === -1 ? order.length : firstSystem, 0, id);
|
|
142
|
+
writeFolderOrder(m, order);
|
|
143
|
+
folder.sortOrder = required(
|
|
144
|
+
folderOrderPositions(order).find((item) => item.id === id),
|
|
145
|
+
"the new folder is in the order"
|
|
146
|
+
).sortOrder;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { status: "ok" as const, folder };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function renameFolder(m: MailboxContext, id: string, name: string) {
|
|
153
|
+
const normalizedName = name.trim();
|
|
154
|
+
|
|
155
|
+
if (!normalizedName || normalizedName.length > 80) {
|
|
156
|
+
return { status: "invalid" as const };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
!getFolder(m, id) ||
|
|
161
|
+
systemFolderDefinitions.some((item) => item.id === id)
|
|
162
|
+
) {
|
|
163
|
+
return { status: "not_found" as const };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (folderNameTaken(m, normalizedName, id)) {
|
|
167
|
+
return { status: "conflict" as const };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const [folder] = m.db
|
|
171
|
+
.update(folders)
|
|
172
|
+
.set({ name: normalizedName })
|
|
173
|
+
.where(eq(folders.id, id))
|
|
174
|
+
.returning()
|
|
175
|
+
.all();
|
|
176
|
+
|
|
177
|
+
if (!folder) return { status: "not_found" as const };
|
|
178
|
+
void m.announce({ inboxChanged: false });
|
|
179
|
+
|
|
180
|
+
return { status: "ok" as const, folder };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function deleteFolder(m: MailboxContext, id: string) {
|
|
184
|
+
const deleted = m.db
|
|
185
|
+
.delete(folders)
|
|
186
|
+
.where(eq(folders.id, id))
|
|
187
|
+
.returning({ id: folders.id })
|
|
188
|
+
.all()[0];
|
|
189
|
+
|
|
190
|
+
if (!deleted) return { status: "not_found" as const };
|
|
191
|
+
void m.announce({ inboxChanged: false });
|
|
192
|
+
|
|
193
|
+
return { status: "ok" as const };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Sets the one order of every folder but Inbox: each system folder and
|
|
198
|
+
* custom folder exactly once, so system folders can sit among custom ones.
|
|
199
|
+
*/
|
|
200
|
+
export function reorderFolders(m: MailboxContext, folderIds: string[]) {
|
|
201
|
+
const customIds = m.db
|
|
202
|
+
.select({ id: folders.id })
|
|
203
|
+
.from(folders)
|
|
204
|
+
.all()
|
|
205
|
+
.map((folder) => folder.id);
|
|
206
|
+
|
|
207
|
+
if (!isCompleteFolderOrder(folderIds, customIds)) {
|
|
208
|
+
return { status: "conflict" as const };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
writeFolderOrder(m, folderIds);
|
|
212
|
+
// Every folder is now where the person put it, so a later restore leaves
|
|
213
|
+
// the imports' folders where they are too.
|
|
214
|
+
m.state.storage.kv.delete(IMPORTED_FOLDERS_KEY);
|
|
215
|
+
void m.announce({ inboxChanged: false });
|
|
216
|
+
|
|
217
|
+
return { status: "ok" as const };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Stores a complete order of every folder but Inbox (see `reorderFolders`).
|
|
222
|
+
* Synchronous writes, so they land together, also inside an import's or a
|
|
223
|
+
* restore's transaction.
|
|
224
|
+
*/
|
|
225
|
+
function writeFolderOrder(m: MailboxContext, folderIds: string[]) {
|
|
226
|
+
const system = new Set<string>(MOVABLE_SYSTEM_FOLDER_IDS);
|
|
227
|
+
|
|
228
|
+
for (const { id, sortOrder } of folderOrderPositions(folderIds)) {
|
|
229
|
+
if (system.has(id)) {
|
|
230
|
+
m.db
|
|
231
|
+
.insert(systemFolderOrder)
|
|
232
|
+
.values({ folderId: id, sortOrder })
|
|
233
|
+
.onConflictDoUpdate({
|
|
234
|
+
target: systemFolderOrder.folderId,
|
|
235
|
+
set: { sortOrder },
|
|
236
|
+
})
|
|
237
|
+
.run();
|
|
238
|
+
} else {
|
|
239
|
+
m.db.update(folders).set({ sortOrder }).where(eq(folders.id, id)).run();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|