types.go 5.6 KB

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