| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"heckel.io/ntfy/v2/util" |
| 6 |
"net/http" |
| 7 |
"strings" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
// RequestOption is a generic request option that can be added to Client calls |
| 12 |
type RequestOption = func(r *http.Request) error |
| 13 |
|
| 14 |
// PublishOption is an option that can be passed to the Client.Publish call |
| 15 |
type PublishOption = RequestOption |
| 16 |
|
| 17 |
// SubscribeOption is an option that can be passed to a Client.Subscribe or Client.Poll call |
| 18 |
type SubscribeOption = RequestOption |
| 19 |
|
| 20 |
// WithMessage sets the notification message. This is an alternative way to passing the message body. |
| 21 |
func WithMessage(message string) PublishOption { |
| 22 |
return WithHeader("X-Message", message) |
| 23 |
} |
| 24 |
|
| 25 |
// WithTitle adds a title to a message |
| 26 |
func WithTitle(title string) PublishOption { |
| 27 |
return WithHeader("X-Title", title) |
| 28 |
} |
| 29 |
|
| 30 |
// WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max), |
| 31 |
// or the corresponding names (see util.ParsePriority). |
| 32 |
func WithPriority(priority string) PublishOption { |
| 33 |
return WithHeader("X-Priority", priority) |
| 34 |
} |
| 35 |
|
| 36 |
// WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list |
| 37 |
// of tags. To use a slice, use WithTags instead |
| 38 |
func WithTagsList(tags string) PublishOption { |
| 39 |
return WithHeader("X-Tags", tags) |
| 40 |
} |
| 41 |
|
| 42 |
// WithTags adds a list of a tags to a message |
| 43 |
func WithTags(tags []string) PublishOption { |
| 44 |
return WithTagsList(strings.Join(tags, ",")) |
| 45 |
} |
| 46 |
|
| 47 |
// WithDelay instructs the server to send the message at a later date. The delay parameter can be a |
| 48 |
// Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery |
| 49 |
// for details. |
| 50 |
func WithDelay(delay string) PublishOption { |
| 51 |
return WithHeader("X-Delay", delay) |
| 52 |
} |
| 53 |
|
| 54 |
// WithClick makes the notification action open the given URL as opposed to entering the detail view |
| 55 |
func WithClick(url string) PublishOption { |
| 56 |
return WithHeader("X-Click", url) |
| 57 |
} |
| 58 |
|
| 59 |
// WithIcon makes the notification use the given URL as its icon |
| 60 |
func WithIcon(icon string) PublishOption { |
| 61 |
return WithHeader("X-Icon", icon) |
| 62 |
} |
| 63 |
|
| 64 |
// WithActions adds custom user actions to the notification. The value can be either a JSON array or the |
| 65 |
// simple format definition. See https://ntfy.sh/docs/publish/#action-buttons for details. |
| 66 |
func WithActions(value string) PublishOption { |
| 67 |
return WithHeader("X-Actions", value) |
| 68 |
} |
| 69 |
|
| 70 |
// WithAttach sets a URL that will be used by the client to download an attachment |
| 71 |
func WithAttach(attach string) PublishOption { |
| 72 |
return WithHeader("X-Attach", attach) |
| 73 |
} |
| 74 |
|
| 75 |
// WithMarkdown instructs the server to interpret the message body as Markdown |
| 76 |
func WithMarkdown() PublishOption { |
| 77 |
return WithHeader("X-Markdown", "yes") |
| 78 |
} |
| 79 |
|
| 80 |
// WithTemplate instructs the server to use a specific template for the message. If templateName is is "yes" or "1", |
| 81 |
// the server will interpret the message and title as a template. |
| 82 |
func WithTemplate(templateName string) PublishOption { |
| 83 |
return WithHeader("X-Template", templateName) |
| 84 |
} |
| 85 |
|
| 86 |
// WithFilename sets a filename for the attachment, and/or forces the HTTP body to interpreted as an attachment |
| 87 |
func WithFilename(filename string) PublishOption { |
| 88 |
return WithHeader("X-Filename", filename) |
| 89 |
} |
| 90 |
|
| 91 |
// WithSequenceID sets a sequence ID for the message, allowing updates to existing notifications |
| 92 |
func WithSequenceID(sequenceID string) PublishOption { |
| 93 |
return WithHeader("X-Sequence-ID", sequenceID) |
| 94 |
} |
| 95 |
|
| 96 |
// WithEmail instructs the server to also send the message to the given e-mail address |
| 97 |
func WithEmail(email string) PublishOption { |
| 98 |
return WithHeader("X-Email", email) |
| 99 |
} |
| 100 |
|
| 101 |
// WithBasicAuth adds the Authorization header for basic auth to the request |
| 102 |
func WithBasicAuth(user, pass string) PublishOption { |
| 103 |
return WithHeader("Authorization", util.BasicAuth(user, pass)) |
| 104 |
} |
| 105 |
|
| 106 |
// WithBearerAuth adds the Authorization header for Bearer auth to the request |
| 107 |
func WithBearerAuth(token string) PublishOption { |
| 108 |
return WithHeader("Authorization", fmt.Sprintf("Bearer %s", token)) |
| 109 |
} |
| 110 |
|
| 111 |
// WithEmptyAuth clears the Authorization header |
| 112 |
func WithEmptyAuth() PublishOption { |
| 113 |
return RemoveHeader("Authorization") |
| 114 |
} |
| 115 |
|
| 116 |
// WithNoCache instructs the server not to cache the message server-side |
| 117 |
func WithNoCache() PublishOption { |
| 118 |
return WithHeader("X-Cache", "no") |
| 119 |
} |
| 120 |
|
| 121 |
// WithNoFirebase instructs the server not to forward the message to Firebase |
| 122 |
func WithNoFirebase() PublishOption { |
| 123 |
return WithHeader("X-Firebase", "no") |
| 124 |
} |
| 125 |
|
| 126 |
// WithSince limits the number of messages returned from the server. The parameter since can be a Unix |
| 127 |
// timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll). |
| 128 |
func WithSince(since string) SubscribeOption { |
| 129 |
return WithQueryParam("since", since) |
| 130 |
} |
| 131 |
|
| 132 |
// WithSinceAll instructs the server to return all messages for the given topic from the server |
| 133 |
func WithSinceAll() SubscribeOption { |
| 134 |
return WithSince("all") |
| 135 |
} |
| 136 |
|
| 137 |
// WithSinceDuration instructs the server to return all messages since the given duration ago |
| 138 |
func WithSinceDuration(since time.Duration) SubscribeOption { |
| 139 |
return WithSinceUnixTime(time.Now().Add(-1 * since).Unix()) |
| 140 |
} |
| 141 |
|
| 142 |
// WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp |
| 143 |
func WithSinceUnixTime(since int64) SubscribeOption { |
| 144 |
return WithSince(fmt.Sprintf("%d", since)) |
| 145 |
} |
| 146 |
|
| 147 |
// WithPoll instructs the server to close the connection after messages have been returned. Don't use this option |
| 148 |
// directly. Use Client.Poll instead. |
| 149 |
func WithPoll() SubscribeOption { |
| 150 |
return WithQueryParam("poll", "1") |
| 151 |
} |
| 152 |
|
| 153 |
// WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled |
| 154 |
// messages (see WithDelay). The messages will have a future date. |
| 155 |
func WithScheduled() SubscribeOption { |
| 156 |
return WithQueryParam("scheduled", "1") |
| 157 |
} |
| 158 |
|
| 159 |
// WithFilter is a generic subscribe option meant to be used to filter for certain messages only |
| 160 |
func WithFilter(param, value string) SubscribeOption { |
| 161 |
return WithQueryParam(param, value) |
| 162 |
} |
| 163 |
|
| 164 |
// WithMessageFilter instructs the server to only return messages that match the exact message |
| 165 |
func WithMessageFilter(message string) SubscribeOption { |
| 166 |
return WithQueryParam("message", message) |
| 167 |
} |
| 168 |
|
| 169 |
// WithTitleFilter instructs the server to only return messages with a title that match the exact string |
| 170 |
func WithTitleFilter(title string) SubscribeOption { |
| 171 |
return WithQueryParam("title", title) |
| 172 |
} |
| 173 |
|
| 174 |
// WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages |
| 175 |
// without priority also implicitly match priority 3. |
| 176 |
func WithPriorityFilter(priority int) SubscribeOption { |
| 177 |
return WithQueryParam("priority", fmt.Sprintf("%d", priority)) |
| 178 |
} |
| 179 |
|
| 180 |
// WithTagsFilter instructs the server to only return messages that contain all of the given tags |
| 181 |
func WithTagsFilter(tags []string) SubscribeOption { |
| 182 |
return WithQueryParam("tags", strings.Join(tags, ",")) |
| 183 |
} |
| 184 |
|
| 185 |
// WithHeader is a generic option to add headers to a request |
| 186 |
func WithHeader(header, value string) RequestOption { |
| 187 |
return func(r *http.Request) error { |
| 188 |
if value != "" { |
| 189 |
r.Header.Set(header, value) |
| 190 |
} |
| 191 |
return nil |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// WithQueryParam is a generic option to add query parameters to a request |
| 196 |
func WithQueryParam(param, value string) RequestOption { |
| 197 |
return func(r *http.Request) error { |
| 198 |
if value != "" { |
| 199 |
q := r.URL.Query() |
| 200 |
q.Add(param, value) |
| 201 |
r.URL.RawQuery = q.Encode() |
| 202 |
} |
| 203 |
return nil |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// RemoveHeader is a generic option to remove a header from a request |
| 208 |
func RemoveHeader(header string) RequestOption { |
| 209 |
return func(r *http.Request) error { |
| 210 |
if header != "" { |
| 211 |
delete(r.Header, header) |
| 212 |
} |
| 213 |
return nil |
| 214 |
} |
| 215 |
} |