types.go 4.6 KB

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