-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
service.go
288 lines (244 loc) · 8.01 KB
/
service.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package notification
import (
"context"
"time"
"golang.org/x/sync/errgroup"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/rs/zerolog"
)
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)
Delete(ctx context.Context, id int) error
Send(event domain.NotificationEvent, payload domain.NotificationPayload)
Test(ctx context.Context, notification domain.Notification) error
}
type service struct {
log zerolog.Logger
repo domain.NotificationRepo
senders []domain.NotificationSender
}
func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
s := &service{
log: log.With().Str("module", "notification").Logger(),
repo: repo,
senders: []domain.NotificationSender{},
}
s.registerSenders()
return s
}
func (s *service) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
n, count, err := s.repo.Find(ctx, params)
if err != nil {
s.log.Error().Err(err).Msgf("could not find notification with params: %+v", params)
return nil, 0, err
}
return n, count, err
}
func (s *service) FindByID(ctx context.Context, id int) (*domain.Notification, error) {
n, err := s.repo.FindByID(ctx, id)
if err != nil {
s.log.Error().Err(err).Msgf("could not find notification by id: %v", id)
return nil, err
}
return n, err
}
func (s *service) Store(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
_, err := s.repo.Store(ctx, n)
if err != nil {
s.log.Error().Err(err).Msgf("could not store notification: %+v", n)
return nil, err
}
// reset senders
s.senders = []domain.NotificationSender{}
// re register senders
s.registerSenders()
return nil, nil
}
func (s *service) Update(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
_, err := s.repo.Update(ctx, n)
if err != nil {
s.log.Error().Err(err).Msgf("could not update notification: %+v", n)
return nil, err
}
// reset senders
s.senders = []domain.NotificationSender{}
// re register senders
s.registerSenders()
return nil, nil
}
func (s *service) Delete(ctx context.Context, id int) error {
err := s.repo.Delete(ctx, id)
if err != nil {
s.log.Error().Err(err).Msgf("could not delete notification: %v", id)
return err
}
// reset senders
s.senders = []domain.NotificationSender{}
// re register senders
s.registerSenders()
return nil
}
func (s *service) registerSenders() {
senders, err := s.repo.List(context.Background())
if err != nil {
s.log.Error().Err(err).Msg("could not find notifications")
return
}
for _, n := range senders {
if n.Enabled {
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))
}
}
}
return
}
// Send notifications
func (s *service) Send(event domain.NotificationEvent, payload domain.NotificationPayload) {
if len(s.senders) > 0 {
s.log.Debug().Msgf("sending notification for %v", string(event))
}
go func() {
for _, sender := range s.senders {
// check if sender is active and have notification types
if sender.CanSend(event) {
sender.Send(event, payload)
}
}
}()
return
}
func (s *service) Test(ctx context.Context, notification domain.Notification) error {
var agent domain.NotificationSender
// send test events
events := []domain.NotificationPayload{
{
Subject: "Test Notification",
Message: "autobrr goes brr!!",
Event: domain.NotificationEventTest,
Timestamp: time.Now(),
},
{
Subject: "New release!",
Message: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Event: domain.NotificationEventPushApproved,
ReleaseName: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Filter: "TV",
Indexer: "MockIndexer",
Status: domain.ReleasePushStatusApproved,
Action: "Send to qBittorrent",
ActionType: domain.ActionTypeQbittorrent,
ActionClient: "qBittorrent",
Rejections: nil,
Protocol: domain.ReleaseProtocolTorrent,
Implementation: domain.ReleaseImplementationIRC,
Timestamp: time.Now(),
},
{
Subject: "New release!",
Message: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Event: domain.NotificationEventPushRejected,
ReleaseName: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Filter: "TV",
Indexer: "MockIndexer",
Status: domain.ReleasePushStatusRejected,
Action: "Send to Sonarr",
ActionType: domain.ActionTypeSonarr,
ActionClient: "Sonarr",
Rejections: []string{"Unknown Series"},
Protocol: domain.ReleaseProtocolTorrent,
Implementation: domain.ReleaseImplementationIRC,
Timestamp: time.Now(),
},
{
Subject: "New release!",
Message: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Event: domain.NotificationEventPushError,
ReleaseName: "Best.Show.Ever.S18E21.1080p.AMZN.WEB-DL.DDP2.0.H.264-GROUP",
Filter: "TV",
Indexer: "MockIndexer",
Status: domain.ReleasePushStatusErr,
Action: "Send to Sonarr",
ActionType: domain.ActionTypeSonarr,
ActionClient: "Sonarr",
Rejections: []string{"error pushing to client"},
Protocol: domain.ReleaseProtocolTorrent,
Implementation: domain.ReleaseImplementationIRC,
Timestamp: time.Now(),
},
{
Subject: "IRC Disconnected unexpectedly",
Message: "Network: P2P-Network",
Event: domain.NotificationEventIRCDisconnected,
Timestamp: time.Now(),
},
{
Subject: "IRC Reconnected",
Message: "Network: P2P-Network",
Event: domain.NotificationEventIRCReconnected,
Timestamp: time.Now(),
},
{
Subject: "New update available!",
Message: "v1.6.0",
Event: domain.NotificationEventAppUpdateAvailable,
Timestamp: time.Now(),
},
}
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)
default:
s.log.Error().Msgf("unsupported notification type: %v", notification.Type)
return errors.New("unsupported notification type")
}
g, _ := errgroup.WithContext(ctx)
for _, event := range events {
e := event
if !enabledEvent(notification.Events, e.Event) {
continue
}
if err := agent.Send(e.Event, e); err != nil {
s.log.Error().Err(err).Msgf("error sending test notification: %#v", notification)
return err
}
time.Sleep(1 * time.Second)
}
if err := g.Wait(); err != nil {
s.log.Error().Err(err).Msgf("Something went wrong sending test notifications to %v", notification.Type)
return err
}
return nil
}
func enabledEvent(events []string, e domain.NotificationEvent) bool {
for _, v := range events {
if v == string(e) {
return true
}
}
return false
}