types.go 4.9 KB

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