types.go 6.3 KB

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