Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with uberbringer token --raw. Every other token, every existing one included, gets 403 token_scope for it, with the raw elements in details, and keeps every other route. The notify module prints a notification card from a title, message, priority and tags, with topic, click, up to three actions, image, markdown and time as options; it decodes strictly and refuses a payload over its length caps. The ntfy module takes one ntfy message as ntfy sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does not know, cuts text over the caps, and answers 204 for other events and binary bodies. Both print every message plain: one message cannot tell the first of a sequence from a replacement, since a publisher may choose the sequence ID. internal/pkg/ntfy holds ntfy's message types. Both print the same card. The priority picks the form: a dark URGENT rule and a double box, a double box headed HIGH, a single box with the title in its border, or a rule with no box. Tags print as markers in front of the title, the rest on a Tags row; links print as QR codes with their labels; attached PNG, JPEG, GIF and WebP images print after the card. A line-level markdown subset is laid out without a dependency, keeping marks inside code spans and URLs, and emoji are removed after a few typed ones become markers. A card that cannot be laid out prints as a plain receipt, and the server logs the fault once at ERROR and reports it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the markers table is a setting with the user's own keys, also settable as UBERBRINGER_NTFY_MARKERS=name=value,... uberbringer token --modules issues a token limited to those modules' print and preview routes, job status and the module list; each route in the table says which tokens it takes, and a scope claim that could read as absent makes the token invalid. image elements take optional, which also shrinks a too-wide image to the paper; a QR code that could be too wide for the paper prints with smaller modules; box takes title_bold. Submit takes a Meta whose source shows in job status and, with a copy of the request ID, in every worker log line of the job. The four ntfy cards passed on the TM-T88IV, posted as the client recipe posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md describes the modules, the card and the client recipe, with tokens read from a header file; docs/swagger.json is regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa15cb27b1
commit
1335ed0687
119 files changed
+5626
-468
No files matched your search
@@ -0,0 +1,124 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"uberbringer/internal/pkg/ntfy"
|
||||
"uberbringer/internal/pkg/receipt"
|
||||
)
|
||||
|
||||
// Ntfy returns the ntfy module: the card of one ntfy message, as the ntfy
|
||||
// command line client passes it to a command in $NTFY_RAW.
|
||||
func Ntfy() Module {
|
||||
return ntfyModule{}
|
||||
}
|
||||
|
||||
type ntfyModule struct{}
|
||||
|
||||
func (ntfyModule) Name() string { return "ntfy" }
|
||||
|
||||
func (ntfyModule) Description() string {
|
||||
return "One ntfy message object, e.g. the CLI's $NTFY_RAW"
|
||||
}
|
||||
|
||||
// Build lays out the message as a card. The body is decoded leniently:
|
||||
// fields it does not know are ignored, so that messages from newer ntfy
|
||||
// versions still print. Events other than message, and messages with a
|
||||
// binary body, give ErrSkip.
|
||||
func (ntfyModule) Build(_ context.Context, body []byte, env Env) (receipt.Job, error) {
|
||||
m, err := decodeNtfy(body)
|
||||
if err != nil {
|
||||
return receipt.Job{}, err
|
||||
}
|
||||
n, err := FromNtfy(m)
|
||||
if err != nil {
|
||||
return receipt.Job{}, err
|
||||
}
|
||||
return noticeJob(n, env)
|
||||
}
|
||||
|
||||
// decodeNtfy decodes body, one JSON object, as an ntfy message, ignoring
|
||||
// unknown fields. A problem is returned as a *PayloadError.
|
||||
func decodeNtfy(body []byte) (ntfy.Message, error) {
|
||||
var (
|
||||
object map[string]json.RawMessage
|
||||
m ntfy.Message
|
||||
)
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
if err := dec.Decode(&object); err != nil {
|
||||
return m, &PayloadError{Problems: []string{bodyProblem(err)}}
|
||||
}
|
||||
if object == nil {
|
||||
return m, &PayloadError{Problems: []string{"body: expected a JSON object, got null"}}
|
||||
}
|
||||
if _, err := dec.Token(); !errors.Is(err, io.EOF) {
|
||||
return m, &PayloadError{Problems: []string{"body: trailing data after the payload"}}
|
||||
}
|
||||
if err := json.Unmarshal(body, &m); err != nil {
|
||||
var typeErr *json.UnmarshalTypeError
|
||||
if errors.As(err, &typeErr) && typeErr.Field != "" {
|
||||
return m, &PayloadError{Problems: []string{fmt.Sprintf("%s: expected %s", typeErr.Field, describe(typeErr.Type))}}
|
||||
}
|
||||
return m, &PayloadError{Problems: []string{"body: " + err.Error()}}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// FromNtfy turns an ntfy message into a Notice. It returns ErrSkip for an
|
||||
// event other than message, such as open or keepalive, and for a message
|
||||
// whose body is binary (encoding base64). A message without an event
|
||||
// counts as a message.
|
||||
//
|
||||
// The links are the click URL and the view actions' URLs; the other
|
||||
// actions are left out, since their headers, bodies and values can hold
|
||||
// secrets. The text ntfy gives a file sent without a message is dropped,
|
||||
// since the file prints on its own. The notice is never marked as an
|
||||
// update: a publisher may choose a message's sequence ID, so one message
|
||||
// cannot tell the first of its sequence from one that replaces it.
|
||||
func FromNtfy(m ntfy.Message) (Notice, error) {
|
||||
switch m.Event {
|
||||
case "", ntfy.EventMessage:
|
||||
default:
|
||||
return Notice{}, ErrSkip
|
||||
}
|
||||
if strings.EqualFold(m.Encoding, ntfy.EncodingBase64) {
|
||||
return Notice{}, ErrSkip
|
||||
}
|
||||
n := Notice{
|
||||
ID: m.ID,
|
||||
Topic: m.Topic,
|
||||
Title: m.Title,
|
||||
Message: m.Message,
|
||||
Markdown: strings.EqualFold(m.ContentType, ntfy.ContentTypeMarkdown),
|
||||
Priority: m.Priority(),
|
||||
Tags: m.Tags,
|
||||
}
|
||||
if m.Time > 0 {
|
||||
n.Time = time.Unix(m.Time, 0)
|
||||
}
|
||||
if m.Click != "" {
|
||||
n.Links = append(n.Links, Link{URL: m.Click})
|
||||
}
|
||||
for _, a := range m.Actions {
|
||||
if strings.EqualFold(a.Action, ntfy.ActionView) {
|
||||
n.Links = append(n.Links, Link{Label: a.Label, URL: a.URL})
|
||||
}
|
||||
}
|
||||
if a := m.Attachment; a != nil && a.URL != "" {
|
||||
if n.Message == fmt.Sprintf(ntfy.AttachmentMessage, a.Name) {
|
||||
n.Message = ""
|
||||
}
|
||||
n.File = &File{Name: a.Name, Type: a.Type, Size: a.Size, URL: a.URL}
|
||||
if a.Expires > 0 {
|
||||
n.File.Expires = time.Unix(a.Expires, 0)
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
Reference in new issue
Block a user