feat(notifications): add Shoutrrr support (#1326)

This commit is contained in:
ze0s 2023-12-30 16:34:25 +01:00 committed by GitHub
parent 3dd1629a3f
commit 57a91bb99a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 149 additions and 31 deletions

View file

@ -124,16 +124,20 @@ func (s *service) registerSenders() {
switch n.Type {
case domain.NotificationTypeDiscord:
s.senders = append(s.senders, NewDiscordSender(s.log, n))
case domain.NotificationTypeNotifiarr:
s.senders = append(s.senders, NewNotifiarrSender(s.log, n))
case domain.NotificationTypeTelegram:
s.senders = append(s.senders, NewTelegramSender(s.log, n))
case domain.NotificationTypePushover:
s.senders = append(s.senders, NewPushoverSender(s.log, n))
case domain.NotificationTypeGotify:
s.senders = append(s.senders, NewGotifySender(s.log, n))
case domain.NotificationTypeLunaSea:
s.senders = append(s.senders, NewLunaSeaSender(s.log, n))
case domain.NotificationTypeNotifiarr:
s.senders = append(s.senders, NewNotifiarrSender(s.log, n))
case domain.NotificationTypeNtfy:
s.senders = append(s.senders, NewNtfySender(s.log, n))
case domain.NotificationTypePushover:
s.senders = append(s.senders, NewPushoverSender(s.log, n))
case domain.NotificationTypeShoutrrr:
s.senders = append(s.senders, NewShoutrrrSender(s.log, n))
case domain.NotificationTypeTelegram:
s.senders = append(s.senders, NewTelegramSender(s.log, n))
}
}
}
@ -241,18 +245,20 @@ func (s *service) Test(ctx context.Context, notification domain.Notification) er
switch notification.Type {
case domain.NotificationTypeDiscord:
agent = NewDiscordSender(s.log, notification)
case domain.NotificationTypeNotifiarr:
agent = NewNotifiarrSender(s.log, notification)
case domain.NotificationTypeTelegram:
agent = NewTelegramSender(s.log, notification)
case domain.NotificationTypePushover:
agent = NewPushoverSender(s.log, notification)
case domain.NotificationTypeGotify:
agent = NewGotifySender(s.log, notification)
case domain.NotificationTypeNtfy:
agent = NewNtfySender(s.log, notification)
case domain.NotificationTypeLunaSea:
agent = NewLunaSeaSender(s.log, notification)
case domain.NotificationTypeNotifiarr:
agent = NewNotifiarrSender(s.log, notification)
case domain.NotificationTypeNtfy:
agent = NewNtfySender(s.log, notification)
case domain.NotificationTypePushover:
agent = NewPushoverSender(s.log, notification)
case domain.NotificationTypeShoutrrr:
agent = NewShoutrrrSender(s.log, notification)
case domain.NotificationTypeTelegram:
agent = NewTelegramSender(s.log, notification)
default:
s.log.Error().Msgf("unsupported notification type: %v", notification.Type)
return errors.New("unsupported notification type")

View file

@ -0,0 +1,69 @@
package notification
import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/containrrr/shoutrrr"
"github.com/rs/zerolog"
)
type shoutrrrSender struct {
log zerolog.Logger
Settings domain.Notification
builder NotificationBuilderPlainText
}
func NewShoutrrrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
return &shoutrrrSender{
log: log.With().Str("sender", "shoutrrr").Logger(),
Settings: settings,
builder: NotificationBuilderPlainText{},
}
}
func (s *shoutrrrSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
message := s.builder.BuildBody(payload)
if err := shoutrrr.Send(s.Settings.Host, message); err != nil {
return err
}
s.log.Debug().Msg("notification successfully sent to via shoutrrr")
return nil
}
func (s *shoutrrrSender) CanSend(event domain.NotificationEvent) bool {
if s.isEnabled() && s.isEnabledEvent(event) {
return true
}
return false
}
func (s *shoutrrrSender) isEnabled() bool {
if s.Settings.Enabled {
if s.Settings.Host == "" {
s.log.Warn().Msg("shoutrrr missing host")
return false
}
if s.Settings.Token == "" {
s.log.Warn().Msg("shoutrrr missing application token")
return false
}
return true
}
return false
}
func (s *shoutrrrSender) isEnabledEvent(event domain.NotificationEvent) bool {
for _, e := range s.Settings.Events {
if e == string(event) {
return true
}
}
return false
}