1
0
Fork 0

Adding upstream version 0.8.9.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 10:16:14 +02:00
parent 3b2c48b5e4
commit c0c4addb85
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
285 changed files with 25880 additions and 0 deletions

119
pkg/services/join/join.go Normal file
View file

@ -0,0 +1,119 @@
package join
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
const (
// hookURL defines the Join API endpoint for sending push notifications.
hookURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush"
contentType = "text/plain"
)
// ErrSendFailed indicates a failure to send a notification to Join devices.
var ErrSendFailed = errors.New("failed to send notification to join devices")
// Service sends notifications to Join devices.
type Service struct {
standard.Standard
Config *Config
pkr format.PropKeyResolver
}
// Send delivers a notification message to Join devices.
func (service *Service) Send(message string, params *types.Params) error {
config := service.Config
if params == nil {
params = &types.Params{}
}
title, found := (*params)["title"]
if !found {
title = config.Title
}
icon, found := (*params)["icon"]
if !found {
icon = config.Icon
}
devices := strings.Join(config.Devices, ",")
return service.sendToDevices(devices, message, title, icon)
}
func (service *Service) sendToDevices(devices, message, title, icon string) error {
config := service.Config
apiURL, err := url.Parse(hookURL)
if err != nil {
return fmt.Errorf("parsing Join API URL: %w", err)
}
data := url.Values{}
data.Set("deviceIds", devices)
data.Set("apikey", config.APIKey)
data.Set("text", message)
if len(title) > 0 {
data.Set("title", title)
}
if len(icon) > 0 {
data.Set("icon", icon)
}
apiURL.RawQuery = data.Encode()
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodPost,
apiURL.String(),
nil,
)
if err != nil {
return fmt.Errorf("creating HTTP request: %w", err)
}
req.Header.Set("Content-Type", contentType)
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("sending HTTP request to Join: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("%w: %q, response status %q", ErrSendFailed, devices, res.Status)
}
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)
if err := service.Config.setURL(&service.pkr, configURL); err != nil {
return err
}
return nil
}
// GetID returns the identifier for this service.
func (service *Service) GetID() string {
return Scheme
}

View file

@ -0,0 +1,79 @@
package join
import (
"errors"
"fmt"
"net/url"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
// Scheme identifies this service in configuration URLs.
const Scheme = "join"
// ErrDevicesMissing indicates that no devices are specified in the configuration.
var (
ErrDevicesMissing = errors.New("devices missing from config URL")
ErrAPIKeyMissing = errors.New("API key missing from config URL")
)
// Config holds settings for the Join notification service.
type Config struct {
APIKey string `url:"pass"`
Devices []string ` desc:"Comma separated list of device IDs" key:"devices"`
Title string ` desc:"If set creates a notification" key:"title" optional:""`
Icon string ` desc:"Icon URL" key:"icon" optional:""`
}
// Enums returns the fields that should use an EnumFormatter for their values.
func (config *Config) Enums() map[string]types.EnumFormatter {
return map[string]types.EnumFormatter{}
}
// GetURL generates a URL from the current configuration values.
func (config *Config) GetURL() *url.URL {
resolver := format.NewPropKeyResolver(config)
return config.getURL(&resolver)
}
// SetURL updates the configuration from a URL representation.
func (config *Config) SetURL(url *url.URL) error {
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
return &url.URL{
User: url.UserPassword("Token", config.APIKey),
Host: "join",
Scheme: Scheme,
ForceQuery: true,
RawQuery: format.BuildQuery(resolver),
}
}
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
password, _ := url.User.Password()
config.APIKey = password
for key, vals := range url.Query() {
if err := resolver.Set(key, vals[0]); err != nil {
return fmt.Errorf("setting config property %q from URL query: %w", key, err)
}
}
if url.String() != "join://dummy@dummy.com" {
if len(config.Devices) < 1 {
return ErrDevicesMissing
}
if len(config.APIKey) < 1 {
return ErrAPIKeyMissing
}
}
return nil
}

View file

@ -0,0 +1,12 @@
package join
// ErrorMessage for error events within the pushover service.
type ErrorMessage string
const (
// APIKeyMissing should be used when a config URL is missing a token.
APIKeyMissing ErrorMessage = "API key missing from config URL" //nolint:gosec // false positive
// DevicesMissing should be used when a config URL is missing devices.
DevicesMissing ErrorMessage = "devices missing from config URL"
)

View file

@ -0,0 +1,173 @@
package join_test
import (
"net/url"
"os"
"testing"
"github.com/jarcoal/httpmock"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/services/join"
)
func TestJoin(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Join Suite")
}
var (
service *join.Service
config *join.Config
pkr format.PropKeyResolver
envJoinURL *url.URL
_ = ginkgo.BeforeSuite(func() {
service = &join.Service{}
envJoinURL, _ = url.Parse(os.Getenv("SHOUTRRR_JOIN_URL"))
})
)
var _ = ginkgo.Describe("the join service", func() {
ginkgo.When("running integration tests", func() {
ginkgo.It("should work", func() {
if envJoinURL.String() == "" {
return
}
serviceURL, _ := url.Parse(envJoinURL.String())
err := service.Initialize(serviceURL, testutils.TestLogger())
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = service.Send("this is an integration test", nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("returns the correct service identifier", func() {
gomega.Expect(service.GetID()).To(gomega.Equal("join"))
})
})
})
var _ = ginkgo.Describe("the join config", func() {
ginkgo.BeforeEach(func() {
config = &join.Config{}
pkr = format.NewPropKeyResolver(config)
})
ginkgo.When("updating it using an url", func() {
ginkgo.It("should update the API key using the password part of the url", func() {
url := createURL("dummy", "TestToken", "testDevice")
err := config.SetURL(url)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.APIKey).To(gomega.Equal("TestToken"))
})
ginkgo.It("should error if supplied with an empty token", func() {
url := createURL("user", "", "testDevice")
expectErrorMessageGivenURL(join.APIKeyMissing, url)
})
})
ginkgo.When("getting the current config", func() {
ginkgo.It("should return the config that is currently set as an url", func() {
config.APIKey = "test-token"
url := config.GetURL()
password, _ := url.User.Password()
gomega.Expect(password).To(gomega.Equal(config.APIKey))
gomega.Expect(url.Scheme).To(gomega.Equal("join"))
})
})
ginkgo.When("setting a config key", func() {
ginkgo.It("should split it by commas if the key is devices", func() {
err := pkr.Set("devices", "a,b,c,d")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Devices).To(gomega.Equal([]string{"a", "b", "c", "d"}))
})
ginkgo.It("should update icon when an icon is supplied", func() {
err := pkr.Set("icon", "https://example.com/icon.png")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Icon).To(gomega.Equal("https://example.com/icon.png"))
})
ginkgo.It("should update the title when it is supplied", func() {
err := pkr.Set("title", "new title")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Title).To(gomega.Equal("new title"))
})
ginkgo.It("should return an error if the key is not recognized", func() {
err := pkr.Set("devicey", "a,b,c,d")
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
ginkgo.When("getting a config key", func() {
ginkgo.It("should join it with commas if the key is devices", func() {
config.Devices = []string{"a", "b", "c"}
value, err := pkr.Get("devices")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(value).To(gomega.Equal("a,b,c"))
})
ginkgo.It("should return an error if the key is not recognized", func() {
_, err := pkr.Get("devicey")
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
ginkgo.When("listing the query fields", func() {
ginkgo.It(
"should return the keys \"devices\", \"icon\", \"title\" in alphabetical order",
func() {
fields := pkr.QueryFields()
gomega.Expect(fields).To(gomega.Equal([]string{"devices", "icon", "title"}))
},
)
})
ginkgo.When("parsing the configuration URL", func() {
ginkgo.It("should be identical after de-/serialization", func() {
input := "join://Token:apikey@join?devices=dev1%2Cdev2&icon=warning&title=hey"
config := &join.Config{}
gomega.Expect(config.SetURL(testutils.URLMust(input))).To(gomega.Succeed())
gomega.Expect(config.GetURL().String()).To(gomega.Equal(input))
})
})
ginkgo.Describe("sending the payload", func() {
var err error
ginkgo.BeforeEach(func() {
httpmock.Activate()
})
ginkgo.AfterEach(func() {
httpmock.DeactivateAndReset()
})
ginkgo.It("should not report an error if the server accepts the payload", func() {
config := join.Config{
APIKey: "apikey",
Devices: []string{"dev1"},
}
serviceURL := config.GetURL()
service := join.Service{}
err = service.Initialize(serviceURL, nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
httpmock.RegisterResponder(
"POST",
"https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush",
httpmock.NewStringResponder(200, ``),
)
err = service.Send("Message", nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
})
})
func createURL(username string, token string, devices string) *url.URL {
return &url.URL{
User: url.UserPassword("Token", token),
Host: username,
RawQuery: "devices=" + devices,
}
}
func expectErrorMessageGivenURL(msg join.ErrorMessage, url *url.URL) {
err := config.SetURL(url)
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).To(gomega.Equal(string(msg)))
}