@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,185 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Badge,
|
|
3
|
+
Button,
|
|
4
|
+
DialogFooter,
|
|
5
|
+
Input,
|
|
6
|
+
Label,
|
|
7
|
+
Skeleton,
|
|
8
|
+
Textarea,
|
|
9
|
+
responseJson,
|
|
10
|
+
} from "@open-workspace/core/ui";
|
|
11
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
12
|
+
import { LoaderCircle, Save } from "lucide-react";
|
|
13
|
+
import { useState } from "react";
|
|
14
|
+
import type { FormEvent } from "react";
|
|
15
|
+
import { toast } from "sonner";
|
|
16
|
+
|
|
17
|
+
import { mailApi } from "../lib/mail-api";
|
|
18
|
+
import type {
|
|
19
|
+
MailboxAiConfiguration,
|
|
20
|
+
MailboxAiSettings as MailboxAiSettingsValue,
|
|
21
|
+
} from "./types";
|
|
22
|
+
|
|
23
|
+
export function MailboxAiSettings({
|
|
24
|
+
mailboxId,
|
|
25
|
+
active,
|
|
26
|
+
}: {
|
|
27
|
+
mailboxId: string;
|
|
28
|
+
active: boolean;
|
|
29
|
+
}) {
|
|
30
|
+
const settings = useQuery({
|
|
31
|
+
queryKey: ["mailbox-ai", mailboxId],
|
|
32
|
+
queryFn: async () =>
|
|
33
|
+
responseJson(
|
|
34
|
+
await mailApi.api.mail.mailboxes[":id"].ai.$get({
|
|
35
|
+
param: { id: mailboxId },
|
|
36
|
+
})
|
|
37
|
+
),
|
|
38
|
+
enabled: active,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (settings.isLoading) {
|
|
42
|
+
return (
|
|
43
|
+
<div className="space-y-4 py-2">
|
|
44
|
+
<Skeleton className="h-14 w-full" />
|
|
45
|
+
<Skeleton className="h-28 w-full" />
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (settings.isError) {
|
|
51
|
+
return (
|
|
52
|
+
<div className="border-border/70 flex items-center justify-between gap-4 border-y py-4">
|
|
53
|
+
<p className="text-muted-foreground text-sm">
|
|
54
|
+
Mailbox AI settings are unavailable.
|
|
55
|
+
</p>
|
|
56
|
+
<Button
|
|
57
|
+
type="button"
|
|
58
|
+
variant="outline"
|
|
59
|
+
onClick={() => void settings.refetch()}
|
|
60
|
+
>
|
|
61
|
+
Try again
|
|
62
|
+
</Button>
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return settings.data ? (
|
|
68
|
+
<MailboxAiSettingsForm
|
|
69
|
+
key={`${mailboxId}:${settings.data.settings.configuration.instructions}:${settings.data.settings.configuration.confidenceThreshold}`}
|
|
70
|
+
mailboxId={mailboxId}
|
|
71
|
+
initial={settings.data.settings}
|
|
72
|
+
/>
|
|
73
|
+
) : null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function MailboxAiSettingsForm({
|
|
77
|
+
mailboxId,
|
|
78
|
+
initial,
|
|
79
|
+
}: {
|
|
80
|
+
mailboxId: string;
|
|
81
|
+
initial: MailboxAiSettingsValue;
|
|
82
|
+
}) {
|
|
83
|
+
const client = useQueryClient();
|
|
84
|
+
const [configuration, setConfiguration] = useState(initial.configuration);
|
|
85
|
+
|
|
86
|
+
const save = useMutation({
|
|
87
|
+
mutationFn: async (input: MailboxAiConfiguration) =>
|
|
88
|
+
responseJson(
|
|
89
|
+
await mailApi.api.mail.mailboxes[":id"].ai.$put({
|
|
90
|
+
param: { id: mailboxId },
|
|
91
|
+
json: input,
|
|
92
|
+
})
|
|
93
|
+
),
|
|
94
|
+
onSuccess: async (result) => {
|
|
95
|
+
setConfiguration(result.settings.configuration);
|
|
96
|
+
toast.success("Mailbox AI rules updated");
|
|
97
|
+
await client.invalidateQueries({ queryKey: ["mailbox-ai", mailboxId] });
|
|
98
|
+
},
|
|
99
|
+
onError: (error) => toast.error(error.message),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
function submit(event: FormEvent) {
|
|
103
|
+
event.preventDefault();
|
|
104
|
+
save.mutate(configuration);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<form className="space-y-5" onSubmit={submit}>
|
|
109
|
+
<div className="border-border/70 flex items-start justify-between gap-4 border-y py-4">
|
|
110
|
+
<div>
|
|
111
|
+
<p className="text-sm font-medium">Processing status</p>
|
|
112
|
+
<p className="text-muted-foreground mt-1 text-xs">
|
|
113
|
+
Set for the whole workspace in its configuration. Your settings are
|
|
114
|
+
kept while it is off.
|
|
115
|
+
</p>
|
|
116
|
+
</div>
|
|
117
|
+
<Badge variant={initial.globalEnabled ? "default" : "outline"}>
|
|
118
|
+
{initial.globalEnabled ? "Enabled" : "Off"}
|
|
119
|
+
</Badge>
|
|
120
|
+
</div>
|
|
121
|
+
|
|
122
|
+
<div className="space-y-2">
|
|
123
|
+
<Label htmlFor={`mailbox-ai-confidence-${mailboxId}`}>
|
|
124
|
+
Minimum confidence
|
|
125
|
+
</Label>
|
|
126
|
+
<div className="flex items-center gap-2">
|
|
127
|
+
<Input
|
|
128
|
+
id={`mailbox-ai-confidence-${mailboxId}`}
|
|
129
|
+
className="w-24 tabular-nums"
|
|
130
|
+
type="number"
|
|
131
|
+
min={50}
|
|
132
|
+
max={100}
|
|
133
|
+
step={5}
|
|
134
|
+
required
|
|
135
|
+
value={configuration.confidenceThreshold}
|
|
136
|
+
onChange={(event) =>
|
|
137
|
+
setConfiguration({
|
|
138
|
+
...configuration,
|
|
139
|
+
confidenceThreshold: Number(event.target.value),
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
/>
|
|
143
|
+
<span className="text-muted-foreground text-sm">%</span>
|
|
144
|
+
</div>
|
|
145
|
+
<p className="text-muted-foreground text-xs">
|
|
146
|
+
Lower-confidence decisions stay in Inbox.
|
|
147
|
+
</p>
|
|
148
|
+
</div>
|
|
149
|
+
|
|
150
|
+
<div className="space-y-2">
|
|
151
|
+
<Label htmlFor={`mailbox-ai-instructions-${mailboxId}`}>
|
|
152
|
+
Classification rules
|
|
153
|
+
</Label>
|
|
154
|
+
<Textarea
|
|
155
|
+
id={`mailbox-ai-instructions-${mailboxId}`}
|
|
156
|
+
className="min-h-32 resize-y"
|
|
157
|
+
maxLength={4000}
|
|
158
|
+
placeholder="For example: partnership requests go to Product. Treat newsletters as spam only when clearly unsolicited."
|
|
159
|
+
value={configuration.instructions}
|
|
160
|
+
onChange={(event) =>
|
|
161
|
+
setConfiguration({
|
|
162
|
+
...configuration,
|
|
163
|
+
instructions: event.target.value,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
/>
|
|
167
|
+
<p className="text-muted-foreground text-xs">
|
|
168
|
+
Refer to custom folders by the names shown in navigation. If nothing
|
|
169
|
+
matches, mail stays in Inbox.
|
|
170
|
+
</p>
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
<DialogFooter>
|
|
174
|
+
<Button type="submit" disabled={save.isPending}>
|
|
175
|
+
{save.isPending ? (
|
|
176
|
+
<LoaderCircle className="animate-spin" />
|
|
177
|
+
) : (
|
|
178
|
+
<Save />
|
|
179
|
+
)}
|
|
180
|
+
Save rules
|
|
181
|
+
</Button>
|
|
182
|
+
</DialogFooter>
|
|
183
|
+
</form>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { noteD1Bookmark, useAuth } from "@open-workspace/core/ui";
|
|
2
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
import { useLocation } from "react-router";
|
|
5
|
+
|
|
6
|
+
import { parseMailboxRealtimeUpdate } from "../../shared/mail";
|
|
7
|
+
import { noteMailboxVersion, watchMailboxVersions } from "./mail-query-cache";
|
|
8
|
+
import { useMailboxes } from "./use-mail-data";
|
|
9
|
+
|
|
10
|
+
type MailboxConnection = {
|
|
11
|
+
socket: WebSocket | null;
|
|
12
|
+
retryAttempt: number;
|
|
13
|
+
retryTimer: ReturnType<typeof setTimeout> | null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const PRESENCE_HEARTBEAT_MS = 25_000;
|
|
17
|
+
|
|
18
|
+
function realtimeUrl(mailboxId: string, visibility: "visible" | "hidden") {
|
|
19
|
+
const url = new URL(
|
|
20
|
+
`/api/mail/mailboxes/${encodeURIComponent(mailboxId)}/realtime`,
|
|
21
|
+
window.location.origin
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
25
|
+
url.searchParams.set("visibility", visibility);
|
|
26
|
+
|
|
27
|
+
return url;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function mailboxFromPath(pathname: string, mailboxIds: string[]) {
|
|
31
|
+
if (pathname === "/") return mailboxIds[0] ?? null;
|
|
32
|
+
const match = /^\/mail\/([^/]+)\/?$/u.exec(pathname);
|
|
33
|
+
|
|
34
|
+
const encoded = match?.[1];
|
|
35
|
+
|
|
36
|
+
if (encoded === undefined) return null;
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const mailboxId = decodeURIComponent(encoded);
|
|
40
|
+
|
|
41
|
+
return mailboxIds.includes(mailboxId) ? mailboxId : null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function MailboxRealtimeConnections() {
|
|
48
|
+
const auth = useAuth();
|
|
49
|
+
const client = useQueryClient();
|
|
50
|
+
const location = useLocation();
|
|
51
|
+
const mailboxQuery = useMailboxes();
|
|
52
|
+
|
|
53
|
+
const mailboxIds =
|
|
54
|
+
mailboxQuery.data?.mailboxes.map((mailbox) => mailbox.id) ?? [];
|
|
55
|
+
|
|
56
|
+
const mailboxKey = mailboxIds.join("\0");
|
|
57
|
+
const userId = auth.user?.id;
|
|
58
|
+
const { sessionVersion } = auth;
|
|
59
|
+
const activeMailboxId = mailboxFromPath(location.pathname, mailboxIds);
|
|
60
|
+
|
|
61
|
+
useEffect(() => watchMailboxVersions(client), [client]);
|
|
62
|
+
|
|
63
|
+
if (!userId || !mailboxKey) return null;
|
|
64
|
+
|
|
65
|
+
// A new session (another person, or signing in again) opens new sockets.
|
|
66
|
+
return (
|
|
67
|
+
<MailboxSockets
|
|
68
|
+
key={`${userId}\0${sessionVersion ?? ""}`}
|
|
69
|
+
mailboxKey={mailboxKey}
|
|
70
|
+
activeMailboxId={activeMailboxId}
|
|
71
|
+
/>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One socket per mailbox in `mailboxKey`, telling each whether it is on screen. */
|
|
76
|
+
function MailboxSockets({
|
|
77
|
+
mailboxKey,
|
|
78
|
+
activeMailboxId,
|
|
79
|
+
}: {
|
|
80
|
+
mailboxKey: string;
|
|
81
|
+
activeMailboxId: string | null;
|
|
82
|
+
}) {
|
|
83
|
+
const client = useQueryClient();
|
|
84
|
+
const activeMailboxRef = useRef<string | null>(activeMailboxId);
|
|
85
|
+
const sendPresenceRef = useRef<(() => void) | null>(null);
|
|
86
|
+
// Before the connection effect, so sockets opened in the same commit
|
|
87
|
+
// announce the current mailbox; later changes are announced at once.
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
activeMailboxRef.current = activeMailboxId;
|
|
90
|
+
sendPresenceRef.current?.();
|
|
91
|
+
}, [activeMailboxId]);
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
const ids = mailboxKey.split("\0");
|
|
95
|
+
const connections = new Map<string, MailboxConnection>();
|
|
96
|
+
let stopped = false;
|
|
97
|
+
let ownershipRefresh: Promise<void> | null = null;
|
|
98
|
+
|
|
99
|
+
function refreshOwnership() {
|
|
100
|
+
if (!ownershipRefresh) {
|
|
101
|
+
ownershipRefresh = Promise.all([
|
|
102
|
+
client.invalidateQueries({ queryKey: ["auth-state"] }),
|
|
103
|
+
client.invalidateQueries({ queryKey: ["mailboxes"] }),
|
|
104
|
+
])
|
|
105
|
+
.then(() => undefined)
|
|
106
|
+
.catch((error) => {
|
|
107
|
+
console.error(
|
|
108
|
+
"Could not refresh realtime connection ownership",
|
|
109
|
+
error
|
|
110
|
+
);
|
|
111
|
+
})
|
|
112
|
+
.finally(() => {
|
|
113
|
+
ownershipRefresh = null;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return ownershipRefresh;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function visibilityFor(mailboxId: string) {
|
|
121
|
+
return document.visibilityState === "visible" &&
|
|
122
|
+
activeMailboxRef.current === mailboxId
|
|
123
|
+
? ("visible" as const)
|
|
124
|
+
: ("hidden" as const);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function sendMailboxPresence(mailboxId: string) {
|
|
128
|
+
const connection = connections.get(mailboxId);
|
|
129
|
+
|
|
130
|
+
if (connection?.socket?.readyState === WebSocket.OPEN) {
|
|
131
|
+
connection.socket.send(visibilityFor(mailboxId));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function sendPresence() {
|
|
136
|
+
for (const mailboxId of connections.keys()) {
|
|
137
|
+
sendMailboxPresence(mailboxId);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
sendPresenceRef.current = sendPresence;
|
|
142
|
+
|
|
143
|
+
function connect(mailboxId: string) {
|
|
144
|
+
const connection = connections.get(mailboxId);
|
|
145
|
+
|
|
146
|
+
if (!connection || stopped || !navigator.onLine) return;
|
|
147
|
+
|
|
148
|
+
if (
|
|
149
|
+
connection.socket?.readyState === WebSocket.OPEN ||
|
|
150
|
+
connection.socket?.readyState === WebSocket.CONNECTING
|
|
151
|
+
) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const socket = new WebSocket(
|
|
156
|
+
realtimeUrl(mailboxId, visibilityFor(mailboxId))
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
let opened = false;
|
|
160
|
+
connection.socket = socket;
|
|
161
|
+
socket.addEventListener("open", () => {
|
|
162
|
+
if (stopped || connection.socket !== socket) return;
|
|
163
|
+
opened = true;
|
|
164
|
+
connection.retryAttempt = 0;
|
|
165
|
+
sendMailboxPresence(mailboxId);
|
|
166
|
+
});
|
|
167
|
+
// The server announces the mailbox's version on open (covering changes
|
|
168
|
+
// made before this socket existed, e.g. while reconnecting) and after
|
|
169
|
+
// each change; only data older than it is refetched.
|
|
170
|
+
socket.addEventListener("message", (message) => {
|
|
171
|
+
if (stopped || connection.socket !== socket) return;
|
|
172
|
+
const update = parseMailboxRealtimeUpdate(message.data);
|
|
173
|
+
|
|
174
|
+
if (!update) return;
|
|
175
|
+
// First, so the refetches below read D1 at least as new as the change.
|
|
176
|
+
noteD1Bookmark(update.bookmark);
|
|
177
|
+
noteMailboxVersion(client, mailboxId, update.version);
|
|
178
|
+
});
|
|
179
|
+
socket.addEventListener("close", (event) => {
|
|
180
|
+
if (connection.socket === socket) connection.socket = null;
|
|
181
|
+
|
|
182
|
+
if (stopped || event.code === 1000) return;
|
|
183
|
+
|
|
184
|
+
if (event.code === 1008) {
|
|
185
|
+
void refreshOwnership();
|
|
186
|
+
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const delay = Math.min(30_000, 500 * 2 ** connection.retryAttempt);
|
|
191
|
+
connection.retryAttempt += 1;
|
|
192
|
+
connection.retryTimer = setTimeout(
|
|
193
|
+
() => {
|
|
194
|
+
connection.retryTimer = null;
|
|
195
|
+
void (async () => {
|
|
196
|
+
// Browsers expose a rejected WebSocket handshake as an abnormal
|
|
197
|
+
// close, so refresh access before retrying a socket that never opened.
|
|
198
|
+
if (!opened && navigator.onLine) await refreshOwnership();
|
|
199
|
+
connect(mailboxId);
|
|
200
|
+
})();
|
|
201
|
+
},
|
|
202
|
+
delay + Math.floor(Math.random() * Math.min(delay, 1000))
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
for (const mailboxId of ids) {
|
|
208
|
+
connections.set(mailboxId, {
|
|
209
|
+
socket: null,
|
|
210
|
+
retryAttempt: 0,
|
|
211
|
+
retryTimer: null,
|
|
212
|
+
});
|
|
213
|
+
connect(mailboxId);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const reconnect = () => {
|
|
217
|
+
for (const mailboxId of ids) connect(mailboxId);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
document.addEventListener("visibilitychange", sendPresence);
|
|
221
|
+
window.addEventListener("online", reconnect);
|
|
222
|
+
|
|
223
|
+
const heartbeat = () => {
|
|
224
|
+
const mailboxId = activeMailboxRef.current;
|
|
225
|
+
|
|
226
|
+
if (document.visibilityState === "visible" && mailboxId) {
|
|
227
|
+
sendMailboxPresence(mailboxId);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const presenceTimer = window.setInterval(heartbeat, PRESENCE_HEARTBEAT_MS);
|
|
232
|
+
|
|
233
|
+
return () => {
|
|
234
|
+
stopped = true;
|
|
235
|
+
document.removeEventListener("visibilitychange", sendPresence);
|
|
236
|
+
window.removeEventListener("online", reconnect);
|
|
237
|
+
window.clearInterval(presenceTimer);
|
|
238
|
+
|
|
239
|
+
if (sendPresenceRef.current === sendPresence) {
|
|
240
|
+
sendPresenceRef.current = null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
for (const connection of connections.values()) {
|
|
244
|
+
if (connection.retryTimer) clearTimeout(connection.retryTimer);
|
|
245
|
+
connection.socket?.close(1000, "Client disconnected");
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}, [client, mailboxKey]);
|
|
249
|
+
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useParams } from "react-router";
|
|
2
|
+
|
|
3
|
+
import { MailShell } from "./mail-shell";
|
|
4
|
+
import { useMailboxes } from "./use-mail-data";
|
|
5
|
+
|
|
6
|
+
/** The mail app at "/" (the default mailbox) and at `MAIL_ROUTE`. */
|
|
7
|
+
export function MailboxRoute() {
|
|
8
|
+
const { mailboxId, folder, conversationId } = useParams();
|
|
9
|
+
const mailboxes = useMailboxes();
|
|
10
|
+
// "/" is keyed by the mailbox it shows, so its redirect to /mail/:id keeps
|
|
11
|
+
// the screen (and its requests in flight) instead of starting over.
|
|
12
|
+
const key = mailboxId ?? mailboxes.data?.mailboxes[0]?.id ?? "default";
|
|
13
|
+
|
|
14
|
+
return <MailShell key={key} route={{ mailboxId, folder, conversationId }} />;
|
|
15
|
+
}
|