2021-10-27 18:56:17 +00:00
|
|
|
package server
|
|
|
|
|
2021-10-29 17:58:14 +00:00
|
|
|
import (
|
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
"time"
|
|
|
|
)
|
2021-10-27 18:56:17 +00:00
|
|
|
|
|
|
|
// List of possible events
|
|
|
|
const (
|
|
|
|
openEvent = "open"
|
|
|
|
keepaliveEvent = "keepalive"
|
2021-10-29 17:58:14 +00:00
|
|
|
messageEvent = "message"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
messageIDLength = 10
|
2021-10-27 18:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// message represents a message published to a topic
|
|
|
|
type message struct {
|
2022-01-02 22:56:12 +00:00
|
|
|
ID string `json:"id"` // Random message ID
|
|
|
|
Time int64 `json:"time"` // Unix time in seconds
|
|
|
|
Event string `json:"event"` // One of the above
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
Priority int `json:"priority,omitempty"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
2022-01-04 23:25:49 +00:00
|
|
|
Click string `json:"click,omitempty"`
|
|
|
|
Attachment *attachment `json:"attachment,omitempty"`
|
2022-01-02 22:56:12 +00:00
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type attachment struct {
|
2022-01-04 18:45:29 +00:00
|
|
|
Name string `json:"name"`
|
2022-01-06 00:04:56 +00:00
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Size int64 `json:"size,omitempty"`
|
|
|
|
Expires int64 `json:"expires,omitempty"`
|
|
|
|
PreviewURL string `json:"preview_url,omitempty"`
|
2022-01-04 18:45:29 +00:00
|
|
|
URL string `json:"url"`
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// messageEncoder is a function that knows how to encode a message
|
|
|
|
type messageEncoder func(msg *message) (string, error)
|
|
|
|
|
|
|
|
// newMessage creates a new message with the current timestamp
|
2021-10-29 17:58:14 +00:00
|
|
|
func newMessage(event, topic, msg string) *message {
|
2021-10-27 18:56:17 +00:00
|
|
|
return &message{
|
2021-11-27 21:12:08 +00:00
|
|
|
ID: util.RandomString(messageIDLength),
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
Event: event,
|
|
|
|
Topic: topic,
|
|
|
|
Priority: 0,
|
|
|
|
Tags: nil,
|
|
|
|
Title: "",
|
|
|
|
Message: msg,
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOpenMessage is a convenience method to create an open message
|
2021-10-29 17:58:14 +00:00
|
|
|
func newOpenMessage(topic string) *message {
|
|
|
|
return newMessage(openEvent, topic, "")
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newKeepaliveMessage is a convenience method to create a keepalive message
|
2021-10-29 17:58:14 +00:00
|
|
|
func newKeepaliveMessage(topic string) *message {
|
|
|
|
return newMessage(keepaliveEvent, topic, "")
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newDefaultMessage is a convenience method to create a notification message
|
2021-10-29 17:58:14 +00:00
|
|
|
func newDefaultMessage(topic, msg string) *message {
|
|
|
|
return newMessage(messageEvent, topic, msg)
|
2021-10-27 18:56:17 +00:00
|
|
|
}
|