Notify a phone that has this installation installed

A `webpush` alert channel, and the PWA it needs to arrive. The payload is
encrypted to the subscription (RFC 8291) and the request signed with this
installation's own keypair (RFC 8292), both over `http-ece` — `pywebpush`
does the same in one call but brings `requests` and `aiohttp` with it, two
HTTP stacks beside httpx on a machine that may be a Raspberry Pi.

The manifest and the worker are hand-written rather than `vite-plugin-pwa`:
there is nothing worth precaching when the page carrying the credential is
`no-store`, so the worker handles `push` and `notificationclick` and nothing
else. `registration.scope` is the app's root in both places it runs, which is
why the payload carries no URL.

A run finishing in error is the first event worth waking someone for; `ok`
and `cancelled` describe to nothing, so a nightly batch that works stays
quiet. The events were already on the bus — only the filter changed.

`WEBPUSH_FILE` is a derived path, so the keypair lands on the data volume
with the alerts beside it. Off it, a rebuild would silently stop every phone
being notified: the key they subscribed against would be gone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014EbeFPm6WNC3YD9vrqqT3a
This commit is contained in:
2026-08-30 12:12:37 +02:00
co-authored by Claude Opus 5
parent 989d008d37
commit 45cc7504e1
23 changed files with 907 additions and 12 deletions
+71 -2
View File
@@ -1,6 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router"
import { Plus, Send, Trash2 } from "lucide-react"
import { Bell, BellOff, Plus, Send, Trash2 } from "lucide-react"
import { useState } from "react"
import { type AlertsConfig, AlertsService, type Channel } from "@/client"
@@ -17,6 +17,7 @@ import {
} from "@/components/ui/select"
import { Switch } from "@/components/ui/switch"
import useCustomToast from "@/hooks/useCustomToast"
import * as webpush from "@/lib/webpush"
import { handleError } from "@/utils"
export const Route = createFileRoute("/_layout/alerts")({
@@ -47,6 +48,7 @@ const EVENTS: [string, string][] = [
["engine_degraded", "The engine is struggling"],
["cascade_dropped", "Work was given up on"],
["queue_unavailable", "The queue is unreachable"],
["run_finished", "A run failed"],
]
/** The settings each kind of channel needs, in the order they read best. */
@@ -61,9 +63,18 @@ const FIELDS: Record<Channel["kind"], [string, string, string][]> = {
// The message has to be one a flow declares, like anything a dashboard
// writes to. A notification widget bound to it is what shows the alert.
dashboard: [["message", "Message", "house.notice"]],
// Nothing to configure: it goes to whichever browsers subscribed here, which
// is a button rather than a setting.
webpush: [],
}
const KINDS: Channel["kind"][] = ["ntfy", "smtp", "webhook", "dashboard"]
const KINDS: Channel["kind"][] = [
"ntfy",
"smtp",
"webhook",
"dashboard",
"webpush",
]
/** A setting may hold a `{"$secret": "name"}` reference rather than a literal,
* so text that parses as JSON is stored as JSON and survives a round trip. */
@@ -83,6 +94,62 @@ function showSetting(value: unknown): string {
return typeof value === "string" ? value : JSON.stringify(value)
}
/**
* The one channel whose setting is on the device rather than in the config:
* a browser has to ask its own permission, and what it hands back is stored
* against this installation.
*/
function ThisBrowser() {
const { showSuccessToast, showErrorToast } = useCustomToast()
const { data: subscribed, refetch } = useQuery({
queryKey: ["alerts", "webpush", "this-browser"],
queryFn: async () => (await webpush.current()) !== null,
enabled: webpush.supported(),
})
const change = useMutation({
mutationFn: async (wanted: boolean) =>
wanted ? webpush.subscribe() : webpush.unsubscribe(),
onSuccess: (_result, wanted) => {
showSuccessToast(
wanted
? "This browser will be notified"
: "This browser will no longer be notified",
)
refetch()
},
onError: (error: Error) => showErrorToast(error.message),
})
if (!webpush.supported()) {
return (
<p className="text-sm text-muted-foreground">
This browser cannot receive notifications. They need HTTPS — over plain
http on a local address, no browser will allow them.
</p>
)
}
return (
<div className="flex flex-wrap items-center justify-between gap-3">
<span className="text-sm text-muted-foreground">
{subscribed
? "This browser is subscribed."
: "This browser is not subscribed yet. Each device subscribes itself."}
</span>
<Button
variant="outline"
size="sm"
disabled={change.isPending}
onClick={() => change.mutate(!subscribed)}
>
{subscribed ? <BellOff /> : <Bell />}
{subscribed ? "Unsubscribe" : "Subscribe"}
</Button>
</div>
)
}
function Alerts() {
const { data } = useQuery({
queryKey: alertsKey,
@@ -266,6 +333,8 @@ function AlertsForm({ initial }: { initial: AlertsConfig }) {
</div>
</div>
{channel.kind === "webpush" ? <ThisBrowser /> : null}
<div className="grid gap-3 sm:grid-cols-3">
{FIELDS[channel.kind].map(([key, label, placeholder]) => (
<div key={key} className="grid gap-1.5">