Debian Codegraph @

pkg/services/chat/matrix/matrix_service.go

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