message.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package server
  2. import (
  3. "time"
  4. "heckel.io/ntfy/util"
  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. UnifiedPush bool `json:"unifiedpush,omitempty"` //this could be 'up'
  26. }
  27. // messageEncoder is a function that knows how to encode a message
  28. type messageEncoder func(msg *message) (string, error)
  29. // newMessage creates a new message with the current timestamp
  30. func newMessage(event, topic, msg string) *message {
  31. return &message{
  32. ID: util.RandomString(messageIDLength),
  33. Time: time.Now().Unix(),
  34. Event: event,
  35. Topic: topic,
  36. Priority: 0,
  37. Tags: nil,
  38. Title: "",
  39. Message: msg,
  40. }
  41. }
  42. // newOpenMessage is a convenience method to create an open message
  43. func newOpenMessage(topic string) *message {
  44. return newMessage(openEvent, topic, "")
  45. }
  46. // newKeepaliveMessage is a convenience method to create a keepalive message
  47. func newKeepaliveMessage(topic string) *message {
  48. return newMessage(keepaliveEvent, topic, "")
  49. }
  50. // newDefaultMessage is a convenience method to create a notification message
  51. func newDefaultMessage(topic, msg string) *message {
  52. return newMessage(messageEvent, topic, msg)
  53. }