Debian Codegraph @

pkg/services/chat/mattermost/mattermost.go

raw
1 package mattermost
2
3 import (
4 "bytes"
5 "context"
6 "crypto/tls"
7 "fmt"
8 "net/http"
9 "net/url"
10 "time"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/format"
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 Mattermost channel or user.
18 type Service struct {
19 standard.Standard
20
21 Config *Config
22 pkr format.PropKeyResolver
23 httpClient *http.Client
24 }
25
26 // defaultHTTPTimeout is the default timeout for HTTP requests.
27 const defaultHTTPTimeout = 10 * time.Second
28
29 // GetHTTPClient returns the service's HTTP client for testing purposes.
30 func (s *Service) GetHTTPClient() *http.Client {
31 return s.httpClient
32 }
33
34 // GetID returns the service identifier.
35 func (s *Service) GetID() string {
36 return Scheme
37 }
38
39 // Initialize configures the service with a URL and logger.
40 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
41 s.SetLogger(logger)
42 s.Config = &Config{}
43 s.pkr = format.NewPropKeyResolver(s.Config)
44
45 err := s.Config.setURL(&s.pkr, serviceURL)
46 if err != nil {
47 return err
48 }
49
50 var transport *http.Transport
51 if s.Config.DisableTLS {
52 transport = &http.Transport{} // Plain HTTP
53 } else {
54 transport = &http.Transport{
55 TLSClientConfig: &tls.Config{
56 InsecureSkipVerify: false, // Explicitly safe when TLS is enabled
57 MinVersion: tls.VersionTLS12, // Enforce TLS 1.2 or higher
58 },
59 }
60 }
61
62 s.httpClient = &http.Client{Transport: transport}
63
64 return nil
65 }
66
67 // Send delivers a notification message to Mattermost.
68 func (s *Service) Send(message string, params *types.Params) error {
69 config := s.Config
70 serviceURL := buildURL(config)
71
72 if err := s.pkr.UpdateConfigFromParams(config, params); err != nil {
73 return fmt.Errorf("updating config from params: %w", err)
74 }
75
76 json, err := CreateJSONPayload(config, message, params)
77 if err != nil {
78 return fmt.Errorf("creating JSON payload: %w", err)
79 }
80
81 ctx, cancel := context.WithTimeout(
82 context.Background(),
83 defaultHTTPTimeout,
84 )
85 defer cancel()
86
87 req, err := http.NewRequestWithContext(
88 ctx,
89 http.MethodPost,
90 serviceURL.String(),
91 bytes.NewReader(json),
92 )
93 if err != nil {
94 return fmt.Errorf("creating POST request: %w", err)
95 }
96
97 req.Header.Set("Content-Type", "application/json")
98
99 res, err := s.httpClient.Do(req)
100 if err != nil {
101 return fmt.Errorf("executing POST request to Mattermost API: %w", err)
102 }
103
104 defer func() { _ = res.Body.Close() }()
105
106 if res.StatusCode != http.StatusOK {
107 return fmt.Errorf("%w: %s", ErrSendFailed, res.Status)
108 }
109
110 return nil
111 }
112
113 // buildURL constructs the API URL for Mattermost based on the Config.
114 // Returns a *url.URL that is valid by construction.
115 func buildURL(config *Config) *url.URL {
116 scheme := "https"
117 if config.DisableTLS {
118 scheme = "http"
119 }
120
121 return &url.URL{
122 Scheme: scheme,
123 Host: config.Host,
124 Path: "/hooks/" + config.Token,
125 }
126 }