message.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package server
  2. import (
  3. "heckel.io/ntfy/util"
  4. "time"
  5. )
  6. // List of possible events
  7. const (
  8. openEvent = "open"
  9. keepaliveEvent = "keepalive"
  10. messageEvent = "message"
  11. )
  12. const (
  13. messageIDLength = 10
  14. )
  15. // message represents a message published to a topic
  16. type message struct {
  17. ID string `json:"id"` // Random message ID
  18. Time int64 `json:"time"` // Unix time in seconds
  19. Event string `json:"event"` // One of the above
  20. Topic string `json:"topic"`
  21. Priority int `json:"priority,omitempty"`
  22. Tags []string `json:"tags,omitempty"`
  23. Title string `json:"title,omitempty"`
  24. Message string `json:"message,omitempty"`
  25. }
  26. // messageEncoder is a function that knows how to encode a message
  27. type messageEncoder func(msg *message) (string, error)
  28. // newMessage creates a new message with the current timestamp
  29. func newMessage(event, topic, msg string) *message {
  30. return &message{
  31. ID: util.RandomString(messageIDLength),
  32. Time: time.Now().Unix(),
  33. Event: event,
  34. Topic: topic,
  35. Priority: 0,
  36. Tags: nil,
  37. Title: "",
  38. Message: msg,
  39. }
  40. }
  41. // newOpenMessage is a convenience method to create an open message
  42. func newOpenMessage(topic string) *message {
  43. return newMessage(openEvent, topic, "")
  44. }
  45. // newKeepaliveMessage is a convenience method to create a keepalive message
  46. func newKeepaliveMessage(topic string) *message {
  47. return newMessage(keepaliveEvent, topic, "")
  48. }
  49. // newDefaultMessage is a convenience method to create a notification message
  50. func newDefaultMessage(topic, msg string) *message {
  51. return newMessage(messageEvent, topic, msg)
  52. }