-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
notifiarr.go
176 lines (147 loc) · 4.99 KB
/
notifiarr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package notification
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"net/http"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/rs/zerolog"
)
type notifiarrMessage struct {
Event string `json:"event"`
Data notifiarrMessageData `json:"data"`
}
type notifiarrMessageData struct {
Subject string `json:"subject"`
Message string `json:"message"`
Event domain.NotificationEvent `json:"event"`
ReleaseName *string `json:"release_name,omitempty"`
Filter *string `json:"filter,omitempty"`
Indexer *string `json:"indexer,omitempty"`
InfoHash *string `json:"info_hash,omitempty"`
Size *uint64 `json:"size,omitempty"`
Status *domain.ReleasePushStatus `json:"status,omitempty"`
Action *string `json:"action,omitempty"`
ActionType *domain.ActionType `json:"action_type,omitempty"`
ActionClient *string `json:"action_client,omitempty"`
Rejections []string `json:"rejections,omitempty"`
Protocol *domain.ReleaseProtocol `json:"protocol,omitempty"` // torrent, usenet
Implementation *domain.ReleaseImplementation `json:"implementation,omitempty"` // irc, rss, api
Timestamp time.Time `json:"timestamp"`
}
type notifiarrSender struct {
log zerolog.Logger
Settings domain.Notification
baseUrl string
}
func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
return ¬ifiarrSender{
log: log.With().Str("sender", "notifiarr").Logger(),
Settings: settings,
baseUrl: "https://notifiarr.com/api/v1/notification/autobrr",
}
}
func (s *notifiarrSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
m := notifiarrMessage{
Event: string(event),
Data: s.buildMessage(payload),
}
jsonData, err := json.Marshal(m)
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client could not marshal data: %v", m)
return errors.Wrap(err, "could not marshal data: %+v", m)
}
req, err := http.NewRequest(http.MethodPost, s.baseUrl, bytes.NewBuffer(jsonData))
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
return errors.Wrap(err, "could not create request")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "autobrr")
req.Header.Set("X-API-Key", s.Settings.APIKey)
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 30 * time.Second}
res, err := client.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer res.Body.Close()
s.log.Trace().Msgf("notifiarr status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", string(body))
return errors.New("bad status: %v body: %v", res.StatusCode, string(body))
}
s.log.Debug().Msg("notification successfully sent to notifiarr")
return nil
}
func (s *notifiarrSender) CanSend(event domain.NotificationEvent) bool {
if s.isEnabled() && s.isEnabledEvent(event) {
return true
}
return false
}
func (s *notifiarrSender) isEnabled() bool {
if s.Settings.Enabled {
if s.Settings.APIKey == "" {
s.log.Warn().Msg("notifiarr missing api key")
return false
}
return true
}
return false
}
func (s *notifiarrSender) isEnabledEvent(event domain.NotificationEvent) bool {
for _, e := range s.Settings.Events {
if e == string(event) {
return true
}
}
return false
}
func (s *notifiarrSender) buildMessage(payload domain.NotificationPayload) notifiarrMessageData {
m := notifiarrMessageData{
Event: payload.Event,
Timestamp: payload.Timestamp,
}
if payload.Subject != "" && payload.Message != "" {
m.Subject = payload.Subject
m.Message = payload.Message
}
if payload.ReleaseName != "" {
m.ReleaseName = &payload.ReleaseName
}
if payload.Status != "" {
m.Status = &payload.Status
}
if payload.Indexer != "" {
m.Indexer = &payload.Indexer
}
if payload.Filter != "" {
m.Filter = &payload.Filter
}
if payload.Action != "" || payload.ActionClient != "" {
m.Action = &payload.Action
if payload.ActionClient != "" {
m.ActionClient = &payload.ActionClient
}
}
if len(payload.Rejections) > 0 {
m.Rejections = payload.Rejections
}
return m
}