Debian Codegraph @

pkg/services/chat/signal/signal.go

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