| 1 | package rocketchat |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 | ) |
| 16 | |
| 17 | // Service sends notifications to a pre-configured Rocket.Chat channel or user. |
| 18 | // It implements the types.Service interface for Rocket.Chat integration. |
| 19 | type Service struct { |
| 20 | standard.Standard |
| 21 | |
| 22 | // Config holds the Rocket.Chat service configuration. |
| 23 | Config *Config |
| 24 | Client *http.Client |
| 25 | httpClient types.HTTPClient |
| 26 | } |
| 27 | |
| 28 | // defaultHTTPTimeout is the default timeout for HTTP requests. |
| 29 | const defaultHTTPTimeout = 10 * time.Second |
| 30 | |
| 31 | // GetID returns the service identifier. |
| 32 | func (s *Service) GetID() string { |
| 33 | return Scheme |
| 34 | } |
| 35 | |
| 36 | // Initialize configures the service with a URL and logger. |
| 37 | // |
| 38 | // Params: |
| 39 | // - serviceURL: The configuration URL containing Rocket.Chat connection details |
| 40 | // - logger: The logger to use for this service |
| 41 | // |
| 42 | // Returns: |
| 43 | // - error: An error if configuration fails, nil otherwise |
| 44 | func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 45 | s.SetLogger(logger) |
| 46 | |
| 47 | s.Config = &Config{} |
| 48 | if s.Client == nil { |
| 49 | s.Client = &http.Client{ |
| 50 | Timeout: defaultHTTPTimeout, // Set a default timeout |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if err := s.Config.SetURL(serviceURL); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | // Send delivers a notification message to Rocket.Chat. |
| 62 | // |
| 63 | // Params: |
| 64 | // - message: The message text to send |
| 65 | // - params: Optional parameters that can override configuration (username, channel) |
| 66 | // |
| 67 | // Returns: |
| 68 | // - error: An error if sending fails, nil on success |
| 69 | func (s *Service) Send(message string, params *types.Params) error { |
| 70 | var res *http.Response |
| 71 | |
| 72 | var err error |
| 73 | |
| 74 | config := s.Config |
| 75 | serviceURL := buildURL(config) |
| 76 | |
| 77 | json, err := CreateJSONPayload(config, message, params) |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("creating JSON payload: %w", err) |
| 80 | } |
| 81 | |
| 82 | ctx, cancel := context.WithTimeout( |
| 83 | context.Background(), |
| 84 | defaultHTTPTimeout, |
| 85 | ) |
| 86 | defer cancel() |
| 87 | |
| 88 | req, err := http.NewRequestWithContext( |
| 89 | ctx, |
| 90 | http.MethodPost, |
| 91 | serviceURL, |
| 92 | bytes.NewReader(json), |
| 93 | ) |
| 94 | if err != nil { |
| 95 | return fmt.Errorf("creating request: %w", err) |
| 96 | } |
| 97 | |
| 98 | req.Header.Set("Content-Type", "application/json") |
| 99 | |
| 100 | res, err = s.httpClientOrDefault().Do(req) |
| 101 | if err != nil { |
| 102 | return fmt.Errorf( |
| 103 | "posting to URL: %w\nHOST: %s\nPORT: %s", |
| 104 | err, |
| 105 | config.Host, |
| 106 | config.Port, |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | defer func() { _ = res.Body.Close() }() |
| 111 | |
| 112 | if res.StatusCode != http.StatusOK { |
| 113 | resBody, err := io.ReadAll(res.Body) |
| 114 | if err != nil { |
| 115 | return fmt.Errorf( |
| 116 | "%w: %d", |
| 117 | ErrNotificationFailed, |
| 118 | res.StatusCode, |
| 119 | ) |
| 120 | } |
| 121 | |
| 122 | return fmt.Errorf("%w: %d %s", |
| 123 | ErrNotificationFailed, |
| 124 | res.StatusCode, |
| 125 | resBody, |
| 126 | ) |
| 127 | } |
| 128 | |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // SetHTTPClient sets a custom HTTP client for the service. |
| 133 | func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 134 | s.httpClient = client |
| 135 | if s.Client != nil { |
| 136 | if c, ok := client.(*http.Client); ok { |
| 137 | s.Client = c |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // httpClientOrDefault returns the custom client or the internal Client. |
| 143 | func (s *Service) httpClientOrDefault() types.HTTPClient { |
| 144 | if s.httpClient != nil { |
| 145 | return s.httpClient |
| 146 | } |
| 147 | |
| 148 | return s.Client |
| 149 | } |
| 150 | |
| 151 | // buildURL constructs the API URL for Rocket.Chat based on the Config. |
| 152 | // |
| 153 | // Params: |
| 154 | // - config: The configuration containing host, port, and token information |
| 155 | // |
| 156 | // Returns: |
| 157 | // - The complete Rocket.Chat webhook URL as a string |
| 158 | func buildURL(config *Config) string { |
| 159 | base := config.Host |
| 160 | if config.Port != "" { |
| 161 | base = net.JoinHostPort(config.Host, config.Port) |
| 162 | } |
| 163 | |
| 164 | return fmt.Sprintf( |
| 165 | "https://%s/hooks/%s/%s", |
| 166 | base, |
| 167 | config.TokenA, |
| 168 | config.TokenB, |
| 169 | ) |
| 170 | } |