message.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. Attachment *attachment `json:"attachment,omitempty"`
  26. }
  27. type attachment struct {
  28. Name string `json:"name"`
  29. Type string `json:"type"`
  30. Size int64 `json:"size"`
  31. Expires int64 `json:"expires"`
  32. PreviewURL string `json:"preview_url"`
  33. URL string `json:"url"`
  34. }
  35. // messageEncoder is a function that knows how to encode a message
  36. type messageEncoder func(msg *message) (string, error)
  37. // newMessage creates a new message with the current timestamp
  38. func newMessage(event, topic, msg string) *message {
  39. return &message{
  40. ID: util.RandomString(messageIDLength),
  41. Time: time.Now().Unix(),
  42. Event: event,
  43. Topic: topic,
  44. Priority: 0,
  45. Tags: nil,
  46. Title: "",
  47. Message: msg,
  48. }
  49. }
  50. // newOpenMessage is a convenience method to create an open message
  51. func newOpenMessage(topic string) *message {
  52. return newMessage(openEvent, topic, "")
  53. }
  54. // newKeepaliveMessage is a convenience method to create a keepalive message
  55. func newKeepaliveMessage(topic string) *message {
  56. return newMessage(keepaliveEvent, topic, "")
  57. }
  58. // newDefaultMessage is a convenience method to create a notification message
  59. func newDefaultMessage(topic, msg string) *message {
  60. return newMessage(messageEvent, topic, msg)
  61. }