message.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. Title string `json:"title,omitempty"`
  25. Message string `json:"message,omitempty"`
  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. }