Debian Codegraph @

pkg/services/chat/mattermost/mattermost.go

raw symbols SCIP
1 package mattermostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src . bytes/"
5 "contextscip-go gomod github.com/golang/go/src . context/"
6 "crypto/tlsscip-go gomod github.com/golang/go/src . `crypto/tls`/"
7 "fmtscip-go gomod github.com/golang/go/src . fmt/"
8 "net/httpscip-go gomod github.com/golang/go/src . `net/http`/"
9 "net/urlscip-go gomod github.com/golang/go/src . `net/url`/"
10 "timescip-go gomod github.com/golang/go/src . time/"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/"
13 "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/"
14 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
15 )
16
17 // Service sends notifications to a pre-configured Mattermost channel or user.
18 type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service# struct {
19 standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.Standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Standard.
20
21 Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#
22 pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#pkr. formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#
23 httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#httpClient. typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#
24 }
25
26 // defaultHTTPTimeout is the default timeout for HTTP requests.
27 const defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/defaultHTTPTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second.
28
29 // GetID returns the service identifier.
30 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#GetID().() string {
31 return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Scheme.
32 }
33
34 // Initialize configures the service with a URL and logger.
35 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Initialize().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) error {
36 s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger)
37 s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#{}
38 s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#pkr. = formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config.)
39
40 err := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config..setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#setURL().(&s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#pkr., serviceURL)
41 if err != nil {
42 return err
43 }
44
45 var transport *httpscip-go gomod github.com/golang/go/src . `net/http`/.Transportscip-go gomod github.com/golang/go/src . `net/http`/Transport#
46 if s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config..DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#DisableTLS. {
47 transport = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Transportscip-go gomod github.com/golang/go/src . `net/http`/Transport#{} // Plain HTTP
48 } else {
49 transport = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Transportscip-go gomod github.com/golang/go/src . `net/http`/Transport#{
50 TLSClientConfigscip-go gomod github.com/golang/go/src . `net/http`/Transport#TLSClientConfig.: &tlsscip-go gomod github.com/golang/go/src . `crypto/tls`/.Configscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#{
51 InsecureSkipVerifyscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#InsecureSkipVerify.: false, // Explicitly safe when TLS is enabled
52 MinVersionscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#MinVersion.: tlsscip-go gomod github.com/golang/go/src . `crypto/tls`/.VersionTLS12scip-go gomod github.com/golang/go/src . `crypto/tls`/VersionTLS12., // Enforce TLS 1.2 or higher
53 },
54 }
55 }
56
57 s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#httpClient. = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{Transportscip-go gomod github.com/golang/go/src . `net/http`/Client#Transport.: transport}
58
59 return nil
60 }
61
62 // Send delivers a notification message to Mattermost.
63 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Send().(message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) error {
64 config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#Config.
65 serviceURL := buildURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/buildURL().(config)
66
67 if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(config, params); err != nil {
68 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("updating config from params: %w", err)
69 }
70
71 json, err := CreateJSONPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/CreateJSONPayload().(config, message, params)
72 if err != nil {
73 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating JSON payload: %w", err)
74 }
75
76 ctx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().(
77 contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(),
78 defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/defaultHTTPTimeout.,
79 )
80 defer cancel()
81
82 req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(
83 ctx,
84 httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost.,
85 serviceURL.Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().(),
86 bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(json),
87 )
88 if err != nil {
89 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating POST request: %w", err)
90 }
91
92 req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Content-Type", "application/json")
93
94 client := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#httpClient.
95 if client == nil {
96 transport := &httpscip-go gomod github.com/golang/go/src . `net/http`/.Transportscip-go gomod github.com/golang/go/src . `net/http`/Transport#{
97 TLSClientConfigscip-go gomod github.com/golang/go/src . `net/http`/Transport#TLSClientConfig.: &tlsscip-go gomod github.com/golang/go/src . `crypto/tls`/.Configscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#{
98 InsecureSkipVerifyscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#InsecureSkipVerify.: false,
99 MinVersionscip-go gomod github.com/golang/go/src . `crypto/tls`/Config#MinVersion.: tlsscip-go gomod github.com/golang/go/src . `crypto/tls`/.VersionTLS12scip-go gomod github.com/golang/go/src . `crypto/tls`/VersionTLS12.,
100 },
101 }
102 client = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{Transportscip-go gomod github.com/golang/go/src . `net/http`/Client#Transport.: transport}
103 }
104
105 res, err := client.Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req)
106 if err != nil {
107 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("executing POST request to Mattermost API: %w", err)
108 }
109
110 defer func() { _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }()
111
112 if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusOKscip-go gomod github.com/golang/go/src . `net/http`/StatusOK. {
113 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %s", ErrSendFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/ErrSendFailed., res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.)
114 }
115
116 return nil
117 }
118
119 // SetHTTPClient sets a custom HTTP client for the service.
120 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#SetHTTPClient().(client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#) {
121 s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Service#httpClient. = client
122 }
123
124 // buildURL constructs the API URL for Mattermost based on the Config.
125 // Returns a *url.URL that is valid by construction.
126 func buildURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/buildURL().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#) *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL# {
127 scheme := "https"
128 if config.DisableTLSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#DisableTLS. {
129 scheme = "http"
130 }
131
132 return &urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#{
133 Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.: scheme,
134 Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.: config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#Host.,
135 Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path.: "/hooks/" + config.Tokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#Token.,
136 }
137 }