Debian Codegraph @

pkg/services/chat/matrix/matrix_service.go

raw symbols SCIP
1 package matrix
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8 "strings"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/format"
11 "github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
12 "github.com/nicholas-fedor/shoutrrr/pkg/types"
13 )
14
15 // Service sends notifications via the Matrix protocol.
16 type Service struct {
17 standard.Standard
18
19 Config *Config
20 client *client
21 pkr format.PropKeyResolver
22 httpClient types.HTTPClient
23 }
24
25 // Scheme identifies this service in configuration URLs.
26 const Scheme = "matrix"
27
28 // GetID returns the identifier for this service.
29 func (s *Service) GetID() string {
30 return Scheme
31 }
32
33 // Initialize configures the service with a URL and logger.
34 func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error {
35 s.SetLogger(logger)
36 s.Config = &Config{
37 EnumlessConfig: standard.EnumlessConfig{},
38 User: "",
39 Password: "",
40 DisableTLS: false,
41 Host: "",
42 Rooms: nil,
43 Title: "",
44 }
45 s.pkr = format.NewPropKeyResolver(s.Config)
46
47 if err := s.Config.setURL(&s.pkr, serviceURL); err != nil {
48 return err
49 }
50
51 if serviceURL.Hostname() != "dummy.com" && serviceURL.Host != "" {
52 s.client = newClient(s.Config.Host, s.Config.DisableTLS, logger)
53 if s.httpClient != nil {
54 switch c := s.httpClient.(type) {
55 case *http.Client:
56 s.client.httpClient = &DefaultHTTPClient{client: c}
57 default:
58 s.client.httpClient = s.httpClient
59 }
60 }
61
62 if s.Config.User != "" {
63 return s.client.login(context.Background(), s.Config.User, s.Config.Password)
64 }
65
66 s.client.useToken(s.Config.Password)
67 }
68
69 return nil
70 }
71
72 // Send delivers a notification message to Matrix rooms.
73 func (s *Service) Send(message string, params *types.Params) error {
74 return s.SendWithContext(context.Background(), message, params)
75 }
76
77 // SendWithContext delivers a notification message to Matrix rooms with the provided context.
78 func (s *Service) SendWithContext(ctx context.Context, message string, params *types.Params) error {
79 if s.client == nil {
80 return ErrClientNotInitialized
81 }
82
83 // Make a per-call copy of the config to avoid mutating the shared s.Config
84 cfg := *s.Config
85
86 if err := s.pkr.UpdateConfigFromParams(&cfg, params); err != nil {
87 return fmt.Errorf("updating config from params: %w", err)
88 }
89
90 // Create message with title if provided
91 fullMessage := createMessage(message, cfg.Title)
92
93 sendErrors := s.client.sendMessage(ctx, fullMessage, cfg.Rooms)
94 if len(sendErrors) > 0 {
95 for _, err := range sendErrors {
96 s.Logf("error sending message: %v", err)
97 }
98
99 return fmt.Errorf(
100 "%v error(s) sending message, with initial error: %w",
101 len(sendErrors),
102 sendErrors[0],
103 )
104 }
105
106 return nil
107 }
108
109 // SetHTTPClient sets a custom HTTP client for the service (propagated to internal client).
110 func (s *Service) SetHTTPClient(client types.HTTPClient) {
111 s.httpClient = client
112 if s.client != nil {
113 switch c := client.(type) {
114 case *http.Client:
115 s.client.httpClient = &DefaultHTTPClient{client: c}
116 default:
117 s.client.httpClient = client
118 }
119 }
120 }
121
122 // createMessage creates the full message body by prepending the title if provided.
123 // Format: If title is "Alert" and message is "Hello", output is "Alert\n\nHello".
124 func createMessage(message, title string) string {
125 trimmedTitle := strings.TrimSpace(title)
126 if trimmedTitle == "" {
127 return message
128 }
129
130 return trimmedTitle + "\n\n" + message
131 }