message.go 2.0 KB

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