diff --git a/internal/app/uberbringer/ntfy.go b/internal/app/uberbringer/ntfy.go index 05cdec9..bac151a 100644 --- a/internal/app/uberbringer/ntfy.go +++ b/internal/app/uberbringer/ntfy.go @@ -174,7 +174,7 @@ func (p *ntfyPrinter) Message(_ context.Context, m ntfy.Message, updated bool) e env := p.env() job, err := modules.Card(n, env.Card, env) p.reportFallback(m.ID, err) - return p.submit(job, m.ID) + return p.submit(job, printqueue.Meta{Ref: m.ID, Sequence: m.Notification()}) } // Digest prints the held messages on one receipt. @@ -182,13 +182,15 @@ func (p *ntfyPrinter) Digest(_ context.Context, d ntfy.Digest) error { env := p.env() job, err := modules.DigestCard(d, env.Card, env) p.reportFallback(digestRef, err) - return p.submit(job, digestRef) + return p.submit(job, printqueue.Meta{Ref: digestRef}) } -// submit validates job and queues it. Card and DigestCard return valid -// jobs, so a job that fails validation is a bug: it is logged and -// reported, and dropped rather than retried. -func (p *ntfyPrinter) submit(job receipt.Job, ref string) error { +// submit validates job and queues it with meta, from the subscription. +// Card and DigestCard return valid jobs, so a job that fails validation +// is a bug: it is logged and reported, and dropped rather than retried. +func (p *ntfyPrinter) submit(job receipt.Job, meta printqueue.Meta) error { + meta.Source = "ntfy:" + p.name + ref := meta.Ref if err := job.Validate(); err != nil { problems := 1 var ve *receipt.ValidationError @@ -199,7 +201,7 @@ func (p *ntfyPrinter) submit(job receipt.Job, ref string) error { reportNtfy(p.name, ref, fmt.Errorf("the receipt failed validation with %d problems", problems)) return nil } - status, err := p.jobs.Submit(job, printqueue.Meta{Source: "ntfy:" + p.name, Ref: ref}) + status, err := p.jobs.Submit(job, meta) if err != nil { return err } diff --git a/internal/app/uberbringer/ntfy_test.go b/internal/app/uberbringer/ntfy_test.go index 2c18c8b..ca503a4 100644 --- a/internal/app/uberbringer/ntfy_test.go +++ b/internal/app/uberbringer/ntfy_test.go @@ -244,13 +244,19 @@ func TestNtfyPrinter(t *testing.T) { if len(q.jobs) != 2 { t.Fatalf("%d jobs, want the card and the digest", len(q.jobs)) } - if q.metas[0] != (printqueue.Meta{Source: "ntfy:home", Ref: "msg1"}) || q.metas[1] != (printqueue.Meta{Source: "ntfy:home", Ref: "digest"}) { + if q.metas[0] != (printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Sequence: "alerts/msg1"}) || + q.metas[1] != (printqueue.Meta{Source: "ntfy:home", Ref: "digest"}) { t.Errorf("metas %+v", q.metas) } if title := q.jobs[0].Elements[0].Title; title != "Disk almost full (updated)" { t.Errorf("card title %q, want the update marked", title) } + update := ntfy.Message{ID: "msg3", SequenceID: "disk", Event: "message", Topic: "alerts", Title: "Disk full"} + if err := p.Message(ctx, update, false); err != nil || len(q.metas) != 3 || q.metas[2].Sequence != "alerts/disk" { + t.Errorf("metas %+v, %v; want a card of a named sequence to carry its notification", q.metas, err) + } + q.err = printqueue.ErrQueueFull if err := p.Message(ctx, m, false); !errors.Is(err, printqueue.ErrQueueFull) { t.Errorf("err = %v, want the full queue back", err) diff --git a/internal/pkg/ntfy/message.go b/internal/pkg/ntfy/message.go index be800bd..be1c462 100644 --- a/internal/pkg/ntfy/message.go +++ b/internal/pkg/ntfy/message.go @@ -97,6 +97,17 @@ func (m Message) Sequence() string { return m.ID } +// Notification names the notification m belongs to across topics, +// "/", since sequences are per topic. It is empty when m +// has neither a sequence nor an ID. +func (m Message) Notification() string { + seq := m.Sequence() + if seq == "" { + return "" + } + return m.Topic + "/" + seq +} + // Action is a button on a notification. View actions open URL; http // actions send a request with Method, Headers and Body; broadcast actions // send an Android intent with Extras; copy actions copy Value to the diff --git a/internal/pkg/ntfy/message_test.go b/internal/pkg/ntfy/message_test.go index 361c68e..940ad19 100644 --- a/internal/pkg/ntfy/message_test.go +++ b/internal/pkg/ntfy/message_test.go @@ -47,3 +47,21 @@ func TestDecodeMessage(t *testing.T) { t.Errorf("attachment %+v", m.Attachment) } } + +// Notification joins the topic and the sequence, which falls back to the +// ID, and is empty for an event with neither. +func TestNotification(t *testing.T) { + for _, tt := range []struct { + m ntfy.Message + want string + }{ + {ntfy.Message{ID: "m1", SequenceID: "backup", Topic: "alerts"}, "alerts/backup"}, + {ntfy.Message{ID: "m1", Topic: "alerts"}, "alerts/m1"}, + {ntfy.Message{Event: ntfy.EventMessageDelete, SequenceID: "backup", Topic: "jobs"}, "jobs/backup"}, + {ntfy.Message{Event: ntfy.EventKeepalive, Topic: "alerts"}, ""}, + } { + if got := tt.m.Notification(); got != tt.want { + t.Errorf("%+v: Notification() = %q, want %q", tt.m, got, tt.want) + } + } +} diff --git a/internal/pkg/printqueue/queue.go b/internal/pkg/printqueue/queue.go index e43a824..7e5ed9f 100644 --- a/internal/pkg/printqueue/queue.go +++ b/internal/pkg/printqueue/queue.go @@ -150,6 +150,11 @@ type Meta struct { // Ref identifies the job at its source, e.g. the ID of the request // that sent it, or of the ntfy message. Ref string + + // Sequence is the ntfy notification a card came from, + // "/"; empty for other jobs. It is stored with the + // job. + Sequence string } // entry is a job the queue keeps. @@ -265,6 +270,7 @@ func (q *Queue) Submit(job receipt.Job, meta Meta) (Status, error) { ID: id, Source: meta.Source, Ref: meta.Ref, + Sequence: meta.Sequence, CreatedAt: time.Now().UTC(), Deliveries: []Delivery{ {Seq: q.nextDelivery, Printer: DefaultPrinter, State: StoredQueued}, diff --git a/internal/pkg/printqueue/queue_store_test.go b/internal/pkg/printqueue/queue_store_test.go index 648916a..4bbda62 100644 --- a/internal/pkg/printqueue/queue_store_test.go +++ b/internal/pkg/printqueue/queue_store_test.go @@ -595,3 +595,25 @@ func waitFor(t *testing.T, cond func() bool) { time.Sleep(2 * time.Millisecond) } } + +// A job's source, reference and ntfy notification are stored with it. +func TestMetaIsStored(t *testing.T) { + inner := printqueue.NewMemoryStore() + q := newQueueOn(inner, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{}) + meta := printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Sequence: "alerts/backup"} + st, err := q.Submit(job("x"), meta) + if err != nil { + t.Fatal(err) + } + loaded, err := inner.Load(context.Background()) + if err != nil { + t.Fatal(err) + } + if len(loaded.Records) != 1 { + t.Fatalf("records %+v", loaded.Records) + } + r := loaded.Records[0] + if r.ID != st.ID || r.Source != meta.Source || r.Ref != meta.Ref || r.Sequence != meta.Sequence || r.Unique { + t.Errorf("record %+v, want the job's meta", r) + } +}