Debian Codegraph @

pkg/services/chat/zulip/zulip_config.go

raw
1 package zulip
2
3 import (
4 "net/url"
5
6 "github.com/nicholas-fedor/shoutrrr/pkg/format"
7 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
8 "github.com/nicholas-fedor/shoutrrr/pkg/types"
9 )
10
11 // MessageType indicates the type of Zulip message to send.
12 type MessageType string
13
14 // Config for the zulip service.
15 type Config struct {
16 standard.EnumlessConfig
17
18 BotMail string `desc:"Bot e-mail address" url:"user"`
19 BotKey string `desc:"API key" url:"pass"`
20 Host string `desc:"API server hostname (with optional port)" url:"host"`
21 Type MessageType `desc:"Message type (channel or direct)" key:"type" optional:""`
22 Stream string `desc:"Target stream name" key:"stream" optional:""`
23 Topic string `desc:"Stream topic" key:"topic" optional:""`
24 Title string `desc:"Notification title prepended to message" key:"title" optional:""`
25 To string `desc:"Comma-separated user IDs or emails for DMs" key:"to" optional:""`
26 ReadBySender bool `desc:"Mark the message read by its sender" key:"read_by_sender" optional:"" default:"No"`
27 }
28
29 const (
30 // MessageTypeChannel sends a message to a channel (stream).
31 MessageTypeChannel MessageType = "channel"
32 // MessageTypeDirect sends a direct message to users.
33 MessageTypeDirect MessageType = "direct"
34 )
35
36 // Scheme is the identifying part of this service's configuration URL.
37 const Scheme = "zulip"
38
39 // Clone creates a copy of the Config.
40 func (c *Config) Clone() *Config {
41 return &Config{
42 BotMail: c.BotMail,
43 BotKey: c.BotKey,
44 Host: c.Host,
45 Type: c.Type,
46 Stream: c.Stream,
47 Topic: c.Topic,
48 Title: c.Title,
49 To: c.To,
50 ReadBySender: c.ReadBySender,
51 }
52 }
53
54 // GetURL returns a URL representation of its current field values.
55 func (c *Config) GetURL() *url.URL {
56 resolver := format.NewPropKeyResolver(c)
57
58 return c.getURL(&resolver)
59 }
60
61 // SetURL updates a ServiceConfig from a URL representation of its field values.
62 func (c *Config) SetURL(serviceURL *url.URL) error {
63 resolver := format.NewPropKeyResolver(c)
64
65 return c.setURL(&resolver, serviceURL)
66 }
67
68 // getURL constructs a URL from the Config's fields using the provided resolver.
69 func (c *Config) getURL(_ types.ConfigQueryResolver) *url.URL {
70 query := &url.Values{}
71 if c.Stream != "" {
72 query.Set("stream", c.Stream)
73 }
74
75 if c.Topic != "" {
76 query.Set("topic", c.Topic)
77 }
78
79 if c.Title != "" {
80 query.Set("title", c.Title)
81 }
82
83 if c.Type != "" {
84 query.Set("type", string(c.Type))
85 }
86
87 if c.To != "" {
88 query.Set("to", c.To)
89 }
90
91 if c.ReadBySender {
92 query.Set("read_by_sender", "true")
93 }
94
95 return &url.URL{
96 User: url.UserPassword(c.BotMail, c.BotKey),
97 Host: c.Host,
98 RawQuery: query.Encode(),
99 Scheme: Scheme,
100 }
101 }
102
103 // setURL updates the Config from a URL using the provided resolver.
104 func (c *Config) setURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error {
105 var isSet bool
106
107 c.BotMail = serviceURL.User.Username()
108 c.BotKey, isSet = serviceURL.User.Password()
109 c.Host = serviceURL.Host
110
111 // Allow dummy URL during documentation generation
112 if !isDummyURL(serviceURL) {
113 if c.BotMail == "" {
114 return ErrMissingBotMail
115 }
116
117 if !isSet {
118 return ErrMissingAPIKey
119 }
120
121 if c.Host == "" {
122 return ErrMissingHost
123 }
124 }
125
126 c.Stream = serviceURL.Query().Get("stream")
127 c.Topic = serviceURL.Query().Get("topic")
128 c.Title = serviceURL.Query().Get("title")
129 c.Type = MessageType(serviceURL.Query().Get("type"))
130 c.To = serviceURL.Query().Get("to")
131 c.ReadBySender = serviceURL.Query().Get("read_by_sender") == "true"
132
133 return nil
134 }
135
136 // CreateConfigFromURL creates a new Config from a URL for use within the zulip service.
137 func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) {
138 config := Config{}
139 err := config.setURL(nil, serviceURL)
140
141 return &config, err
142 }
143
144 // isDummyURL checks if the given URL is the dummy URL used for documentation generation.
145 // It compares URL components instead of string comparison to avoid issues with
146 // URL formatting differences.
147 func isDummyURL(u *url.URL) bool {
148 return u.Host == "dummy.com" && u.User.Username() == "dummy"
149 }