fix(notifications): disable notification and events have no effect (#1754)

fix(notifications): disable notificatio nand events
This commit is contained in:
ze0s 2024-10-06 14:12:01 +02:00 committed by GitHub
parent 009647fcd1
commit ca2d956e02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 118 additions and 125 deletions

View file

@ -50,7 +50,7 @@ const (
type discordSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
httpClient *http.Client
}
@ -59,7 +59,7 @@ func (a *discordSender) Name() string {
return "discord"
}
func NewDiscordSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewDiscordSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &discordSender{
log: log.With().Str("sender", "discord").Logger(),
Settings: settings,

View file

@ -26,7 +26,7 @@ type gotifyMessage struct {
type gotifySender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
builder MessageBuilderPlainText
httpClient *http.Client
@ -36,7 +36,7 @@ func (s *gotifySender) Name() string {
return "gotify"
}
func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewGotifySender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &gotifySender{
log: log.With().Str("sender", "gotify").Logger(),
Settings: settings,

View file

@ -27,7 +27,7 @@ type LunaSeaMessage struct {
type lunaSeaSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
builder MessageBuilderPlainText
httpClient *http.Client
@ -43,7 +43,7 @@ func (s *lunaSeaSender) rewriteWebhookURL(url string) string {
return lunaWebhook.ReplaceAllString(url, "/custom/")
} // `custom` is not mentioned in their docs, so I thought this would be a good idea to add to avoid user errors
func NewLunaSeaSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewLunaSeaSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &lunaSeaSender{
log: log.With().Str("sender", "lunasea").Logger(),
Settings: settings,

View file

@ -44,7 +44,7 @@ type notifiarrMessageData struct {
type notifiarrSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
baseUrl string
httpClient *http.Client
@ -54,7 +54,7 @@ func (s *notifiarrSender) Name() string {
return "notifiarr"
}
func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewNotifiarrSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &notifiarrSender{
log: log.With().Str("sender", "notifiarr").Logger(),
Settings: settings,

View file

@ -25,7 +25,7 @@ type ntfyMessage struct {
type ntfySender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
builder MessageBuilderPlainText
httpClient *http.Client
@ -35,7 +35,7 @@ func (s *ntfySender) Name() string {
return "ntfy"
}
func NewNtfySender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewNtfySender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &ntfySender{
log: log.With().Str("sender", "ntfy").Logger(),
Settings: settings,

View file

@ -32,7 +32,7 @@ type pushoverMessage struct {
type pushoverSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
baseUrl string
builder MessageBuilderHTML
@ -43,7 +43,7 @@ func (s *pushoverSender) Name() string {
return "pushover"
}
func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewPushoverSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &pushoverSender{
log: log.With().Str("sender", "pushover").Logger(),
Settings: settings,

View file

@ -19,11 +19,11 @@ import (
type Service interface {
Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error)
FindByID(ctx context.Context, id int) (*domain.Notification, error)
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
Update(ctx context.Context, n domain.Notification) (*domain.Notification, error)
Store(ctx context.Context, notification *domain.Notification) error
Update(ctx context.Context, notification *domain.Notification) error
Delete(ctx context.Context, id int) error
Send(event domain.NotificationEvent, payload domain.NotificationPayload)
Test(ctx context.Context, notification domain.Notification) error
Test(ctx context.Context, notification *domain.Notification) error
}
type service struct {
@ -64,30 +64,30 @@ func (s *service) FindByID(ctx context.Context, id int) (*domain.Notification, e
return notification, err
}
func (s *service) Store(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
_, err := s.repo.Store(ctx, notification)
func (s *service) Store(ctx context.Context, notification *domain.Notification) error {
err := s.repo.Store(ctx, notification)
if err != nil {
s.log.Error().Err(err).Msgf("could not store notification: %+v", notification)
return nil, err
return err
}
// register sender
s.registerSender(notification)
return nil, nil
return nil
}
func (s *service) Update(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
_, err := s.repo.Update(ctx, notification)
func (s *service) Update(ctx context.Context, notification *domain.Notification) error {
err := s.repo.Update(ctx, notification)
if err != nil {
s.log.Error().Err(err).Msgf("could not update notification: %+v", notification)
return nil, err
return err
}
// register sender
s.registerSender(notification)
return nil, nil
return nil
}
func (s *service) Delete(ctx context.Context, id int) error {
@ -111,33 +111,36 @@ func (s *service) registerSenders() {
}
for _, notificationSender := range notificationSenders {
s.registerSender(notificationSender)
s.registerSender(&notificationSender)
}
return
}
// registerSender registers an enabled notification via it's id
func (s *service) registerSender(notification domain.Notification) {
if notification.Enabled {
switch notification.Type {
case domain.NotificationTypeDiscord:
s.senders[notification.ID] = NewDiscordSender(s.log, notification)
case domain.NotificationTypeGotify:
s.senders[notification.ID] = NewGotifySender(s.log, notification)
case domain.NotificationTypeLunaSea:
s.senders[notification.ID] = NewLunaSeaSender(s.log, notification)
case domain.NotificationTypeNotifiarr:
s.senders[notification.ID] = NewNotifiarrSender(s.log, notification)
case domain.NotificationTypeNtfy:
s.senders[notification.ID] = NewNtfySender(s.log, notification)
case domain.NotificationTypePushover:
s.senders[notification.ID] = NewPushoverSender(s.log, notification)
case domain.NotificationTypeShoutrrr:
s.senders[notification.ID] = NewShoutrrrSender(s.log, notification)
case domain.NotificationTypeTelegram:
s.senders[notification.ID] = NewTelegramSender(s.log, notification)
}
func (s *service) registerSender(notification *domain.Notification) {
if !notification.Enabled {
delete(s.senders, notification.ID)
return
}
switch notification.Type {
case domain.NotificationTypeDiscord:
s.senders[notification.ID] = NewDiscordSender(s.log, notification)
case domain.NotificationTypeGotify:
s.senders[notification.ID] = NewGotifySender(s.log, notification)
case domain.NotificationTypeLunaSea:
s.senders[notification.ID] = NewLunaSeaSender(s.log, notification)
case domain.NotificationTypeNotifiarr:
s.senders[notification.ID] = NewNotifiarrSender(s.log, notification)
case domain.NotificationTypeNtfy:
s.senders[notification.ID] = NewNtfySender(s.log, notification)
case domain.NotificationTypePushover:
s.senders[notification.ID] = NewPushoverSender(s.log, notification)
case domain.NotificationTypeShoutrrr:
s.senders[notification.ID] = NewShoutrrrSender(s.log, notification)
case domain.NotificationTypeTelegram:
s.senders[notification.ID] = NewTelegramSender(s.log, notification)
}
return
@ -163,7 +166,7 @@ func (s *service) Send(event domain.NotificationEvent, payload domain.Notificati
return
}
func (s *service) Test(ctx context.Context, notification domain.Notification) error {
func (s *service) Test(ctx context.Context, notification *domain.Notification) error {
var agent domain.NotificationSender
// send test events

View file

@ -9,7 +9,7 @@ import (
type shoutrrrSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
builder MessageBuilderPlainText
}
@ -17,7 +17,7 @@ func (s *shoutrrrSender) Name() string {
return "shoutrrr"
}
func NewShoutrrrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewShoutrrrSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
return &shoutrrrSender{
log: log.With().Str("sender", "shoutrrr").Logger(),
Settings: settings,

View file

@ -30,7 +30,7 @@ type TelegramMessage struct {
type telegramSender struct {
log zerolog.Logger
Settings domain.Notification
Settings *domain.Notification
ThreadID int
builder MessageBuilderHTML
@ -41,7 +41,7 @@ func (s *telegramSender) Name() string {
return "telegram"
}
func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
func NewTelegramSender(log zerolog.Logger, settings *domain.Notification) domain.NotificationSender {
threadID := 0
if t := settings.Topic; t != "" {
var err error