types.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package server
  2. import (
  3. "heckel.io/ntfy/util"
  4. "net/http"
  5. "time"
  6. )
  7. // List of possible events
  8. const (
  9. openEvent = "open"
  10. keepaliveEvent = "keepalive"
  11. messageEvent = "message"
  12. pollRequestEvent = "poll_request"
  13. )
  14. const (
  15. messageIDLength = 12
  16. )
  17. // message represents a message published to a topic
  18. type message struct {
  19. ID string `json:"id"` // Random message ID
  20. Time int64 `json:"time"` // Unix time in seconds
  21. Event string `json:"event"` // One of the above
  22. Topic string `json:"topic"`
  23. Priority int `json:"priority,omitempty"`
  24. Tags []string `json:"tags,omitempty"`
  25. Click string `json:"click,omitempty"`
  26. Attachment *attachment `json:"attachment,omitempty"`
  27. Title string `json:"title,omitempty"`
  28. Message string `json:"message,omitempty"`
  29. Encoding string `json:"encoding,omitempty"` // empty for raw UTF-8, or "base64" for encoded bytes
  30. }
  31. type attachment struct {
  32. Name string `json:"name"`
  33. Type string `json:"type,omitempty"`
  34. Size int64 `json:"size,omitempty"`
  35. Expires int64 `json:"expires,omitempty"`
  36. URL string `json:"url"`
  37. Owner string `json:"-"` // IP address of uploader, used for rate limiting
  38. }
  39. // messageEncoder is a function that knows how to encode a message
  40. type messageEncoder func(msg *message) (string, error)
  41. // newMessage creates a new message with the current timestamp
  42. func newMessage(event, topic, msg string) *message {
  43. return &message{
  44. ID: util.RandomString(messageIDLength),
  45. Time: time.Now().Unix(),
  46. Event: event,
  47. Topic: topic,
  48. Priority: 0,
  49. Tags: nil,
  50. Title: "",
  51. Message: msg,
  52. }
  53. }
  54. // newOpenMessage is a convenience method to create an open message
  55. func newOpenMessage(topic string) *message {
  56. return newMessage(openEvent, topic, "")
  57. }
  58. // newKeepaliveMessage is a convenience method to create a keepalive message
  59. func newKeepaliveMessage(topic string) *message {
  60. return newMessage(keepaliveEvent, topic, "")
  61. }
  62. // newDefaultMessage is a convenience method to create a notification message
  63. func newDefaultMessage(topic, msg string) *message {
  64. return newMessage(messageEvent, topic, msg)
  65. }
  66. func validMessageID(s string) bool {
  67. return util.ValidRandomString(s, messageIDLength)
  68. }
  69. type sinceMarker struct {
  70. time time.Time
  71. id string
  72. }
  73. func newSinceTime(timestamp int64) sinceMarker {
  74. return sinceMarker{time.Unix(timestamp, 0), ""}
  75. }
  76. func newSinceID(id string) sinceMarker {
  77. return sinceMarker{time.Unix(0, 0), id}
  78. }
  79. func (t sinceMarker) IsAll() bool {
  80. return t == sinceAllMessages
  81. }
  82. func (t sinceMarker) IsNone() bool {
  83. return t == sinceNoMessages
  84. }
  85. func (t sinceMarker) IsID() bool {
  86. return t.id != ""
  87. }
  88. func (t sinceMarker) Time() time.Time {
  89. return t.time
  90. }
  91. func (t sinceMarker) ID() string {
  92. return t.id
  93. }
  94. var (
  95. sinceAllMessages = sinceMarker{time.Unix(0, 0), ""}
  96. sinceNoMessages = sinceMarker{time.Unix(1, 0), ""}
  97. )
  98. type queryFilter struct {
  99. Message string
  100. Title string
  101. Tags []string
  102. Priority []int
  103. }
  104. func parseQueryFilters(r *http.Request) (*queryFilter, error) {
  105. messageFilter := readParam(r, "x-message", "message", "m")
  106. titleFilter := readParam(r, "x-title", "title", "t")
  107. tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",")
  108. priorityFilter := make([]int, 0)
  109. for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") {
  110. priority, err := util.ParsePriority(p)
  111. if err != nil {
  112. return nil, err
  113. }
  114. priorityFilter = append(priorityFilter, priority)
  115. }
  116. return &queryFilter{
  117. Message: messageFilter,
  118. Title: titleFilter,
  119. Tags: tagsFilter,
  120. Priority: priorityFilter,
  121. }, nil
  122. }
  123. func (q *queryFilter) Pass(msg *message) bool {
  124. if msg.Event != messageEvent {
  125. return true // filters only apply to messages
  126. }
  127. if q.Message != "" && msg.Message != q.Message {
  128. return false
  129. }
  130. if q.Title != "" && msg.Title != q.Title {
  131. return false
  132. }
  133. messagePriority := msg.Priority
  134. if messagePriority == 0 {
  135. messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0)
  136. }
  137. if len(q.Priority) > 0 && !util.InIntList(q.Priority, messagePriority) {
  138. return false
  139. }
  140. if len(q.Tags) > 0 && !util.InStringListAll(msg.Tags, q.Tags) {
  141. return false
  142. }
  143. return true
  144. }