Files
ihasmail/web/src/store/session.ts
T
jcoffey-dev bd6a605d61 Group six more clusters out of web/src/lib
Takes the flat module count from 66 to 42, continuing what admin/ and
calendar/ started.

  lib/mailbox/  archiveDate, emptyFolder, folderMove, labelTree,
                mailboxName, mailboxRoute
  lib/sieve/    sieve, sieveApply, sieveFolders
  lib/input/    keyboard, swipe, touch, listSelection, dropUpload
  lib/notify/   notify, webpush, webpushEnable
  lib/sw/       swCache, swFacts, staleBuild
  lib/text/     html, markdown, text, emlName

FOUR THINGS THE FILENAMES GET WRONG, each checked by reading the file
rather than trusting what it is called:

  - appFolder is not a mailbox. It is the `ihasmail` folder in JMAP
    *Files*, where the client keeps signature images and synced settings.
    It stays flat.
  - format holds no formatting of text. It re-exports the date and clock
    formatters, so it belongs with dates/datetime, not with text/.
  - preview is the file viewer deciding what it can show without
    downloading, and source is where to point someone asking for this
    instance's AGPL source. Neither is about text.
  - notify is not Web Push. It is the tab title, the favicon badge and
    the new-mail sound -- in-app notification, which is why it sits with
    webpush rather than under sw/ with the service worker's own concerns.

threadScroll stays flat too: it decides where a conversation opens, which
is view state rather than a gesture, and input/ is honest only if
everything in it interprets something the reader did.

No behavior change. Almost every reference was on the @/ alias; eight
relative imports in files that did not move, or that moved away from a
sibling, needed rewriting by hand.
2026-09-15 23:17:50 -07:00

158 lines
5.5 KiB
TypeScript

import { create } from "zustand";
import { apiFetch, ApiError, CAP, client } from "@/jmap/client";
import type { Id, JmapSession } from "@/jmap/types";
import { push, type PushState } from "@/jmap/push";
import { accountForCapability, ownAccountForCapability } from "@/lib/accountRouting";
import { setServerLocale } from "@/lib/datetime";
import { flushSettingsPush, stopSettingsSync } from "@/lib/settingsSync";
import { reloadIfServerRebuilt } from "@/lib/sw/staleBuild";
import { unsubscribeThisDevice } from "@/lib/notify/webpush";
import { clearAllData, clearSignedInData, setDeviceTrusted } from "@/lib/storage";
import { startIdleLogout, stopIdleLogout } from "@/lib/idleLogout";
export type AuthStatus = "loading" | "anonymous" | "authenticated";
interface SessionState {
status: AuthStatus;
session: JmapSession | null;
/** Selected mail account (defaults to primary). */
accountId: Id | null;
error: string | null;
pushConnected: boolean;
/** Finer than pushConnected: tells "reconnecting" from "not connected". */
pushState: PushState;
bootstrap(): Promise<void>;
login(username: string, password: string, totp: string, remember: boolean): Promise<void>;
logout(): Promise<void>;
refresh(): Promise<void>;
setAccount(id: Id): void;
/** The account to read and write for a capability, honoring the account switcher. */
accountFor(cap: string): Id | null;
/** The user's own account for a capability, whatever they are looking at. */
ownAccountFor(cap: string): Id | null;
}
export const useSession = create<SessionState>((set, get) => ({
status: "loading",
session: null,
accountId: null,
error: null,
pushConnected: false,
pushState: "disconnected",
async bootstrap() {
try {
const s = await apiFetch<JmapSession>("/api/auth/session");
applySession(s, set);
} catch (err) {
if (err instanceof ApiError && err.status === 401) set({ status: "anonymous", session: null, accountId: null });
else set({ status: "anonymous", error: (err as Error).message });
}
},
async login(username, password, totp, remember) {
set({ error: null });
const s = await apiFetch<JmapSession>("/api/auth/login", {
method: "POST",
body: JSON.stringify({ username, password, totp: totp || undefined, remember }),
});
applySession(s, set);
},
async logout() {
push.stop();
setServerLocale(null);
// Anything still sitting in the debounce is written while the session can
// still write it; a setting changed seconds before signing out is not lost.
try {
await flushSettingsPush();
} catch {
/* ignore */
}
// A push subscription lives on the account, not the session, so signing out
// without removing it leaves this browser notifying for a mailbox nobody is
// signed into. On a shared machine that is somebody else's mail.
try {
await unsubscribeThisDevice();
} catch {
/* never block signing out over this */
}
stopSettingsSync();
try {
await apiFetch("/api/auth/logout", { method: "POST" });
} catch {
/* ignore */
}
stopIdleLogout();
// Unconditional. The push subscription above is removed for exactly this
// reason -- that a browser left holding someone's mail is somebody else's
// problem next -- and the address book cached here is the same argument.
clearSignedInData();
client.session = null;
set({ status: "anonymous", session: null, accountId: null });
},
async refresh() {
try {
const s = await apiFetch<JmapSession>("/api/auth/session?refresh=1");
client.session = s;
setServerLocale(s.ihasmail?.userLocale);
set({ session: s });
} catch {
/* ignore */
}
},
setAccount(id) {
set({ accountId: id });
},
accountFor(cap) {
return accountForCapability(get().session, get().accountId, cap);
},
ownAccountFor(cap) {
return ownAccountForCapability(get().session, cap);
},
}));
function applySession(s: JmapSession, set: (p: Partial<SessionState>) => void) {
client.session = s;
setServerLocale(s.ihasmail?.userLocale);
// `remember` is the answer to "is this device yours", given at sign-in and
// carried on the session -- so a reload arrives at the same answer without
// the client storing it, which on an untrusted device it could not do anyway.
const trusted = Boolean(s.ihasmail?.remember);
setDeviceTrusted(trusted);
if (trusted) {
stopIdleLogout();
} else {
// Residue from an earlier trusted session on this machine is exactly what
// an untrusted sign-in is asking us not to keep.
clearAllData();
startIdleLogout(() => void useSession.getState().logout());
}
const accountId = s.primaryAccounts[CAP.mail] ?? Object.keys(s.accounts)[0] ?? null;
set({ status: "authenticated", session: s, accountId, error: null });
}
client.onUnauthenticated(() => {
push.stop();
stopSettingsSync();
stopIdleLogout();
clearSignedInData();
client.session = null;
// Ask before showing the sign-in form rather than after. A deploy is the
// usual reason to be signed out here, and reloading a form someone has
// already started typing into would throw the password away.
void reloadIfServerRebuilt().then((reloading) => {
if (!reloading) useSession.setState({ status: "anonymous", session: null, accountId: null });
});
});
push.onConnection((state) => useSession.setState({ pushConnected: state === "connected", pushState: state }));
export function hasCap(cap: string): boolean {
return client.hasCapability(cap);
}