options.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package client
  2. import (
  3. "fmt"
  4. "heckel.io/ntfy/util"
  5. "net/http"
  6. "strings"
  7. "time"
  8. )
  9. // RequestOption is a generic request option that can be added to Client calls
  10. type RequestOption = func(r *http.Request) error
  11. // PublishOption is an option that can be passed to the Client.Publish call
  12. type PublishOption = RequestOption
  13. // SubscribeOption is an option that can be passed to a Client.Subscribe or Client.Poll call
  14. type SubscribeOption = RequestOption
  15. // WithMessage sets the notification message. This is an alternative way to passing the message body.
  16. func WithMessage(message string) PublishOption {
  17. return WithHeader("X-Message", message)
  18. }
  19. // WithTitle adds a title to a message
  20. func WithTitle(title string) PublishOption {
  21. return WithHeader("X-Title", title)
  22. }
  23. // WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max),
  24. // or the corresponding names (see util.ParsePriority).
  25. func WithPriority(priority string) PublishOption {
  26. return WithHeader("X-Priority", priority)
  27. }
  28. // WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list
  29. // of tags. To use a slice, use WithTags instead
  30. func WithTagsList(tags string) PublishOption {
  31. return WithHeader("X-Tags", tags)
  32. }
  33. // WithTags adds a list of a tags to a message
  34. func WithTags(tags []string) PublishOption {
  35. return WithTagsList(strings.Join(tags, ","))
  36. }
  37. // WithDelay instructs the server to send the message at a later date. The delay parameter can be a
  38. // Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery
  39. // for details.
  40. func WithDelay(delay string) PublishOption {
  41. return WithHeader("X-Delay", delay)
  42. }
  43. // WithClick makes the notification action open the given URL as opposed to entering the detail view
  44. func WithClick(url string) PublishOption {
  45. return WithHeader("X-Click", url)
  46. }
  47. // WithAttach sets a URL that will be used by the client to download an attachment
  48. func WithAttach(attach string) PublishOption {
  49. return WithHeader("X-Attach", attach)
  50. }
  51. // WithFilename sets a filename for the attachment, and/or forces the HTTP body to interpreted as an attachment
  52. func WithFilename(filename string) PublishOption {
  53. return WithHeader("X-Filename", filename)
  54. }
  55. // WithEmail instructs the server to also send the message to the given e-mail address
  56. func WithEmail(email string) PublishOption {
  57. return WithHeader("X-Email", email)
  58. }
  59. // WithBasicAuth adds the Authorization header for basic auth to the request
  60. func WithBasicAuth(user, pass string) PublishOption {
  61. return WithHeader("Authorization", util.BasicAuth(user, pass))
  62. }
  63. // WithNoCache instructs the server not to cache the message server-side
  64. func WithNoCache() PublishOption {
  65. return WithHeader("X-Cache", "no")
  66. }
  67. // WithNoFirebase instructs the server not to forward the message to Firebase
  68. func WithNoFirebase() PublishOption {
  69. return WithHeader("X-Firebase", "no")
  70. }
  71. // WithSince limits the number of messages returned from the server. The parameter since can be a Unix
  72. // timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
  73. func WithSince(since string) SubscribeOption {
  74. return WithQueryParam("since", since)
  75. }
  76. // WithSinceAll instructs the server to return all messages for the given topic from the server
  77. func WithSinceAll() SubscribeOption {
  78. return WithSince("all")
  79. }
  80. // WithSinceDuration instructs the server to return all messages since the given duration ago
  81. func WithSinceDuration(since time.Duration) SubscribeOption {
  82. return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
  83. }
  84. // WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
  85. func WithSinceUnixTime(since int64) SubscribeOption {
  86. return WithSince(fmt.Sprintf("%d", since))
  87. }
  88. // WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
  89. // directly. Use Client.Poll instead.
  90. func WithPoll() SubscribeOption {
  91. return WithQueryParam("poll", "1")
  92. }
  93. // WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
  94. // messages (see WithDelay). The messages will have a future date.
  95. func WithScheduled() SubscribeOption {
  96. return WithQueryParam("scheduled", "1")
  97. }
  98. // WithFilter is a generic subscribe option meant to be used to filter for certain messages only
  99. func WithFilter(param, value string) SubscribeOption {
  100. return WithQueryParam(param, value)
  101. }
  102. // WithMessageFilter instructs the server to only return messages that match the exact message
  103. func WithMessageFilter(message string) SubscribeOption {
  104. return WithQueryParam("message", message)
  105. }
  106. // WithTitleFilter instructs the server to only return messages with a title that match the exact string
  107. func WithTitleFilter(title string) SubscribeOption {
  108. return WithQueryParam("title", title)
  109. }
  110. // WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages
  111. // without priority also implicitly match priority 3.
  112. func WithPriorityFilter(priority int) SubscribeOption {
  113. return WithQueryParam("priority", fmt.Sprintf("%d", priority))
  114. }
  115. // WithTagsFilter instructs the server to only return messages that contain all of the given tags
  116. func WithTagsFilter(tags []string) SubscribeOption {
  117. return WithQueryParam("tags", strings.Join(tags, ","))
  118. }
  119. // WithHeader is a generic option to add headers to a request
  120. func WithHeader(header, value string) RequestOption {
  121. return func(r *http.Request) error {
  122. if value != "" {
  123. r.Header.Set(header, value)
  124. }
  125. return nil
  126. }
  127. }
  128. // WithQueryParam is a generic option to add query parameters to a request
  129. func WithQueryParam(param, value string) RequestOption {
  130. return func(r *http.Request) error {
  131. if value != "" {
  132. q := r.URL.Query()
  133. q.Add(param, value)
  134. r.URL.RawQuery = q.Encode()
  135. }
  136. return nil
  137. }
  138. }