A card from an ntfy subscription now carries its notification, "<topic>/<sequence ID>", in its Meta, and the queue stores it with the job. ntfy's sequence IDs are per topic and one subscription reads several topics, so the topic is part of the key; a message without a sequence_id is its own sequence. Digests and jobs from the routes carry none. Nothing reads it yet: it lets an ntfy message_delete find the waiting job of its notification, also after a restart, once cancelling comes. Part of stage 1c of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
4.9 KiB
Go
156 lines
4.9 KiB
Go
// Package ntfy holds the wire types of ntfy (https://ntfy.sh), the
|
|
// notification service: the message objects its subscribe API streams, one
|
|
// JSON object per line. Its Subscriber reads that stream from a server and
|
|
// hands each message to be printed, keeping to a paper budget and to quiet
|
|
// hours.
|
|
package ntfy
|
|
|
|
import "strings"
|
|
|
|
// Event types of a Message.
|
|
const (
|
|
EventOpen = "open"
|
|
EventKeepalive = "keepalive"
|
|
EventMessage = "message"
|
|
EventPollRequest = "poll_request"
|
|
EventMessageDelete = "message_delete"
|
|
EventMessageClear = "message_clear"
|
|
)
|
|
|
|
// Priorities. A message without one has the default priority.
|
|
const (
|
|
PriorityMin = 1
|
|
PriorityLow = 2
|
|
PriorityDefault = 3
|
|
PriorityHigh = 4
|
|
PriorityMax = 5
|
|
)
|
|
|
|
// Content types and encodings of a message body.
|
|
const (
|
|
ContentTypeMarkdown = "text/markdown"
|
|
EncodingBase64 = "base64"
|
|
)
|
|
|
|
// Message is one object of the subscribe API, such as the ntfy command
|
|
// line client passes to a command in $NTFY_RAW. Only event "message"
|
|
// carries a notification.
|
|
type Message struct {
|
|
ID string `json:"id"`
|
|
|
|
// SequenceID groups the messages that update one notification, which a
|
|
// later message of the sequence replaces. ntfy leaves it out when it
|
|
// equals ID. A publisher may choose it, so the first message of a
|
|
// sequence can have a SequenceID that differs from its ID.
|
|
SequenceID string `json:"sequence_id,omitempty"`
|
|
|
|
// Time is when the message was published, in Unix seconds.
|
|
Time int64 `json:"time"`
|
|
Expires int64 `json:"expires,omitempty"`
|
|
Event string `json:"event"`
|
|
Topic string `json:"topic"`
|
|
Title string `json:"title,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
|
|
// RawPriority is 1-5, or 0 when the message has none.
|
|
RawPriority int `json:"priority,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
Click string `json:"click,omitempty"`
|
|
Icon string `json:"icon,omitempty"`
|
|
Actions []Action `json:"actions,omitempty"`
|
|
|
|
Attachment *Attachment `json:"attachment,omitempty"`
|
|
PollID string `json:"poll_id,omitempty"`
|
|
|
|
// ContentType is empty for plain text, or text/markdown.
|
|
ContentType string `json:"content_type,omitempty"`
|
|
|
|
// Encoding is empty for UTF-8 text, or base64 for a binary body.
|
|
Encoding string `json:"encoding,omitempty"`
|
|
}
|
|
|
|
// Priority returns the message's priority, 1-5. A missing or out-of-range
|
|
// priority is the default, 3.
|
|
func (m Message) Priority() int {
|
|
if m.RawPriority < PriorityMin || m.RawPriority > PriorityMax {
|
|
return PriorityDefault
|
|
}
|
|
return m.RawPriority
|
|
}
|
|
|
|
// Printable reports whether m carries a notification with a text body: an
|
|
// event message, or a message without an event, that is not base64.
|
|
func (m Message) Printable() bool {
|
|
switch m.Event {
|
|
case "", EventMessage:
|
|
return !strings.EqualFold(m.Encoding, EncodingBase64)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Sequence names the notification m belongs to: its SequenceID, or its ID
|
|
// when it has none. Messages of one sequence update one notification.
|
|
func (m Message) Sequence() string {
|
|
if m.SequenceID != "" {
|
|
return m.SequenceID
|
|
}
|
|
return m.ID
|
|
}
|
|
|
|
// Notification names the notification m belongs to across topics,
|
|
// "<topic>/<sequence>", 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
|
|
// clipboard.
|
|
type Action struct {
|
|
ID string `json:"id"`
|
|
Action string `json:"action"`
|
|
Label string `json:"label"`
|
|
Clear bool `json:"clear"`
|
|
URL string `json:"url,omitempty"`
|
|
Method string `json:"method,omitempty"`
|
|
Headers map[string]string `json:"headers,omitempty"`
|
|
Body string `json:"body,omitempty"`
|
|
Intent string `json:"intent,omitempty"`
|
|
Extras map[string]string `json:"extras,omitempty"`
|
|
Value string `json:"value,omitempty"`
|
|
}
|
|
|
|
// Action types.
|
|
const (
|
|
ActionView = "view"
|
|
ActionHTTP = "http"
|
|
ActionBroadcast = "broadcast"
|
|
ActionCopy = "copy"
|
|
)
|
|
|
|
// Attachment is a file attached to a message.
|
|
type Attachment struct {
|
|
Name string `json:"name"`
|
|
|
|
// Type is the file's media type, e.g. image/png. It may be empty.
|
|
Type string `json:"type,omitempty"`
|
|
|
|
// Size is in bytes.
|
|
Size int64 `json:"size,omitempty"`
|
|
|
|
// Expires is when the file is deleted from the server, in Unix
|
|
// seconds; 0 for a file hosted elsewhere.
|
|
Expires int64 `json:"expires,omitempty"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// AttachmentMessage is the message ntfy gives a message published with an
|
|
// attachment and no text, with the file's name in place of %s.
|
|
const AttachmentMessage = "You received a file: %s"
|