Debian Codegraph @

pkg/services/push/ntfy/ntfy_config.go

raw
1 package ntfy
2
3 import (
4 "errors"
5 "fmt"
6 "net/url"
7 "strings"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/format"
10 "github.com/nicholas-fedor/shoutrrr/pkg/types"
11 )
12
13 // Config holds the configuration for the Ntfy service.
14 type Config struct {
15 Title string `default:"" desc:"Message title" key:"title"`
16 Host string `default:"ntfy.sh" desc:"Server hostname and port" url:"host"`
17 Topic string ` desc:"Target topic name" url:"path" required:""`
18 Password string ` desc:"Auth password" url:"password" optional:""`
19 Username string ` desc:"Auth username" url:"user" optional:""`
20 Scheme string `default:"https" desc:"Server protocol, http or https" key:"scheme"`
21 Tags []string ` desc:"List of tags that may or not map to emojis" key:"tags" optional:""`
22 Priority priority `default:"default" desc:"Message priority with 1=min, 3=default and 5=max" key:"priority"`
23 Actions []string ` desc:"Custom user action buttons for notifications, see https://docs.ntfy.sh/publish/#action-buttons" key:"actions" optional:"" sep:";"`
24 Click string ` desc:"Website opened when notification is clicked" key:"click" optional:""`
25 Attach string ` desc:"URL of an attachment, see attach via URL" key:"attach" optional:""`
26 Filename string ` desc:"File name of the attachment" key:"filename" optional:""`
27 Delay string ` desc:"Timestamp or duration for delayed delivery, see https://docs.ntfy.sh/publish/#scheduled-delivery" key:"delay,at,in" optional:""`
28 Email string ` desc:"E-mail address for e-mail notifications" key:"email" optional:""`
29 Icon string ` desc:"URL to use as notification icon" key:"icon" optional:""`
30 Cache bool `default:"yes" desc:"Cache messages" key:"cache"`
31 Firebase bool `default:"yes" desc:"Send to firebase" key:"firebase"`
32 DisableTLSVerification bool `default:"no" desc:"Disable TLS certificate verification" key:"disabletlsverification"`
33 DisableTLS bool `default:"no" desc:"Disable TLS entirely (force HTTP scheme)" key:"disabletls"`
34 }
35
36 // Scheme is the identifying part of this service's configuration URL.
37 const (
38 Scheme = "ntfy"
39 )
40
41 // ErrTopicRequired indicates that the topic is missing from the config URL.
42 var ErrTopicRequired = errors.New("topic is required")
43
44 // Enums returns the fields that use an EnumFormatter for their values.
45 func (*Config) Enums() map[string]types.EnumFormatter {
46 return map[string]types.EnumFormatter{
47 "Priority": Priority.Enum,
48 }
49 }
50
51 // GetAPIURL constructs the API URL for the Ntfy service based on the configuration.
52 func (c *Config) GetAPIURL() string {
53 path := c.Topic
54 if !strings.HasPrefix(c.Topic, "/") {
55 path = "/" + path
56 }
57
58 apiURL := url.URL{
59 Scheme: c.Scheme,
60 Host: c.Host,
61 Path: path,
62 }
63
64 return apiURL.String()
65 }
66
67 // GetURL returns a URL representation of the Config's current field values.
68 func (c *Config) GetURL() *url.URL {
69 resolver := format.NewPropKeyResolver(c)
70
71 return c.getURL(&resolver)
72 }
73
74 // QueryFields returns the list of query parameter names for the Config struct.
75 func (c *Config) QueryFields() []string {
76 return format.GetConfigQueryResolver(c).QueryFields()
77 }
78
79 // SetURL updates the Config from a URL representation of its field values.
80 func (c *Config) SetURL(serviceURL *url.URL) error {
81 resolver := format.NewPropKeyResolver(c)
82
83 return c.setURL(&resolver, serviceURL)
84 }
85
86 // getURL constructs a URL from the Config's fields using the provided resolver.
87 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
88 path := c.Topic
89 if !strings.HasPrefix(c.Topic, "/") {
90 path = "/" + path
91 }
92
93 result := &url.URL{
94 Host: c.Host,
95 Scheme: Scheme,
96 ForceQuery: true,
97 Path: path,
98 RawQuery: format.BuildQuery(resolver),
99 }
100 if c.Username != "" {
101 if c.Password != "" {
102 result.User = url.UserPassword(c.Username, c.Password)
103 } else {
104 result.User = url.User(c.Username)
105 }
106 }
107
108 return result
109 }
110
111 // setURL updates the Config from a URL using the provided resolver.
112 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
113 if serviceURL.User != nil {
114 password, _ := serviceURL.User.Password()
115 c.Password = password
116 c.Username = serviceURL.User.Username()
117 } else {
118 c.Password = ""
119 c.Username = ""
120 }
121
122 c.Host = serviceURL.Host
123 c.Topic = strings.TrimPrefix(serviceURL.Path, "/")
124
125 serviceURL.RawQuery = strings.ReplaceAll(serviceURL.RawQuery, ";", "%3b")
126 for key, vals := range serviceURL.Query() {
127 if err := resolver.Set(key, vals[0]); err != nil {
128 return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
129 }
130 }
131
132 if serviceURL.String() != "ntfy://[email protected]" {
133 if c.Topic == "" {
134 return ErrTopicRequired
135 }
136 }
137
138 return nil
139 }