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. Actions []*action `json:"actions,omitempty"`
  29. Attachment *attachment `json:"attachment,omitempty"`
  30. Icon *icon `json:"icon,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 raw UTF-8, or "base64" for encoded bytes
  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 icon struct {
  43. URL string `json:"url"`
  44. Type string `json:"type,omitempty"`
  45. Size int64 `json:"size,omitempty"`
  46. }
  47. type action struct {
  48. ID string `json:"id"`
  49. Action string `json:"action"` // "view", "broadcast", or "http"
  50. Label string `json:"label"` // action button label
  51. Clear bool `json:"clear"` // clear notification after successful execution
  52. URL string `json:"url,omitempty"` // used in "view" and "http" actions
  53. Method string `json:"method,omitempty"` // used in "http" action, default is POST (!)
  54. Headers map[string]string `json:"headers,omitempty"` // used in "http" action
  55. Body string `json:"body,omitempty"` // used in "http" action
  56. Intent string `json:"intent,omitempty"` // used in "broadcast" action
  57. Extras map[string]string `json:"extras,omitempty"` // used in "broadcast" action
  58. }
  59. func newAction() *action {
  60. return &action{
  61. Headers: make(map[string]string),
  62. Extras: make(map[string]string),
  63. }
  64. }
  65. // publishMessage is used as input when publishing as JSON
  66. type publishMessage struct {
  67. Topic string `json:"topic"`
  68. Title string `json:"title"`
  69. Message string `json:"message"`
  70. Priority int `json:"priority"`
  71. Tags []string `json:"tags"`
  72. Click string `json:"click"`
  73. Actions []action `json:"actions"`
  74. Attach string `json:"attach"`
  75. Icon string `json:"icon"`
  76. Filename string `json:"filename"`
  77. Email string `json:"email"`
  78. Delay string `json:"delay"`
  79. }
  80. // messageEncoder is a function that knows how to encode a message
  81. type messageEncoder func(msg *message) (string, error)
  82. // newMessage creates a new message with the current timestamp
  83. func newMessage(event, topic, msg string) *message {
  84. return &message{
  85. ID: util.RandomString(messageIDLength),
  86. Time: time.Now().Unix(),
  87. Event: event,
  88. Topic: topic,
  89. Message: msg,
  90. }
  91. }
  92. // newOpenMessage is a convenience method to create an open message
  93. func newOpenMessage(topic string) *message {
  94. return newMessage(openEvent, topic, "")
  95. }
  96. // newKeepaliveMessage is a convenience method to create a keepalive message
  97. func newKeepaliveMessage(topic string) *message {
  98. return newMessage(keepaliveEvent, topic, "")
  99. }
  100. // newDefaultMessage is a convenience method to create a notification message
  101. func newDefaultMessage(topic, msg string) *message {
  102. return newMessage(messageEvent, topic, msg)
  103. }
  104. // newPollRequestMessage is a convenience method to create a poll request message
  105. func newPollRequestMessage(topic, pollID string) *message {
  106. m := newMessage(pollRequestEvent, topic, newMessageBody)
  107. m.PollID = pollID
  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.InIntList(q.Priority, messagePriority) {
  185. return false
  186. }
  187. if len(q.Tags) > 0 && !util.InStringListAll(msg.Tags, q.Tags) {
  188. return false
  189. }
  190. return true
  191. }