| 1 |
package join |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 |
) |
| 15 |
|
| 16 |
// Service sends notifications to Join devices. |
| 17 |
type Service struct { |
| 18 |
standard.Standard |
| 19 |
|
| 20 |
Config *Config |
| 21 |
pkr format.PropKeyResolver |
| 22 |
httpClient types.HTTPClient |
| 23 |
} |
| 24 |
|
| 25 |
const ( |
| 26 |
// hookURL defines the Join API endpoint for sending push notifications. |
| 27 |
hookURL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush" |
| 28 |
contentType = "text/plain" |
| 29 |
defaultTimeout = 10 * time.Second |
| 30 |
) |
| 31 |
|
| 32 |
// GetID returns the identifier for this service. |
| 33 |
func (s *Service) GetID() string { |
| 34 |
return Scheme |
| 35 |
} |
| 36 |
|
| 37 |
// Initialize configures the service with a URL and logger. |
| 38 |
func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 39 |
s.SetLogger(logger) |
| 40 |
s.Config = &Config{} |
| 41 |
s.pkr = format.NewPropKeyResolver(s.Config) |
| 42 |
|
| 43 |
if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
|
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
// Send delivers a notification message to Join devices. |
| 51 |
func (s *Service) Send(message string, params *types.Params) error { |
| 52 |
config := s.Config |
| 53 |
|
| 54 |
if params == nil { |
| 55 |
params = &types.Params{} |
| 56 |
} |
| 57 |
|
| 58 |
title, found := (*params)["title"] |
| 59 |
if !found { |
| 60 |
title = config.Title |
| 61 |
} |
| 62 |
|
| 63 |
icon, found := (*params)["icon"] |
| 64 |
if !found { |
| 65 |
icon = config.Icon |
| 66 |
} |
| 67 |
|
| 68 |
devices := strings.Join(config.Devices, ",") |
| 69 |
|
| 70 |
return s.sendToDevices(devices, message, title, icon) |
| 71 |
} |
| 72 |
|
| 73 |
// SetHTTPClient sets a custom HTTP client for the service. |
| 74 |
func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 75 |
s.httpClient = client |
| 76 |
} |
| 77 |
|
| 78 |
func (s *Service) sendToDevices(devices, message, title, icon string) error { |
| 79 |
config := s.Config |
| 80 |
|
| 81 |
apiURL, err := url.Parse(hookURL) |
| 82 |
if err != nil { |
| 83 |
return fmt.Errorf("parsing Join API URL: %w", err) |
| 84 |
} |
| 85 |
|
| 86 |
data := url.Values{} |
| 87 |
data.Set("deviceIds", devices) |
| 88 |
data.Set("apikey", config.APIKey) |
| 89 |
data.Set("text", message) |
| 90 |
|
| 91 |
if title != "" { |
| 92 |
data.Set("title", title) |
| 93 |
} |
| 94 |
|
| 95 |
if title != "" { |
| 96 |
data.Set("icon", icon) |
| 97 |
} |
| 98 |
|
| 99 |
apiURL.RawQuery = data.Encode() |
| 100 |
|
| 101 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 102 |
defer cancel() |
| 103 |
|
| 104 |
req, err := http.NewRequestWithContext( |
| 105 |
ctx, |
| 106 |
http.MethodPost, |
| 107 |
apiURL.String(), |
| 108 |
http.NoBody, |
| 109 |
) |
| 110 |
if err != nil { |
| 111 |
return fmt.Errorf("creating HTTP request: %w", err) |
| 112 |
} |
| 113 |
|
| 114 |
req.Header.Set("Content-Type", contentType) |
| 115 |
|
| 116 |
client := s.httpClient |
| 117 |
if client == nil { |
| 118 |
client = &http.Client{Timeout: defaultTimeout} |
| 119 |
} |
| 120 |
|
| 121 |
res, err := client.Do(req) |
| 122 |
if err != nil { |
| 123 |
return fmt.Errorf("sending HTTP request to Join: %w", err) |
| 124 |
} |
| 125 |
|
| 126 |
defer func() { _ = res.Body.Close() }() |
| 127 |
|
| 128 |
if res.StatusCode != http.StatusOK { |
| 129 |
return fmt.Errorf( |
| 130 |
"%w: %q, response status %q", |
| 131 |
ErrSendFailed, |
| 132 |
devices, |
| 133 |
res.Status, |
| 134 |
) |
| 135 |
} |
| 136 |
|
| 137 |
return nil |
| 138 |
} |