Be somewhere a phone can share to

ihasmail could hand a file to the share sheet as of #306, and was still
not in it. Share a photo from the gallery, a link from the browser or a
document from a file manager and ihasmail was not among the places it
could go, which is the one piece of operating-system integration a mail
app is expected to have.

A share is a POST that navigates, and there is nothing on this side that
can answer one: the app is a client-side router with no endpoint at that
address, and the server behind it would need a route that understood the
composer. So the service worker intercepts it, takes the form body, puts
the files and text in its cache, and redirects to the app -- which finds
them on start and opens a draft holding them. The subject is the shared
title, the text and the link become the body, and files are attached and
begin uploading. Nothing is addressed: a share says what to send, never
who to.

The body is pushed in above the signature rather than passed to open(),
because open() only fits a signature when it is given no body at all --
the obvious version drops the signature from every message that started
as a share, and nothing about the draft looks wrong afterwards.

Collected on every start rather than when the launch URL says so. A share
to a signed-out ihasmail lands on the sign-in page, and there is no
account to attach to until it is done, so the payload has to outlive a
redirect and a login -- which the query string does not. What that costs
is a stash nobody came back for, so it carries a timestamp and expires
after ten minutes.

`accept` names wildcard families and explicit types and extensions both.
A mail client attaches anything, but wildcards are not in the
specification and operating systems differ over which form they match on,
so the explicit list is what holds if the families are ignored.

The cache name the worker and the app have to agree on now has one home
on the app side. It was written out twice, and a drift would not fail --
a push verification would simply never complete and a share would arrive
at an empty composer.

One case is deliberately left to fail loudly: an app still installed
whose worker has been cleared away POSTs to the server, which answers
405. A server route would trade a plain error for a silent nothing, and
the payload is gone in both -- it only ever existed in that request body.

Verified by test, not on a device: Android is the only place this exists
at all, and the extension driving Chrome is not connected here. The
handoff is pinned from the tab's side against a cache shaped exactly as
the worker leaves it, since the two files never see each other.
This commit is contained in:
2026-09-07 22:40:44 -07:00
parent f39d6ac30c
commit 82470e8db0
10 changed files with 482 additions and 1 deletions
+24
View File
@@ -18,6 +18,7 @@ import { CalendarSidebar } from "./calendar/CalendarSidebar";
import { ShortcutsDialog, useGlobalShortcuts } from "./Shortcuts";
import { MailboxPicker } from "./mail/MailboxPicker";
import { formatSize } from "@/lib/format";
import { collectShare } from "@/lib/shareTarget";
import { TranslateBoundary } from "@/ui/TranslateBoundary";
import { t } from "@/lib/i18n";
@@ -35,6 +36,7 @@ export function AppShell({ children }: { children: ReactNode }) {
const [drawer, setDrawer] = useState(false);
const [helpOpen, setHelpOpen] = useState(false);
const openCompose = useCompose((s) => s.open);
const openShare = useCompose((s) => s.openFromShare);
const pushState = useSession((s) => s.pushState);
const session = useSession((s) => s.session);
const logout = useSession((s) => s.logout);
@@ -73,6 +75,28 @@ export function AppShell({ children }: { children: ReactNode }) {
}
}, [openCompose, navigate]);
/*
* A share from the operating system, collected rather than read off the URL.
*
* The other deep links above arrive as a query the app can read on the spot.
* A share cannot: it is a POST, the service worker answered it, and what it
* left behind has to survive the redirect -- and, when nobody was signed in,
* a trip through the sign-in page as well. So this asks on every start
* instead of only when `?share=1` says so, and finds nothing almost every
* time. The `at` stamp is what stops an abandoned one turning up days later.
*
* It runs here rather than in `main.tsx` because attaching needs an account:
* `addFiles` uploads as it goes, and there is nothing to upload to until the
* session is in place. AppShell only exists once there is one.
*/
useEffect(() => {
void collectShare().then((share) => {
if (!share) return;
openShare(share);
if (new URLSearchParams(window.location.search).has("share")) navigate("/mail", { replace: true });
});
}, [openShare, navigate]);
/*
* There is no account switcher any more.
*