| 1 | package join |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 9 | ) |
| 10 | |
| 11 | // Config holds settings for the Join notification service. |
| 12 | type Config struct { |
| 13 | APIKey string `url:"pass"` |
| 14 | Devices []string ` desc:"Comma separated list of device IDs" key:"devices"` |
| 15 | Title string ` desc:"If set creates a notification" key:"title" optional:""` |
| 16 | Icon string ` desc:"Icon URL" key:"icon" optional:""` |
| 17 | } |
| 18 | |
| 19 | // Scheme identifies this service in configuration URLs. |
| 20 | const Scheme = "join" |
| 21 | |
| 22 | // Enums returns the fields that should use an EnumFormatter for their values. |
| 23 | func (c *Config) Enums() map[string]types.EnumFormatter { |
| 24 | return map[string]types.EnumFormatter{} |
| 25 | } |
| 26 | |
| 27 | // GetURL generates a URL from the current configuration values. |
| 28 | func (c *Config) GetURL() *url.URL { |
| 29 | resolver := format.NewPropKeyResolver(c) |
| 30 | |
| 31 | return c.getURL(&resolver) |
| 32 | } |
| 33 | |
| 34 | // SetURL updates the configuration from a URL representation. |
| 35 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 36 | resolver := format.NewPropKeyResolver(c) |
| 37 | |
| 38 | return c.setURL(&resolver, serviceURL) |
| 39 | } |
| 40 | |
| 41 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 42 | return &url.URL{ |
| 43 | User: url.UserPassword("Token", c.APIKey), |
| 44 | Host: "join", |
| 45 | Scheme: Scheme, |
| 46 | ForceQuery: true, |
| 47 | RawQuery: format.BuildQuery(resolver), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 52 | password, _ := serviceURL.User.Password() |
| 53 | c.APIKey = password |
| 54 | |
| 55 | for key, vals := range serviceURL.Query() { |
| 56 | if err := resolver.Set(key, vals[0]); err != nil { |
| 57 | return fmt.Errorf("setting config property %q from URL query: %w", key, err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if serviceURL.String() != "join://[email protected]" { |
| 62 | if len(c.Devices) < 1 { |
| 63 | return ErrDevicesMissing |
| 64 | } |
| 65 | |
| 66 | if len(c.APIKey) < 1 { |
| 67 | return ErrAPIKeyMissing |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return nil |
| 72 | } |