| 1 |
package model |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net/netip" |
| 6 |
"time" |
| 7 |
|
| 8 |
"heckel.io/ntfy/v2/log" |
| 9 |
"heckel.io/ntfy/v2/util" |
| 10 |
) |
| 11 |
|
| 12 |
// List of possible events |
| 13 |
const ( |
| 14 |
OpenEvent = "open" |
| 15 |
KeepaliveEvent = "keepalive" |
| 16 |
MessageEvent = "message" |
| 17 |
MessageDeleteEvent = "message_delete" |
| 18 |
MessageClearEvent = "message_clear" |
| 19 |
PollRequestEvent = "poll_request" |
| 20 |
) |
| 21 |
|
| 22 |
// messageIDLength is the length of a randomly generated message ID |
| 23 |
const messageIDLength = 12 |
| 24 |
|
| 25 |
// Errors for message operations |
| 26 |
var ( |
| 27 |
ErrUnexpectedMessageType = errors.New("unexpected message type") |
| 28 |
ErrMessageNotFound = errors.New("message not found") |
| 29 |
) |
| 30 |
|
| 31 |
// Message represents a message published to a topic |
| 32 |
type Message struct { |
| 33 |
ID string `json:"id"` // Random message ID |
| 34 |
SequenceID string `json:"sequence_id,omitempty"` // Message sequence ID for updating message contents (omitted if same as ID) |
| 35 |
Time int64 `json:"time"` // Unix time in seconds |
| 36 |
Expires int64 `json:"expires,omitempty"` // Unix time in seconds (not required for open/keepalive) |
| 37 |
Event string `json:"event"` // One of the above |
| 38 |
Topic string `json:"topic"` |
| 39 |
Title string `json:"title,omitempty"` |
| 40 |
Message string `json:"message,omitempty"` |
| 41 |
Priority int `json:"priority,omitempty"` |
| 42 |
Tags []string `json:"tags,omitempty"` |
| 43 |
Click string `json:"click,omitempty"` |
| 44 |
Icon string `json:"icon,omitempty"` |
| 45 |
Actions []*Action `json:"actions,omitempty"` |
| 46 |
Attachment *Attachment `json:"attachment,omitempty"` |
| 47 |
PollID string `json:"poll_id,omitempty"` |
| 48 |
ContentType string `json:"content_type,omitempty"` // text/plain by default (if empty), or text/markdown |
| 49 |
Encoding string `json:"encoding,omitempty"` // Empty for raw UTF-8, or "base64" for encoded bytes |
| 50 |
Sender netip.Addr `json:"-"` // IP address of uploader, used for rate limiting |
| 51 |
User string `json:"-"` // UserID of the uploader, used to associated attachments |
| 52 |
} |
| 53 |
|
| 54 |
// Context returns a log context for the message |
| 55 |
func (m *Message) Context() log.Context { |
| 56 |
fields := map[string]any{ |
| 57 |
"topic": m.Topic, |
| 58 |
"message_id": m.ID, |
| 59 |
"message_sequence_id": m.SequenceID, |
| 60 |
"message_time": m.Time, |
| 61 |
"message_event": m.Event, |
| 62 |
"message_body_size": len(m.Message), |
| 63 |
} |
| 64 |
if m.Sender.IsValid() { |
| 65 |
fields["message_sender"] = m.Sender.String() |
| 66 |
} |
| 67 |
if m.User != "" { |
| 68 |
fields["message_user"] = m.User |
| 69 |
} |
| 70 |
return fields |
| 71 |
} |
| 72 |
|
| 73 |
// SanitizeUTF8 replaces invalid UTF-8 sequences and strips NUL bytes from all user-supplied |
| 74 |
// string fields. This is called early in the publish path so that all downstream consumers |
| 75 |
// (Firebase, WebPush, SMTP, cache) receive clean UTF-8 strings. |
| 76 |
func (m *Message) SanitizeUTF8() { |
| 77 |
m.Topic = util.SanitizeUTF8(m.Topic) |
| 78 |
m.Message = util.SanitizeUTF8(m.Message) |
| 79 |
m.Title = util.SanitizeUTF8(m.Title) |
| 80 |
m.Click = util.SanitizeUTF8(m.Click) |
| 81 |
m.Icon = util.SanitizeUTF8(m.Icon) |
| 82 |
m.ContentType = util.SanitizeUTF8(m.ContentType) |
| 83 |
for i, tag := range m.Tags { |
| 84 |
m.Tags[i] = util.SanitizeUTF8(tag) |
| 85 |
} |
| 86 |
if m.Attachment != nil { |
| 87 |
m.Attachment.Name = util.SanitizeUTF8(m.Attachment.Name) |
| 88 |
m.Attachment.Type = util.SanitizeUTF8(m.Attachment.Type) |
| 89 |
m.Attachment.URL = util.SanitizeUTF8(m.Attachment.URL) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// ForJSON returns a copy of the message suitable for JSON output. |
| 94 |
// It clears the SequenceID if it equals the ID to reduce redundancy. |
| 95 |
func (m *Message) ForJSON() *Message { |
| 96 |
if m.SequenceID == m.ID { |
| 97 |
clone := *m |
| 98 |
clone.SequenceID = "" |
| 99 |
return &clone |
| 100 |
} |
| 101 |
return m |
| 102 |
} |
| 103 |
|
| 104 |
// Attachment represents a file attachment on a message |
| 105 |
type Attachment struct { |
| 106 |
Name string `json:"name"` |
| 107 |
Type string `json:"type,omitempty"` |
| 108 |
Size int64 `json:"size,omitempty"` |
| 109 |
Expires int64 `json:"expires,omitempty"` |
| 110 |
URL string `json:"url"` |
| 111 |
} |
| 112 |
|
| 113 |
// Action represents a user-defined action on a message |
| 114 |
type Action struct { |
| 115 |
ID string `json:"id"` |
| 116 |
Action string `json:"action"` // "view", "broadcast", "http", or "copy" |
| 117 |
Label string `json:"label"` // action button label |
| 118 |
Clear bool `json:"clear"` // clear notification after successful execution |
| 119 |
URL string `json:"url,omitempty"` // used in "view" and "http" actions |
| 120 |
Method string `json:"method,omitempty"` // used in "http" action, default is POST (!) |
| 121 |
Headers map[string]string `json:"headers,omitempty"` // used in "http" action |
| 122 |
Body string `json:"body,omitempty"` // used in "http" action |
| 123 |
Intent string `json:"intent,omitempty"` // used in "broadcast" action |
| 124 |
Extras map[string]string `json:"extras,omitempty"` // used in "broadcast" action |
| 125 |
Value string `json:"value,omitempty"` // used in "copy" action |
| 126 |
} |
| 127 |
|
| 128 |
// NewAction creates a new action with initialized maps |
| 129 |
func NewAction() *Action { |
| 130 |
return &Action{ |
| 131 |
Headers: make(map[string]string), |
| 132 |
Extras: make(map[string]string), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// GenerateMessageID creates a new random message ID |
| 137 |
func GenerateMessageID() string { |
| 138 |
return util.RandomString(messageIDLength) |
| 139 |
} |
| 140 |
|
| 141 |
// ValidMessageID returns true if the given string is a valid message ID |
| 142 |
func ValidMessageID(s string) bool { |
| 143 |
return util.ValidRandomString(s, messageIDLength) |
| 144 |
} |
| 145 |
|
| 146 |
// NewMessage creates a new message with the current timestamp |
| 147 |
func NewMessage(event, topic, msg string) *Message { |
| 148 |
return &Message{ |
| 149 |
ID: GenerateMessageID(), |
| 150 |
Time: time.Now().Unix(), |
| 151 |
Event: event, |
| 152 |
Topic: topic, |
| 153 |
Message: msg, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// NewOpenMessage is a convenience method to create an open message |
| 158 |
func NewOpenMessage(topic string) *Message { |
| 159 |
return NewMessage(OpenEvent, topic, "") |
| 160 |
} |
| 161 |
|
| 162 |
// NewKeepaliveMessage is a convenience method to create a keepalive message |
| 163 |
func NewKeepaliveMessage(topic string) *Message { |
| 164 |
return NewMessage(KeepaliveEvent, topic, "") |
| 165 |
} |
| 166 |
|
| 167 |
// NewDefaultMessage is a convenience method to create a notification message |
| 168 |
func NewDefaultMessage(topic, msg string) *Message { |
| 169 |
return NewMessage(MessageEvent, topic, msg) |
| 170 |
} |
| 171 |
|
| 172 |
// NewActionMessage creates a new action message (message_delete or message_clear) |
| 173 |
func NewActionMessage(event, topic, sequenceID string) *Message { |
| 174 |
m := NewMessage(event, topic, "") |
| 175 |
m.SequenceID = sequenceID |
| 176 |
return m |
| 177 |
} |
| 178 |
|
| 179 |
// NewPollRequestMessage is a convenience method to create a poll request message |
| 180 |
func NewPollRequestMessage(topic, pollID string) *Message { |
| 181 |
m := NewMessage(PollRequestEvent, topic, "New message") |
| 182 |
m.PollID = pollID |
| 183 |
return m |
| 184 |
} |
| 185 |
|
| 186 |
// SinceMarker represents a point in time or message ID from which to retrieve messages |
| 187 |
type SinceMarker struct { |
| 188 |
time time.Time |
| 189 |
id string |
| 190 |
} |
| 191 |
|
| 192 |
// NewSinceTime creates a new SinceMarker from a Unix timestamp |
| 193 |
func NewSinceTime(timestamp int64) SinceMarker { |
| 194 |
return SinceMarker{time.Unix(timestamp, 0), ""} |
| 195 |
} |
| 196 |
|
| 197 |
// NewSinceID creates a new SinceMarker from a message ID |
| 198 |
func NewSinceID(id string) SinceMarker { |
| 199 |
return SinceMarker{time.Unix(0, 0), id} |
| 200 |
} |
| 201 |
|
| 202 |
// IsAll returns true if this is the "all messages" marker |
| 203 |
func (t SinceMarker) IsAll() bool { |
| 204 |
return t == SinceAllMessages |
| 205 |
} |
| 206 |
|
| 207 |
// IsNone returns true if this is the "no messages" marker |
| 208 |
func (t SinceMarker) IsNone() bool { |
| 209 |
return t == SinceNoMessages |
| 210 |
} |
| 211 |
|
| 212 |
// IsLatest returns true if this is the "latest message" marker |
| 213 |
func (t SinceMarker) IsLatest() bool { |
| 214 |
return t == SinceLatestMessage |
| 215 |
} |
| 216 |
|
| 217 |
// IsID returns true if this marker references a specific message ID |
| 218 |
func (t SinceMarker) IsID() bool { |
| 219 |
return t.id != "" && t.id != SinceLatestMessage.id |
| 220 |
} |
| 221 |
|
| 222 |
// Time returns the time component of the marker |
| 223 |
func (t SinceMarker) Time() time.Time { |
| 224 |
return t.time |
| 225 |
} |
| 226 |
|
| 227 |
// ID returns the message ID component of the marker |
| 228 |
func (t SinceMarker) ID() string { |
| 229 |
return t.id |
| 230 |
} |
| 231 |
|
| 232 |
// Common SinceMarker values for subscribing to messages |
| 233 |
var ( |
| 234 |
SinceAllMessages = SinceMarker{time.Unix(0, 0), ""} |
| 235 |
SinceNoMessages = SinceMarker{time.Unix(1, 0), ""} |
| 236 |
SinceLatestMessage = SinceMarker{time.Unix(0, 0), "latest"} |
| 237 |
) |