Debian Codegraph @

pkg/services/chat/matrix/matrix_client.go

raw symbols SCIP
1 package matrixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/
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/randscip-go gomod github.com/golang/go/src . `crypto/rand`/"
7 "crypto/sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/"
8 "encoding/hexscip-go gomod github.com/golang/go/src . `encoding/hex`/"
9 "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/"
10 "errorsscip-go gomod github.com/golang/go/src . errors/"
11 "fmtscip-go gomod github.com/golang/go/src . fmt/"
12 "ioscip-go gomod github.com/golang/go/src . io/"
13 "netscip-go gomod github.com/golang/go/src . net/"
14 "net/httpscip-go gomod github.com/golang/go/src . `net/http`/"
15 "net/urlscip-go gomod github.com/golang/go/src . `net/url`/"
16 "stringsscip-go gomod github.com/golang/go/src . strings/"
17 "timescip-go gomod github.com/golang/go/src . time/"
18
19 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
20 "github.com/nicholas-fedor/shoutrrr/pkg/utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/"
21 )
22
23 // HTTPClient defines the interface for HTTP operations.
24 type HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/HTTPClient# interface {
25 Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/HTTPClient#Do.(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error)
26 }
27
28 // client manages interactions with the Matrix API.
29 type clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client# struct {
30 apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL. urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#
31 accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken. string
32 loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#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#
33 httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#httpClient. HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/HTTPClient#
34 }
35
36 // DefaultHTTPClient is the default implementation using http.DefaultClient with timeout.
37 type DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient# struct {
38 clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#client. *httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#
39 }
40
41 const (
42 // schemeHTTPS is the URL scheme for HTTPS.
43 schemeHTTPSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/schemeHTTPS. = "https"
44
45 // schemeHTTP is the URL scheme for HTTP.
46 schemeHTTPscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/schemeHTTP. = "http"
47
48 // tokenHintLength is the length of the token hint shown in logs.
49 tokenHintLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/tokenHintLength. = 3
50
51 // minSliceLength is the minimum length for slice operations.
52 minSliceLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/minSliceLength. = 1
53
54 // httpClientErrorStatus is the HTTP status code threshold for client errors.
55 httpClientErrorStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/httpClientErrorStatus. = 400
56
57 // defaultHTTPTimeout is the timeout for HTTP requests.
58 defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/defaultHTTPTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second.
59
60 // transactionIDRandLen is the length of random bytes in transaction ID.
61 transactionIDRandLenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/transactionIDRandLen. = 8
62
63 // byteMask is the mask for extracting a single byte from an integer.
64 byteMaskscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/byteMask. = 0xFF
65
66 // bitsPerByte is the number of bits in a byte.
67 bitsPerBytescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/bitsPerByte. = 8
68
69 // authorizationHeader is the HTTP header name for Bearer token authentication.
70 authorizationHeaderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/authorizationHeader. = "Authorization"
71
72 // bearerPrefix is the prefix for Bearer token authentication.
73 bearerPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/bearerPrefix. = "Bearer "
74
75 // deviceIDLength is the length of the generated device ID.
76 // The Matrix spec recommends 10 characters for sufficient uniqueness.
77 deviceIDLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDLength. = 10
78
79 // deviceIDPrefix is the prefix for generated device IDs.
80 // This aids identification in Matrix admin interfaces.
81 deviceIDPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDPrefix. = "SH"
82
83 // deviceIDHashBytes is the number of hash bytes used for device ID generation.
84 // 8 bytes from SHA-256 provides enough characters for the suffix.
85 deviceIDHashBytesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDHashBytes. = 8
86
87 // opaqueIDCharset is the character set allowed by the Matrix Opaque Identifier Grammar.
88 // Reference: https://spec.matrix.org/v1.18/appendices/#opaque-identifiers
89 opaqueIDCharsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/opaqueIDCharset. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
90 opaqueIDCharsetLenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/opaqueIDCharsetLen. = byte(len(opaqueIDCharsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/opaqueIDCharset.))
91 )
92
93 // Do performs the HTTP request.
94 func (c *DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#) Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#Do().(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error) {
95 resp, err := c.clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#client..Doscip-go gomod github.com/golang/go/src . `net/http`/Client#Do().(req)
96 if err != nil {
97 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("performing HTTP request: %w", err)
98 }
99
100 return resp, nil
101 }
102
103 // apiDo performs an HTTP request to the Matrix API.
104 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) apiDoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiDo().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, method, path string, request, response any) error {
105 err := c.doSingleRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#doSingleRequest().(ctx, method, path, request, response)
106 if err == nil {
107 return nil
108 }
109
110 // If it's a M_LIMIT_EXCEEDED rate limited error, propagate it immediately.
111 // This allows the caller to handle rate limiting as appropriate
112 if isRateLimitedErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/isRateLimitedError().(err) {
113 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Rate limited, returning error to caller: %v\n", err)
114
115 return err
116 }
117
118 // For non-retryable errors, return immediately
119 return err
120 }
121
122 // apiGet performs a GET request to the Matrix API with the provided context.
123 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) apiGetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiGet().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, path string, response any) error {
124 reqCtx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().(
125 ctx,
126 defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/defaultHTTPTimeout.,
127 )
128 defer cancel()
129
130 req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(
131 reqCtx,
132 httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodGetscip-go gomod github.com/golang/go/src . `net/http`/MethodGet.,
133 c.buildURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#buildURL().(path),
134 httpscip-go gomod github.com/golang/go/src . `net/http`/.NoBodyscip-go gomod github.com/golang/go/src . `net/http`/NoBody.,
135 )
136 if err != nil {
137 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating GET request: %w", err)
138 }
139
140 c.setAuthorizationHeaderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#setAuthorizationHeader().(req)
141
142 res, err := c.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#httpClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/HTTPClient#Do.(req)
143 if err != nil {
144 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("executing GET request: %w", err)
145 }
146
147 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().() }()
148
149 body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.)
150 if err != nil {
151 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("reading GET response body: %w", err)
152 }
153
154 if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpClientErrorStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/httpClientErrorStatus. {
155 resError := &apiResErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResError#{}
156 if err = jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, resError); err == nil {
157 return resError
158 }
159
160 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().(
161 "%w: %v (unmarshal error: %w)",
162 ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/ErrUnexpectedStatus.,
163 res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status., err,
164 )
165 }
166
167 if err = jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, response); err != nil {
168 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("unmarshaling GET response: %w", err)
169 }
170
171 return nil
172 }
173
174 // apiPost performs a POST request to the Matrix API with the provided context.
175 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) apiPostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPost().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, path string, request, response any) error {
176 return c.apiDoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiDo().(ctx, httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., path, request, response)
177 }
178
179 // apiPut performs a PUT request to the Matrix API with the provided context.
180 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) apiPutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPut().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, path string, request, response any) error {
181 return c.apiDoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiDo().(ctx, httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPutscip-go gomod github.com/golang/go/src . `net/http`/MethodPut., path, request, response)
182 }
183
184 // buildURL returns a copy of the base API URL with the specified path set.
185 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) buildURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#buildURL().(path string) string {
186 u := c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL.
187 u.Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path. = path
188
189 return u.Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().()
190 }
191
192 // doSingleRequest performs a single HTTP request without retry logic.
193 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) doSingleRequestscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#doSingleRequest().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, method, path string, request, response any) error {
194 body, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(request)
195 if err != nil {
196 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling %s request: %w", method, err)
197 }
198
199 reqCtx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().(ctx, defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/defaultHTTPTimeout.)
200 defer cancel()
201
202 req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(
203 reqCtx,
204 method,
205 c.buildURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#buildURL().(path),
206 bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(body),
207 )
208 if err != nil {
209 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating %s request: %w", method, err)
210 }
211
212 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", contentTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/contentType.)
213 c.setAuthorizationHeaderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#setAuthorizationHeader().(req)
214
215 res, err := c.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#httpClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/HTTPClient#Do.(req)
216 if err != nil {
217 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("executing %s request: %w", method, err)
218 }
219
220 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().() }()
221
222 body, err = ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.)
223 if err != nil {
224 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("reading %s response body: %w", method, err)
225 }
226
227 if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= httpClientErrorStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/httpClientErrorStatus. {
228 resError := &apiResErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResError#{}
229 if err = jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, resError); err == nil {
230 return resError
231 }
232
233 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().(
234 "%w: %v (unmarshal error: %w)",
235 ErrUnexpectedStatusscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/ErrUnexpectedStatus.,
236 res.Statusscip-go gomod github.com/golang/go/src . `net/http`/Response#Status.,
237 err,
238 )
239 }
240
241 if err = jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(body, response); err != nil {
242 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("unmarshaling %s response: %w", method, err)
243 }
244
245 return nil
246 }
247
248 // getJoinedRooms retrieves the list of rooms the client has joined.
249 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) getJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#getJoinedRooms().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#) ([]string, error) {
250 response := apiResJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResJoinedRooms#{}
251
252 err := c.apiGetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiGet().(ctx, apiJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiJoinedRooms., &response)
253 if err != nil {
254 return []string{}, err
255 }
256
257 return response.Roomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResJoinedRooms#Rooms., nil
258 }
259
260 // joinRoom joins a specified room and returns its ID.
261 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) joinRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#joinRoom().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, room string) (string, error) {
262 resRoom := apiResRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResRoom#{}
263
264 err := c.apiPostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPost().(
265 ctx,
266 fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().(apiRoomJoinscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiRoomJoin., room),
267 &struct{}{},
268 &resRoom,
269 )
270 if err != nil {
271 return "", err
272 }
273
274 return resRoom.RoomIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResRoom#RoomID., nil
275 }
276
277 // logf logs a formatted message using the client's logger.
278 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().(format string, v ...any) {
279 c.loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logger..Printfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#Printf.(format, v...)
280 }
281
282 // login authenticates the client using a username and password.
283 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) loginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#login().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, user, password string) error {
284 c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL..RawQueryscip-go gomod github.com/golang/go/src . `net/url`/URL#RawQuery. = ""
285
286 resLogin := apiResLoginFlowsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLoginFlows#{}
287 if err := c.apiGetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiGet().(ctx, apiLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiLogin., &resLogin); err != nil {
288 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to get login flows: %w", err)
289 }
290
291 flows := make([]string, 0, len(resLogin.Flowsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLoginFlows#Flows.))
292 for _, flow := range resLogin.Flowsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLoginFlows#Flows. {
293 flows = append(flows, string(flow.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flow#Type.))
294
295 // Prefer password login when a user is configured
296 if flow.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flow#Type. == flowLoginPasswordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flowLoginPassword. && user != "" {
297 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Using login flow '%v'", flow.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flow#Type.)
298
299 return c.loginPasswordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#loginPassword().(ctx, user, password)
300 }
301
302 // Only use token login when no user is configured
303 if flow.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flow#Type. == flowLoginTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flowLoginToken. && user == "" {
304 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Using login flow '%v'", flow.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flow#Type.)
305
306 return c.loginTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#loginToken().(ctx, password)
307 }
308 }
309
310 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().(
311 "%w: %v",
312 ErrUnsupportedLoginFlowsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/ErrUnsupportedLoginFlows.,
313 stringsscip-go gomod github.com/golang/go/src . strings/.Joinscip-go gomod github.com/golang/go/src . strings/Join().(flows, ", "),
314 )
315 }
316
317 // loginPassword performs a password-based login to the Matrix server.
318 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) loginPasswordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#loginPassword().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, user, password string) error {
319 response := apiResLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#{}
320
321 deviceID := generateDeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/generateDeviceID().(user, c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL..Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.)
322 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Using device ID: %v\n", deviceID)
323
324 //nolint:exhaustruct_v5 // Intentional zero-value initialization for apiReqLogin
325 err := c.apiPostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPost().(
326 ctx,
327 apiLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiLogin.,
328 apiReqLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#{
329 Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#Type.: flowLoginPasswordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flowLoginPassword.,
330 Passwordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#Password.: password,
331 Identifierscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#Identifier.: newUserIdentifierscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/newUserIdentifier().(user),
332 DeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#DeviceID.: deviceID,
333 },
334 &response,
335 )
336 if err != nil {
337 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to log in: %w", err)
338 }
339
340 c.accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken. = response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.
341
342 tokenHint := ""
343 if len(response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.) > tokenHintLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/tokenHintLength. {
344 tokenHint = response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.[:tokenHintLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/tokenHintLength.]
345 }
346
347 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("AccessToken: %v...\n", tokenHint)
348 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("HomeServer: %v\n", response.HomeServerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#HomeServer.)
349 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("User: %v\n", response.UserIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#UserID.)
350 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("DeviceID: %v\n", response.DeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#DeviceID.)
351
352 return nil
353 }
354
355 // loginToken performs a token-based login to the Matrix server.
356 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) loginTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#loginToken().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, token string) error {
357 response := apiResLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#{}
358
359 // For token-based login, use an empty user string in the device ID hash.
360 // This ensures all token-based logins to the same host reuse the same device.
361 deviceID := generateDeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/generateDeviceID().("", c.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL..Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.)
362 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Using device ID: %v\n", deviceID)
363
364 //nolint:exhaustruct_v5 // Intentional zero-value initialization for apiReqLogin
365 err := c.apiPostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPost().(
366 ctx,
367 apiLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiLogin.,
368 apiReqLoginscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#{
369 Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#Type.: flowLoginTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/flowLoginToken.,
370 Tokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#Token.: token,
371 DeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqLogin#DeviceID.: deviceID,
372 },
373 &response,
374 )
375 if err != nil {
376 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to log in with token: %w", err)
377 }
378
379 c.accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken. = response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.
380
381 tokenHint := ""
382 if len(response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.) > tokenHintLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/tokenHintLength. {
383 tokenHint = response.AccessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#AccessToken.[:tokenHintLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/tokenHintLength.]
384 }
385
386 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("AccessToken: %v...\n", tokenHint)
387 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("HomeServer: %v\n", response.HomeServerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#HomeServer.)
388 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("User: %v\n", response.UserIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#UserID.)
389 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("DeviceID: %v\n", response.DeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResLogin#DeviceID.)
390
391 return nil
392 }
393
394 // sendMessage sends a message to the specified rooms or all joined rooms if none are specified.
395 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) sendMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendMessage().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, message string, rooms []string) []error {
396 if len(rooms) >= minSliceLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/minSliceLength. {
397 return c.sendToExplicitRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendToExplicitRooms().(ctx, rooms, message)
398 }
399
400 return c.sendToJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendToJoinedRooms().(ctx, message)
401 }
402
403 // sendMessageToRoom sends a message to a specific room using PUT method with transaction ID.
404 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) sendMessageToRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendMessageToRoom().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, message, roomID string) error {
405 resEvent := apiResEventscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResEvent#{}
406 txnID := generateTransactionIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/generateTransactionID().()
407
408 path := fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().(apiSendMessagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiSendMessage., roomID, txnID)
409
410 request := apiReqSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqSend#{
411 MsgTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqSend#MsgType.: msgTypeTextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/msgTypeText.,
412 Bodyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiReqSend#Body.: message,
413 }
414
415 return c.apiPutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiPut().(ctx, path, request, &resEvent)
416 }
417
418 // sendToExplicitRooms sends a message to explicitly specified rooms and collects any errors.
419 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) sendToExplicitRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendToExplicitRooms().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, rooms []string, message string) []error {
420 var errs []error
421
422 for _, room := range rooms {
423 room = stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(room)
424 if room == "" {
425 errs = append(errs, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w", ErrEmptyRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/ErrEmptyRoom.))
426
427 continue
428 }
429
430 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Sending message to '%v'...\n", room)
431
432 if !stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(room, "!") {
433 roomID, err := c.joinRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#joinRoom().(ctx, room)
434 if err != nil {
435 errs = append(errs, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("error joining room %v: %w", room, err))
436
437 continue
438 }
439
440 if room != roomID {
441 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Resolved room alias '%v' to ID '%v'", room, roomID)
442 }
443
444 room = roomID
445 }
446
447 if err := c.sendMessageToRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendMessageToRoom().(ctx, message, room); err != nil {
448 errs = append(
449 errs,
450 fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send message to room '%v': %w", room, err),
451 )
452 }
453 }
454
455 return errs
456 }
457
458 // sendToJoinedRooms sends a message to all joined rooms and collects any errors.
459 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) sendToJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendToJoinedRooms().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, message string) []error {
460 var errs []error
461
462 joinedRooms, err := c.getJoinedRoomsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#getJoinedRooms().(ctx)
463 if err != nil {
464 return append(errs, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to get joined rooms: %w", err))
465 }
466
467 for _, roomID := range joinedRooms {
468 c.logfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logf().("Sending message to '%v'...\n", roomID)
469
470 if err := c.sendMessageToRoomscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#sendMessageToRoom().(ctx, message, roomID); err != nil {
471 errs = append(
472 errs,
473 fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send message to room '%v': %w", roomID, err),
474 )
475 }
476 }
477
478 return errs
479 }
480
481 // setAuthorizationHeader sets the Authorization header with Bearer token if access token is available.
482 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) setAuthorizationHeaderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#setAuthorizationHeader().(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) {
483 if c.accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken. != "" {
484 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().(authorizationHeaderscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/authorizationHeader., bearerPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/bearerPrefix.+c.accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken.)
485 }
486 }
487
488 // useToken sets the access token for the client.
489 func (c *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#) useTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#useToken().(token string) {
490 c.accessTokenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#accessToken. = token
491 }
492
493 // isRateLimitedError checks if the error is a rate limiting error.
494 func isRateLimitedErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/isRateLimitedError().(err error) bool {
495 if err == nil {
496 return false
497 }
498
499 var apiErr *apiResErrorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResError#
500 if errorsscip-go gomod github.com/golang/go/src . errors/.Asscip-go gomod github.com/golang/go/src . errors/As().(err, &apiErr) {
501 return apiErr.IsRateLimitedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/apiResError#IsRateLimited().()
502 }
503
504 return false
505 }
506
507 // generateTransactionID generates a unique transaction ID using a timestamp
508 // and random bytes to ensure uniqueness across requests.
509 func generateTransactionIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/generateTransactionID().() string {
510 now := timescip-go gomod github.com/golang/go/src . time/.Nowscip-go gomod github.com/golang/go/src . time/Now().().UnixNanoscip-go gomod github.com/golang/go/src . time/Time#UnixNano().()
511 randBytes := make([]byte, transactionIDRandLenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/transactionIDRandLen.)
512
513 if _, err := randscip-go gomod github.com/golang/go/src . `crypto/rand`/.Readscip-go gomod github.com/golang/go/src . `crypto/rand`/Read().(randBytes); err != nil {
514 // Fallback: use timestamp-based bytes if crypto/rand fails
515 for i := range randBytes {
516 randBytes[i] = byte((now >> (bitsPerBytescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/bitsPerByte. * i)) & byteMaskscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/byteMask.)
517 }
518 }
519
520 return fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().("%d-%s", now, hexscip-go gomod github.com/golang/go/src . `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src . `encoding/hex`/EncodeToString().(randBytes))
521 }
522
523 // generateDeviceID generates a deterministic device ID based on the user and
524 // host. This ensures the same logical client reuses the same device across
525 // restarts, preventing device accumulation on the Matrix server.
526 //
527 // The device ID follows the Matrix spec's Opaque Identifier Grammar:
528 // characters from [0-9], [A-Z], [a-z], "-", ".", "_", and "~".
529 // The format is "SH" + 8 characters derived from SHA-256(user:host),
530 // totaling 10 characters as recommended by the Matrix spec.
531 //
532 // The host is normalized to exclude default ports (443 for HTTPS, 80 for HTTP)
533 // to prevent device churn when ports are omitted or explicitly included.
534 func generateDeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/generateDeviceID().(user, host string) string {
535 // Normalize host by removing default ports to prevent device churn.
536 // url.Host may include a port (e.g., "matrix.example.com:443"), but we want
537 // consistent device IDs regardless of whether the default port is explicit.
538 normalizedHost := normalizeHostForDeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/normalizeHostForDeviceID().(host)
539
540 // Create a deterministic hash from user and host.
541 // Using user + ":" + host ensures different users on the same host
542 // get different device IDs, and the same user on different hosts
543 // also gets different device IDs.
544 hash := sha256scip-go gomod github.com/golang/go/src . `crypto/sha256`/.Sum256scip-go gomod github.com/golang/go/src . `crypto/sha256`/Sum256().([]byte(user + ":" + normalizedHost))
545
546 // Encode hash bytes to a device ID using the Opaque Identifier Grammar.
547 // We use base32-like encoding with the allowed character set.
548 // The hash provides sufficient entropy for uniqueness per user+host.
549 deviceID := deviceIDPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDPrefix.
550 for i := 0; i < deviceIDHashBytesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDHashBytes. && len(deviceID) < deviceIDLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/deviceIDLength.; i++ {
551 // Map each byte to a character in the allowed set.
552 // Using modulo with a character set size ensures we stay within bounds.
553 deviceID += string(opaqueIDCharsetscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/opaqueIDCharset.[hash[i]%opaqueIDCharsetLenscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/opaqueIDCharsetLen.])
554 }
555
556 return deviceID
557 }
558
559 // normalizeHostForDeviceID normalizes a host string by removing default ports.
560 // This prevents device churn when the port is omitted or explicitly included
561 // in the URL (e.g., "matrix.example.com:443" vs "matrix.example.com").
562 func normalizeHostForDeviceIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/normalizeHostForDeviceID().(host string) string {
563 // Check for explicit port using net.SplitHostPort.
564 // If no port is present, return the host as-is.
565 hostname, port, err := netscip-go gomod github.com/golang/go/src . net/.SplitHostPortscip-go gomod github.com/golang/go/src . net/SplitHostPort().(host)
566 if err != nil {
567 // No port present, return host as-is.
568 return host
569 }
570
571 // Remove default ports to normalize the host.
572 // Port 443 is the default for HTTPS, 80 for HTTP.
573 if port == "443" || port == "80" {
574 return hostname
575 }
576
577 // Non-default port, keep it for uniqueness.
578 return host
579 }
580
581 // newClient creates a new Matrix client with the specified host and TLS settings.
582 //
583 //nolint:exhaustruct_v5 // Intentional partial initialization
584 func newClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/newClient().(host string, disableTLS bool, 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#) *clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client# {
585 client := &clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#{
586 loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logger.: logger,
587 apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL.: urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#{
588 Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.: host,
589 Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.: schemeHTTPSscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/schemeHTTPS.,
590 },
591 httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#httpClient.: &DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#{
592 clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/DefaultHTTPClient#client.: &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{
593 Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/defaultHTTPTimeout.,
594 },
595 },
596 }
597
598 if client.loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logger. == nil {
599 client.loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logger. = utilscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/.DiscardLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/util`/DiscardLogger.
600 }
601
602 if disableTLS {
603 client.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL..Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme. = schemeHTTPscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/schemeHTTP.
604 }
605
606 client.loggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#logger..Printfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#Printf.("Using server: %v\n", client.apiURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix`/client#apiURL..Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().())
607
608 return client
609 }