types.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. Title string `json:"title,omitempty"`
  24. Message string `json:"message,omitempty"`
  25. Priority int `json:"priority,omitempty"`
  26. Tags []string `json:"tags,omitempty"`
  27. Click string `json:"click,omitempty"`
  28. Actions []*action `json:"actions,omitempty"`
  29. Attachment *attachment `json:"attachment,omitempty"`
  30. PollID string `json:"poll_id,omitempty"`
  31. Sender string `json:"-"` // IP address of uploader, used for rate limiting
  32. Encoding string `json:"encoding,omitempty"` // empty for raw UTF-8, or "base64" for encoded bytes
  33. }
  34. type attachment struct {
  35. Name string `json:"name"`
  36. Type string `json:"type,omitempty"`
  37. Size int64 `json:"size,omitempty"`
  38. Expires int64 `json:"expires,omitempty"`
  39. URL string `json:"url"`
  40. }
  41. type action struct {
  42. ID string `json:"id"`
  43. Action string `json:"action"` // "view", "broadcast", or "http"
  44. Label string `json:"label"` // action button label
  45. Clear bool `json:"clear"` // clear notification after successful execution
  46. URL string `json:"url,omitempty"` // used in "view" and "http" actions
  47. Method string `json:"method,omitempty"` // used in "http" action, default is POST (!)
  48. Headers map[string]string `json:"headers,omitempty"` // used in "http" action
  49. Body string `json:"body,omitempty"` // used in "http" action
  50. Intent string `json:"intent,omitempty"` // used in "broadcast" action
  51. Extras map[string]string `json:"extras,omitempty"` // used in "broadcast" action
  52. }
  53. func newAction() *action {
  54. return &action{
  55. Headers: make(map[string]string),
  56. Extras: make(map[string]string),
  57. }
  58. }
  59. // publishMessage is used as input when publishing as JSON
  60. type publishMessage struct {
  61. Topic string `json:"topic"`
  62. Title string `json:"title"`
  63. Message string `json:"message"`
  64. Priority int `json:"priority"`
  65. Tags []string `json:"tags"`
  66. Click string `json:"click"`
  67. Actions []action `json:"actions"`
  68. Attach string `json:"attach"`
  69. Filename string `json:"filename"`
  70. Email string `json:"email"`
  71. Delay string `json:"delay"`
  72. }
  73. // messageEncoder is a function that knows how to encode a message
  74. type messageEncoder func(msg *message) (string, error)
  75. // newMessage creates a new message with the current timestamp
  76. func newMessage(event, topic, msg string) *message {
  77. return &message{
  78. ID: util.RandomString(messageIDLength),
  79. Time: time.Now().Unix(),
  80. Event: event,
  81. Topic: topic,
  82. Message: msg,
  83. }
  84. }
  85. // newOpenMessage is a convenience method to create an open message
  86. func newOpenMessage(topic string) *message {
  87. return newMessage(openEvent, topic, "")
  88. }
  89. // newKeepaliveMessage is a convenience method to create a keepalive message
  90. func newKeepaliveMessage(topic string) *message {
  91. return newMessage(keepaliveEvent, topic, "")
  92. }
  93. // newDefaultMessage is a convenience method to create a notification message
  94. func newDefaultMessage(topic, msg string) *message {
  95. return newMessage(messageEvent, topic, msg)
  96. }
  97. // newPollRequestMessage is a convenience method to create a poll request message
  98. func newPollRequestMessage(topic, pollID string) *message {
  99. m := newMessage(pollRequestEvent, topic, newMessageBody)
  100. m.PollID = pollID
  101. return m
  102. }
  103. func validMessageID(s string) bool {
  104. return util.ValidRandomString(s, messageIDLength)
  105. }
  106. type sinceMarker struct {
  107. time time.Time
  108. id string
  109. }
  110. func newSinceTime(timestamp int64) sinceMarker {
  111. return sinceMarker{time.Unix(timestamp, 0), ""}
  112. }
  113. func newSinceID(id string) sinceMarker {
  114. return sinceMarker{time.Unix(0, 0), id}
  115. }
  116. func (t sinceMarker) IsAll() bool {
  117. return t == sinceAllMessages
  118. }
  119. func (t sinceMarker) IsNone() bool {
  120. return t == sinceNoMessages
  121. }
  122. func (t sinceMarker) IsID() bool {
  123. return t.id != ""
  124. }
  125. func (t sinceMarker) Time() time.Time {
  126. return t.time
  127. }
  128. func (t sinceMarker) ID() string {
  129. return t.id
  130. }
  131. var (
  132. sinceAllMessages = sinceMarker{time.Unix(0, 0), ""}
  133. sinceNoMessages = sinceMarker{time.Unix(1, 0), ""}
  134. )
  135. type queryFilter struct {
  136. ID string
  137. Message string
  138. Title string
  139. Tags []string
  140. Priority []int
  141. }
  142. func parseQueryFilters(r *http.Request) (*queryFilter, error) {
  143. idFilter := readParam(r, "x-id", "id")
  144. messageFilter := readParam(r, "x-message", "message", "m")
  145. titleFilter := readParam(r, "x-title", "title", "t")
  146. tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",")
  147. priorityFilter := make([]int, 0)
  148. for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") {
  149. priority, err := util.ParsePriority(p)
  150. if err != nil {
  151. return nil, errHTTPBadRequestPriorityInvalid
  152. }
  153. priorityFilter = append(priorityFilter, priority)
  154. }
  155. return &queryFilter{
  156. ID: idFilter,
  157. Message: messageFilter,
  158. Title: titleFilter,
  159. Tags: tagsFilter,
  160. Priority: priorityFilter,
  161. }, nil
  162. }
  163. func (q *queryFilter) Pass(msg *message) bool {
  164. if msg.Event != messageEvent {
  165. return true // filters only apply to messages
  166. } else if q.ID != "" && msg.ID != q.ID {
  167. return false
  168. } else if q.Message != "" && msg.Message != q.Message {
  169. return false
  170. } else if q.Title != "" && msg.Title != q.Title {
  171. return false
  172. }
  173. messagePriority := msg.Priority
  174. if messagePriority == 0 {
  175. messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0)
  176. }
  177. if len(q.Priority) > 0 && !util.InIntList(q.Priority, messagePriority) {
  178. return false
  179. }
  180. if len(q.Tags) > 0 && !util.InStringListAll(msg.Tags, q.Tags) {
  181. return false
  182. }
  183. return true
  184. }