Debian Codegraph @

pkg/services/chat/signal/signal.go

raw
1 package signal
2
3 import (
4 "errors"
5 "fmt"
6 "net/url"
7 "time"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/format"
10 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
11 "github.com/nicholas-fedor/shoutrrr/pkg/types"
12 )
13
14 // Service sends notifications to Signal recipients via signal-cli-rest-api.
15 type Service struct {
16 standard.Standard
17
18 Config *Config
19 pkr format.PropKeyResolver
20 httpClient types.HTTPClient
21 // injectedHTTPClient is true when SetHTTPClient supplied the client.
22 injectedHTTPClient bool
23 }
24
25 var (
26 _ types.Service = (*Service)(nil)
27 _ types.HTTPClientSetter = (*Service)(nil)
28 )
29
30 // GetID returns the identifier for this service.
31 //
32 // Returns:
33 // - string: the service identifier.
34 func (s *Service) GetID() string {
35 return Scheme
36 }
37
38 // Initialize configures the service with a URL and logger.
39 //
40 // Parameters:
41 // - serviceURL: the configuration URL for the Signal service
42 // - logger: the logger to use for logging
43 //
44 // Returns:
45 // - error: if configuration fails, nil otherwise
46 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
47 s.SetLogger(logger)
48 s.Config = &Config{}
49 s.pkr = format.NewPropKeyResolver(s.Config)
50
51 if err := s.pkr.SetDefaultProps(s.Config); err != nil {
52 return fmt.Errorf("setting default props: %w", err)
53 }
54
55 if err := s.Config.setURL(&s.pkr, serviceURL); err != nil {
56 return err
57 }
58
59 if s.httpClient == nil {
60 s.httpClient = s.newHTTPClient(s.Config.SkipTLSVerify)
61 }
62
63 return nil
64 }
65
66 // Send delivers a notification message to Signal recipients.
67 //
68 // Parameters:
69 // - message: the text message to send
70 // - params: optional configuration overrides including title and attachments
71 //
72 // Returns:
73 // - error: if the send operation fails, nil otherwise
74 func (s *Service) Send(message string, params *types.Params) error {
75 config := *s.Config
76 if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil {
77 return fmt.Errorf("updating config from params: %w", err)
78 }
79
80 return s.sendMessage(message, &config)
81 }
82
83 // ServiceTimeout returns the HTTP timeout used for a Signal send.
84 //
85 // Parameters:
86 // - params: Unused. The budget does not depend on send parameters.
87 //
88 // Returns:
89 // - [defaultHTTPTimeout].
90 func (*Service) ServiceTimeout(*types.Params) time.Duration {
91 return defaultHTTPTimeout
92 }
93
94 // SetHTTPClient sets a custom HTTP client for the service.
95 //
96 // Parameters:
97 // - client: the HTTP client to use for API requests
98 func (s *Service) SetHTTPClient(client types.HTTPClient) {
99 s.httpClient = client
100 s.injectedHTTPClient = client != nil
101 }
102
103 // sendMessage sends a message to all configured recipients.
104 // Mixed recipient types are sent as separate /v2/send calls because the REST API
105 // rejects phones, groups, and usernames in the same request.
106 //
107 // Parameters:
108 // - message: the message text to send
109 // - config: the service configuration
110 //
111 // Returns:
112 // - error: if sending fails, nil otherwise
113 func (s *Service) sendMessage(message string, config *Config) error {
114 if len(config.Recipients) == 0 {
115 return ErrNoRecipients
116 }
117
118 var errs []error
119
120 for _, batch := range batchRecipients(config.Recipients) {
121 batchConfig := *config
122 batchConfig.Recipients = batch
123
124 payload := createPayload(message, &batchConfig)
125
126 req, cancel, err := s.createRequest(&batchConfig, &payload)
127 if err != nil {
128 if cancel != nil {
129 cancel()
130 }
131
132 errs = append(errs, err)
133
134 continue
135 }
136
137 err = s.sendRequest(req, &batchConfig)
138
139 cancel()
140
141 if err != nil {
142 errs = append(errs, err)
143 }
144 }
145
146 return errors.Join(errs...)
147 }