Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3b2c48b5e4
commit
c0c4addb85
285 changed files with 25880 additions and 0 deletions
99
pkg/services/ntfy/ntfy.go
Normal file
99
pkg/services/ntfy/ntfy.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package ntfy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/meta"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient"
|
||||
)
|
||||
|
||||
// Service sends notifications to Ntfy.
|
||||
type Service struct {
|
||||
standard.Standard
|
||||
Config *Config
|
||||
pkr format.PropKeyResolver
|
||||
}
|
||||
|
||||
// Send delivers a notification message to Ntfy.
|
||||
func (service *Service) Send(message string, params *types.Params) error {
|
||||
config := service.Config
|
||||
|
||||
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
|
||||
return fmt.Errorf("updating config from params: %w", err)
|
||||
}
|
||||
|
||||
if err := service.sendAPI(config, message); err != nil {
|
||||
return fmt.Errorf("failed to send ntfy notification: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize configures the service with a URL and logger.
|
||||
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
|
||||
service.SetLogger(logger)
|
||||
service.Config = &Config{}
|
||||
service.pkr = format.NewPropKeyResolver(service.Config)
|
||||
|
||||
_ = service.pkr.SetDefaultProps(service.Config)
|
||||
|
||||
return service.Config.setURL(&service.pkr, configURL)
|
||||
}
|
||||
|
||||
// GetID returns the service identifier.
|
||||
func (service *Service) GetID() string {
|
||||
return Scheme
|
||||
}
|
||||
|
||||
// sendAPI sends a notification to the Ntfy API.
|
||||
func (service *Service) sendAPI(config *Config, message string) error {
|
||||
response := apiResponse{}
|
||||
request := message
|
||||
jsonClient := jsonclient.NewClient()
|
||||
|
||||
headers := jsonClient.Headers()
|
||||
headers.Del("Content-Type")
|
||||
headers.Set("User-Agent", "shoutrrr/"+meta.Version)
|
||||
addHeaderIfNotEmpty(&headers, "Title", config.Title)
|
||||
addHeaderIfNotEmpty(&headers, "Priority", config.Priority.String())
|
||||
addHeaderIfNotEmpty(&headers, "Tags", strings.Join(config.Tags, ","))
|
||||
addHeaderIfNotEmpty(&headers, "Delay", config.Delay)
|
||||
addHeaderIfNotEmpty(&headers, "Actions", strings.Join(config.Actions, ";"))
|
||||
addHeaderIfNotEmpty(&headers, "Click", config.Click)
|
||||
addHeaderIfNotEmpty(&headers, "Attach", config.Attach)
|
||||
addHeaderIfNotEmpty(&headers, "X-Icon", config.Icon)
|
||||
addHeaderIfNotEmpty(&headers, "Filename", config.Filename)
|
||||
addHeaderIfNotEmpty(&headers, "Email", config.Email)
|
||||
|
||||
if !config.Cache {
|
||||
headers.Add("Cache", "no")
|
||||
}
|
||||
|
||||
if !config.Firebase {
|
||||
headers.Add("Firebase", "no")
|
||||
}
|
||||
|
||||
if err := jsonClient.Post(config.GetAPIURL(), request, &response); err != nil {
|
||||
if jsonClient.ErrorResponse(err, &response) {
|
||||
// apiResponse implements Error
|
||||
return &response
|
||||
}
|
||||
|
||||
return fmt.Errorf("posting to Ntfy API: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// addHeaderIfNotEmpty adds a header to the request if the value is non-empty.
|
||||
func addHeaderIfNotEmpty(headers *http.Header, key string, value string) {
|
||||
if value != "" {
|
||||
headers.Add(key, value)
|
||||
}
|
||||
}
|
119
pkg/services/ntfy/ntfy_config.go
Normal file
119
pkg/services/ntfy/ntfy_config.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package ntfy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt" // Add this import
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// Scheme is the identifying part of this service's configuration URL.
|
||||
const (
|
||||
Scheme = "ntfy"
|
||||
)
|
||||
|
||||
// ErrTopicRequired indicates that the topic is missing from the config URL.
|
||||
var ErrTopicRequired = errors.New("topic is required")
|
||||
|
||||
// Config holds the configuration for the Ntfy service.
|
||||
type Config struct {
|
||||
Title string `default:"" desc:"Message title" key:"title"`
|
||||
Host string `default:"ntfy.sh" desc:"Server hostname and port" url:"host"`
|
||||
Topic string ` desc:"Target topic name" url:"path" required:""`
|
||||
Password string ` desc:"Auth password" url:"password" optional:""`
|
||||
Username string ` desc:"Auth username" url:"user" optional:""`
|
||||
Scheme string `default:"https" desc:"Server protocol, http or https" key:"scheme"`
|
||||
Tags []string ` desc:"List of tags that may or not map to emojis" key:"tags" optional:""`
|
||||
Priority priority `default:"default" desc:"Message priority with 1=min, 3=default and 5=max" key:"priority"`
|
||||
Actions []string ` desc:"Custom user action buttons for notifications, see https://docs.ntfy.sh/publish/#action-buttons" key:"actions" optional:"" sep:";"`
|
||||
Click string ` desc:"Website opened when notification is clicked" key:"click" optional:""`
|
||||
Attach string ` desc:"URL of an attachment, see attach via URL" key:"attach" optional:""`
|
||||
Filename string ` desc:"File name of the attachment" key:"filename" optional:""`
|
||||
Delay string ` desc:"Timestamp or duration for delayed delivery, see https://docs.ntfy.sh/publish/#scheduled-delivery" key:"delay,at,in" optional:""`
|
||||
Email string ` desc:"E-mail address for e-mail notifications" key:"email" optional:""`
|
||||
Icon string ` desc:"URL to use as notification icon" key:"icon" optional:""`
|
||||
Cache bool `default:"yes" desc:"Cache messages" key:"cache"`
|
||||
Firebase bool `default:"yes" desc:"Send to firebase" key:"firebase"`
|
||||
}
|
||||
|
||||
// Enums returns the fields that use an EnumFormatter for their values.
|
||||
func (*Config) Enums() map[string]types.EnumFormatter {
|
||||
return map[string]types.EnumFormatter{
|
||||
"Priority": Priority.Enum,
|
||||
}
|
||||
}
|
||||
|
||||
// GetURL returns a URL representation of the Config's current field values.
|
||||
func (config *Config) GetURL() *url.URL {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.getURL(&resolver)
|
||||
}
|
||||
|
||||
// SetURL updates the Config from a URL representation of its field values.
|
||||
func (config *Config) SetURL(url *url.URL) error {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.setURL(&resolver, url)
|
||||
}
|
||||
|
||||
// GetAPIURL constructs the API URL for the Ntfy service based on the configuration.
|
||||
func (config *Config) GetAPIURL() string {
|
||||
path := config.Topic
|
||||
if !strings.HasPrefix(config.Topic, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
var creds *url.Userinfo
|
||||
if config.Password != "" {
|
||||
creds = url.UserPassword(config.Username, config.Password)
|
||||
}
|
||||
|
||||
apiURL := url.URL{
|
||||
Scheme: config.Scheme,
|
||||
Host: config.Host,
|
||||
Path: path,
|
||||
User: creds,
|
||||
}
|
||||
|
||||
return apiURL.String()
|
||||
}
|
||||
|
||||
// getURL constructs a URL from the Config's fields using the provided resolver.
|
||||
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
|
||||
return &url.URL{
|
||||
User: url.UserPassword(config.Username, config.Password),
|
||||
Host: config.Host,
|
||||
Scheme: Scheme,
|
||||
ForceQuery: true,
|
||||
Path: config.Topic,
|
||||
RawQuery: format.BuildQuery(resolver),
|
||||
}
|
||||
}
|
||||
|
||||
// setURL updates the Config from a URL using the provided resolver.
|
||||
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
|
||||
password, _ := url.User.Password()
|
||||
config.Password = password
|
||||
config.Username = url.User.Username()
|
||||
config.Host = url.Host
|
||||
config.Topic = strings.TrimPrefix(url.Path, "/")
|
||||
|
||||
url.RawQuery = strings.ReplaceAll(url.RawQuery, ";", "%3b")
|
||||
for key, vals := range url.Query() {
|
||||
if err := resolver.Set(key, vals[0]); err != nil {
|
||||
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
|
||||
}
|
||||
}
|
||||
|
||||
if url.String() != "ntfy://dummy@dummy.com" {
|
||||
if config.Topic == "" {
|
||||
return ErrTopicRequired
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
19
pkg/services/ntfy/ntfy_json.go
Normal file
19
pkg/services/ntfy/ntfy_json.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package ntfy
|
||||
|
||||
import "fmt"
|
||||
|
||||
//nolint:errname
|
||||
type apiResponse struct {
|
||||
Code int64 `json:"code"`
|
||||
Message string `json:"error"`
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
func (e *apiResponse) Error() string {
|
||||
msg := fmt.Sprintf("server response: %v (%v)", e.Message, e.Code)
|
||||
if e.Link != "" {
|
||||
return msg + ", see: " + e.Link
|
||||
}
|
||||
|
||||
return msg
|
||||
}
|
55
pkg/services/ntfy/ntfy_priority.go
Normal file
55
pkg/services/ntfy/ntfy_priority.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package ntfy
|
||||
|
||||
import (
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// Priority levels as constants.
|
||||
const (
|
||||
PriorityMin priority = 1
|
||||
PriorityLow priority = 2
|
||||
PriorityDefault priority = 3
|
||||
PriorityHigh priority = 4
|
||||
PriorityMax priority = 5
|
||||
)
|
||||
|
||||
// Priority defines the notification priority levels.
|
||||
var Priority = &priorityVals{
|
||||
Min: PriorityMin,
|
||||
Low: PriorityLow,
|
||||
Default: PriorityDefault,
|
||||
High: PriorityHigh,
|
||||
Max: PriorityMax,
|
||||
Enum: format.CreateEnumFormatter(
|
||||
[]string{
|
||||
"",
|
||||
"Min",
|
||||
"Low",
|
||||
"Default",
|
||||
"High",
|
||||
"Max",
|
||||
}, map[string]int{
|
||||
"1": int(PriorityMin),
|
||||
"2": int(PriorityLow),
|
||||
"3": int(PriorityDefault),
|
||||
"4": int(PriorityHigh),
|
||||
"5": int(PriorityMax),
|
||||
"urgent": int(PriorityMax),
|
||||
}),
|
||||
}
|
||||
|
||||
type priority int
|
||||
|
||||
type priorityVals struct {
|
||||
Min priority
|
||||
Low priority
|
||||
Default priority
|
||||
High priority
|
||||
Max priority
|
||||
Enum types.EnumFormatter
|
||||
}
|
||||
|
||||
func (p priority) String() string {
|
||||
return Priority.Enum.Print(int(p))
|
||||
}
|
162
pkg/services/ntfy/ntfy_test.go
Normal file
162
pkg/services/ntfy/ntfy_test.go
Normal file
|
@ -0,0 +1,162 @@
|
|||
package ntfy
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
|
||||
gomegaformat "github.com/onsi/gomega/format"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
)
|
||||
|
||||
func TestNtfy(t *testing.T) {
|
||||
gomegaformat.CharactersAroundMismatchToInclude = 20
|
||||
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Shoutrrr Ntfy Suite")
|
||||
}
|
||||
|
||||
var (
|
||||
service = &Service{}
|
||||
envBarkURL *url.URL
|
||||
logger *log.Logger = testutils.TestLogger()
|
||||
_ = ginkgo.BeforeSuite(func() {
|
||||
envBarkURL, _ = url.Parse(os.Getenv("SHOUTRRR_NTFY_URL"))
|
||||
})
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("the ntfy service", func() {
|
||||
ginkgo.When("running integration tests", func() {
|
||||
ginkgo.It("should not error out", func() {
|
||||
if envBarkURL.String() == "" {
|
||||
ginkgo.Skip("No integration test ENV URL was set")
|
||||
|
||||
return
|
||||
}
|
||||
configURL := testutils.URLMust(envBarkURL.String())
|
||||
gomega.Expect(service.Initialize(configURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(service.Send("This is an integration test message", nil)).
|
||||
To(gomega.Succeed())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the config", func() {
|
||||
ginkgo.When("getting a API URL", func() {
|
||||
ginkgo.It("should return the expected URL", func() {
|
||||
gomega.Expect((&Config{
|
||||
Host: "host:8080",
|
||||
Scheme: "http",
|
||||
Topic: "topic",
|
||||
}).GetAPIURL()).To(gomega.Equal("http://host:8080/topic"))
|
||||
})
|
||||
})
|
||||
ginkgo.When("only required fields are set", func() {
|
||||
ginkgo.It("should set the optional fields to the defaults", func() {
|
||||
serviceURL := testutils.URLMust("ntfy://hostname/topic")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(*service.Config).To(gomega.Equal(Config{
|
||||
Host: "hostname",
|
||||
Topic: "topic",
|
||||
Scheme: "https",
|
||||
Tags: []string{""},
|
||||
Actions: []string{""},
|
||||
Priority: 3,
|
||||
Firebase: true,
|
||||
Cache: true,
|
||||
}))
|
||||
})
|
||||
})
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.It("should be identical after de-/serialization", func() {
|
||||
testURL := "ntfy://user:pass@example.com:2225/topic?cache=No&click=CLICK&firebase=No&icon=ICON&priority=Max&scheme=http&title=TITLE"
|
||||
config := &Config{}
|
||||
pkr := format.NewPropKeyResolver(config)
|
||||
gomega.Expect(config.setURL(&pkr, testutils.URLMust(testURL))).
|
||||
To(gomega.Succeed(), "verifying")
|
||||
gomega.Expect(config.GetURL().String()).To(gomega.Equal(testURL))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("sending the push payload", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
|
||||
ginkgo.It("should not report an error if the server accepts the payload", func() {
|
||||
serviceURL := testutils.URLMust("ntfy://:devicekey@hostname/testtopic")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
service.Config.GetAPIURL(),
|
||||
testutils.JSONRespondMust(200, apiResponse{
|
||||
Code: http.StatusOK,
|
||||
Message: "OK",
|
||||
}),
|
||||
)
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.Succeed())
|
||||
})
|
||||
|
||||
ginkgo.It("should not panic if a server error occurs", func() {
|
||||
serviceURL := testutils.URLMust("ntfy://:devicekey@hostname/testtopic")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
service.Config.GetAPIURL(),
|
||||
testutils.JSONRespondMust(500, apiResponse{
|
||||
Code: 500,
|
||||
Message: "someone turned off the internet",
|
||||
}),
|
||||
)
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
|
||||
})
|
||||
|
||||
ginkgo.It("should not panic if a communication error occurs", func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
serviceURL := testutils.URLMust("ntfy://:devicekey@nonresolvablehostname/testtopic")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the basic service API", func() {
|
||||
ginkgo.Describe("the service config", func() {
|
||||
ginkgo.It("should implement basic service config API methods correctly", func() {
|
||||
testutils.TestConfigGetInvalidQueryValue(&Config{})
|
||||
testutils.TestConfigSetInvalidQueryValue(&Config{}, "ntfy://host/topic?foo=bar")
|
||||
testutils.TestConfigSetDefaultValues(&Config{})
|
||||
testutils.TestConfigGetEnumsCount(&Config{}, 1)
|
||||
testutils.TestConfigGetFieldsCount(&Config{}, 15)
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("the service instance", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("should implement basic service API methods correctly", func() {
|
||||
serviceURL := testutils.URLMust("ntfy://:devicekey@hostname/testtopic")
|
||||
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
|
||||
testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("should return the correct service ID", func() {
|
||||
service := &Service{}
|
||||
gomega.Expect(service.GetID()).To(gomega.Equal("ntfy"))
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue