types.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. func newAction() *action {
  53. return &action{
  54. Headers: make(map[string]string),
  55. Extras: make(map[string]string),
  56. }
  57. }
  58. // publishMessage is used as input when publishing as JSON
  59. type publishMessage struct {
  60. Topic string `json:"topic"`
  61. Title string `json:"title"`
  62. Message string `json:"message"`
  63. Priority int `json:"priority"`
  64. Tags []string `json:"tags"`
  65. Click string `json:"click"`
  66. Actions []action `json:"actions"`
  67. Attach string `json:"attach"`
  68. Filename string `json:"filename"`
  69. Email string `json:"email"`
  70. Delay string `json:"delay"`
  71. }
  72. // messageEncoder is a function that knows how to encode a message
  73. type messageEncoder func(msg *message) (string, error)
  74. // newMessage creates a new message with the current timestamp
  75. func newMessage(event, topic, msg string) *message {
  76. return &message{
  77. ID: util.RandomString(messageIDLength),
  78. Time: time.Now().Unix(),
  79. Event: event,
  80. Topic: topic,
  81. Priority: 0,
  82. Tags: nil,
  83. Title: "",
  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. func validMessageID(s string) bool {
  100. return util.ValidRandomString(s, messageIDLength)
  101. }
  102. type sinceMarker struct {
  103. time time.Time
  104. id string
  105. }
  106. func newSinceTime(timestamp int64) sinceMarker {
  107. return sinceMarker{time.Unix(timestamp, 0), ""}
  108. }
  109. func newSinceID(id string) sinceMarker {
  110. return sinceMarker{time.Unix(0, 0), id}
  111. }
  112. func (t sinceMarker) IsAll() bool {
  113. return t == sinceAllMessages
  114. }
  115. func (t sinceMarker) IsNone() bool {
  116. return t == sinceNoMessages
  117. }
  118. func (t sinceMarker) IsID() bool {
  119. return t.id != ""
  120. }
  121. func (t sinceMarker) Time() time.Time {
  122. return t.time
  123. }
  124. func (t sinceMarker) ID() string {
  125. return t.id
  126. }
  127. var (
  128. sinceAllMessages = sinceMarker{time.Unix(0, 0), ""}
  129. sinceNoMessages = sinceMarker{time.Unix(1, 0), ""}
  130. )
  131. type queryFilter struct {
  132. Message string
  133. Title string
  134. Tags []string
  135. Priority []int
  136. }
  137. func parseQueryFilters(r *http.Request) (*queryFilter, error) {
  138. messageFilter := readParam(r, "x-message", "message", "m")
  139. titleFilter := readParam(r, "x-title", "title", "t")
  140. tagsFilter := util.SplitNoEmpty(readParam(r, "x-tags", "tags", "tag", "ta"), ",")
  141. priorityFilter := make([]int, 0)
  142. for _, p := range util.SplitNoEmpty(readParam(r, "x-priority", "priority", "prio", "p"), ",") {
  143. priority, err := util.ParsePriority(p)
  144. if err != nil {
  145. return nil, err
  146. }
  147. priorityFilter = append(priorityFilter, priority)
  148. }
  149. return &queryFilter{
  150. Message: messageFilter,
  151. Title: titleFilter,
  152. Tags: tagsFilter,
  153. Priority: priorityFilter,
  154. }, nil
  155. }
  156. func (q *queryFilter) Pass(msg *message) bool {
  157. if msg.Event != messageEvent {
  158. return true // filters only apply to messages
  159. }
  160. if q.Message != "" && msg.Message != q.Message {
  161. return false
  162. }
  163. if q.Title != "" && msg.Title != q.Title {
  164. return false
  165. }
  166. messagePriority := msg.Priority
  167. if messagePriority == 0 {
  168. messagePriority = 3 // For query filters, default priority (3) is the same as "not set" (0)
  169. }
  170. if len(q.Priority) > 0 && !util.InIntList(q.Priority, messagePriority) {
  171. return false
  172. }
  173. if len(q.Tags) > 0 && !util.InStringListAll(msg.Tags, q.Tags) {
  174. return false
  175. }
  176. return true
  177. }