@open-workspace/email 0.4.2 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail-sender-intel.ts +5 -1
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +52 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +391 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +12 -11
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +758 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +119 -1816
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +700 -0
- package/worker/mail/auth-headers.ts +57 -43
- package/worker/mail/auto-reply-routes.ts +260 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound-threading.ts +15 -2
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +19 -8
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +698 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +206 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +316 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +455 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +253 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +161 -1
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { and, eq, gt, isNull, lte, max, min, or } from "drizzle-orm";
|
|
2
|
+
|
|
3
|
+
import { jmapChanges as log } from "./schema";
|
|
4
|
+
import type { MailboxContext, MailboxDatabase } from "./support";
|
|
5
|
+
|
|
6
|
+
/** The newest change's number in the change log (jmap_changes), or 0. */
|
|
7
|
+
export function currentSeq(db: MailboxDatabase) {
|
|
8
|
+
return (
|
|
9
|
+
db
|
|
10
|
+
.select({ seq: max(log.seq) })
|
|
11
|
+
.from(log)
|
|
12
|
+
.get()?.seq ?? 0
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The mailbox's JMAP state: `c` and the change log's newest entry. The
|
|
18
|
+
* letter names this format, so a state any other way of counting gave (0.4's
|
|
19
|
+
* plain counter) never reads as a position in the log: the client gets
|
|
20
|
+
* cannotCalculateChanges and resyncs instead of missing changes.
|
|
21
|
+
*/
|
|
22
|
+
export function stateOf(seq: number) {
|
|
23
|
+
return `c${seq}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function jmapState(db: MailboxDatabase) {
|
|
27
|
+
return stateOf(currentSeq(db));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Moves the state on for a change no trigger records (the log's triggers
|
|
32
|
+
* cover what JMAP shows), so streams and push see something happened.
|
|
33
|
+
*/
|
|
34
|
+
export function bumpJmapState(db: MailboxDatabase) {
|
|
35
|
+
db.insert(log).values({ type: "State", id: "", kind: "updated" }).run();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type JmapChangeType = "Email" | "Thread" | "Mailbox";
|
|
39
|
+
|
|
40
|
+
export type JmapChanges = {
|
|
41
|
+
oldState: string;
|
|
42
|
+
newState: string;
|
|
43
|
+
hasMoreChanges: boolean;
|
|
44
|
+
created: string[];
|
|
45
|
+
updated: string[];
|
|
46
|
+
destroyed: string[];
|
|
47
|
+
/** Mailbox only: an email changed, so mailbox counts may have too. */
|
|
48
|
+
emailsChanged: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The change log position a JMAP state names, if this log can answer from
|
|
53
|
+
* it: a `c<seq>` state, not newer than the log, not older than its oldest
|
|
54
|
+
* kept entry.
|
|
55
|
+
*/
|
|
56
|
+
export function stateSeq(db: MailboxDatabase, state: string, current: number) {
|
|
57
|
+
const match = /^c(\d+)$/u.exec(state);
|
|
58
|
+
|
|
59
|
+
if (!match) return null;
|
|
60
|
+
const since = Number(match[1]);
|
|
61
|
+
|
|
62
|
+
const oldest =
|
|
63
|
+
db
|
|
64
|
+
.select({ seq: min(log.seq) })
|
|
65
|
+
.from(log)
|
|
66
|
+
.get()?.seq ?? current + 1;
|
|
67
|
+
|
|
68
|
+
return since > current || since < oldest - 1 ? null : since;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The ids of `type` that changed in any way since `state`, as `userId` sees
|
|
73
|
+
* them. The caller has already checked the state.
|
|
74
|
+
*/
|
|
75
|
+
export function changedSince(
|
|
76
|
+
db: MailboxDatabase,
|
|
77
|
+
type: JmapChangeType,
|
|
78
|
+
since: number,
|
|
79
|
+
userId: string
|
|
80
|
+
) {
|
|
81
|
+
const rows = db
|
|
82
|
+
.selectDistinct({ id: log.id })
|
|
83
|
+
.from(log)
|
|
84
|
+
.where(
|
|
85
|
+
and(
|
|
86
|
+
eq(log.type, type),
|
|
87
|
+
gt(log.seq, since),
|
|
88
|
+
or(isNull(log.userId), eq(log.userId, userId))
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
.all();
|
|
92
|
+
|
|
93
|
+
return new Set(rows.map((row) => row.id));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Foo/changes from the change log: the objects of `type` created, updated
|
|
98
|
+
* or destroyed since `sinceState`, as `userId` sees them (rows for another
|
|
99
|
+
* person's drafts or read state are left out), at most `maxChanges` ids.
|
|
100
|
+
* Null when `sinceState` can't be answered: not a state of this log, newer
|
|
101
|
+
* than it, or older than its oldest kept entry.
|
|
102
|
+
*/
|
|
103
|
+
export function jmapChanges(
|
|
104
|
+
m: MailboxContext,
|
|
105
|
+
input: {
|
|
106
|
+
type: JmapChangeType;
|
|
107
|
+
sinceState: string;
|
|
108
|
+
userId: string;
|
|
109
|
+
maxChanges: number | null;
|
|
110
|
+
}
|
|
111
|
+
) {
|
|
112
|
+
const current = currentSeq(m.db);
|
|
113
|
+
const since = stateSeq(m.db, input.sinceState, current);
|
|
114
|
+
|
|
115
|
+
if (since === null) return null;
|
|
116
|
+
|
|
117
|
+
// Iterate the native cursor so maxChanges can stop before materializing the journal.
|
|
118
|
+
const rows = m.state.storage.sql.exec<{
|
|
119
|
+
seq: number;
|
|
120
|
+
id: string;
|
|
121
|
+
kind: string;
|
|
122
|
+
}>(
|
|
123
|
+
`select seq, id, kind from jmap_changes
|
|
124
|
+
where type = ? and seq > ? and (user_id is null or user_id = ?)
|
|
125
|
+
order by seq`,
|
|
126
|
+
input.type,
|
|
127
|
+
since,
|
|
128
|
+
input.userId
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
// Each id's first creation or destruction in range, and its last change:
|
|
132
|
+
// whether it existed before and exists now decide created, updated or
|
|
133
|
+
// destroyed. (Updates don't say: a conversation can be noted as updated
|
|
134
|
+
// by its first email just before its own row is written.)
|
|
135
|
+
const seen = new Map<string, { lifecycle: string | null; last: string }>();
|
|
136
|
+
let newState = current;
|
|
137
|
+
let hasMoreChanges = false;
|
|
138
|
+
|
|
139
|
+
for (const row of rows) {
|
|
140
|
+
const known = seen.get(row.id);
|
|
141
|
+
|
|
142
|
+
if (!known && input.maxChanges !== null && seen.size >= input.maxChanges) {
|
|
143
|
+
// Stop before this id; the next call goes on from here.
|
|
144
|
+
newState = row.seq - 1;
|
|
145
|
+
hasMoreChanges = true;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
seen.set(row.id, {
|
|
150
|
+
lifecycle: known?.lifecycle ?? (row.kind === "updated" ? null : row.kind),
|
|
151
|
+
last: row.kind,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const created: string[] = [];
|
|
156
|
+
const updated: string[] = [];
|
|
157
|
+
const destroyed: string[] = [];
|
|
158
|
+
|
|
159
|
+
for (const [id, { lifecycle, last }] of seen) {
|
|
160
|
+
const existedBefore = lifecycle !== "created";
|
|
161
|
+
const existsNow = last !== "destroyed";
|
|
162
|
+
|
|
163
|
+
if (existedBefore && existsNow) updated.push(id);
|
|
164
|
+
else if (existsNow) created.push(id);
|
|
165
|
+
else if (existedBefore) destroyed.push(id);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const emailsChanged =
|
|
169
|
+
input.type === "Mailbox" &&
|
|
170
|
+
Boolean(
|
|
171
|
+
m.db
|
|
172
|
+
.select({ seq: log.seq })
|
|
173
|
+
.from(log)
|
|
174
|
+
.where(
|
|
175
|
+
and(
|
|
176
|
+
eq(log.type, "Email"),
|
|
177
|
+
gt(log.seq, since),
|
|
178
|
+
lte(log.seq, newState),
|
|
179
|
+
or(isNull(log.userId), eq(log.userId, input.userId))
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
.limit(1)
|
|
183
|
+
.get()
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
oldState: stateOf(since),
|
|
188
|
+
newState: stateOf(newState),
|
|
189
|
+
hasMoreChanges,
|
|
190
|
+
created,
|
|
191
|
+
updated,
|
|
192
|
+
destroyed,
|
|
193
|
+
emailsChanged,
|
|
194
|
+
} satisfies JmapChanges;
|
|
195
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { inJsonList } from "@open-workspace/core/module";
|
|
2
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
3
|
+
import { alias } from "drizzle-orm/sqlite-core";
|
|
4
|
+
|
|
5
|
+
import { replyThreadHeaders } from "../mail/rfc";
|
|
6
|
+
import { jmapState } from "./jmap-change-log";
|
|
7
|
+
import { emailPlacement } from "./jmap-store";
|
|
8
|
+
import {
|
|
9
|
+
draftHtmlFallback,
|
|
10
|
+
draftReplyParent,
|
|
11
|
+
replySenderColumns,
|
|
12
|
+
} from "./mailbox-drafts";
|
|
13
|
+
import { drafts, emails } from "./schema";
|
|
14
|
+
import type { MailboxContext } from "./support";
|
|
15
|
+
|
|
16
|
+
/** Read the requested Email/get fields in batches, within the DO's SQL bind limit. */
|
|
17
|
+
export function jmapEmails(
|
|
18
|
+
m: MailboxContext,
|
|
19
|
+
userId: string,
|
|
20
|
+
ids: string[],
|
|
21
|
+
properties: readonly string[] | null = null
|
|
22
|
+
) {
|
|
23
|
+
const wants = (...names: string[]) =>
|
|
24
|
+
!properties || names.some((name) => properties.includes(name));
|
|
25
|
+
|
|
26
|
+
const structure = wants("bodyStructure", "htmlBody");
|
|
27
|
+
const text = structure || wants("textBody", "bodyValues");
|
|
28
|
+
const files = wants("hasAttachment", "attachments", "bodyStructure");
|
|
29
|
+
const dates = wants("receivedAt", "sentAt");
|
|
30
|
+
const threadHeaders = wants("inReplyTo", "references");
|
|
31
|
+
const messageIds = ids.filter((id) => !id.startsWith("drf_"));
|
|
32
|
+
const draftIds = ids.filter((id) => id.startsWith("drf_"));
|
|
33
|
+
|
|
34
|
+
const rows = messageIds.length
|
|
35
|
+
? m.db
|
|
36
|
+
.select({
|
|
37
|
+
id: emails.id,
|
|
38
|
+
...(wants("subject") && { subject: emails.subject }),
|
|
39
|
+
...(wants("preview") && { preview: emails.preview }),
|
|
40
|
+
...(wants("receivedAt") && { timelineAt: emails.timelineAt }),
|
|
41
|
+
// Mail stored before sent_at and raw_size were kept has neither:
|
|
42
|
+
// its own timestamp stands for one and its text and files for the
|
|
43
|
+
// other, without a storage request per message.
|
|
44
|
+
...(wants("sentAt") && {
|
|
45
|
+
sentAt:
|
|
46
|
+
sql`coalesce(${emails.sentAt}, ${emails.timelineAt})`.mapWith(
|
|
47
|
+
emails.sentAt
|
|
48
|
+
),
|
|
49
|
+
}),
|
|
50
|
+
...(wants("size") && {
|
|
51
|
+
rawSize: sql<number>`coalesce(${emails.rawSize},
|
|
52
|
+
length(cast(coalesce(${emails.bodyText}, '') as blob)) +
|
|
53
|
+
coalesce((select sum(value ->> 'size') from json_each(${emails.attachmentsJson})), 0))`.mapWith(
|
|
54
|
+
Number
|
|
55
|
+
),
|
|
56
|
+
}),
|
|
57
|
+
...(wants("threadId") && { conversationId: emails.conversationId }),
|
|
58
|
+
...(wants("messageId") && {
|
|
59
|
+
messageIdHeader: emails.messageIdHeader,
|
|
60
|
+
}),
|
|
61
|
+
...(wants("inReplyTo") && { inReplyToJson: emails.inReplyToJson }),
|
|
62
|
+
...(wants("references") && { referencesJson: emails.referencesJson }),
|
|
63
|
+
...(wants("from") && { fromJson: emails.fromJson }),
|
|
64
|
+
...(wants("to") && { toJson: emails.toJson }),
|
|
65
|
+
...(wants("cc") && { ccJson: emails.ccJson }),
|
|
66
|
+
...(wants("bcc") && { bccJson: emails.bccJson }),
|
|
67
|
+
...(wants("replyTo") && { replyToJson: emails.replyToJson }),
|
|
68
|
+
...(text && {
|
|
69
|
+
direction: emails.direction,
|
|
70
|
+
bodyText: emails.bodyText,
|
|
71
|
+
quotedText: emails.quotedText,
|
|
72
|
+
}),
|
|
73
|
+
...((structure || wants("bodyValues")) && {
|
|
74
|
+
bodyHtmlR2Key: emails.bodyHtmlR2Key,
|
|
75
|
+
requestFingerprint: emails.requestFingerprint,
|
|
76
|
+
}),
|
|
77
|
+
...(files && { attachmentsJson: emails.attachmentsJson }),
|
|
78
|
+
})
|
|
79
|
+
.from(emails)
|
|
80
|
+
.where(inJsonList(emails.id, messageIds))
|
|
81
|
+
.all()
|
|
82
|
+
: [];
|
|
83
|
+
|
|
84
|
+
const byId = new Map(rows.map((row) => [row.id, row]));
|
|
85
|
+
|
|
86
|
+
const placement = wants("mailboxIds", "keywords")
|
|
87
|
+
? emailPlacement(m.db, userId, messageIds)
|
|
88
|
+
: null;
|
|
89
|
+
|
|
90
|
+
const forwardBody = text || wants("preview", "size");
|
|
91
|
+
const forwardContent = forwardBody || files;
|
|
92
|
+
const source = alias(emails, "forward_source");
|
|
93
|
+
|
|
94
|
+
const draftQuery = m.db
|
|
95
|
+
.select({
|
|
96
|
+
id: drafts.id,
|
|
97
|
+
...(wants("subject") && { subject: drafts.subject }),
|
|
98
|
+
...(dates && { updatedAt: drafts.updatedAt }),
|
|
99
|
+
...(wants("to") && { to: drafts.to }),
|
|
100
|
+
...(wants("cc") && { cc: drafts.cc }),
|
|
101
|
+
...(wants("bcc") && { bcc: drafts.bcc }),
|
|
102
|
+
...(wants("replyTo") && { replyTo: drafts.replyTo }),
|
|
103
|
+
...((text || wants("preview", "size")) && { bodyText: drafts.bodyText }),
|
|
104
|
+
...(forwardBody && {
|
|
105
|
+
bodyHtml:
|
|
106
|
+
structure || wants("bodyValues")
|
|
107
|
+
? drafts.bodyHtml
|
|
108
|
+
: draftHtmlFallback,
|
|
109
|
+
}),
|
|
110
|
+
...((files || forwardBody) && { attachments: drafts.attachments }),
|
|
111
|
+
...(forwardContent && {
|
|
112
|
+
forwardId: source.id,
|
|
113
|
+
sourceFiles: source.attachmentsJson,
|
|
114
|
+
selectedIds: drafts.forwardedAttachmentIds,
|
|
115
|
+
}),
|
|
116
|
+
...(forwardBody && {
|
|
117
|
+
forwardBody: {
|
|
118
|
+
id: source.id,
|
|
119
|
+
fromJson: source.fromJson,
|
|
120
|
+
toJson: source.toJson,
|
|
121
|
+
ccJson: source.ccJson,
|
|
122
|
+
subject: source.subject,
|
|
123
|
+
timelineAt: source.timelineAt,
|
|
124
|
+
direction: source.direction,
|
|
125
|
+
bodyText: source.bodyText,
|
|
126
|
+
quotedText: source.quotedText,
|
|
127
|
+
preview: source.preview,
|
|
128
|
+
bodyHtmlR2Key: source.bodyHtmlR2Key,
|
|
129
|
+
requestFingerprint: source.requestFingerprint,
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
...(wants("from") && {
|
|
133
|
+
replySenderSource: replySenderColumns,
|
|
134
|
+
}),
|
|
135
|
+
...(threadHeaders && {
|
|
136
|
+
parentMessageId: emails.messageIdHeader,
|
|
137
|
+
parentInReplyTo: emails.inReplyToJson,
|
|
138
|
+
parentReferences: emails.referencesJson,
|
|
139
|
+
}),
|
|
140
|
+
})
|
|
141
|
+
.from(drafts)
|
|
142
|
+
.where(and(eq(drafts.userId, userId), inJsonList(drafts.id, draftIds)));
|
|
143
|
+
|
|
144
|
+
// A reply inherits its sender and thread headers from its parent.
|
|
145
|
+
const withHeaders =
|
|
146
|
+
threadHeaders || wants("from")
|
|
147
|
+
? draftQuery.leftJoin(emails, draftReplyParent)
|
|
148
|
+
: draftQuery;
|
|
149
|
+
|
|
150
|
+
const withSource = forwardContent
|
|
151
|
+
? withHeaders
|
|
152
|
+
.$dynamic()
|
|
153
|
+
.leftJoin(
|
|
154
|
+
source,
|
|
155
|
+
and(eq(drafts.kind, "forward"), eq(drafts.sourceEmailId, source.id))
|
|
156
|
+
)
|
|
157
|
+
: withHeaders;
|
|
158
|
+
|
|
159
|
+
const saved = draftIds.length ? withSource.all() : [];
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
state: jmapState(m.db),
|
|
163
|
+
emails: messageIds.flatMap((id) => {
|
|
164
|
+
const email = byId.get(id);
|
|
165
|
+
|
|
166
|
+
if (!email) return [];
|
|
167
|
+
const place = placement?.get(id);
|
|
168
|
+
|
|
169
|
+
return [{ email, mailboxIds: place?.mailboxIds, seen: place?.seen }];
|
|
170
|
+
}),
|
|
171
|
+
drafts: saved.map(
|
|
172
|
+
({ parentMessageId, parentInReplyTo, parentReferences, ...draft }) => ({
|
|
173
|
+
...draft,
|
|
174
|
+
thread: replyThreadHeaders({
|
|
175
|
+
messageIdHeader: parentMessageId ?? null,
|
|
176
|
+
inReplyToJson: parentInReplyTo ?? [],
|
|
177
|
+
referencesJson: parentReferences ?? [],
|
|
178
|
+
}),
|
|
179
|
+
})
|
|
180
|
+
),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export type JmapEmailRow = ReturnType<typeof jmapEmails>["emails"][number];
|
|
185
|
+
|
|
186
|
+
export type JmapDraftRow = ReturnType<typeof jmapEmails>["drafts"][number];
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { asc, count, eq } from "drizzle-orm";
|
|
2
|
+
|
|
3
|
+
import { DRAFTS_FOLDER_ID } from "../../shared/folder-order";
|
|
4
|
+
import {
|
|
5
|
+
defaultSystemFolderOrder,
|
|
6
|
+
systemFolderDefinitions,
|
|
7
|
+
} from "./folder-model";
|
|
8
|
+
import { EMAIL_MAILBOXES_SQL, unread } from "./jmap-query-filter";
|
|
9
|
+
import { systemFolders } from "./mailbox-folders";
|
|
10
|
+
import { drafts, folders } from "./schema";
|
|
11
|
+
import type { MailboxContext } from "./support";
|
|
12
|
+
|
|
13
|
+
export function mailboxIds(m: MailboxContext) {
|
|
14
|
+
const custom = m.db
|
|
15
|
+
.select({ id: folders.id })
|
|
16
|
+
.from(folders)
|
|
17
|
+
.orderBy(asc(folders.sortOrder), asc(folders.name))
|
|
18
|
+
.all();
|
|
19
|
+
|
|
20
|
+
return [...systemFolderDefinitions, ...custom, { id: DRAFTS_FOLDER_ID }].map(
|
|
21
|
+
(row) => row.id
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function messageCounts(sql: SqlStorage, userId: string) {
|
|
26
|
+
const isUnread = unread(userId);
|
|
27
|
+
|
|
28
|
+
return new Map(
|
|
29
|
+
sql
|
|
30
|
+
.exec<{
|
|
31
|
+
mailbox: string;
|
|
32
|
+
total: number;
|
|
33
|
+
unread: number;
|
|
34
|
+
threads: number;
|
|
35
|
+
unread_threads: number;
|
|
36
|
+
}>(
|
|
37
|
+
`
|
|
38
|
+
select mailbox,
|
|
39
|
+
count(*) as total,
|
|
40
|
+
sum(is_unread) as unread,
|
|
41
|
+
count(distinct conversation_id) as threads,
|
|
42
|
+
count(distinct case when is_unread then conversation_id end) as unread_threads
|
|
43
|
+
from (
|
|
44
|
+
select m.value as mailbox, e.conversation_id, ${isUnread.sql} as is_unread
|
|
45
|
+
from emails e join conversations c on c.id = e.conversation_id,
|
|
46
|
+
json_each(${EMAIL_MAILBOXES_SQL}) m
|
|
47
|
+
)
|
|
48
|
+
group by mailbox`,
|
|
49
|
+
...isUnread.params
|
|
50
|
+
)
|
|
51
|
+
.toArray()
|
|
52
|
+
.map((row) => [row.mailbox, row])
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Without counts, Mailbox/query, import and metadata-only gets read just the folders. */
|
|
57
|
+
export function mailboxes(
|
|
58
|
+
m: MailboxContext,
|
|
59
|
+
userId: string,
|
|
60
|
+
withCounts: boolean
|
|
61
|
+
) {
|
|
62
|
+
const system = systemFolders(m).map(({ id, name, sortOrder }) => ({
|
|
63
|
+
id,
|
|
64
|
+
name,
|
|
65
|
+
sortOrder,
|
|
66
|
+
role: id === "spam" ? "junk" : id,
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
const custom = m.db
|
|
70
|
+
.select({
|
|
71
|
+
id: folders.id,
|
|
72
|
+
name: folders.name,
|
|
73
|
+
sortOrder: folders.sortOrder,
|
|
74
|
+
})
|
|
75
|
+
.from(folders)
|
|
76
|
+
.orderBy(asc(folders.sortOrder), asc(folders.name))
|
|
77
|
+
.all();
|
|
78
|
+
|
|
79
|
+
const sent = system.find((row) => row.id === "sent");
|
|
80
|
+
|
|
81
|
+
const rows = [
|
|
82
|
+
...system,
|
|
83
|
+
...custom.map((folder) => ({ ...folder, role: null })),
|
|
84
|
+
{
|
|
85
|
+
id: DRAFTS_FOLDER_ID,
|
|
86
|
+
name: "Drafts",
|
|
87
|
+
role: "drafts",
|
|
88
|
+
sortOrder: (sent?.sortOrder ?? defaultSystemFolderOrder("sent")) + 1,
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
if (!withCounts) return rows;
|
|
93
|
+
const counts = messageCounts(m.state.storage.sql, userId);
|
|
94
|
+
|
|
95
|
+
const draftCount =
|
|
96
|
+
m.db
|
|
97
|
+
.select({ total: count() })
|
|
98
|
+
.from(drafts)
|
|
99
|
+
.where(eq(drafts.userId, userId))
|
|
100
|
+
.get()?.total ?? 0;
|
|
101
|
+
|
|
102
|
+
return rows.map((row) => {
|
|
103
|
+
const countsHere = counts.get(row.id);
|
|
104
|
+
const isDrafts = row.id === DRAFTS_FOLDER_ID;
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
...row,
|
|
108
|
+
totalEmails: isDrafts ? draftCount : (countsHere?.total ?? 0),
|
|
109
|
+
unreadEmails: isDrafts ? 0 : (countsHere?.unread ?? 0),
|
|
110
|
+
totalThreads: isDrafts ? draftCount : (countsHere?.threads ?? 0),
|
|
111
|
+
unreadThreads: isDrafts ? 0 : (countsHere?.unread_threads ?? 0),
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type JmapMailboxRow = ReturnType<typeof mailboxes>[number];
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { DRAFTS_FOLDER_ID } from "../../shared/folder-order";
|
|
2
|
+
import type { ImportPlacement } from "../../shared/mail-import";
|
|
3
|
+
import { systemFolderDefinitions } from "./folder-model";
|
|
4
|
+
import type { Placement } from "./placement";
|
|
5
|
+
|
|
6
|
+
/** How an Email/set patch changes an email's Mailboxes: all of them, or some added and removed. */
|
|
7
|
+
export type MailboxChange = {
|
|
8
|
+
replace: string[] | null;
|
|
9
|
+
add: string[];
|
|
10
|
+
remove: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where an email's conversation goes for a change to its Mailboxes
|
|
15
|
+
* (conversations move as a whole, as in the app), decided by what the
|
|
16
|
+
* client added and removed, since a move takes one Mailbox off and puts
|
|
17
|
+
* another on while the email may be in two:
|
|
18
|
+
*
|
|
19
|
+
* - Trash, then Spam, added: there.
|
|
20
|
+
* - A custom folder added: the Inbox, with that folder.
|
|
21
|
+
* - Inbox added: the Inbox, out of any folder taken off.
|
|
22
|
+
* - Archive added, or Inbox taken off (a label's Inbox): Archive.
|
|
23
|
+
* - A folder taken off alone: the Inbox, out of it.
|
|
24
|
+
* - Sent follows the email's direction and decides nothing.
|
|
25
|
+
*
|
|
26
|
+
* "unchanged" when only Sent was touched; null when the change can't apply:
|
|
27
|
+
* Drafts, an unknown folder, two folders, or nothing left.
|
|
28
|
+
*/
|
|
29
|
+
export function placementForChange(
|
|
30
|
+
folderExists: (id: string) => boolean,
|
|
31
|
+
email: { mailboxIds: readonly string[]; sent: boolean },
|
|
32
|
+
change: MailboxChange
|
|
33
|
+
) {
|
|
34
|
+
const current = email.mailboxIds;
|
|
35
|
+
const before = new Set(current);
|
|
36
|
+
const after = new Set(change.replace ?? current);
|
|
37
|
+
|
|
38
|
+
for (const id of change.add) after.add(id);
|
|
39
|
+
|
|
40
|
+
for (const id of change.remove) after.delete(id);
|
|
41
|
+
|
|
42
|
+
const added = [...after].filter((id) => !before.has(id) && id !== "sent");
|
|
43
|
+
const removed = [...before].filter((id) => !after.has(id) && id !== "sent");
|
|
44
|
+
|
|
45
|
+
if (!after.size || after.has(DRAFTS_FOLDER_ID)) return null;
|
|
46
|
+
|
|
47
|
+
// Sent alone holds sent mail only: received mail can't go there.
|
|
48
|
+
if (after.size === 1 && after.has("sent") && !email.sent) return null;
|
|
49
|
+
|
|
50
|
+
const isCustom = (id: string) =>
|
|
51
|
+
!systemFolderDefinitions.some((mailbox) => mailbox.id === id);
|
|
52
|
+
|
|
53
|
+
const addedFolders = added.filter(isCustom);
|
|
54
|
+
|
|
55
|
+
if (addedFolders.length > 1) return null;
|
|
56
|
+
const [folderId] = addedFolders;
|
|
57
|
+
|
|
58
|
+
if (folderId && (email.sent || !folderExists(folderId))) return null;
|
|
59
|
+
|
|
60
|
+
// Leaving the Inbox keeps the conversation's folder for when it returns, as the app does.
|
|
61
|
+
if (added.includes("trash")) {
|
|
62
|
+
return { mailboxState: "trash" } satisfies Placement;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (added.includes("spam")) {
|
|
66
|
+
if (email.sent) return null;
|
|
67
|
+
|
|
68
|
+
return { mailboxState: "spam" } satisfies Placement;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (folderId) return { mailboxState: "active", folderId } satisfies Placement;
|
|
72
|
+
|
|
73
|
+
if (added.includes("inbox")) {
|
|
74
|
+
return removed.some(isCustom)
|
|
75
|
+
? ({ mailboxState: "active", folderId: null } satisfies Placement)
|
|
76
|
+
: ({ mailboxState: "active" } satisfies Placement);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (added.includes("archive") || removed.includes("inbox")) {
|
|
80
|
+
return { mailboxState: "archive" } satisfies Placement;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (removed.some(isCustom)) {
|
|
84
|
+
return { mailboxState: "active", folderId: null } satisfies Placement;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Taken out of Trash, Spam or Archive with nothing new: back to the Inbox.
|
|
88
|
+
if (removed.length) return { mailboxState: "active" } satisfies Placement;
|
|
89
|
+
|
|
90
|
+
// Nothing changes but Sent.
|
|
91
|
+
return added.length ? null : "unchanged";
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** An import names all its mailboxes at once. Reject combinations that reading it back would drop. */
|
|
95
|
+
export function placementForImport(
|
|
96
|
+
folderExists: (id: string) => boolean,
|
|
97
|
+
mailboxIds: string[]
|
|
98
|
+
) {
|
|
99
|
+
const sent = mailboxIds.includes("sent");
|
|
100
|
+
|
|
101
|
+
const place = placementForChange(
|
|
102
|
+
folderExists,
|
|
103
|
+
{ sent, mailboxIds: [] },
|
|
104
|
+
{
|
|
105
|
+
replace: mailboxIds,
|
|
106
|
+
add: [],
|
|
107
|
+
remove: [],
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (!place) return null;
|
|
112
|
+
|
|
113
|
+
const placement: ImportPlacement =
|
|
114
|
+
place === "unchanged"
|
|
115
|
+
? "sent"
|
|
116
|
+
: place.mailboxState === "active"
|
|
117
|
+
? "inbox"
|
|
118
|
+
: place.mailboxState;
|
|
119
|
+
|
|
120
|
+
const folderId = place === "unchanged" ? null : (place.folderId ?? null);
|
|
121
|
+
|
|
122
|
+
// A label also appears in Inbox, and archived outgoing mail also appears in Sent.
|
|
123
|
+
if (mailboxIds.length > 1) {
|
|
124
|
+
const inboxLabel = folderId && mailboxIds.includes("inbox");
|
|
125
|
+
const archivedSent = sent && placement === "archive";
|
|
126
|
+
|
|
127
|
+
if (mailboxIds.length !== 2 || !(inboxLabel || archivedSent)) return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
placement,
|
|
132
|
+
folderId,
|
|
133
|
+
direction: sent
|
|
134
|
+
? ("outgoing" as const)
|
|
135
|
+
: placement === "inbox"
|
|
136
|
+
? ("incoming" as const)
|
|
137
|
+
: null,
|
|
138
|
+
};
|
|
139
|
+
}
|