Debian Codegraph @

pkg/services/specialized/notifiarr/notifiarr_config.go

raw
1 package notifiarr
2
3 import (
4 "fmt"
5 "net/url"
6 "strings"
7
8 "github.com/nicholas-fedor/shoutrrr/pkg/format"
9 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
10 "github.com/nicholas-fedor/shoutrrr/pkg/types"
11 )
12
13 // Config holds settings for the Notifiarr notification service.
14 type Config struct {
15 standard.EnumlessConfig
16
17 // The API key for Notifiarr authentication
18 APIKey string `desc:"The Notifiarr API key" key:"apikey" required:"true"`
19 // Optional name of the app/script for notifications
20 Name string `default:"Shoutrrr" desc:"Name of the app/script for notifications" key:"name" optional:""`
21 // Optional Discord channel ID for Discord notifications
22 Channel string `desc:"Discord channel ID for notifications" key:"channel" optional:""`
23 // Optional thumbnail URL for Discord notifications
24 Thumbnail string `desc:"Thumbnail URL for Discord notifications" key:"thumbnail" optional:""`
25 // Optional image URL for Discord notifications
26 Image string `desc:"Image URL for Discord notifications" key:"image" optional:""`
27 // Optional color for Discord notifications
28 Color string `desc:"Color for Discord notifications" key:"color" optional:""`
29 // Custom query parameters from webhook URL
30 webhookQuery url.Values
31 }
32
33 // Scheme identifies this service in configuration URLs.
34 const (
35 Scheme = "notifiarr"
36 // expectedPartsAfterSplit is the expected number of parts after splitting on "/passthrough/".
37 expectedPartsAfterSplit = 2
38 )
39
40 // DefaultConfig creates a new Config with default values and its associated PropKeyResolver.
41 func DefaultConfig() (*Config, format.PropKeyResolver) {
42 config := &Config{}
43 pkr := format.NewPropKeyResolver(config)
44 // Panic on error since failure indicates a programming error in struct tags
45 // that would be caught during development
46 if err := pkr.SetDefaultProps(config); err != nil {
47 panic(fmt.Sprintf("setting default properties: %v", err))
48 }
49
50 return config, pkr
51 }
52
53 // ConfigFromWebhookURL constructs a Config from a parsed webhook URL.
54 func ConfigFromWebhookURL(webhookURL *url.URL) (*Config, format.PropKeyResolver, error) {
55 config, pkr := DefaultConfig()
56
57 // Extract query parameters
58 webhookQuery := webhookURL.Query()
59
60 // Set config properties from query params
61 customQuery, err := format.SetConfigPropsFromQuery(&pkr, webhookQuery)
62 if err != nil {
63 return nil, pkr, fmt.Errorf("setting config properties from query: %w", err)
64 }
65
66 // Store custom query parameters
67 config.webhookQuery = customQuery
68
69 // Handle service URLs (scheme = notifiarr)
70 if webhookURL.Scheme == Scheme {
71 config.APIKey = webhookURL.Host
72 } else if strings.Contains(webhookURL.Path, "/passthrough/") {
73 // The webhook URL path should contain the API key after "/passthrough/"
74 parts := strings.Split(webhookURL.Path, "/passthrough/")
75 if len(parts) == expectedPartsAfterSplit {
76 config.APIKey = strings.Split(parts[1], "/")[0]
77 }
78 }
79
80 return config, pkr, nil
81 }
82
83 // GetURL generates a URL from the current configuration values.
84 func (c *Config) GetURL() *url.URL {
85 resolver := format.NewPropKeyResolver(c)
86
87 return c.getURL(&resolver)
88 }
89
90 // SetURL updates the configuration from a service URL.
91 func (c *Config) SetURL(serviceURL *url.URL) error {
92 resolver := format.NewPropKeyResolver(c)
93
94 return c.setURL(&resolver, serviceURL)
95 }
96
97 // getURL generates a service URL from the configuration using the provided resolver.
98 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
99 var query url.Values
100 if c.webhookQuery == nil {
101 query = format.BuildQueryWithCustomFields(resolver, url.Values{})
102 } else {
103 query = format.BuildQueryWithCustomFields(resolver, c.webhookQuery)
104 }
105
106 query.Del("apikey")
107 serviceURL := &url.URL{
108 Scheme: Scheme,
109 Host: c.APIKey,
110 RawQuery: query.Encode(),
111 }
112
113 return serviceURL
114 }
115
116 // setURL updates the configuration from a service URL using the provided resolver.
117 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
118 c.APIKey = serviceURL.Host
119
120 // Set config properties from query
121 serviceQuery := serviceURL.Query()
122
123 _, err := format.SetConfigPropsFromQuery(resolver, serviceQuery)
124 if err != nil {
125 return fmt.Errorf("setting config properties from service URL query: %w", err)
126 }
127
128 return nil
129 }