@open-workspace/email 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InfiniteData,
|
|
3
|
+
Query,
|
|
4
|
+
QueryClient,
|
|
5
|
+
QueryKey,
|
|
6
|
+
} from "@tanstack/react-query";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
|
|
9
|
+
import type { Mailbox, MailFolder } from "./types";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Mail data in React Query carries the mailbox change counter it reflects
|
|
13
|
+
* (`version`, per page for paged lists). The realtime socket announces the
|
|
14
|
+
* mailbox's current counter when it opens and after every change, and only
|
|
15
|
+
* data older than that is refetched.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type Versioned = { version: number };
|
|
19
|
+
|
|
20
|
+
type MailboxesData = { mailboxes: (Mailbox & Versioned)[] };
|
|
21
|
+
|
|
22
|
+
type FoldersData = Versioned & { folders: MailFolder[] };
|
|
23
|
+
|
|
24
|
+
type Paged<T> = InfiniteData<T & Versioned, unknown>;
|
|
25
|
+
|
|
26
|
+
/** The query groups that hold one mailbox's data, by the first key part. */
|
|
27
|
+
const MAILBOX_GROUPS = ["folders", "conversations", "conversation"] as const;
|
|
28
|
+
|
|
29
|
+
/** A key of one mailbox's query: its group, then the mailbox id. */
|
|
30
|
+
const mailboxQueryKey = z
|
|
31
|
+
.tuple([z.enum(MAILBOX_GROUPS), z.string()])
|
|
32
|
+
.rest(z.unknown());
|
|
33
|
+
|
|
34
|
+
// The versions mail queries' data carries (see `Versioned` above).
|
|
35
|
+
const mailboxesVersions = z.object({
|
|
36
|
+
mailboxes: z.array(
|
|
37
|
+
z.object({ id: z.string(), version: z.number().optional() })
|
|
38
|
+
),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const folderVersion = z.object({ version: z.number() });
|
|
42
|
+
|
|
43
|
+
const pagesVersions = z.object({
|
|
44
|
+
pages: z.array(z.object({ version: z.number() })),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
type MailRefreshState = {
|
|
48
|
+
timer: ReturnType<typeof setTimeout> | null;
|
|
49
|
+
running: boolean;
|
|
50
|
+
/** Mailboxes whose data is refreshed regardless of versions. */
|
|
51
|
+
forced: Set<string>;
|
|
52
|
+
/** Mailboxes to compare with their announced version. */
|
|
53
|
+
checked: Set<string>;
|
|
54
|
+
/** The highest version the server announced per mailbox. */
|
|
55
|
+
announced: Map<string, number>;
|
|
56
|
+
/** Queries already refetched for an announced version, so a refetch that comes back older does not loop. */
|
|
57
|
+
refetchedFor: Map<string, number>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const refreshStates = new WeakMap<QueryClient, MailRefreshState>();
|
|
61
|
+
|
|
62
|
+
function refreshState(client: QueryClient) {
|
|
63
|
+
let state = refreshStates.get(client);
|
|
64
|
+
|
|
65
|
+
if (!state) {
|
|
66
|
+
state = {
|
|
67
|
+
timer: null,
|
|
68
|
+
running: false,
|
|
69
|
+
forced: new Set(),
|
|
70
|
+
checked: new Set(),
|
|
71
|
+
announced: new Map(),
|
|
72
|
+
refetchedFor: new Map(),
|
|
73
|
+
};
|
|
74
|
+
refreshStates.set(client, state);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return state;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The oldest version a query's data reflects for `mailboxId`; undefined without data. */
|
|
81
|
+
function queryVersion(query: Query, mailboxId: string): number | undefined {
|
|
82
|
+
const [group] = query.queryKey;
|
|
83
|
+
const { data } = query.state;
|
|
84
|
+
|
|
85
|
+
if (!data) return undefined;
|
|
86
|
+
|
|
87
|
+
if (group === "mailboxes") {
|
|
88
|
+
const parsed = mailboxesVersions.safeParse(data);
|
|
89
|
+
|
|
90
|
+
return parsed.success
|
|
91
|
+
? parsed.data.mailboxes.find((mailbox) => mailbox.id === mailboxId)
|
|
92
|
+
?.version
|
|
93
|
+
: undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (group === "folders") {
|
|
97
|
+
const parsed = folderVersion.safeParse(data);
|
|
98
|
+
|
|
99
|
+
return parsed.success ? parsed.data.version : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const parsed = pagesVersions.safeParse(data);
|
|
103
|
+
|
|
104
|
+
if (!parsed.success || !parsed.data.pages.length) return undefined;
|
|
105
|
+
|
|
106
|
+
return Math.min(...parsed.data.pages.map((page) => page.version));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function mailboxQueries(client: QueryClient, mailboxId: string) {
|
|
110
|
+
return [
|
|
111
|
+
...client.getQueryCache().findAll({ queryKey: ["mailboxes"] }),
|
|
112
|
+
...MAILBOX_GROUPS.flatMap((group) =>
|
|
113
|
+
client.getQueryCache().findAll({ queryKey: [group, mailboxId] })
|
|
114
|
+
),
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function refreshMailboxQueries(
|
|
119
|
+
client: QueryClient,
|
|
120
|
+
state: MailRefreshState,
|
|
121
|
+
forced: string[],
|
|
122
|
+
checked: string[]
|
|
123
|
+
) {
|
|
124
|
+
const work: Promise<unknown>[] = [];
|
|
125
|
+
|
|
126
|
+
if (forced.length) {
|
|
127
|
+
work.push(client.invalidateQueries({ queryKey: ["mailboxes"] }));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const mailboxId of forced) {
|
|
131
|
+
for (const group of MAILBOX_GROUPS) {
|
|
132
|
+
work.push(client.invalidateQueries({ queryKey: [group, mailboxId] }));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const stale = new Map<string, QueryKey>();
|
|
137
|
+
|
|
138
|
+
for (const mailboxId of checked) {
|
|
139
|
+
const announced = state.announced.get(mailboxId);
|
|
140
|
+
|
|
141
|
+
if (announced === undefined) continue;
|
|
142
|
+
|
|
143
|
+
for (const query of mailboxQueries(client, mailboxId)) {
|
|
144
|
+
const version = queryVersion(query, mailboxId);
|
|
145
|
+
|
|
146
|
+
if (version === undefined || version >= announced) continue;
|
|
147
|
+
|
|
148
|
+
if ((state.refetchedFor.get(query.queryHash) ?? -1) >= announced) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
state.refetchedFor.set(query.queryHash, announced);
|
|
153
|
+
stale.set(query.queryHash, query.queryKey);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
for (const queryKey of stale.values()) {
|
|
158
|
+
work.push(client.invalidateQueries({ queryKey, exact: true }));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const results = await Promise.allSettled(work);
|
|
162
|
+
|
|
163
|
+
const failures = results.flatMap((result) =>
|
|
164
|
+
result.status === "rejected" ? [result.reason] : []
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
if (failures.length) {
|
|
168
|
+
throw new AggregateError(failures, "Could not refresh mail data");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function schedule(client: QueryClient, state: MailRefreshState) {
|
|
173
|
+
if (state.timer || state.running) return;
|
|
174
|
+
state.timer = setTimeout(() => {
|
|
175
|
+
state.timer = null;
|
|
176
|
+
state.running = true;
|
|
177
|
+
const forced = [...state.forced];
|
|
178
|
+
|
|
179
|
+
const checked = [...state.checked].filter(
|
|
180
|
+
(mailboxId) => !state.forced.has(mailboxId)
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
state.forced.clear();
|
|
184
|
+
state.checked.clear();
|
|
185
|
+
|
|
186
|
+
void refreshMailboxQueries(client, state, forced, checked)
|
|
187
|
+
.catch((error) => {
|
|
188
|
+
console.error("Could not refresh mail data", error);
|
|
189
|
+
})
|
|
190
|
+
.finally(() => {
|
|
191
|
+
state.running = false;
|
|
192
|
+
|
|
193
|
+
// Events received during the request are drained only after it ends,
|
|
194
|
+
// so an older response can never overwrite a newer refresh.
|
|
195
|
+
if (state.forced.size || state.checked.size) schedule(client, state);
|
|
196
|
+
});
|
|
197
|
+
}, 100);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Refetches all of a mailbox's mail data shortly, e.g. after an action changed it. */
|
|
201
|
+
export function scheduleMailboxRefresh(client: QueryClient, mailboxId: string) {
|
|
202
|
+
const state = refreshState(client);
|
|
203
|
+
state.forced.add(mailboxId);
|
|
204
|
+
schedule(client, state);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The server announced `version` for a mailbox: refetch what is older. */
|
|
208
|
+
export function noteMailboxVersion(
|
|
209
|
+
client: QueryClient,
|
|
210
|
+
mailboxId: string,
|
|
211
|
+
version: number
|
|
212
|
+
) {
|
|
213
|
+
const state = refreshState(client);
|
|
214
|
+
state.announced.set(
|
|
215
|
+
mailboxId,
|
|
216
|
+
Math.max(state.announced.get(mailboxId) ?? -1, version)
|
|
217
|
+
);
|
|
218
|
+
state.checked.add(mailboxId);
|
|
219
|
+
schedule(client, state);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Checks data that arrives after an announcement, e.g. a request that was in
|
|
224
|
+
* flight when the socket opened and read the mailbox before a change.
|
|
225
|
+
*/
|
|
226
|
+
export function watchMailboxVersions(client: QueryClient) {
|
|
227
|
+
return client.getQueryCache().subscribe((event) => {
|
|
228
|
+
if (event.type !== "updated" || event.action.type !== "success") return;
|
|
229
|
+
const [group] = event.query.queryKey;
|
|
230
|
+
const state = refreshStates.get(client);
|
|
231
|
+
|
|
232
|
+
if (!state?.announced.size) return;
|
|
233
|
+
const mailboxKey = mailboxQueryKey.safeParse(event.query.queryKey);
|
|
234
|
+
|
|
235
|
+
const mailboxIds =
|
|
236
|
+
group === "mailboxes"
|
|
237
|
+
? [...state.announced.keys()]
|
|
238
|
+
: mailboxKey.success
|
|
239
|
+
? [mailboxKey.data[1]]
|
|
240
|
+
: [];
|
|
241
|
+
|
|
242
|
+
let pending = false;
|
|
243
|
+
|
|
244
|
+
for (const id of mailboxIds) {
|
|
245
|
+
const announced = state.announced.get(id);
|
|
246
|
+
const version = queryVersion(event.query, id);
|
|
247
|
+
|
|
248
|
+
if (
|
|
249
|
+
announced !== undefined &&
|
|
250
|
+
version !== undefined &&
|
|
251
|
+
version < announced
|
|
252
|
+
) {
|
|
253
|
+
state.checked.add(id);
|
|
254
|
+
pending = true;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (pending) schedule(client, state);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
type ConversationPage = Versioned & {
|
|
263
|
+
mailboxState: string;
|
|
264
|
+
folderId: string | null;
|
|
265
|
+
hasIncoming: boolean;
|
|
266
|
+
hasOutgoing: boolean;
|
|
267
|
+
unreadCount: number;
|
|
268
|
+
messages: {
|
|
269
|
+
id: string;
|
|
270
|
+
direction: string;
|
|
271
|
+
isRead: boolean;
|
|
272
|
+
viewedBy: { userId: string; name: string; readAt: string }[];
|
|
273
|
+
}[];
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
type ConversationListPage = Versioned & {
|
|
277
|
+
conversations: {
|
|
278
|
+
conversationId: string;
|
|
279
|
+
unreadCount: number;
|
|
280
|
+
isUnread: boolean;
|
|
281
|
+
}[];
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
function folderContains(folder: MailFolder, conversation: ConversationPage) {
|
|
285
|
+
const inInbox =
|
|
286
|
+
conversation.hasIncoming && conversation.mailboxState === "active";
|
|
287
|
+
|
|
288
|
+
if (folder.kind === "custom") {
|
|
289
|
+
return inInbox && conversation.folderId === folder.id;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
switch (folder.systemType) {
|
|
293
|
+
case "inbox": {
|
|
294
|
+
return inInbox;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
case "sent": {
|
|
298
|
+
return conversation.hasOutgoing && conversation.mailboxState !== "trash";
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
case "archive":
|
|
302
|
+
case "spam":
|
|
303
|
+
case "trash": {
|
|
304
|
+
return conversation.mailboxState === folder.systemType;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
default: {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Applies this person's own read change to the cache instead of refetching:
|
|
315
|
+
* every query that reflected the version just before the change (`version`
|
|
316
|
+
* − 1) gets the change and moves to `version`, so the realtime echo of it
|
|
317
|
+
* refetches nothing. Data at any other version is left to the version check.
|
|
318
|
+
* Returns false when the message's previous state is not known that exactly;
|
|
319
|
+
* the caller then refreshes the mailbox.
|
|
320
|
+
*/
|
|
321
|
+
export function applyOwnReadChange(
|
|
322
|
+
client: QueryClient,
|
|
323
|
+
input: {
|
|
324
|
+
mailboxId: string;
|
|
325
|
+
conversationId: string;
|
|
326
|
+
messageId: string;
|
|
327
|
+
isRead: boolean;
|
|
328
|
+
version: number;
|
|
329
|
+
reader: { userId: string; name: string };
|
|
330
|
+
}
|
|
331
|
+
) {
|
|
332
|
+
const before = input.version - 1;
|
|
333
|
+
|
|
334
|
+
const conversationKey = [
|
|
335
|
+
"conversation",
|
|
336
|
+
input.mailboxId,
|
|
337
|
+
input.conversationId,
|
|
338
|
+
];
|
|
339
|
+
|
|
340
|
+
const conversation =
|
|
341
|
+
client.getQueryData<Paged<ConversationPage>>(conversationKey);
|
|
342
|
+
|
|
343
|
+
const head = conversation?.pages[0];
|
|
344
|
+
|
|
345
|
+
const holding = conversation?.pages.find((page) =>
|
|
346
|
+
page.messages.some((message) => message.id === input.messageId)
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
const message = holding?.messages.find((item) => item.id === input.messageId);
|
|
350
|
+
|
|
351
|
+
if (
|
|
352
|
+
!head ||
|
|
353
|
+
!holding ||
|
|
354
|
+
!message ||
|
|
355
|
+
head.version !== before ||
|
|
356
|
+
holding.version !== before
|
|
357
|
+
) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const delta =
|
|
362
|
+
message.direction === "incoming" && message.isRead !== input.isRead
|
|
363
|
+
? input.isRead
|
|
364
|
+
? -1
|
|
365
|
+
: 1
|
|
366
|
+
: 0;
|
|
367
|
+
|
|
368
|
+
const bump = <T extends Versioned>(page: T): T =>
|
|
369
|
+
page.version === before ? { ...page, version: input.version } : page;
|
|
370
|
+
|
|
371
|
+
for (const query of client
|
|
372
|
+
.getQueryCache()
|
|
373
|
+
.findAll({ queryKey: ["conversation", input.mailboxId] })) {
|
|
374
|
+
client.setQueryData<Paged<ConversationPage>>(
|
|
375
|
+
query.queryKey,
|
|
376
|
+
(data) =>
|
|
377
|
+
data && {
|
|
378
|
+
...data,
|
|
379
|
+
pages: data.pages.map((page, index) => {
|
|
380
|
+
if (page.version !== before) return page;
|
|
381
|
+
|
|
382
|
+
if (query.queryKey[2] !== input.conversationId) return bump(page);
|
|
383
|
+
|
|
384
|
+
return {
|
|
385
|
+
...page,
|
|
386
|
+
version: input.version,
|
|
387
|
+
unreadCount:
|
|
388
|
+
index === 0
|
|
389
|
+
? Math.max(0, page.unreadCount + delta)
|
|
390
|
+
: page.unreadCount,
|
|
391
|
+
messages: page.messages.map((item) =>
|
|
392
|
+
item.id === input.messageId
|
|
393
|
+
? {
|
|
394
|
+
...item,
|
|
395
|
+
isRead: input.isRead,
|
|
396
|
+
viewedBy: input.isRead
|
|
397
|
+
? item.viewedBy.some(
|
|
398
|
+
(viewer) => viewer.userId === input.reader.userId
|
|
399
|
+
)
|
|
400
|
+
? item.viewedBy
|
|
401
|
+
: [
|
|
402
|
+
...item.viewedBy,
|
|
403
|
+
{
|
|
404
|
+
...input.reader,
|
|
405
|
+
readAt: new Date().toISOString(),
|
|
406
|
+
},
|
|
407
|
+
]
|
|
408
|
+
: item.viewedBy.filter(
|
|
409
|
+
(viewer) => viewer.userId !== input.reader.userId
|
|
410
|
+
),
|
|
411
|
+
}
|
|
412
|
+
: item
|
|
413
|
+
),
|
|
414
|
+
};
|
|
415
|
+
}),
|
|
416
|
+
}
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
for (const query of client
|
|
421
|
+
.getQueryCache()
|
|
422
|
+
.findAll({ queryKey: ["conversations", input.mailboxId] })) {
|
|
423
|
+
client.setQueryData<Paged<ConversationListPage>>(
|
|
424
|
+
query.queryKey,
|
|
425
|
+
(data) =>
|
|
426
|
+
data && {
|
|
427
|
+
...data,
|
|
428
|
+
pages: data.pages.map((page) =>
|
|
429
|
+
page.version === before
|
|
430
|
+
? {
|
|
431
|
+
...page,
|
|
432
|
+
version: input.version,
|
|
433
|
+
conversations: page.conversations.map((item) => {
|
|
434
|
+
if (item.conversationId !== input.conversationId) {
|
|
435
|
+
return item;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const unreadCount = Math.max(0, item.unreadCount + delta);
|
|
439
|
+
|
|
440
|
+
return { ...item, unreadCount, isUnread: unreadCount > 0 };
|
|
441
|
+
}),
|
|
442
|
+
}
|
|
443
|
+
: page
|
|
444
|
+
),
|
|
445
|
+
}
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
client.setQueryData<FoldersData>(["folders", input.mailboxId], (data) =>
|
|
450
|
+
data && data.version === before
|
|
451
|
+
? {
|
|
452
|
+
...data,
|
|
453
|
+
version: input.version,
|
|
454
|
+
folders: data.folders.map((folder) =>
|
|
455
|
+
folderContains(folder, head)
|
|
456
|
+
? {
|
|
457
|
+
...folder,
|
|
458
|
+
unreadCount: Math.max(0, folder.unreadCount + delta),
|
|
459
|
+
}
|
|
460
|
+
: folder
|
|
461
|
+
),
|
|
462
|
+
}
|
|
463
|
+
: data
|
|
464
|
+
);
|
|
465
|
+
const inInbox = head.hasIncoming && head.mailboxState === "active";
|
|
466
|
+
client.setQueryData<MailboxesData>(
|
|
467
|
+
["mailboxes"],
|
|
468
|
+
(data) =>
|
|
469
|
+
data && {
|
|
470
|
+
...data,
|
|
471
|
+
mailboxes: data.mailboxes.map((mailbox) =>
|
|
472
|
+
mailbox.id !== input.mailboxId || mailbox.version !== before
|
|
473
|
+
? mailbox
|
|
474
|
+
: {
|
|
475
|
+
...mailbox,
|
|
476
|
+
version: input.version,
|
|
477
|
+
unreadCount: inInbox
|
|
478
|
+
? Math.max(0, mailbox.unreadCount + delta)
|
|
479
|
+
: mailbox.unreadCount,
|
|
480
|
+
}
|
|
481
|
+
),
|
|
482
|
+
}
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
2
|
+
import type { JSONParsed, JSONValue } from "hono/utils/types";
|
|
3
|
+
|
|
4
|
+
import type { MailBootstrap } from "../../worker/mail/bootstrap";
|
|
5
|
+
import {
|
|
6
|
+
conversationsQuery,
|
|
7
|
+
foldersQuery,
|
|
8
|
+
mailboxesQuery,
|
|
9
|
+
} from "./mail-queries";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Puts mail's part of the app's startup data (worker/mail/bootstrap.ts)
|
|
13
|
+
* where the mail screen's queries read it: the mailbox list and, for the
|
|
14
|
+
* mailbox in the URL, its folders and the first page of its folder.
|
|
15
|
+
*/
|
|
16
|
+
export function seedMail({
|
|
17
|
+
queryClient,
|
|
18
|
+
data,
|
|
19
|
+
}: {
|
|
20
|
+
queryClient: QueryClient;
|
|
21
|
+
data: JSONValue;
|
|
22
|
+
}) {
|
|
23
|
+
// SAFETY: core hands each module the JSON of what its own server module's
|
|
24
|
+
// `bootstrap` returned for this request, and mail's is `mailBootstrap`
|
|
25
|
+
// (worker/mail/bootstrap.ts), built from this same source.
|
|
26
|
+
const slice = data as JSONParsed<MailBootstrap>;
|
|
27
|
+
queryClient.setQueryData(mailboxesQuery().queryKey, {
|
|
28
|
+
ok: true as const,
|
|
29
|
+
mailboxes: slice.mailboxes,
|
|
30
|
+
});
|
|
31
|
+
const { mailbox } = slice;
|
|
32
|
+
|
|
33
|
+
if (!mailbox) return;
|
|
34
|
+
queryClient.setQueryData(foldersQuery(mailbox.id).queryKey, {
|
|
35
|
+
ok: true as const,
|
|
36
|
+
...mailbox.folders,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (!mailbox.conversations) return;
|
|
40
|
+
queryClient.setQueryData(
|
|
41
|
+
conversationsQuery(mailbox.id, mailbox.folder, "", false).queryKey,
|
|
42
|
+
{
|
|
43
|
+
pages: [{ ok: true as const, ...mailbox.conversations }],
|
|
44
|
+
pageParams: [undefined],
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { cn } from "@open-workspace/core/ui";
|
|
2
|
+
import { lazy, Suspense, useRef, useState } from "react";
|
|
3
|
+
import type { CSSProperties } from "react";
|
|
4
|
+
|
|
5
|
+
import { ConversationList } from "./conversation-list";
|
|
6
|
+
import { FolderManager } from "./folder-manager";
|
|
7
|
+
import { AppBar } from "./mail-app-bar";
|
|
8
|
+
import { MobileDock } from "./mail-dock";
|
|
9
|
+
import type { MailRoute } from "./mail-location";
|
|
10
|
+
import { NavTree } from "./mail-nav";
|
|
11
|
+
import { NavRail } from "./mail-nav-rail";
|
|
12
|
+
import { PaneSplitter, useListWidth } from "./pane-splitter";
|
|
13
|
+
import { ReaderPane } from "./reader-pane";
|
|
14
|
+
import { useMailController } from "./use-mail-controller";
|
|
15
|
+
|
|
16
|
+
const ComposeWindow = lazy(async () => {
|
|
17
|
+
const module = await import("./compose-window");
|
|
18
|
+
|
|
19
|
+
return { default: module.ComposeWindow };
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Workspace layout: a top bar with global search over separate solid panes —
|
|
24
|
+
* navigation (mailbox tree, or an icon rail below xl), a dense column list and
|
|
25
|
+
* a reading pane with its own command bar, split by a resizable gap. Below lg
|
|
26
|
+
* it is one pane at a time with a bottom dock of every folder.
|
|
27
|
+
*/
|
|
28
|
+
/** The list pane's width, as the CSS variable the layout reads. */
|
|
29
|
+
function listWidthStyle(
|
|
30
|
+
width: number
|
|
31
|
+
): CSSProperties & Record<"--mail-list-w", string> {
|
|
32
|
+
return {
|
|
33
|
+
"--mail-list-w": `clamp(20rem, ${width}px, calc(100% - 26.625rem))`,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function MailShell({ route }: { route: MailRoute }) {
|
|
38
|
+
const mail = useMailController(route, { listWithConversation: true });
|
|
39
|
+
/** Folder settings opened from the phone's dock. */
|
|
40
|
+
const [managingFolders, setManagingFolders] = useState(false);
|
|
41
|
+
const reading = Boolean(mail.conversationId);
|
|
42
|
+
const mainRef = useRef<HTMLElement>(null);
|
|
43
|
+
const [listWidth, setListWidth] = useListWidth();
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<div className="bg-background text-foreground flex h-dvh min-h-0 flex-col gap-1.5 overflow-hidden p-2 pt-1 text-[13px] lg:gap-2 lg:p-2.5 lg:pt-1.5">
|
|
47
|
+
<AppBar mail={mail} />
|
|
48
|
+
|
|
49
|
+
<div className="flex min-h-0 flex-1 gap-2.5">
|
|
50
|
+
<aside
|
|
51
|
+
aria-label="Navigation"
|
|
52
|
+
className="mail-pane hidden w-16 shrink-0 flex-col overflow-hidden lg:flex xl:hidden"
|
|
53
|
+
>
|
|
54
|
+
<NavRail mail={mail} />
|
|
55
|
+
</aside>
|
|
56
|
+
<aside
|
|
57
|
+
aria-label="Navigation"
|
|
58
|
+
className="mail-pane hidden w-60 shrink-0 flex-col overflow-hidden xl:flex"
|
|
59
|
+
>
|
|
60
|
+
<NavTree mail={mail} />
|
|
61
|
+
</aside>
|
|
62
|
+
|
|
63
|
+
<main
|
|
64
|
+
ref={mainRef}
|
|
65
|
+
style={listWidthStyle(listWidth)}
|
|
66
|
+
className="flex min-w-0 flex-1"
|
|
67
|
+
>
|
|
68
|
+
<section
|
|
69
|
+
aria-label="Conversation list"
|
|
70
|
+
className={cn(
|
|
71
|
+
"mail-pane min-w-0 flex-col overflow-hidden lg:flex lg:w-(--mail-list-w) lg:shrink-0",
|
|
72
|
+
reading ? "hidden" : "flex flex-1 lg:flex-none"
|
|
73
|
+
)}
|
|
74
|
+
>
|
|
75
|
+
<ConversationList mail={mail} />
|
|
76
|
+
</section>
|
|
77
|
+
<PaneSplitter
|
|
78
|
+
containerRef={mainRef}
|
|
79
|
+
width={listWidth}
|
|
80
|
+
onWidthChange={setListWidth}
|
|
81
|
+
/>
|
|
82
|
+
<section
|
|
83
|
+
aria-label="Reading pane"
|
|
84
|
+
className={cn(
|
|
85
|
+
"mail-pane min-w-0 flex-1 flex-col overflow-hidden",
|
|
86
|
+
reading ? "flex" : "hidden lg:flex"
|
|
87
|
+
)}
|
|
88
|
+
>
|
|
89
|
+
<ReaderPane mail={mail} />
|
|
90
|
+
</section>
|
|
91
|
+
</main>
|
|
92
|
+
</div>
|
|
93
|
+
|
|
94
|
+
{!reading && !mail.selection.active && (
|
|
95
|
+
<MobileDock
|
|
96
|
+
mail={mail}
|
|
97
|
+
onManageFolders={() => setManagingFolders(true)}
|
|
98
|
+
/>
|
|
99
|
+
)}
|
|
100
|
+
{mail.mailbox && (
|
|
101
|
+
<FolderManager
|
|
102
|
+
key={mail.mailbox.id}
|
|
103
|
+
mailboxId={mail.mailbox.id}
|
|
104
|
+
activeFolderId={mail.folder}
|
|
105
|
+
folders={mail.folders ?? []}
|
|
106
|
+
onSelectFolder={mail.selectFolder}
|
|
107
|
+
controlled={{
|
|
108
|
+
open: managingFolders,
|
|
109
|
+
onOpenChange: setManagingFolders,
|
|
110
|
+
}}
|
|
111
|
+
/>
|
|
112
|
+
)}
|
|
113
|
+
|
|
114
|
+
{mail.composeOpen && mail.mailbox && (
|
|
115
|
+
<Suspense fallback={null}>
|
|
116
|
+
<ComposeWindow
|
|
117
|
+
key={`${mail.mailbox.id}:${mail.forwardedMessage?.id ?? "compose"}`}
|
|
118
|
+
mailbox={mail.mailbox}
|
|
119
|
+
forwardedMessage={mail.forwardedMessage}
|
|
120
|
+
onClose={mail.closeComposer}
|
|
121
|
+
/>
|
|
122
|
+
</Suspense>
|
|
123
|
+
)}
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
}
|