Add ntfy subscriptions with digests, quiet hours and a cut feed
Security / Vulnerability check (push) Successful in 40s
Test / Lint and test (push) Failing after 6m43s

[[ntfy.subscriptions]] tables make the daemon subscribe to ntfy topics
itself and print each message as a notification card, so the phone's
ntfy app is the only client. Each subscription is one ntfy.Subscriber in
the daemon's errgroup: one event loop owns its state, and one goroutine
per connection reads the JSON stream. internal/app hands its messages to
modules.FromNtfy, Card, Validate and Submit with the source ntfy:<name>
and the message's ID as ref, so ntfy never imports modules.

The cursor is ntfy server time, never an ID. ntfy sends open before the
replay, so only keepalives and messages dealt with move it, and a
reconnect asks for two minutes before it, since a message is dated
before its attachment is stored. The IDs of the messages a replay could
send again are kept, at least the last 512 and 20,000 at most, so a
flood in the overlap never reprints; when the cap forgets IDs a replay
could send, the floor rises past them. A saved floor bounds replays: a
first start sets it to the open event's time, and max_backlog (default
1h, at most 12h), measured by the server's clock as each connection
tells it (its open event, else its Date, then the monotonic clock), never the
host's, so replays are requested unclamped and filtered as they arrive,
raises it when it clamps, so what it left out stays out. The
state, with the held messages and the receipt times, is written
atomically at most once a second and at shutdown. Redirects are never
followed, so credentials never go to another address or over plain http.
A stalled stream reconnects after twice the keepalive plus 15 s,
failures back off 5 to 120 s with jitter, a 429 waits for Retry-After,
and refused credentials, checked at startup through /auth, show as
unauthorized and retry every 5 minutes. A full print queue pauses
reading and retries the message from 1 s to 30 s, and the cursor moves
past it only once it is queued.

The paper budget is a strict window: at most rate_limit receipts (5) in
any rate_window (10m), digests included; the rest are held and print on
one digest card once there is room, and outside quiet hours
bypass_priority (5) and above print at once. Quiet hours hold everything
below their min_priority for one Overnight digest when the window ends,
or drop it; windows cross midnight and keep their times of day when
clocks change, also where the change skips midnight or a window's end.
A message whose sequence printed before, on a card or a digest row, is marked
(updated), or skipped with ntfy.format.updates = "skip"; the first of a
sequence its publisher named prints plain.

/healthz lists the subscriptions without changing how its status code is
decided, and no credential, topic or message text reaches it, the logs
or a config error. Subscriptions come from the file only, with strict
keys and per-entry defaults; credentials are optional, at most one kind.
ntfy.state_dir is required with a subscription: the unit gains
StateDirectory=uberbringer, install.sh writes the path into a new
config, and the Docker image sets it for a volume and runs as the fixed
user 10001. README explains how to lock a self-hosted ntfy server down
so that only your publishers can print.

Breaking: printer.cut_feed (default 3) feeds blank lines before every
cut, so the last line no longer sits right at the cut. They count toward
format.max_lines once per copy, so a job within 3 lines a copy of the
limit now fails. The renderer's option is 0 unless set, so the pinned
receipts keep their bytes.

testing/synctest drives the subscriber on a virtual clock, including a
24-hour soak with floods, repeats, refusals, a full queue, quiet hours
and a restart that fails on any leaked goroutine; it replaces a 24-hour
run on hardware. Tests over real sockets cover net/http's goroutines and
redirects to plain http and to another port.

Checked on the TM-T88IV against a live ntfy server: a phone message, quiet
hours with priorities 3 and 5 and the Overnight digest, a stale cursor, a
30-minute outage with an update, a wrong token, and the three-line cut
feed. The digest and the cut-feed card are pinned in TestPaperReceipts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-11-30 17:50:23 -06:00
1 parent 1335ed0687
commit fb76284129
52 files changed
+6464 -390

No files matched your search

+74 -6
View File
@@ -20,6 +20,7 @@ the daemon.
- [`notify`](#notify)
- [`ntfy`](#ntfy)
- [Notification cards](#notification-cards)
- [Digests](#digests)
## API
@@ -68,7 +69,7 @@ included, carry `Cache-Control: no-store`.
| POST | `/api/v1/preview/:module` | Show the lines that receipt prints, without printing it |
| GET | `/api/v1/modules` | List the modules |
| GET | `/api/v1/jobs/:id` | Job status |
| GET | `/healthz` | Printer and queue health (no auth) |
| GET | `/healthz` | Printer, queue and subscription health (no auth) |
| GET | `/openapi.json` | This API's OpenAPI 3.1 document (no auth) |
| GET | `/` | Version information (no auth) |
@@ -131,8 +132,9 @@ curl -H @"$HOME/.uberbringer-auth" https://printer.example.com/api/v1/jobs/job_0
`status` is `queued`, `printing`, `done` or `failed`. A failed job includes an
`error` field. `source` says where the job came from: `print` for
`/api/v1/print`, or `module:<name>` for a [module](#modules) route. The last
`queue.history` finished jobs are kept.
`/api/v1/print`, `module:<name>` for a [module](#modules) route, or
`ntfy:<name>` for an [ntfy subscription](../README.md#ntfy-subscriptions)
in the config. The last `queue.history` finished jobs are kept.
### Preview a receipt
@@ -153,12 +155,16 @@ curl -X POST https://printer.example.com/api/v1/preview \
│ !!! ACHTUNG !!! │
└─────────────────────┘
[qr: https://example.com]
- - - - - - - partial cut - - - - - - - -
```
- The preview is read back from the bytes the job renders to, so it shows
what the printer receives: wrapping, folded characters, `?` for missing
ones and the final cut.
ones, the blank lines [`printer.cut_feed`](../README.md#configuration)
feeds before a cut (3 by default) and the final cut.
- Centred and right-aligned text is indented as the printer places it,
counted in Font A columns. Text at a larger size shows each character
once.
@@ -172,11 +178,12 @@ With `Accept: application/json` the answer carries the styles too. For
{"type": "qr", "data": "https://example.com"}]`:
```json
{"columns": 42, "codepage": "cp437", "line_count": 3,
{"columns": 42, "codepage": "cp437", "line_count": 6,
"lines": [{"kind": "text", "text": "Total ............................. £12.50",
"runs": [{"from": 0, "to": 5, "bold": true}, {"from": 36, "to": 42, "bold": true}]},
{"kind": "text", "text": " Thanks ?"},
{"kind": "qr", "text": "[qr: https://example.com]", "data": "https://example.com"},
{"kind": "text", "text": ""}, {"kind": "text", "text": ""}, {"kind": "text", "text": ""},
{"kind": "cut", "text": " - - - - - - - partial cut - - - - - - - -", "data": "partial"}],
"warnings": ["elements[1]: 1 character not in cp437 printed as ?"]}
```
@@ -210,6 +217,28 @@ when the worker renders it, before anything is sent; its `error` reads
A printer that accepts connections but does not answer status requests is
reported as `"status": "unknown"` with HTTP `200`.
With [ntfy subscriptions](../README.md#ntfy-subscriptions) configured, an
`ntfy` array lists each one, in the order of the config. The status code
still depends on the printer alone, so a subscription that cannot connect
never turns `/healthz` to `503`:
```json
"ntfy": [{"name": "home", "state": "connected", "connected_since": "2026-11-29T20:58:01Z",
"last_message_at": "2026-11-29T21:04:05Z", "printed": 12, "digested": 2, "held": 0, "last_error": ""}]
```
| Field | Meaning |
|-------|---------|
| `state` | `connecting`, `connected`, `backoff` while it waits to reconnect after a failure, or `unauthorized` when the server refused its credentials; it then tries again every five minutes |
| `connected_since` | When the current connection opened; absent while there is none |
| `last_message_at` | When the last message arrived; absent until one has |
| `printed` | Messages printed on a card of their own since the daemon started |
| `digested` | Messages held for a [digest](#digests) since the daemon started |
| `held` | Messages waiting for the next digest |
| `last_error` | Why the last connection failed, such as `HTTP 401: the server refused the credentials` or `no data for 1m45s`; empty once one opens |
Credentials, topic names and message text never appear.
Details:
- **Brief problems are re-checked.** A printer that looks not ready is read
@@ -282,7 +311,8 @@ rendered.
A job is `{"elements": [...], "copies": 1}`. `copies` is 1–5, and each copy is
cut separately. A partial cut is added after the last element unless that
element is already a `cut`. Boxes, rules, key-value rows and lists are
element is already a `cut`. Before every cut, `printer.cut_feed` blank lines
feed (3 by default), so that the last line does not sit right at the cut. Boxes, rules, key-value rows and lists are
described under [Blocks](#blocks).
### `text`
@@ -652,6 +682,9 @@ curl -X POST https://printer.example.com/api/v1/print/notify \
[qr: https://grafana.lan/d/disk]
Open dashboard
- - - - - - - partial cut - - - - - - - -
```
@@ -704,6 +737,9 @@ attachment expires:
Open dashboard
[image 384 dots: sPs71M8A2Tq9.png]
- - - - - - - partial cut - - - - - - - -
```
@@ -728,6 +764,8 @@ attachment expires:
- A message that updates an earlier one, with the same `sequence_id`,
prints as it is, like any other: one message cannot tell whether it is
the first of its sequence, since a publisher may choose the sequence ID.
An [ntfy subscription](../README.md#ntfy-subscriptions), which remembers
what it printed, marks such a message `(updated)`.
- A message over the [limits](#notify) of `notify` is not refused; what is
over them is cut or left out. A title, topic, tag, label or file name is
cut with `...`, and the message after 4096 bytes. Tags after the 20th are
@@ -850,6 +888,36 @@ bold, the message, then the topic and time. That is a bug: it is logged at
ERROR as `module printed its fallback receipt` and reported to Sentry, and
`uberbringer preview --module` prints a warning.
A card an [ntfy subscription](../README.md#ntfy-subscriptions) prints for
a message that replaces one it printed before, of the same `sequence_id`,
has `(updated)` after its title, unless `ntfy.format.updates = "skip"`
leaves such messages out.
#### Digests
An ntfy subscription prints the messages it held back on one digest: those
over its rate limit as soon as the limit has room again, and those held in
quiet hours when the window ends. A row gives each message's time, its tag
markers and its title, or the first line of a message without one, or else
its topic, cut to 60 characters, in the order they arrived; the bottom
border holds the time from the first held message to the digest.
```text
┌─ ntfy digest · 6 held ─────────────────┐
│ 21:05 [!] Disk almost full │
│ 21:05 [!] Disk almost full (updated) │
│ 21:06 (!) Backup FAILED on nas01 │
│ 21:07 Watchtower │
│ 21:07 Jellyseerr │
│ 21:09 [x] grafana is DOWN │
└───────────────────────── 21:04-21:14 ──┘
```
A digest that holds a message from quiet hours is titled
`Overnight · N held`. It lists 50 messages and counts the rest on a last
row, `... and 13 more`. A digest that cannot be laid out prints as a plain
list of its rows, logged and reported like a card's fallback.
#### Markers
Up to two tags with a marker print it in front of the title, each marker