2021-12-17 01:33:01 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-12-19 19:27:26 +00:00
|
|
|
"fmt"
|
2023-11-17 01:54:58 +00:00
|
|
|
"heckel.io/ntfy/v2/util"
|
2021-12-17 01:33:01 +00:00
|
|
|
"net/http"
|
2021-12-19 19:27:26 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-12-17 01:33:01 +00:00
|
|
|
)
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// RequestOption is a generic request option that can be added to Client calls
|
|
|
|
type RequestOption = func(r *http.Request) error
|
|
|
|
|
|
|
|
// PublishOption is an option that can be passed to the Client.Publish call
|
2021-12-17 14:32:59 +00:00
|
|
|
type PublishOption = RequestOption
|
2021-12-19 19:27:26 +00:00
|
|
|
|
|
|
|
// SubscribeOption is an option that can be passed to a Client.Subscribe or Client.Poll call
|
2021-12-17 14:32:59 +00:00
|
|
|
type SubscribeOption = RequestOption
|
2021-12-17 01:33:01 +00:00
|
|
|
|
2022-01-13 02:24:48 +00:00
|
|
|
// WithMessage sets the notification message. This is an alternative way to passing the message body.
|
|
|
|
func WithMessage(message string) PublishOption {
|
|
|
|
return WithHeader("X-Message", message)
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithTitle adds a title to a message
|
2021-12-17 01:33:01 +00:00
|
|
|
func WithTitle(title string) PublishOption {
|
2021-12-17 14:32:59 +00:00
|
|
|
return WithHeader("X-Title", title)
|
2021-12-17 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max),
|
|
|
|
// or the corresponding names (see util.ParsePriority).
|
2021-12-17 01:33:01 +00:00
|
|
|
func WithPriority(priority string) PublishOption {
|
2021-12-17 14:32:59 +00:00
|
|
|
return WithHeader("X-Priority", priority)
|
2021-12-17 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list
|
|
|
|
// of tags. To use a slice, use WithTags instead
|
|
|
|
func WithTagsList(tags string) PublishOption {
|
2021-12-17 14:32:59 +00:00
|
|
|
return WithHeader("X-Tags", tags)
|
2021-12-17 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithTags adds a list of a tags to a message
|
|
|
|
func WithTags(tags []string) PublishOption {
|
|
|
|
return WithTagsList(strings.Join(tags, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDelay instructs the server to send the message at a later date. The delay parameter can be a
|
|
|
|
// Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery
|
|
|
|
// for details.
|
2021-12-17 01:33:01 +00:00
|
|
|
func WithDelay(delay string) PublishOption {
|
2021-12-17 14:32:59 +00:00
|
|
|
return WithHeader("X-Delay", delay)
|
2021-12-17 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 23:11:36 +00:00
|
|
|
// WithClick makes the notification action open the given URL as opposed to entering the detail view
|
|
|
|
func WithClick(url string) PublishOption {
|
|
|
|
return WithHeader("X-Click", url)
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:31:03 +00:00
|
|
|
// WithIcon makes the notification use the given URL as its icon
|
|
|
|
func WithIcon(icon string) PublishOption {
|
|
|
|
return WithHeader("X-Icon", icon)
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:31:25 +00:00
|
|
|
// WithActions adds custom user actions to the notification. The value can be either a JSON array or the
|
|
|
|
// simple format definition. See https://ntfy.sh/docs/publish/#action-buttons for details.
|
|
|
|
func WithActions(value string) PublishOption {
|
|
|
|
return WithHeader("X-Actions", value)
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:24:48 +00:00
|
|
|
// WithAttach sets a URL that will be used by the client to download an attachment
|
|
|
|
func WithAttach(attach string) PublishOption {
|
|
|
|
return WithHeader("X-Attach", attach)
|
|
|
|
}
|
|
|
|
|
2023-07-09 01:45:03 +00:00
|
|
|
// WithMarkdown instructs the server to interpret the message body as Markdown
|
|
|
|
func WithMarkdown() PublishOption {
|
|
|
|
return WithHeader("X-Markdown", "yes")
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:24:48 +00:00
|
|
|
// WithFilename sets a filename for the attachment, and/or forces the HTTP body to interpreted as an attachment
|
|
|
|
func WithFilename(filename string) PublishOption {
|
|
|
|
return WithHeader("X-Filename", filename)
|
|
|
|
}
|
|
|
|
|
2021-12-24 14:01:29 +00:00
|
|
|
// WithEmail instructs the server to also send the message to the given e-mail address
|
|
|
|
func WithEmail(email string) PublishOption {
|
|
|
|
return WithHeader("X-Email", email)
|
|
|
|
}
|
|
|
|
|
2022-02-02 04:39:57 +00:00
|
|
|
// WithBasicAuth adds the Authorization header for basic auth to the request
|
|
|
|
func WithBasicAuth(user, pass string) PublishOption {
|
|
|
|
return WithHeader("Authorization", util.BasicAuth(user, pass))
|
|
|
|
}
|
|
|
|
|
2023-02-14 02:35:58 +00:00
|
|
|
// WithBearerAuth adds the Authorization header for Bearer auth to the request
|
|
|
|
func WithBearerAuth(token string) PublishOption {
|
|
|
|
return WithHeader("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
}
|
|
|
|
|
2023-08-07 04:44:35 +00:00
|
|
|
// WithEmptyAuth clears the Authorization header
|
|
|
|
func WithEmptyAuth() PublishOption {
|
|
|
|
return RemoveHeader("Authorization")
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithNoCache instructs the server not to cache the message server-side
|
2021-12-17 01:33:01 +00:00
|
|
|
func WithNoCache() PublishOption {
|
|
|
|
return WithHeader("X-Cache", "no")
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithNoFirebase instructs the server not to forward the message to Firebase
|
2021-12-17 01:33:01 +00:00
|
|
|
func WithNoFirebase() PublishOption {
|
|
|
|
return WithHeader("X-Firebase", "no")
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithSince limits the number of messages returned from the server. The parameter since can be a Unix
|
|
|
|
// timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
|
2021-12-17 14:32:59 +00:00
|
|
|
func WithSince(since string) SubscribeOption {
|
|
|
|
return WithQueryParam("since", since)
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithSinceAll instructs the server to return all messages for the given topic from the server
|
|
|
|
func WithSinceAll() SubscribeOption {
|
|
|
|
return WithSince("all")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSinceDuration instructs the server to return all messages since the given duration ago
|
|
|
|
func WithSinceDuration(since time.Duration) SubscribeOption {
|
|
|
|
return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
|
|
|
|
func WithSinceUnixTime(since int64) SubscribeOption {
|
|
|
|
return WithSince(fmt.Sprintf("%d", since))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
|
|
|
|
// directly. Use Client.Poll instead.
|
2021-12-17 14:32:59 +00:00
|
|
|
func WithPoll() SubscribeOption {
|
|
|
|
return WithQueryParam("poll", "1")
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
|
|
|
|
// messages (see WithDelay). The messages will have a future date.
|
2021-12-17 14:32:59 +00:00
|
|
|
func WithScheduled() SubscribeOption {
|
|
|
|
return WithQueryParam("scheduled", "1")
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:22:27 +00:00
|
|
|
// WithFilter is a generic subscribe option meant to be used to filter for certain messages only
|
|
|
|
func WithFilter(param, value string) SubscribeOption {
|
|
|
|
return WithQueryParam(param, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMessageFilter instructs the server to only return messages that match the exact message
|
|
|
|
func WithMessageFilter(message string) SubscribeOption {
|
|
|
|
return WithQueryParam("message", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTitleFilter instructs the server to only return messages with a title that match the exact string
|
|
|
|
func WithTitleFilter(title string) SubscribeOption {
|
|
|
|
return WithQueryParam("title", title)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages
|
|
|
|
// without priority also implicitly match priority 3.
|
|
|
|
func WithPriorityFilter(priority int) SubscribeOption {
|
|
|
|
return WithQueryParam("priority", fmt.Sprintf("%d", priority))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTagsFilter instructs the server to only return messages that contain all of the given tags
|
|
|
|
func WithTagsFilter(tags []string) SubscribeOption {
|
|
|
|
return WithQueryParam("tags", strings.Join(tags, ","))
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithHeader is a generic option to add headers to a request
|
2021-12-17 14:32:59 +00:00
|
|
|
func WithHeader(header, value string) RequestOption {
|
2021-12-17 01:33:01 +00:00
|
|
|
return func(r *http.Request) error {
|
2021-12-17 14:32:59 +00:00
|
|
|
if value != "" {
|
|
|
|
r.Header.Set(header, value)
|
|
|
|
}
|
2021-12-17 01:33:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// WithQueryParam is a generic option to add query parameters to a request
|
2021-12-17 14:32:59 +00:00
|
|
|
func WithQueryParam(param, value string) RequestOption {
|
2021-12-17 01:33:01 +00:00
|
|
|
return func(r *http.Request) error {
|
2021-12-17 14:32:59 +00:00
|
|
|
if value != "" {
|
2021-12-17 01:33:01 +00:00
|
|
|
q := r.URL.Query()
|
2021-12-17 14:32:59 +00:00
|
|
|
q.Add(param, value)
|
2021-12-17 01:33:01 +00:00
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 04:44:35 +00:00
|
|
|
|
|
|
|
// RemoveHeader is a generic option to remove a header from a request
|
|
|
|
func RemoveHeader(header string) RequestOption {
|
|
|
|
return func(r *http.Request) error {
|
|
|
|
if header != "" {
|
|
|
|
delete(r.Header, header)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|