| 1 |
package pushbullet |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient" |
| 12 |
) |
| 13 |
|
| 14 |
// Service providing Pushbullet as a notification service. |
| 15 |
type Service struct { |
| 16 |
standard.Standard |
| 17 |
|
| 18 |
client jsonclient.Client |
| 19 |
Config *Config |
| 20 |
pkr format.PropKeyResolver |
| 21 |
} |
| 22 |
|
| 23 |
// Constants. |
| 24 |
const ( |
| 25 |
pushesEndpoint = "https://api.pushbullet.com/v2/pushes" |
| 26 |
) |
| 27 |
|
| 28 |
// Static errors for push validation. |
| 29 |
var ( |
| 30 |
ErrUnexpectedResponseType = errors.New("unexpected response type, expected note") |
| 31 |
ErrResponseBodyMismatch = errors.New("response body mismatch") |
| 32 |
ErrResponseTitleMismatch = errors.New("response title mismatch") |
| 33 |
ErrPushNotActive = errors.New("push notification is not active") |
| 34 |
) |
| 35 |
|
| 36 |
// GetID returns the service identifier. |
| 37 |
func (s *Service) GetID() string { |
| 38 |
return Scheme |
| 39 |
} |
| 40 |
|
| 41 |
// Initialize loads ServiceConfig from serviceURL and sets logger for this Service. |
| 42 |
func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 43 |
s.SetLogger(logger) |
| 44 |
|
| 45 |
s.Config = &Config{ |
| 46 |
Title: "Shoutrrr notification", // Explicitly set default |
| 47 |
} |
| 48 |
s.pkr = format.NewPropKeyResolver(s.Config) |
| 49 |
|
| 50 |
if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
s.client = jsonclient.NewClient() |
| 55 |
s.client.Headers().Set("Access-Token", s.Config.Token) |
| 56 |
|
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
// Send a push notification via Pushbullet. |
| 61 |
func (s *Service) Send(message string, params *types.Params) error { |
| 62 |
config := *s.Config |
| 63 |
if err := s.pkr.UpdateConfigFromParams(&config, params); err != nil { |
| 64 |
return fmt.Errorf("updating config from params: %w", err) |
| 65 |
} |
| 66 |
|
| 67 |
for _, target := range config.Targets { |
| 68 |
if err := doSend(&config, target, message, s.client); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
// doSend sends a push notification to a specific target and validates the response. |
| 77 |
func doSend(config *Config, target, message string, client jsonclient.Client) error { |
| 78 |
push := NewNotePush(message, config.Title) |
| 79 |
push.SetTarget(target) |
| 80 |
|
| 81 |
response := PushResponse{} |
| 82 |
if err := client.Post(pushesEndpoint, push, &response); err != nil { |
| 83 |
errorResponse := &ResponseError{} |
| 84 |
if client.ErrorResponse(err, errorResponse) { |
| 85 |
return fmt.Errorf("API error: %w", errorResponse) |
| 86 |
} |
| 87 |
|
| 88 |
return fmt.Errorf("failed to push: %w", err) |
| 89 |
} |
| 90 |
|
| 91 |
// Validate response fields |
| 92 |
if response.Type != "note" { |
| 93 |
return fmt.Errorf("%w: got %s", ErrUnexpectedResponseType, response.Type) |
| 94 |
} |
| 95 |
|
| 96 |
if response.Body != message { |
| 97 |
return fmt.Errorf( |
| 98 |
"%w: got %s, expected %s", |
| 99 |
ErrResponseBodyMismatch, |
| 100 |
response.Body, |
| 101 |
message, |
| 102 |
) |
| 103 |
} |
| 104 |
|
| 105 |
if response.Title != config.Title { |
| 106 |
return fmt.Errorf( |
| 107 |
"%w: got %s, expected %s", |
| 108 |
ErrResponseTitleMismatch, |
| 109 |
response.Title, |
| 110 |
config.Title, |
| 111 |
) |
| 112 |
} |
| 113 |
|
| 114 |
if !response.Active { |
| 115 |
return ErrPushNotActive |
| 116 |
} |
| 117 |
|
| 118 |
return nil |
| 119 |
} |