Archive and mark read from the notification itself
Both happen in the background. The phone stays where it is. This was twice described as impossible, here and in FEATURES.md: the service worker was said to have no session, so anything touching mail had to open the app. That is wrong, and checking it rather than repeating it is the whole of this change. ihasmail's session is an httpOnly cookie against its own origin and the only other thing the API asks for is a fixed `x-requested-with` header, which is not a secret and is not held anywhere. A same-origin fetch from the worker carries the cookie like any other. Confirmed against the mock: logging in with curl and then issuing `Email/set` with nothing but that cookie and the static headers marked a message read and moved it to Archive, HTTP 200. Nothing the tab holds in memory is involved, because the API asks for none of it. Two actions, because `maxActions` is two on Android and anything past it is dropped without a word. Archive and Mark as read are the two worth having: they are what somebody does to a notification they have already read the whole of. Reply is not among them -- it would have to open the app, which is what tapping the notification does already. The worker still cannot reach a catalogue. It is plain JavaScript copied into the build, outside the bundle, with no i18n and no idea which mailbox is the archive. So the app writes both down in the same cache it already uses for handoffs, and rewrites them whenever the language, the account or the folder list changes. Where there is no such note -- between installing this worker and next opening ihasmail -- the notification appears with no buttons at all, rather than English ones over a mailbox guessed by name. That also fixes two strings the worker had always shown in English regardless: "New mail" and "(no subject)". A session can be gone by the time a button is pressed. That comes back as a refusal and the notification says so, rather than vanishing as though it had worked. It does not open the app to recover: being interrupted is what the button existed to avoid. The two claims that were wrong are corrected rather than quietly deleted, including the one about push renewal -- which still needs a tab, but for a different reason than the one given. The reason is when the worker runs, not what it may do: it wakes only for a push, and the push stops when the subscription lapses. Two new strings, in all nine catalogues.
This commit is contained in:
@@ -17,6 +17,7 @@ import { AppShell } from "@/views/AppShell";
|
||||
import { MailView } from "@/views/mail/MailView";
|
||||
import { ComposerDock } from "@/views/compose/ComposerDock";
|
||||
import { setUnreadBadge } from "@/lib/notify";
|
||||
import { publishWorkerFacts } from "@/lib/swFacts";
|
||||
import { PAINTED_FROM_CACHE, useSettings, syncedPart } from "@/store/settings";
|
||||
import { armSettingsSync, loadRemoteSettings, queueSettingsPush, settingsAlreadyLoadedFor, settingsSyncAvailable } from "@/lib/settingsSync";
|
||||
import { loadSettingsPolicy } from "@/lib/settingsPolicy";
|
||||
@@ -263,6 +264,21 @@ function AuthedApp() {
|
||||
});
|
||||
}, [inboxUnread, appName]);
|
||||
|
||||
/*
|
||||
* Leave the service worker its briefing.
|
||||
*
|
||||
* Written from here rather than once at startup because everything in it can
|
||||
* change while the app is open -- the language from Settings, the archive
|
||||
* folder from the mailbox list arriving -- and what is written is what the
|
||||
* worker will still be reading a week from now, with no tab to correct it.
|
||||
* See lib/swFacts.ts.
|
||||
*/
|
||||
const archiveId = useMail((s) => s.roleId("archive"));
|
||||
const languageVersion = useLanguageVersion();
|
||||
useEffect(() => {
|
||||
void publishWorkerFacts(accountId, archiveId);
|
||||
}, [accountId, archiveId, languageVersion]);
|
||||
|
||||
// Request notification permission lazily when enabled
|
||||
const notif = useSettings((s) => s.settings.desktopNotifications);
|
||||
useEffect(() => {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { publishWorkerFacts, FACTS_KEY, type WorkerFacts } from "@/lib/swFacts";
|
||||
import { SW_CACHE_NAME } from "@/lib/swCache";
|
||||
import { setCatalog } from "@/lib/i18n";
|
||||
import { catalog as de } from "@/locales/de";
|
||||
|
||||
/**
|
||||
* The briefing is the only thing standing between a notification action and a
|
||||
* button labelled in a language the reader does not use — the worker is plain
|
||||
* JavaScript outside the bundle and cannot reach a catalogue.
|
||||
*
|
||||
* It is also the only place the archive mailbox is named, and getting that
|
||||
* wrong does not fail visibly: a message would be filed somewhere, just not
|
||||
* where Archive means.
|
||||
*/
|
||||
|
||||
function fakeCaches() {
|
||||
const store = new Map<string, string>();
|
||||
const cache = {
|
||||
put: vi.fn(async (key: string, res: Response) => void store.set(key, await res.text())),
|
||||
match: vi.fn(async (key: string) => (store.has(key) ? new Response(store.get(key)) : undefined)),
|
||||
delete: vi.fn(async () => true),
|
||||
};
|
||||
// Only the worker's own cache: a briefing put anywhere else is one the
|
||||
// worker will never read.
|
||||
const other = { put: vi.fn(), match: vi.fn(), delete: vi.fn() };
|
||||
vi.stubGlobal("caches", { open: vi.fn(async (name: string) => (name === SW_CACHE_NAME ? cache : other)) });
|
||||
return { store, cache };
|
||||
}
|
||||
|
||||
const written = (store: Map<string, string>) => JSON.parse(store.get(FACTS_KEY)!) as WorkerFacts;
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
setCatalog("en", { strings: {}, plurals: {} });
|
||||
});
|
||||
|
||||
describe("the worker's briefing", () => {
|
||||
it("names the account and the archive mailbox", async () => {
|
||||
const { store } = fakeCaches();
|
||||
await publishWorkerFacts("a1", "mb-archive");
|
||||
const facts = written(store);
|
||||
expect(facts.accountId).toBe("a1");
|
||||
expect(facts.archiveId).toBe("mb-archive");
|
||||
});
|
||||
|
||||
it("carries the worker's text in the language the tab is in", async () => {
|
||||
// The worker has no catalogue. Everything it will say has to be said here
|
||||
// first, or a German reader gets English buttons on their lock screen.
|
||||
setCatalog("de", de);
|
||||
const { store } = fakeCaches();
|
||||
await publishWorkerFacts("a1", "mb-archive");
|
||||
const facts = written(store);
|
||||
expect(facts.strings.archive).toBe("Archivieren");
|
||||
expect(facts.strings.markRead).toBe("Als gelesen markieren");
|
||||
expect(facts.strings.newMail).toBe("Neue E-Mail");
|
||||
expect(facts.strings.noSubject).toBe("(kein Betreff)");
|
||||
expect(facts.strings.failed).not.toBe("");
|
||||
});
|
||||
|
||||
it("says so when there is no archive folder, rather than inventing one", async () => {
|
||||
// The worker draws no Archive button on a null. An account without an
|
||||
// archive is not a reason to file mail somewhere else.
|
||||
const { store } = fakeCaches();
|
||||
await publishWorkerFacts("a1", null);
|
||||
expect(written(store).archiveId).toBeNull();
|
||||
});
|
||||
|
||||
it("writes nothing before there is an account", async () => {
|
||||
const { cache } = fakeCaches();
|
||||
await publishWorkerFacts(null, null);
|
||||
expect(cache.put).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not throw where the browser has no cache storage", async () => {
|
||||
vi.stubGlobal("caches", undefined);
|
||||
await expect(publishWorkerFacts("a1", "mb-archive")).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("carries every string the worker looks up", async () => {
|
||||
// The worker reads these by name and shows `undefined` for a missing one,
|
||||
// which is the kind of thing that only appears on somebody's lock screen.
|
||||
const { store } = fakeCaches();
|
||||
await publishWorkerFacts("a1", "mb-archive");
|
||||
const facts = written(store);
|
||||
for (const k of ["newMail", "newMessage", "noSubject", "archive", "markRead", "failed"] as const) {
|
||||
expect(facts.strings[k], `missing ${k}`).toBeTruthy();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* What the service worker cannot work out for itself.
|
||||
*
|
||||
* The worker can act on mail — see the note on `jmap()` in sw.js — but it
|
||||
* cannot read a catalogue or a store. It is plain JavaScript copied into the
|
||||
* build, outside the bundle, with no i18n and no idea which mailbox is the
|
||||
* archive. Both of those are things a tab knows and can simply write down.
|
||||
*
|
||||
* So the app leaves a short briefing in the same cache it uses for every other
|
||||
* handoff, and the worker reads it when a notification arrives. Where there is
|
||||
* none, the worker offers no actions at all rather than guessing: an untitled
|
||||
* button that files mail somewhere is worse than a notification you have to
|
||||
* open.
|
||||
*
|
||||
* That means the actions appear once ihasmail has been opened since the worker
|
||||
* was installed, which is the same condition background notifications already
|
||||
* carry — a push subscription has to be renewed from a tab too.
|
||||
*/
|
||||
import { withBase } from "./basePath";
|
||||
import { SW_CACHE_NAME } from "./swCache";
|
||||
import { t } from "./i18n";
|
||||
|
||||
export const FACTS_KEY = "/ihasmail-worker-facts";
|
||||
|
||||
export interface WorkerFacts {
|
||||
/** The account the notifications are about. */
|
||||
accountId: string;
|
||||
/** Where Archive files to; null where the account has no archive folder. */
|
||||
archiveId: string | null;
|
||||
/** The worker's own user-visible text, in the language this tab is in. */
|
||||
strings: {
|
||||
newMail: string;
|
||||
newMessage: string;
|
||||
noSubject: string;
|
||||
archive: string;
|
||||
markRead: string;
|
||||
failed: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the briefing.
|
||||
*
|
||||
* Called again whenever what is in it could have changed — the language, the
|
||||
* account, the archive folder — because it is what the worker will still be
|
||||
* reading in a week's time. Rewriting it is one cache put; there is nothing to
|
||||
* gain by working out whether it differs.
|
||||
*/
|
||||
export async function publishWorkerFacts(accountId: string | null, archiveId: string | null): Promise<void> {
|
||||
if (typeof caches === "undefined" || !accountId) return;
|
||||
const facts: WorkerFacts = {
|
||||
accountId,
|
||||
archiveId,
|
||||
strings: {
|
||||
newMail: t("New mail"),
|
||||
newMessage: t("New message"),
|
||||
noSubject: t("(no subject)"),
|
||||
archive: t("Archive"),
|
||||
markRead: t("Mark as read"),
|
||||
failed: t("Could not do that — open ihasmail and try again"),
|
||||
},
|
||||
};
|
||||
try {
|
||||
const cache = await caches.open(SW_CACHE_NAME);
|
||||
await cache.put(withBase(FACTS_KEY), new Response(JSON.stringify(facts), { headers: { "content-type": "application/json" } }));
|
||||
} catch {
|
||||
/* no cache storage: the worker falls back to a notification with no actions */
|
||||
}
|
||||
}
|
||||
@@ -892,6 +892,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Neue Nachricht",
|
||||
"New mail": "Neue E-Mail",
|
||||
"Could not do that — open ihasmail and try again": "Nicht möglich – öffnen Sie ihasmail und versuchen Sie es erneut",
|
||||
"Sending…": "Wird gesendet…",
|
||||
"Saving…": "Wird gespeichert…",
|
||||
"Error": "Fehler",
|
||||
|
||||
@@ -865,6 +865,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Mensaje nuevo",
|
||||
"New mail": "Correo nuevo",
|
||||
"Could not do that — open ihasmail and try again": "No se pudo hacer eso: abra ihasmail e inténtelo de nuevo",
|
||||
"Sending…": "Enviando…",
|
||||
"Saving…": "Guardando…",
|
||||
"Error": "Error",
|
||||
|
||||
@@ -870,6 +870,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Nouveau message",
|
||||
"New mail": "Nouveau courrier",
|
||||
"Could not do that — open ihasmail and try again": "Impossible : ouvrez ihasmail et réessayez",
|
||||
"Sending…": "Envoi…",
|
||||
"Saving…": "Enregistrement…",
|
||||
"Error": "Erreur",
|
||||
|
||||
@@ -873,6 +873,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "新規メール",
|
||||
"New mail": "新着メール",
|
||||
"Could not do that — open ihasmail and try again": "実行できませんでした - ihasmail を開いてやり直してください",
|
||||
"Sending…": "送信中…",
|
||||
"Saving…": "保存中…",
|
||||
"Error": "エラー",
|
||||
|
||||
@@ -861,6 +861,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Nieuw bericht",
|
||||
"New mail": "Nieuwe e-mail",
|
||||
"Could not do that — open ihasmail and try again": "Dat lukte niet — open ihasmail en probeer het opnieuw",
|
||||
"Sending…": "Bezig met verzenden…",
|
||||
"Saving…": "Bezig met opslaan…",
|
||||
"Error": "Fout",
|
||||
|
||||
@@ -868,6 +868,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Nova mensagem",
|
||||
"New mail": "Novo e-mail",
|
||||
"Could not do that — open ihasmail and try again": "Não foi possível fazer isso — abra o ihasmail e tente novamente",
|
||||
"Sending…": "Enviando…",
|
||||
"Saving…": "Salvando…",
|
||||
"Error": "Erro",
|
||||
|
||||
@@ -867,6 +867,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Новое письмо",
|
||||
"New mail": "Новое письмо",
|
||||
"Could not do that — open ihasmail and try again": "Не удалось — откройте ihasmail и повторите попытку",
|
||||
"Sending…": "Отправка…",
|
||||
"Saving…": "Сохранение…",
|
||||
"Error": "Ошибка",
|
||||
|
||||
@@ -861,6 +861,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "Новий лист",
|
||||
"New mail": "Новий лист",
|
||||
"Could not do that — open ihasmail and try again": "Не вдалося — відкрийте ihasmail і повторіть спробу",
|
||||
"Sending…": "Надсилання…",
|
||||
"Saving…": "Збереження…",
|
||||
"Error": "Помилка",
|
||||
|
||||
@@ -872,6 +872,8 @@ export const catalog: Catalog = {
|
||||
|
||||
// ── Composer status, calendar title ────────────────────────────────
|
||||
"New message": "新邮件",
|
||||
"New mail": "新邮件",
|
||||
"Could not do that — open ihasmail and try again": "无法执行 — 请打开 ihasmail 后重试",
|
||||
"Sending…": "正在发送…",
|
||||
"Saving…": "正在保存…",
|
||||
"Error": "错误",
|
||||
|
||||
Reference in New Issue
Block a user