mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
fix(notifications): Pushover and Telegram formatting (#1362)
* fix(notifications): Pushover and Telegram formatting * fix(notifications): html builder * fix(notifications): escape html * fix(notifications): escaping
This commit is contained in:
parent
c377bc9157
commit
ae4427175f
8 changed files with 140 additions and 38 deletions
|
@ -26,7 +26,7 @@ type gotifyMessage struct {
|
|||
type gotifySender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.No
|
|||
return &gotifySender{
|
||||
log: log.With().Str("sender", "gotify").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
|
@ -46,7 +46,7 @@ func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.No
|
|||
func (s *gotifySender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := gotifyMessage{
|
||||
Message: s.builder.BuildBody(payload),
|
||||
Title: s.builder.BuildTitle(event),
|
||||
Title: BuildTitle(event),
|
||||
}
|
||||
|
||||
data := url.Values{}
|
||||
|
|
|
@ -26,7 +26,7 @@ type LunaSeaMessage struct {
|
|||
type lunaSeaSender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func NewLunaSeaSender(log zerolog.Logger, settings domain.Notification) domain.N
|
|||
return &lunaSeaSender{
|
||||
log: log.With().Str("sender", "lunasea").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
|
@ -50,7 +50,7 @@ func NewLunaSeaSender(log zerolog.Logger, settings domain.Notification) domain.N
|
|||
|
||||
func (s *lunaSeaSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := LunaSeaMessage{
|
||||
Title: s.builder.BuildTitle(event),
|
||||
Title: BuildTitle(event),
|
||||
Body: s.builder.BuildBody(payload),
|
||||
Image: defaultImageURL,
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package notification
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"strings"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
@ -9,35 +10,67 @@ import (
|
|||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type NotificationBuilderPlainText struct{}
|
||||
type MessageBuilder interface {
|
||||
BuildBody(payload domain.NotificationPayload) string
|
||||
}
|
||||
|
||||
type ConditionMessagePart struct {
|
||||
Condition bool
|
||||
Format string
|
||||
Bits []interface{}
|
||||
}
|
||||
|
||||
// MessageBuilderPlainText constructs the body of the notification message in plain text format.
|
||||
type MessageBuilderPlainText struct{}
|
||||
|
||||
// BuildBody constructs the body of the notification message.
|
||||
func (b *NotificationBuilderPlainText) BuildBody(payload domain.NotificationPayload) string {
|
||||
var parts []string
|
||||
func (b *MessageBuilderPlainText) BuildBody(payload domain.NotificationPayload) string {
|
||||
messageParts := []ConditionMessagePart{
|
||||
{payload.Subject != "" && payload.Message != "", "%v\n%v", []interface{}{payload.Subject, payload.Message}},
|
||||
{payload.ReleaseName != "", "New release: %v\n", []interface{}{payload.ReleaseName}},
|
||||
{payload.Size > 0, "Size: %v\n", []interface{}{humanize.Bytes(payload.Size)}},
|
||||
{payload.Status != "", "Status: %v\n", []interface{}{payload.Status.String()}},
|
||||
{payload.Indexer != "", "Indexer: %v\n", []interface{}{payload.Indexer}},
|
||||
{payload.Filter != "", "Filter: %v\n", []interface{}{payload.Filter}},
|
||||
{payload.Action != "", "Action: %v: %v\n", []interface{}{payload.ActionType, payload.Action}},
|
||||
{payload.Action != "" && payload.ActionClient != "", "Client: %v\n", []interface{}{payload.ActionClient}},
|
||||
{len(payload.Rejections) > 0, "Rejections: %v\n", []interface{}{strings.Join(payload.Rejections, ", ")}},
|
||||
}
|
||||
|
||||
buildPart := func(condition bool, format string, a ...interface{}) {
|
||||
if condition {
|
||||
parts = append(parts, fmt.Sprintf(format, a...))
|
||||
return formatMessageContent(messageParts)
|
||||
}
|
||||
|
||||
// MessageBuilderHTML constructs the body of the notification message in HTML format.
|
||||
type MessageBuilderHTML struct{}
|
||||
|
||||
func (b *MessageBuilderHTML) BuildBody(payload domain.NotificationPayload) string {
|
||||
messageParts := []ConditionMessagePart{
|
||||
{payload.Subject != "" && payload.Message != "", "<b>%v</b> %v\n", []interface{}{html.EscapeString(payload.Subject), html.EscapeString(payload.Message)}},
|
||||
{payload.ReleaseName != "", "<b>New release:</b> %v\n", []interface{}{html.EscapeString(payload.ReleaseName)}},
|
||||
{payload.Size > 0, "<b>Size:</b> %v\n", []interface{}{humanize.Bytes(payload.Size)}},
|
||||
{payload.Status != "", "<b>Status:</b> %v\n", []interface{}{html.EscapeString(payload.Status.String())}},
|
||||
{payload.Indexer != "", "<b>Indexer:</b> %v\n", []interface{}{html.EscapeString(payload.Indexer)}},
|
||||
{payload.Filter != "", "<b>Filter:</b> %v\n", []interface{}{html.EscapeString(payload.Filter)}},
|
||||
{payload.Action != "", "<b>Action:</b> %v: %v\n", []interface{}{payload.ActionType, html.EscapeString(payload.Action)}},
|
||||
{payload.Action != "" && payload.ActionClient != "", "<b>Client:</b> %v\n", []interface{}{html.EscapeString(payload.ActionClient)}},
|
||||
{len(payload.Rejections) > 0, "<b>Rejections:</b> %v\n", []interface{}{html.EscapeString(strings.Join(payload.Rejections, ", "))}},
|
||||
}
|
||||
|
||||
return formatMessageContent(messageParts)
|
||||
}
|
||||
|
||||
func formatMessageContent(messageParts []ConditionMessagePart) string {
|
||||
var builder strings.Builder
|
||||
for _, part := range messageParts {
|
||||
if part.Condition {
|
||||
builder.WriteString(fmt.Sprintf(part.Format, part.Bits...))
|
||||
}
|
||||
}
|
||||
|
||||
buildPart(payload.Subject != "" && payload.Message != "", "%v\n%v", payload.Subject, payload.Message)
|
||||
buildPart(payload.ReleaseName != "", "\nNew release: %v", payload.ReleaseName)
|
||||
buildPart(payload.Size > 0, "\nSize: %v", humanize.Bytes(payload.Size))
|
||||
buildPart(payload.Status != "", "\nStatus: %v", payload.Status.String())
|
||||
buildPart(payload.Indexer != "", "\nIndexer: %v", payload.Indexer)
|
||||
buildPart(payload.Filter != "", "\nFilter: %v", payload.Filter)
|
||||
buildPart(payload.Action != "", "\nAction: %v: %v", payload.ActionType, payload.Action)
|
||||
if payload.Action != "" && payload.ActionClient != "" {
|
||||
parts = append(parts, fmt.Sprintf(" Client: %v", payload.ActionClient))
|
||||
}
|
||||
buildPart(len(payload.Rejections) > 0, "\nRejections: %v", strings.Join(payload.Rejections, ", "))
|
||||
|
||||
return strings.Join(parts, "\n")
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// BuildTitle constructs the title of the notification message.
|
||||
func (b *NotificationBuilderPlainText) BuildTitle(event domain.NotificationEvent) string {
|
||||
func BuildTitle(event domain.NotificationEvent) string {
|
||||
titles := map[domain.NotificationEvent]string{
|
||||
domain.NotificationEventAppUpdateAvailable: "Autobrr update available",
|
||||
domain.NotificationEventPushApproved: "Push Approved",
|
||||
|
|
70
internal/notification/message_builder_test.go
Normal file
70
internal/notification/message_builder_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNotificationBuilderPlainText_BuildBody(t *testing.T) {
|
||||
type args struct {
|
||||
payload domain.NotificationPayload
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "build body",
|
||||
args: args{payload: domain.NotificationPayload{
|
||||
Subject: "",
|
||||
Message: "",
|
||||
Event: domain.NotificationEventPushApproved,
|
||||
ReleaseName: "Movie 2024 UHD BluRay 2160p DTS-HD MA 5.1 DV HEVC HYBRID REMUX-GROUP",
|
||||
Filter: "test",
|
||||
Indexer: "mock",
|
||||
InfoHash: "",
|
||||
Size: 0,
|
||||
Status: domain.ReleasePushStatusApproved,
|
||||
Action: "mock",
|
||||
ActionType: domain.ActionTypeQbittorrent,
|
||||
ActionClient: "mock",
|
||||
Rejections: nil,
|
||||
Protocol: domain.ReleaseProtocolTorrent,
|
||||
Implementation: domain.ReleaseImplementationIRC,
|
||||
Timestamp: time.Time{},
|
||||
}},
|
||||
want: "New release: Movie 2024 UHD BluRay 2160p DTS-HD MA 5.1 DV HEVC HYBRID REMUX-GROUP\nStatus: Approved\nIndexer: mock\nFilter: test\nAction: QBITTORRENT: mock\nClient: mock\n",
|
||||
},
|
||||
{
|
||||
name: "build body with rejections",
|
||||
args: args{payload: domain.NotificationPayload{
|
||||
Subject: "",
|
||||
Message: "",
|
||||
Event: domain.NotificationEventPushRejected,
|
||||
ReleaseName: "Movie 2024 UHD BluRay 2160p DTS-HD MA 5.1 DV HEVC HYBRID REMUX-GROUP",
|
||||
Filter: "test",
|
||||
Indexer: "mock",
|
||||
InfoHash: "",
|
||||
Size: 0,
|
||||
Status: domain.ReleasePushStatusRejected,
|
||||
Action: "mock",
|
||||
ActionType: domain.ActionTypeRadarr,
|
||||
ActionClient: "mock",
|
||||
Rejections: []string{"Item already exists"},
|
||||
Protocol: domain.ReleaseProtocolTorrent,
|
||||
Implementation: domain.ReleaseImplementationIRC,
|
||||
Timestamp: time.Time{},
|
||||
}},
|
||||
want: "New release: Movie 2024 UHD BluRay 2160p DTS-HD MA 5.1 DV HEVC HYBRID REMUX-GROUP\nStatus: Rejected\nIndexer: mock\nFilter: test\nAction: RADARR: mock\nClient: mock\nRejections: Item already exists\n",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b := &MessageBuilderPlainText{}
|
||||
assert.Equal(t, tt.want, b.BuildBody(tt.args.payload))
|
||||
})
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ type ntfyMessage struct {
|
|||
type ntfySender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func NewNtfySender(log zerolog.Logger, settings domain.Notification) domain.Noti
|
|||
return &ntfySender{
|
||||
log: log.With().Str("sender", "ntfy").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
|
@ -45,7 +45,7 @@ func NewNtfySender(log zerolog.Logger, settings domain.Notification) domain.Noti
|
|||
func (s *ntfySender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := ntfyMessage{
|
||||
Message: s.builder.BuildBody(payload),
|
||||
Title: s.builder.BuildTitle(event),
|
||||
Title: BuildTitle(event),
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, s.Settings.Host, strings.NewReader(m.Message))
|
||||
|
|
|
@ -33,7 +33,7 @@ type pushoverSender struct {
|
|||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
baseUrl string
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderHTML
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.
|
|||
log: log.With().Str("sender", "pushover").Logger(),
|
||||
Settings: settings,
|
||||
baseUrl: "https://api.pushover.net/1/messages.json",
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderHTML{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
|
@ -52,8 +52,7 @@ func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.
|
|||
}
|
||||
|
||||
func (s *pushoverSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
|
||||
title := s.builder.BuildTitle(event)
|
||||
title := BuildTitle(event)
|
||||
message := s.builder.BuildBody(payload)
|
||||
|
||||
m := pushoverMessage{
|
||||
|
|
|
@ -10,14 +10,14 @@ import (
|
|||
type shoutrrrSender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderPlainText
|
||||
}
|
||||
|
||||
func NewShoutrrrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
return &shoutrrrSender{
|
||||
log: log.With().Str("sender", "shoutrrr").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderPlainText{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ type telegramSender struct {
|
|||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
ThreadID int
|
||||
builder NotificationBuilderPlainText
|
||||
builder MessageBuilderHTML
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.
|
|||
log: log.With().Str("sender", "telegram").Logger(),
|
||||
Settings: settings,
|
||||
ThreadID: threadID,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
builder: MessageBuilderHTML{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue