mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(notifications): add LunaSea support (#1284)
* feat(notifications): add lunasea * fix(web): truncate overflow in PasswordFieldWide * refactor(notifications): centralize msg building Left the building logic in discord.go and notifiarr.go as is because of their unique structure. * refactor: moved components and swapped to outline - Refactored the iconComponentMap to use a single iconStyle variable. * upped size from 4 to 5 * rename NotificationBuilder function
This commit is contained in:
parent
da365da17c
commit
a89a1a55d9
13 changed files with 266 additions and 203 deletions
100
internal/notification/lunasea.go
Normal file
100
internal/notification/lunasea.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// unsure if this is the best approach to send an image with the notification
|
||||
const defaultImageURL = "https://raw.githubusercontent.com/autobrr/autobrr/master/.github/images/logo.png"
|
||||
|
||||
type LunaSeaMessage struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Image string `json:"image,omitempty"`
|
||||
}
|
||||
|
||||
type lunaSeaSender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
}
|
||||
|
||||
func (s *lunaSeaSender) rewriteWebhookURL(url string) string {
|
||||
re := regexp.MustCompile(`/(radarr|sonarr|lidarr|tautulli|overseerr)/`)
|
||||
return re.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 {
|
||||
return &lunaSeaSender{
|
||||
log: log.With().Str("sender", "lunasea").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *lunaSeaSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := LunaSeaMessage{
|
||||
Title: s.builder.BuildTitle(event),
|
||||
Body: s.builder.BuildBody(payload),
|
||||
Image: defaultImageURL,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msg("lunasea client could not marshal data")
|
||||
return errors.Wrap(err, "could not marshal data")
|
||||
}
|
||||
|
||||
rewrittenURL := s.rewriteWebhookURL(s.Settings.Webhook)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, rewrittenURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msg("lunasea client request error")
|
||||
return errors.Wrap(err, "could not create request")
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msg("lunasea client request error")
|
||||
return errors.Wrap(err, "could not make request")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
s.log.Error().Msgf("bad status from lunasea: %v", res.StatusCode)
|
||||
return errors.New("bad status: %v", res.StatusCode)
|
||||
}
|
||||
|
||||
s.log.Debug().Msg("notification successfully sent to lunasea")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *lunaSeaSender) CanSend(event domain.NotificationEvent) bool {
|
||||
if s.Settings.Enabled && s.Settings.Webhook != "" && s.isEnabledEvent(event) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *lunaSeaSender) isEnabledEvent(event domain.NotificationEvent) bool {
|
||||
for _, e := range s.Settings.Events {
|
||||
if e == string(event) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue