mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(notifications): add telegram support (#299)
* feat(notifications): add telegram support * feat(notifications): change list view * refactor(notifications): overall setup * feat(notifications): forms add telegram
This commit is contained in:
parent
2ab7133dd0
commit
38addb99e6
15 changed files with 630 additions and 457 deletions
|
@ -12,51 +12,6 @@ import (
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *service) RunActions(actions []domain.Action, release domain.Release) error {
|
|
||||||
|
|
||||||
for _, action := range actions {
|
|
||||||
// only run active actions
|
|
||||||
if !action.Enabled {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
s.log.Debug().Msgf("process action: %v for '%v'", action.Name, release.TorrentName)
|
|
||||||
|
|
||||||
err := s.runAction(action, release)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
|
|
||||||
|
|
||||||
s.bus.Publish("release:store-action-status", &domain.ReleaseActionStatus{
|
|
||||||
ReleaseID: release.ID,
|
|
||||||
Status: domain.ReleasePushStatusErr,
|
|
||||||
Action: action.Name,
|
|
||||||
Type: action.Type,
|
|
||||||
Rejections: []string{err.Error()},
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
})
|
|
||||||
|
|
||||||
s.bus.Publish("events:release:push", &domain.EventsReleasePushed{
|
|
||||||
ReleaseName: release.TorrentName,
|
|
||||||
Filter: release.Filter.Name,
|
|
||||||
Indexer: release.Indexer,
|
|
||||||
InfoHash: release.TorrentHash,
|
|
||||||
Size: release.Size,
|
|
||||||
Status: domain.ReleasePushStatusErr,
|
|
||||||
Action: action.Name,
|
|
||||||
ActionType: action.Type,
|
|
||||||
Rejections: []string{err.Error()},
|
|
||||||
Protocol: domain.ReleaseProtocolTorrent,
|
|
||||||
Implementation: domain.ReleaseImplementationIRC,
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// safe to delete tmp file
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) RunAction(action *domain.Action, release domain.Release) ([]string, error) {
|
func (s *service) RunAction(action *domain.Action, release domain.Release) ([]string, error) {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -186,7 +141,8 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationEvent := &domain.EventsReleasePushed{
|
payload := &domain.NotificationPayload{
|
||||||
|
Event: domain.NotificationEventPushApproved,
|
||||||
ReleaseName: release.TorrentName,
|
ReleaseName: release.TorrentName,
|
||||||
Filter: release.Filter.Name,
|
Filter: release.Filter.Name,
|
||||||
Indexer: release.Indexer,
|
Indexer: release.Indexer,
|
||||||
|
@ -208,188 +164,29 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
|
||||||
rlsActionStatus.Status = domain.ReleasePushStatusErr
|
rlsActionStatus.Status = domain.ReleasePushStatusErr
|
||||||
rlsActionStatus.Rejections = []string{err.Error()}
|
rlsActionStatus.Rejections = []string{err.Error()}
|
||||||
|
|
||||||
notificationEvent.Status = domain.ReleasePushStatusErr
|
payload.Event = domain.NotificationEventPushError
|
||||||
notificationEvent.Rejections = []string{err.Error()}
|
payload.Status = domain.ReleasePushStatusErr
|
||||||
|
payload.Rejections = []string{err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rejections != nil {
|
if rejections != nil {
|
||||||
rlsActionStatus.Status = domain.ReleasePushStatusRejected
|
rlsActionStatus.Status = domain.ReleasePushStatusRejected
|
||||||
rlsActionStatus.Rejections = rejections
|
rlsActionStatus.Rejections = rejections
|
||||||
|
|
||||||
notificationEvent.Status = domain.ReleasePushStatusRejected
|
payload.Event = domain.NotificationEventPushRejected
|
||||||
notificationEvent.Rejections = rejections
|
payload.Status = domain.ReleasePushStatusRejected
|
||||||
|
payload.Rejections = rejections
|
||||||
}
|
}
|
||||||
|
|
||||||
// send event for actions
|
// send event for actions
|
||||||
s.bus.Publish("release:push", rlsActionStatus)
|
s.bus.Publish("release:push", rlsActionStatus)
|
||||||
|
|
||||||
// send separate event for notifications
|
// send separate event for notifications
|
||||||
s.bus.Publish("events:release:push", notificationEvent)
|
s.bus.Publish("events:notification", &payload.Event, payload)
|
||||||
|
|
||||||
return rejections, err
|
return rejections, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) runAction(action domain.Action, release domain.Release) error {
|
|
||||||
|
|
||||||
var err error
|
|
||||||
var rejections []string
|
|
||||||
|
|
||||||
switch action.Type {
|
|
||||||
case domain.ActionTypeTest:
|
|
||||||
s.test(action.Name)
|
|
||||||
|
|
||||||
case domain.ActionTypeExec:
|
|
||||||
if release.TorrentTmpFile == "" {
|
|
||||||
if err := release.DownloadTorrentFile(); err != nil {
|
|
||||||
s.log.Error().Stack().Err(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.execCmd(release, action)
|
|
||||||
|
|
||||||
case domain.ActionTypeWatchFolder:
|
|
||||||
if release.TorrentTmpFile == "" {
|
|
||||||
if err := release.DownloadTorrentFile(); err != nil {
|
|
||||||
s.log.Error().Stack().Err(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.watchFolder(action, release)
|
|
||||||
|
|
||||||
case domain.ActionTypeWebhook:
|
|
||||||
if release.TorrentTmpFile == "" {
|
|
||||||
if err := release.DownloadTorrentFile(); err != nil {
|
|
||||||
s.log.Error().Stack().Err(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.webhook(action, release)
|
|
||||||
|
|
||||||
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
|
|
||||||
canDownload, err := s.delugeCheckRulesCanDownload(action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !canDownload {
|
|
||||||
rejections = []string{"max active downloads reached, skipping"}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if release.TorrentTmpFile == "" {
|
|
||||||
if err := release.DownloadTorrentFile(); err != nil {
|
|
||||||
s.log.Error().Stack().Err(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = s.deluge(action, release)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
case domain.ActionTypeQbittorrent:
|
|
||||||
canDownload, client, err := s.qbittorrentCheckRulesCanDownload(action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !canDownload {
|
|
||||||
rejections = []string{"max active downloads reached, skipping"}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if release.TorrentTmpFile == "" {
|
|
||||||
if err := release.DownloadTorrentFile(); err != nil {
|
|
||||||
s.log.Error().Stack().Err(err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = s.qbittorrent(client, action, release)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
case domain.ActionTypeRadarr:
|
|
||||||
rejections, err = s.radarr(release, action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
case domain.ActionTypeSonarr:
|
|
||||||
rejections, err = s.sonarr(release, action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
case domain.ActionTypeLidarr:
|
|
||||||
rejections, err = s.lidarr(release, action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
case domain.ActionTypeWhisparr:
|
|
||||||
rejections, err = s.whisparr(release, action)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
s.log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
rlsActionStatus := &domain.ReleaseActionStatus{
|
|
||||||
ReleaseID: release.ID,
|
|
||||||
Status: domain.ReleasePushStatusApproved,
|
|
||||||
Action: action.Name,
|
|
||||||
Type: action.Type,
|
|
||||||
Rejections: []string{},
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
notificationEvent := &domain.EventsReleasePushed{
|
|
||||||
ReleaseName: release.TorrentName,
|
|
||||||
Filter: release.Filter.Name,
|
|
||||||
Indexer: release.Indexer,
|
|
||||||
InfoHash: release.TorrentHash,
|
|
||||||
Size: release.Size,
|
|
||||||
Status: domain.ReleasePushStatusApproved,
|
|
||||||
Action: action.Name,
|
|
||||||
ActionType: action.Type,
|
|
||||||
Rejections: []string{},
|
|
||||||
Protocol: domain.ReleaseProtocolTorrent,
|
|
||||||
Implementation: domain.ReleaseImplementationIRC,
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if rejections != nil {
|
|
||||||
rlsActionStatus.Status = domain.ReleasePushStatusRejected
|
|
||||||
rlsActionStatus.Rejections = rejections
|
|
||||||
|
|
||||||
notificationEvent.Status = domain.ReleasePushStatusRejected
|
|
||||||
notificationEvent.Rejections = rejections
|
|
||||||
}
|
|
||||||
|
|
||||||
// send event for actions
|
|
||||||
s.bus.Publish("release:push", rlsActionStatus)
|
|
||||||
|
|
||||||
// send separate event for notifications
|
|
||||||
s.bus.Publish("events:release:push", notificationEvent)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) CheckCanDownload(actions []domain.Action) bool {
|
func (s *service) CheckCanDownload(actions []domain.Action) bool {
|
||||||
for _, action := range actions {
|
for _, action := range actions {
|
||||||
if !action.Enabled {
|
if !action.Enabled {
|
||||||
|
|
|
@ -17,7 +17,6 @@ type Service interface {
|
||||||
DeleteByFilterID(ctx context.Context, filterID int) error
|
DeleteByFilterID(ctx context.Context, filterID int) error
|
||||||
ToggleEnabled(actionID int) error
|
ToggleEnabled(actionID int) error
|
||||||
|
|
||||||
RunActions(actions []domain.Action, release domain.Release) error
|
|
||||||
RunAction(action *domain.Action, release domain.Release) ([]string, error)
|
RunAction(action *domain.Action, release domain.Release) ([]string, error)
|
||||||
CheckCanDownload(actions []domain.Action) bool
|
CheckCanDownload(actions []domain.Action) bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ func NewNotificationRepo(log logger.Logger, db *DB) domain.NotificationRepo {
|
||||||
func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
||||||
|
|
||||||
queryBuilder := r.db.squirrel.
|
queryBuilder := r.db.squirrel.
|
||||||
Select("id", "name", "type", "enabled", "events", "webhook", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
|
Select("id", "name", "type", "enabled", "events", "webhook", "token", "channel", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
|
||||||
From("notification").
|
From("notification").
|
||||||
OrderBy("name")
|
OrderBy("name")
|
||||||
|
|
||||||
|
@ -49,18 +49,19 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var n domain.Notification
|
var n domain.Notification
|
||||||
|
|
||||||
var webhook sql.NullString
|
var webhook, token, channel sql.NullString
|
||||||
//var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
//var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
||||||
//if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.CreatedAt, &n.UpdatedAt); err != nil {
|
//if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.CreatedAt, &n.UpdatedAt); err != nil {
|
||||||
//var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
//var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
|
||||||
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
|
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &token, &channel, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
|
||||||
r.log.Error().Stack().Err(err).Msg("notification.find: error scanning row")
|
r.log.Error().Stack().Err(err).Msg("notification.find: error scanning row")
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//n.Token = token.String
|
|
||||||
//n.APIKey = apiKey.String
|
//n.APIKey = apiKey.String
|
||||||
n.Webhook = webhook.String
|
n.Webhook = webhook.String
|
||||||
|
n.Token = token.String
|
||||||
|
n.Channel = channel.String
|
||||||
//n.Title = title.String
|
//n.Title = title.String
|
||||||
//n.Icon = icon.String
|
//n.Icon = icon.String
|
||||||
//n.Host = host.String
|
//n.Host = host.String
|
||||||
|
@ -182,6 +183,8 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
|
||||||
|
|
||||||
func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
||||||
webhook := toNullString(notification.Webhook)
|
webhook := toNullString(notification.Webhook)
|
||||||
|
token := toNullString(notification.Token)
|
||||||
|
channel := toNullString(notification.Channel)
|
||||||
|
|
||||||
queryBuilder := r.db.squirrel.
|
queryBuilder := r.db.squirrel.
|
||||||
Insert("notification").
|
Insert("notification").
|
||||||
|
@ -191,6 +194,8 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
||||||
"enabled",
|
"enabled",
|
||||||
"events",
|
"events",
|
||||||
"webhook",
|
"webhook",
|
||||||
|
"token",
|
||||||
|
"channel",
|
||||||
).
|
).
|
||||||
Values(
|
Values(
|
||||||
notification.Name,
|
notification.Name,
|
||||||
|
@ -198,6 +203,8 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
||||||
notification.Enabled,
|
notification.Enabled,
|
||||||
pq.Array(notification.Events),
|
pq.Array(notification.Events),
|
||||||
webhook,
|
webhook,
|
||||||
|
token,
|
||||||
|
channel,
|
||||||
).
|
).
|
||||||
Suffix("RETURNING id").RunWith(r.db.handler)
|
Suffix("RETURNING id").RunWith(r.db.handler)
|
||||||
|
|
||||||
|
@ -218,6 +225,8 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
||||||
|
|
||||||
func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
|
||||||
webhook := toNullString(notification.Webhook)
|
webhook := toNullString(notification.Webhook)
|
||||||
|
token := toNullString(notification.Token)
|
||||||
|
channel := toNullString(notification.Channel)
|
||||||
|
|
||||||
queryBuilder := r.db.squirrel.
|
queryBuilder := r.db.squirrel.
|
||||||
Update("notification").
|
Update("notification").
|
||||||
|
@ -226,6 +235,8 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
|
||||||
Set("enabled", notification.Enabled).
|
Set("enabled", notification.Enabled).
|
||||||
Set("events", pq.Array(notification.Events)).
|
Set("events", pq.Array(notification.Events)).
|
||||||
Set("webhook", webhook).
|
Set("webhook", webhook).
|
||||||
|
Set("token", token).
|
||||||
|
Set("channel", channel).
|
||||||
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
|
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
|
||||||
Where("id = ?", notification.ID)
|
Where("id = ?", notification.ID)
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
|
|
||||||
type NotificationRepo interface {
|
type NotificationRepo interface {
|
||||||
List(ctx context.Context) ([]Notification, error)
|
List(ctx context.Context) ([]Notification, error)
|
||||||
//FindByType(ctx context.Context, nType NotificationType) ([]Notification, error)
|
|
||||||
Find(ctx context.Context, params NotificationQueryParams) ([]Notification, int, error)
|
Find(ctx context.Context, params NotificationQueryParams) ([]Notification, int, error)
|
||||||
FindByID(ctx context.Context, id int) (*Notification, error)
|
FindByID(ctx context.Context, id int) (*Notification, error)
|
||||||
Store(ctx context.Context, notification Notification) (*Notification, error)
|
Store(ctx context.Context, notification Notification) (*Notification, error)
|
||||||
|
@ -15,6 +14,11 @@ type NotificationRepo interface {
|
||||||
Delete(ctx context.Context, notificationID int) error
|
Delete(ctx context.Context, notificationID int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotificationSender interface {
|
||||||
|
Send(event NotificationEvent, payload NotificationPayload) error
|
||||||
|
CanSend(event NotificationEvent) bool
|
||||||
|
}
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -37,6 +41,25 @@ type Notification struct {
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotificationPayload struct {
|
||||||
|
Subject string
|
||||||
|
Message string
|
||||||
|
Event NotificationEvent
|
||||||
|
ReleaseName string
|
||||||
|
Filter string
|
||||||
|
Indexer string
|
||||||
|
InfoHash string
|
||||||
|
Size uint64
|
||||||
|
Status ReleasePushStatus
|
||||||
|
Action string
|
||||||
|
ActionType ActionType
|
||||||
|
ActionClient string
|
||||||
|
Rejections []string
|
||||||
|
Protocol ReleaseProtocol // torrent
|
||||||
|
Implementation ReleaseImplementation // irc, rss, api
|
||||||
|
Timestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type NotificationType string
|
type NotificationType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -57,8 +80,10 @@ type NotificationEvent string
|
||||||
const (
|
const (
|
||||||
NotificationEventPushApproved NotificationEvent = "PUSH_APPROVED"
|
NotificationEventPushApproved NotificationEvent = "PUSH_APPROVED"
|
||||||
NotificationEventPushRejected NotificationEvent = "PUSH_REJECTED"
|
NotificationEventPushRejected NotificationEvent = "PUSH_REJECTED"
|
||||||
|
NotificationEventPushError NotificationEvent = "PUSH_ERROR"
|
||||||
NotificationEventUpdateAvailable NotificationEvent = "UPDATE_AVAILABLE"
|
NotificationEventUpdateAvailable NotificationEvent = "UPDATE_AVAILABLE"
|
||||||
NotificationEventIRCHealth NotificationEvent = "IRC_HEALTH"
|
NotificationEventIRCHealth NotificationEvent = "IRC_HEALTH"
|
||||||
|
NotificationEventTest NotificationEvent = "TEST"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NotificationEventArr []NotificationEvent
|
type NotificationEventArr []NotificationEvent
|
||||||
|
|
|
@ -34,7 +34,7 @@ func NewSubscribers(log logger.Logger, eventbus EventBus.Bus, notificationSvc no
|
||||||
func (s Subscriber) Register() {
|
func (s Subscriber) Register() {
|
||||||
s.eventbus.Subscribe("release:store-action-status", s.releaseActionStatus)
|
s.eventbus.Subscribe("release:store-action-status", s.releaseActionStatus)
|
||||||
s.eventbus.Subscribe("release:push", s.releasePushStatus)
|
s.eventbus.Subscribe("release:push", s.releasePushStatus)
|
||||||
s.eventbus.Subscribe("events:release:push", s.releasePush)
|
s.eventbus.Subscribe("events:notification", s.sendNotification)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Subscriber) releaseActionStatus(actionStatus *domain.ReleaseActionStatus) {
|
func (s Subscriber) releaseActionStatus(actionStatus *domain.ReleaseActionStatus) {
|
||||||
|
@ -54,10 +54,10 @@ func (s Subscriber) releasePushStatus(actionStatus *domain.ReleaseActionStatus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Subscriber) releasePush(event *domain.EventsReleasePushed) {
|
func (s Subscriber) sendNotification(event *domain.NotificationEvent, payload *domain.NotificationPayload) {
|
||||||
s.log.Trace().Msgf("events: 'events:release:push' '%+v'", event)
|
s.log.Trace().Msgf("events: '%v' '%+v'", event, payload)
|
||||||
|
|
||||||
if err := s.notificationSvc.SendEvent(*event); err != nil {
|
if err := s.notificationSvc.Send(*event, *payload); err != nil {
|
||||||
s.log.Error().Err(err).Msgf("events: 'events:release:push' error sending notification")
|
s.log.Error().Err(err).Msgf("events: '%v' error sending notification", event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ type notificationService interface {
|
||||||
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||||
Update(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
|
Delete(ctx context.Context, id int) error
|
||||||
|
Test(ctx context.Context, notification domain.Notification) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type notificationHandler struct {
|
type notificationHandler struct {
|
||||||
|
@ -34,6 +35,7 @@ func newNotificationHandler(encoder encoder, service notificationService) *notif
|
||||||
func (h notificationHandler) Routes(r chi.Router) {
|
func (h notificationHandler) Routes(r chi.Router) {
|
||||||
r.Get("/", h.list)
|
r.Get("/", h.list)
|
||||||
r.Post("/", h.store)
|
r.Post("/", h.store)
|
||||||
|
r.Post("/test", h.test)
|
||||||
r.Put("/{notificationID}", h.update)
|
r.Put("/{notificationID}", h.update)
|
||||||
r.Delete("/{notificationID}", h.delete)
|
r.Delete("/{notificationID}", h.delete)
|
||||||
}
|
}
|
||||||
|
@ -104,3 +106,24 @@ func (h notificationHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h notificationHandler) test(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var (
|
||||||
|
ctx = r.Context()
|
||||||
|
data domain.Notification
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||||
|
// encode error
|
||||||
|
h.encoder.Error(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := h.service.Test(ctx, data)
|
||||||
|
if err != nil {
|
||||||
|
h.encoder.Error(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.encoder.NoContent(w)
|
||||||
|
}
|
||||||
|
|
|
@ -5,16 +5,18 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
|
"github.com/autobrr/autobrr/internal/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiscordMessage struct {
|
type DiscordMessage struct {
|
||||||
Content interface{} `json:"content"`
|
Content interface{} `json:"content"`
|
||||||
Embeds []DiscordEmbeds `json:"embeds"`
|
Embeds []DiscordEmbeds `json:"embeds,omitempty"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ type DiscordEmbeds struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Color int `json:"color"`
|
Color int `json:"color"`
|
||||||
Fields []DiscordEmbedsFields `json:"fields"`
|
Fields []DiscordEmbedsFields `json:"fields,omitempty"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
type DiscordEmbedsFields struct {
|
type DiscordEmbedsFields struct {
|
||||||
|
@ -31,7 +33,46 @@ type DiscordEmbedsFields struct {
|
||||||
Inline bool `json:"inline,omitempty"`
|
Inline bool `json:"inline,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) discordNotification(event domain.EventsReleasePushed, webhookURL string) {
|
type EmbedColors int
|
||||||
|
|
||||||
|
const (
|
||||||
|
LIGHT_BLUE EmbedColors = 5814783 // 58b9ff
|
||||||
|
RED EmbedColors = 15548997 // ed4245
|
||||||
|
GREEN EmbedColors = 5763719 // 57f287
|
||||||
|
GRAY EmbedColors = 10070709 // 99aab5
|
||||||
|
)
|
||||||
|
|
||||||
|
type discordSender struct {
|
||||||
|
log logger.Logger
|
||||||
|
Settings domain.Notification
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDiscordSender(log logger.Logger, settings domain.Notification) domain.NotificationSender {
|
||||||
|
return &discordSender{log: log, Settings: settings}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *discordSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||||
|
m := DiscordMessage{
|
||||||
|
Content: nil,
|
||||||
|
Embeds: []DiscordEmbeds{a.buildEmbed(event, payload)},
|
||||||
|
Username: "brr",
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
a.log.Error().Err(err).Msgf("discord client could not marshal data: %v", m)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, a.Settings.Webhook, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
//req.Header.Set("User-Agent", "autobrr")
|
||||||
|
|
||||||
t := &http.Transport{
|
t := &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{
|
TLSClientConfig: &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
|
@ -39,107 +80,141 @@ func (s *service) discordNotification(event domain.EventsReleasePushed, webhookU
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{Transport: t, Timeout: 30 * time.Second}
|
client := http.Client{Transport: t, Timeout: 30 * time.Second}
|
||||||
|
|
||||||
color := map[domain.ReleasePushStatus]int{
|
|
||||||
domain.ReleasePushStatusApproved: 5814783,
|
|
||||||
domain.ReleasePushStatusRejected: 5814783,
|
|
||||||
domain.ReleasePushStatusErr: 14026000,
|
|
||||||
}
|
|
||||||
|
|
||||||
m := DiscordMessage{
|
|
||||||
Content: nil,
|
|
||||||
Embeds: []DiscordEmbeds{
|
|
||||||
{
|
|
||||||
Title: event.ReleaseName,
|
|
||||||
Description: "New release!",
|
|
||||||
Color: color[event.Status],
|
|
||||||
Fields: []DiscordEmbedsFields{
|
|
||||||
{
|
|
||||||
Name: "Status",
|
|
||||||
Value: event.Status.String(),
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Indexer",
|
|
||||||
Value: event.Indexer,
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Filter",
|
|
||||||
Value: event.Filter,
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Action",
|
|
||||||
Value: event.Action,
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "Action type",
|
|
||||||
Value: string(event.ActionType),
|
|
||||||
Inline: true,
|
|
||||||
},
|
|
||||||
//{
|
|
||||||
// Name: "Action client",
|
|
||||||
// Value: event.ActionClient,
|
|
||||||
// Inline: true,
|
|
||||||
//},
|
|
||||||
},
|
|
||||||
Timestamp: time.Now(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Username: "brr",
|
|
||||||
}
|
|
||||||
|
|
||||||
if event.ActionClient == "" {
|
|
||||||
rej := DiscordEmbedsFields{
|
|
||||||
Name: "Action client",
|
|
||||||
Value: "n/a",
|
|
||||||
Inline: true,
|
|
||||||
}
|
|
||||||
m.Embeds[0].Fields = append(m.Embeds[0].Fields, rej)
|
|
||||||
} else {
|
|
||||||
rej := DiscordEmbedsFields{
|
|
||||||
Name: "Action client",
|
|
||||||
Value: event.ActionClient,
|
|
||||||
Inline: true,
|
|
||||||
}
|
|
||||||
m.Embeds[0].Fields = append(m.Embeds[0].Fields, rej)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(event.Rejections) > 0 {
|
|
||||||
rej := DiscordEmbedsFields{
|
|
||||||
Name: "Reasons",
|
|
||||||
Value: fmt.Sprintf("```\n%v\n```", strings.Join(event.Rejections, " ,")),
|
|
||||||
Inline: false,
|
|
||||||
}
|
|
||||||
m.Embeds[0].Fields = append(m.Embeds[0].Fields, rej)
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonData, err := json.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Err(err).Msgf("discord client could not marshal data: %v", m)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Err(err).Msgf("discord client request error: %v", event.ReleaseName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
//req.Header.Set("User-Agent", "autobrr")
|
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error().Err(err).Msgf("discord client request error: %v", event.ReleaseName)
|
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
|
||||||
return
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
s.log.Debug().Msg("notification successfully sent to discord")
|
a.log.Trace().Msgf("discord status: %v response: %v", res.StatusCode, string(body))
|
||||||
|
|
||||||
return
|
if res.StatusCode != http.StatusNoContent {
|
||||||
|
a.log.Error().Err(err).Msgf("discord client request error: %v", string(body))
|
||||||
|
return fmt.Errorf("err: %v", string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
a.log.Debug().Msg("notification successfully sent to discord")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *discordSender) CanSend(event domain.NotificationEvent) bool {
|
||||||
|
if a.isEnabled() && a.isEnabledEvent(event) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *discordSender) isEnabled() bool {
|
||||||
|
if a.Settings.Enabled && a.Settings.Webhook != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *discordSender) isEnabledEvent(event domain.NotificationEvent) bool {
|
||||||
|
for _, e := range a.Settings.Events {
|
||||||
|
if e == string(event) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *discordSender) buildEmbed(event domain.NotificationEvent, payload domain.NotificationPayload) DiscordEmbeds {
|
||||||
|
|
||||||
|
color := LIGHT_BLUE
|
||||||
|
switch event {
|
||||||
|
case domain.NotificationEventPushApproved:
|
||||||
|
color = GREEN
|
||||||
|
case domain.NotificationEventPushRejected:
|
||||||
|
color = GRAY
|
||||||
|
case domain.NotificationEventPushError:
|
||||||
|
color = RED
|
||||||
|
case domain.NotificationEventTest:
|
||||||
|
color = LIGHT_BLUE
|
||||||
|
}
|
||||||
|
|
||||||
|
var fields []DiscordEmbedsFields
|
||||||
|
|
||||||
|
if payload.Status != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Status",
|
||||||
|
Value: payload.Status.String(),
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if payload.Indexer != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Indexer",
|
||||||
|
Value: payload.Indexer,
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if payload.Filter != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Filter",
|
||||||
|
Value: payload.Filter,
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if payload.Action != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Action",
|
||||||
|
Value: payload.Action,
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if payload.ActionType != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Action type",
|
||||||
|
Value: string(payload.ActionType),
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if payload.ActionClient != "" {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Action client",
|
||||||
|
Value: payload.ActionClient,
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
if len(payload.Rejections) > 0 {
|
||||||
|
f := DiscordEmbedsFields{
|
||||||
|
Name: "Reasons",
|
||||||
|
Value: fmt.Sprintf("```\n%v\n```", strings.Join(payload.Rejections, ", ")),
|
||||||
|
Inline: false,
|
||||||
|
}
|
||||||
|
fields = append(fields, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
embed := DiscordEmbeds{
|
||||||
|
Title: payload.ReleaseName,
|
||||||
|
Description: "New release!",
|
||||||
|
Color: int(color),
|
||||||
|
Fields: fields,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if payload.Subject != "" && payload.Message != "" {
|
||||||
|
embed.Title = payload.Subject
|
||||||
|
embed.Description = payload.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
return embed
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,9 @@ package notification
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/internal/logger"
|
"github.com/autobrr/autobrr/internal/logger"
|
||||||
|
|
||||||
"github.com/containrrr/shoutrrr"
|
|
||||||
t "github.com/containrrr/shoutrrr/pkg/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
|
@ -17,20 +13,26 @@ type Service interface {
|
||||||
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
Store(ctx context.Context, n domain.Notification) (*domain.Notification, error)
|
||||||
Update(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
|
Delete(ctx context.Context, id int) error
|
||||||
Send(event domain.NotificationEvent, msg string) error
|
Send(event domain.NotificationEvent, payload domain.NotificationPayload) error
|
||||||
SendEvent(event domain.EventsReleasePushed) error
|
Test(ctx context.Context, notification domain.Notification) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
log logger.Logger
|
log logger.Logger
|
||||||
repo domain.NotificationRepo
|
repo domain.NotificationRepo
|
||||||
|
senders []domain.NotificationSender
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
|
func NewService(log logger.Logger, repo domain.NotificationRepo) Service {
|
||||||
return &service{
|
s := &service{
|
||||||
log: log,
|
log: log,
|
||||||
repo: repo,
|
repo: repo,
|
||||||
|
senders: []domain.NotificationSender{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.registerSenders()
|
||||||
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
func (s *service) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
||||||
|
@ -42,109 +44,96 @@ func (s *service) FindByID(ctx context.Context, id int) (*domain.Notification, e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Store(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
func (s *service) Store(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||||
return s.repo.Store(ctx, n)
|
_, err := s.repo.Store(ctx, n)
|
||||||
|
if err != nil {
|
||||||
|
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) {
|
func (s *service) Update(ctx context.Context, n domain.Notification) (*domain.Notification, error) {
|
||||||
return s.repo.Update(ctx, n)
|
_, err := s.repo.Update(ctx, n)
|
||||||
|
if err != nil {
|
||||||
|
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 {
|
func (s *service) Delete(ctx context.Context, id int) error {
|
||||||
return s.repo.Delete(ctx, id)
|
err := s.repo.Delete(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
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 {
|
||||||
|
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.NotificationTypeTelegram:
|
||||||
|
s.senders = append(s.senders, NewTelegramSender(s.log, n))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send notifications
|
// Send notifications
|
||||||
func (s *service) Send(event domain.NotificationEvent, msg string) error {
|
func (s *service) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||||
// find notifications for type X
|
s.log.Debug().Msgf("sending notification for %v", string(event))
|
||||||
|
|
||||||
notifications, err := s.repo.List(context.Background())
|
for _, sender := range s.senders {
|
||||||
if err != nil {
|
// check if sender is active and have notification types
|
||||||
return err
|
if sender.CanSend(event) {
|
||||||
|
sender.Send(event, payload)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var urls []string
|
return nil
|
||||||
|
|
||||||
for _, n := range notifications {
|
|
||||||
if !n.Enabled {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch n.Type {
|
func (s *service) Test(ctx context.Context, notification domain.Notification) error {
|
||||||
|
var agent domain.NotificationSender
|
||||||
|
|
||||||
|
switch notification.Type {
|
||||||
case domain.NotificationTypeDiscord:
|
case domain.NotificationTypeDiscord:
|
||||||
urls = append(urls, fmt.Sprintf("discord://%v@%v", n.Token, n.Webhook))
|
agent = NewDiscordSender(s.log, notification)
|
||||||
default:
|
case domain.NotificationTypeTelegram:
|
||||||
return nil
|
agent = NewTelegramSender(s.log, notification)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(urls) == 0 {
|
return agent.Send(domain.NotificationEventTest, domain.NotificationPayload{
|
||||||
return nil
|
Subject: "Test Notification",
|
||||||
}
|
Message: "autobrr goes brr!!",
|
||||||
|
})
|
||||||
sender, err := shoutrrr.CreateSender(urls...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
p := t.Params{"title": "TEST"}
|
|
||||||
items := []t.MessageItem{
|
|
||||||
{
|
|
||||||
Text: "text hello",
|
|
||||||
Fields: []t.Field{
|
|
||||||
{
|
|
||||||
Key: "eventt",
|
|
||||||
Value: "push?",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
//items = append(items, t.MessageItem{
|
|
||||||
// Text: "text hello",
|
|
||||||
// Fields: []t.Field{
|
|
||||||
// {
|
|
||||||
// Key: "eventt",
|
|
||||||
// Value: "push?",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
//})
|
|
||||||
|
|
||||||
sender.SendItems(items, p)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) SendEvent(event domain.EventsReleasePushed) error {
|
|
||||||
notifications, err := s.repo.List(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.send(notifications, event)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *service) send(notifications []domain.Notification, event domain.EventsReleasePushed) error {
|
|
||||||
// find notifications for type X
|
|
||||||
for _, n := range notifications {
|
|
||||||
if !n.Enabled {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.Events == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, evt := range n.Events {
|
|
||||||
if evt == string(event.Status) {
|
|
||||||
switch n.Type {
|
|
||||||
case domain.NotificationTypeDiscord:
|
|
||||||
go s.discordNotification(event, n.Webhook)
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
147
internal/notification/telegram.go
Normal file
147
internal/notification/telegram.go
Normal file
|
@ -0,0 +1,147 @@
|
||||||
|
package notification
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"html"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
|
"github.com/autobrr/autobrr/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TelegramMessage struct {
|
||||||
|
ChatID string `json:"chat_id"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
ParseMode string `json:"parse_mode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type telegramSender struct {
|
||||||
|
log logger.Logger
|
||||||
|
Settings domain.Notification
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTelegramSender(log logger.Logger, settings domain.Notification) domain.NotificationSender {
|
||||||
|
return &telegramSender{
|
||||||
|
log: log,
|
||||||
|
Settings: settings,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||||
|
m := TelegramMessage{
|
||||||
|
ChatID: s.Settings.Channel,
|
||||||
|
Text: s.buildMessage(event, payload),
|
||||||
|
ParseMode: "HTML",
|
||||||
|
//ParseMode: "MarkdownV2",
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error().Err(err).Msgf("telegram client could not marshal data: %v", m)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage", s.Settings.Token)
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
//req.Header.Set("User-Agent", "autobrr")
|
||||||
|
|
||||||
|
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("telegram client request error: %v", event)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
s.log.Trace().Msgf("telegram status: %v response: %v", res.StatusCode, string(body))
|
||||||
|
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
s.log.Error().Err(err).Msgf("telegram client request error: %v", string(body))
|
||||||
|
return fmt.Errorf("err: %v", string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
s.log.Debug().Msg("notification successfully sent to telegram")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramSender) CanSend(event domain.NotificationEvent) bool {
|
||||||
|
if s.isEnabled() && s.isEnabledEvent(event) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramSender) isEnabled() bool {
|
||||||
|
if s.Settings.Enabled && s.Settings.Token != "" && s.Settings.Channel != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramSender) isEnabledEvent(event domain.NotificationEvent) bool {
|
||||||
|
for _, e := range s.Settings.Events {
|
||||||
|
if e == string(event) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *telegramSender) buildMessage(event domain.NotificationEvent, payload domain.NotificationPayload) string {
|
||||||
|
msg := ""
|
||||||
|
|
||||||
|
if payload.Subject != "" && payload.Message != "" {
|
||||||
|
msg += fmt.Sprintf("%v\n<b>%v</b>", payload.Subject, html.EscapeString(payload.Message))
|
||||||
|
}
|
||||||
|
if payload.ReleaseName != "" {
|
||||||
|
msg += fmt.Sprintf("\n<b>New release:</b> %v", html.EscapeString(payload.ReleaseName))
|
||||||
|
}
|
||||||
|
if payload.Status != "" {
|
||||||
|
msg += fmt.Sprintf("\n<b>Status:</b> %v", payload.Status.String())
|
||||||
|
}
|
||||||
|
if payload.Indexer != "" {
|
||||||
|
msg += fmt.Sprintf("\n<b>Indexer:</b> %v", payload.Indexer)
|
||||||
|
}
|
||||||
|
if payload.Filter != "" {
|
||||||
|
msg += fmt.Sprintf("\n<b>Filter:</b> %v", html.EscapeString(payload.Filter))
|
||||||
|
}
|
||||||
|
if payload.Action != "" {
|
||||||
|
action := fmt.Sprintf("\n<b>Action:</b> %v <b>Type:</b> %v", html.EscapeString(payload.Action), payload.ActionType)
|
||||||
|
if payload.ActionClient != "" {
|
||||||
|
action += fmt.Sprintf(" <b>Client:</b> %v", html.EscapeString(payload.ActionClient))
|
||||||
|
}
|
||||||
|
msg += action
|
||||||
|
}
|
||||||
|
if len(payload.Rejections) > 0 {
|
||||||
|
msg += fmt.Sprintf("\nRejections: %v", strings.Join(payload.Rejections, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg
|
||||||
|
}
|
|
@ -135,7 +135,8 @@ export const APIClient = {
|
||||||
getAll: () => appClient.Get<Notification[]>("api/notification"),
|
getAll: () => appClient.Get<Notification[]>("api/notification"),
|
||||||
create: (notification: Notification) => appClient.Post("api/notification", notification),
|
create: (notification: Notification) => appClient.Post("api/notification", notification),
|
||||||
update: (notification: Notification) => appClient.Put(`api/notification/${notification.id}`, notification),
|
update: (notification: Notification) => appClient.Put(`api/notification/${notification.id}`, notification),
|
||||||
delete: (id: number) => appClient.Delete(`api/notification/${id}`)
|
delete: (id: number) => appClient.Delete(`api/notification/${id}`),
|
||||||
|
test: (n: Notification) => appClient.Post("api/notification/test", n)
|
||||||
},
|
},
|
||||||
release: {
|
release: {
|
||||||
find: (query?: string) => appClient.Get<ReleaseFindResponse>(`api/release${query}`),
|
find: (query?: string) => appClient.Get<ReleaseFindResponse>(`api/release${query}`),
|
||||||
|
|
|
@ -17,6 +17,7 @@ interface SlideOverProps<DataType> {
|
||||||
children?: (values: DataType) => React.ReactNode;
|
children?: (values: DataType) => React.ReactNode;
|
||||||
deleteAction?: () => void;
|
deleteAction?: () => void;
|
||||||
type: "CREATE" | "UPDATE";
|
type: "CREATE" | "UPDATE";
|
||||||
|
testFn?: (data: unknown) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function SlideOver<DataType>({
|
function SlideOver<DataType>({
|
||||||
|
@ -28,11 +29,18 @@ function SlideOver<DataType>({
|
||||||
isOpen,
|
isOpen,
|
||||||
toggle,
|
toggle,
|
||||||
type,
|
type,
|
||||||
children
|
children,
|
||||||
|
testFn
|
||||||
}: SlideOverProps<DataType>): React.ReactElement {
|
}: SlideOverProps<DataType>): React.ReactElement {
|
||||||
const cancelModalButtonRef = useRef<HTMLInputElement | null>(null);
|
const cancelModalButtonRef = useRef<HTMLInputElement | null>(null);
|
||||||
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
|
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
|
||||||
|
|
||||||
|
const test = (values: unknown) => {
|
||||||
|
if (testFn) {
|
||||||
|
testFn(values);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Transition.Root show={isOpen} as={Fragment}>
|
<Transition.Root show={isOpen} as={Fragment}>
|
||||||
<Dialog as="div" static className="fixed inset-0 overflow-hidden" open={isOpen} onClose={toggle}>
|
<Dialog as="div" static className="fixed inset-0 overflow-hidden" open={isOpen} onClose={toggle}>
|
||||||
|
@ -110,6 +118,15 @@ function SlideOver<DataType>({
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<div>
|
<div>
|
||||||
|
{testFn && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="mr-2 bg-white dark:bg-gray-700 py-2 px-4 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-blue-500"
|
||||||
|
onClick={() => test(values)}
|
||||||
|
>
|
||||||
|
Test
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|
|
@ -260,6 +260,10 @@ export const NotificationTypeOptions: OptionBasic[] = [
|
||||||
{
|
{
|
||||||
label: "Discord",
|
label: "Discord",
|
||||||
value: "DISCORD"
|
value: "DISCORD"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Telegram",
|
||||||
|
value: "TELEGRAM"
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import type { FieldProps } from "formik";
|
||||||
import { XIcon } from "@heroicons/react/solid";
|
import { XIcon } from "@heroicons/react/solid";
|
||||||
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
|
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
|
||||||
import {
|
import {
|
||||||
|
PasswordFieldWide,
|
||||||
SwitchGroupWide,
|
SwitchGroupWide,
|
||||||
TextFieldWide
|
TextFieldWide
|
||||||
} from "../../components/inputs";
|
} from "../../components/inputs";
|
||||||
|
@ -59,18 +60,17 @@ const Option = (props: OptionProps) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
function FormFieldsDiscord() {
|
function FormFieldsDiscord() {
|
||||||
return (
|
return (
|
||||||
<div className="border-t border-gray-200 dark:border-gray-700 py-5">
|
<div className="border-t border-gray-200 dark:border-gray-700 py-5">
|
||||||
{/*<div className="px-6 space-y-1">*/}
|
<div className="px-6 space-y-1">
|
||||||
{/* <Dialog.Title className="text-lg font-medium text-gray-900 dark:text-white">Credentials</Dialog.Title>*/}
|
<Dialog.Title className="text-lg font-medium text-gray-900 dark:text-white">Settings</Dialog.Title>
|
||||||
{/* <p className="text-sm text-gray-500 dark:text-gray-400">*/}
|
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||||
{/* Api keys etc*/}
|
Create a <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" rel="noopener noreferrer" target="_blank" className="font-medium text-blue-500">webhook integration</a> in your server.
|
||||||
{/* </p>*/}
|
</p>
|
||||||
{/*</div>*/}
|
</div>
|
||||||
|
|
||||||
<TextFieldWide
|
<PasswordFieldWide
|
||||||
name="webhook"
|
name="webhook"
|
||||||
label="Webhook URL"
|
label="Webhook URL"
|
||||||
help="Discord channel webhook url"
|
help="Discord channel webhook url"
|
||||||
|
@ -80,8 +80,33 @@ function FormFieldsDiscord() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function FormFieldsTelegram() {
|
||||||
|
return (
|
||||||
|
<div className="border-t border-gray-200 dark:border-gray-700 py-5">
|
||||||
|
<div className="px-6 space-y-1">
|
||||||
|
<Dialog.Title className="text-lg font-medium text-gray-900 dark:text-white">Settings</Dialog.Title>
|
||||||
|
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||||
|
Read how to <a href="https://core.telegram.org/bots#3-how-do-i-create-a-bot" rel="noopener noreferrer" target="_blank" className="font-medium text-blue-500">create a bot</a>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<PasswordFieldWide
|
||||||
|
name="token"
|
||||||
|
label="Bot token"
|
||||||
|
help="Bot token"
|
||||||
|
/>
|
||||||
|
<PasswordFieldWide
|
||||||
|
name="channel"
|
||||||
|
label="Chat ID"
|
||||||
|
help="Chat ID"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const componentMap: componentMapType = {
|
const componentMap: componentMapType = {
|
||||||
DISCORD: <FormFieldsDiscord/>
|
DISCORD: <FormFieldsDiscord/>,
|
||||||
|
TELEGRAM: <FormFieldsTelegram/>
|
||||||
};
|
};
|
||||||
|
|
||||||
interface NotificationAddFormValues {
|
interface NotificationAddFormValues {
|
||||||
|
@ -113,6 +138,19 @@ export function NotificationAddForm({ isOpen, toggle }: AddProps) {
|
||||||
mutation.mutate(formData as Notification);
|
mutation.mutate(formData as Notification);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const testMutation = useMutation(
|
||||||
|
(n: Notification) => APIClient.notifications.test(n),
|
||||||
|
{
|
||||||
|
onError: (err) => {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const testNotification = (data: unknown) => {
|
||||||
|
testMutation.mutate(data as Notification);
|
||||||
|
};
|
||||||
|
|
||||||
const validate = (values: NotificationAddFormValues) => {
|
const validate = (values: NotificationAddFormValues) => {
|
||||||
const errors = {} as FormikErrors<FormikValues>;
|
const errors = {} as FormikErrors<FormikValues>;
|
||||||
if (!values.name)
|
if (!values.name)
|
||||||
|
@ -253,6 +291,13 @@ export function NotificationAddForm({ isOpen, toggle }: AddProps) {
|
||||||
|
|
||||||
<div className="flex-shrink-0 px-4 border-t border-gray-200 dark:border-gray-700 py-5 sm:px-6">
|
<div className="flex-shrink-0 px-4 border-t border-gray-200 dark:border-gray-700 py-5 sm:px-6">
|
||||||
<div className="space-x-3 flex justify-end">
|
<div className="space-x-3 flex justify-end">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="bg-white dark:bg-gray-700 py-2 px-4 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-blue-500"
|
||||||
|
onClick={() => testNotification(values)}
|
||||||
|
>
|
||||||
|
Test
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="bg-white dark:bg-gray-700 py-2 px-4 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-blue-500"
|
className="bg-white dark:bg-gray-700 py-2 px-4 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-blue-500"
|
||||||
|
@ -348,12 +393,27 @@ export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateP
|
||||||
deleteMutation.mutate(notification.id);
|
deleteMutation.mutate(notification.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const testMutation = useMutation(
|
||||||
|
(n: Notification) => APIClient.notifications.test(n),
|
||||||
|
{
|
||||||
|
onError: (err) => {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const testNotification = (data: unknown) => {
|
||||||
|
testMutation.mutate(data as Notification);
|
||||||
|
};
|
||||||
|
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
id: notification.id,
|
id: notification.id,
|
||||||
enabled: notification.enabled,
|
enabled: notification.enabled,
|
||||||
type: notification.type,
|
type: notification.type,
|
||||||
name: notification.name,
|
name: notification.name,
|
||||||
webhook: notification.webhook,
|
webhook: notification.webhook,
|
||||||
|
token: notification.token,
|
||||||
|
channel: notification.channel,
|
||||||
events: notification.events || []
|
events: notification.events || []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -366,6 +426,7 @@ export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateP
|
||||||
onSubmit={onSubmit}
|
onSubmit={onSubmit}
|
||||||
deleteAction={deleteAction}
|
deleteAction={deleteAction}
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
|
testFn={testNotification}
|
||||||
>
|
>
|
||||||
{(values) => (
|
{(values) => (
|
||||||
<div>
|
<div>
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { useToggle } from "../../hooks/hooks";
|
||||||
import { NotificationAddForm, NotificationUpdateForm } from "../../forms/settings/NotifiactionForms";
|
import { NotificationAddForm, NotificationUpdateForm } from "../../forms/settings/NotifiactionForms";
|
||||||
import { Switch } from "@headlessui/react";
|
import { Switch } from "@headlessui/react";
|
||||||
import { classNames } from "../../utils";
|
import { classNames } from "../../utils";
|
||||||
|
import { componentMapType } from "../../forms/settings/DownloadClientForms";
|
||||||
|
|
||||||
function NotificationSettings() {
|
function NotificationSettings() {
|
||||||
const [addNotificationsIsOpen, toggleAddNotifications] = useToggle(false);
|
const [addNotificationsIsOpen, toggleAddNotifications] = useToggle(false);
|
||||||
|
@ -16,7 +17,7 @@ function NotificationSettings() {
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="divide-y divide-gray-200 lg:col-span-9">
|
<div className="lg:col-span-9">
|
||||||
<NotificationAddForm isOpen={addNotificationsIsOpen} toggle={toggleAddNotifications} />
|
<NotificationAddForm isOpen={addNotificationsIsOpen} toggle={toggleAddNotifications} />
|
||||||
|
|
||||||
<div className="py-6 px-4 sm:p-6 lg:pb-8">
|
<div className="py-6 px-4 sm:p-6 lg:pb-8">
|
||||||
|
@ -42,10 +43,10 @@ function NotificationSettings() {
|
||||||
<section className="mt-6 light:bg-white dark:bg-gray-800 light:shadow sm:rounded-md">
|
<section className="mt-6 light:bg-white dark:bg-gray-800 light:shadow sm:rounded-md">
|
||||||
<ol className="min-w-full">
|
<ol className="min-w-full">
|
||||||
<li className="grid grid-cols-12 gap-4 border-b border-gray-200 dark:border-gray-700">
|
<li className="grid grid-cols-12 gap-4 border-b border-gray-200 dark:border-gray-700">
|
||||||
<div className="col-span-1 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Enabled</div>
|
<div className="col-span-2 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Enabled</div>
|
||||||
<div className="col-span-2 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Name</div>
|
<div className="col-span-4 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Name</div>
|
||||||
<div className="col-span-2 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Type</div>
|
<div className="col-span-2 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Type</div>
|
||||||
<div className="col-span-4 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Events</div>
|
<div className="col-span-3 px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Events</div>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
{data && data.map((n: Notification) => (
|
{data && data.map((n: Notification) => (
|
||||||
|
@ -59,6 +60,29 @@ function NotificationSettings() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const DiscordIcon = () => (
|
||||||
|
<svg viewBox="0 0 71 71" xmlns="http://www.w3.org/2000/svg" className="mr-2 h-4">
|
||||||
|
<path
|
||||||
|
d="M60.104 12.927a58.55 58.55 0 0 0-14.452-4.482.22.22 0 0 0-.232.11 40.783 40.783 0 0 0-1.8 3.696c-5.457-.817-10.886-.817-16.232 0-.484-1.164-1.2-2.586-1.827-3.696a.228.228 0 0 0-.233-.11 58.39 58.39 0 0 0-14.452 4.482.207.207 0 0 0-.095.082C1.577 26.759-.945 40.174.292 53.42a.244.244 0 0 0 .093.166c6.073 4.46 11.956 7.167 17.729 8.962a.23.23 0 0 0 .249-.082 42.08 42.08 0 0 0 3.627-5.9.225.225 0 0 0-.123-.312 38.772 38.772 0 0 1-5.539-2.64.228.228 0 0 1-.022-.377c.372-.28.744-.57 1.1-.862a.22.22 0 0 1 .23-.031c11.62 5.305 24.198 5.305 35.681 0a.219.219 0 0 1 .232.028c.356.293.728.586 1.103.865a.228.228 0 0 1-.02.377 36.384 36.384 0 0 1-5.54 2.637.227.227 0 0 0-.12.316 47.249 47.249 0 0 0 3.623 5.897.225.225 0 0 0 .25.084c5.8-1.795 11.683-4.502 17.756-8.962a.228.228 0 0 0 .093-.163c1.48-15.315-2.48-28.618-10.498-40.412a.18.18 0 0 0-.093-.085zM23.725 45.355c-3.498 0-6.38-3.212-6.38-7.156s2.826-7.156 6.38-7.156c3.582 0 6.437 3.24 6.38 7.156 0 3.944-2.826 7.156-6.38 7.156zm23.592 0c-3.498 0-6.38-3.212-6.38-7.156s2.826-7.156 6.38-7.156c3.582 0 6.437 3.24 6.38 7.156 0 3.944-2.798 7.156-6.38 7.156z"
|
||||||
|
fill="currentColor"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
const TelegramIcon = () => (
|
||||||
|
<svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg" className="mr-2 h-4">
|
||||||
|
<path
|
||||||
|
d="M0 24c0 13.255 10.745 24 24 24s24-10.745 24-24S37.255 0 24 0 0 10.745 0 24zm19.6 11 .408-6.118 11.129-10.043c.488-.433-.107-.645-.755-.252l-13.735 8.665-5.933-1.851c-1.28-.393-1.29-1.273.288-1.906l23.118-8.914c1.056-.48 2.075.254 1.672 1.87l-3.937 18.553c-.275 1.318-1.072 1.633-2.175 1.024l-5.998-4.43L20.8 34.4l-.027.027c-.323.314-.59.573-1.173.573z"
|
||||||
|
clipRule="evenodd" fill="currentColor" fillRule="evenodd"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
const iconComponentMap: componentMapType = {
|
||||||
|
DISCORD: <span className="flex items-center px-2 py-0.5 rounded bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-400"><DiscordIcon /> Discord</span>,
|
||||||
|
TELEGRAM: <span className="flex items-center px-2 py-0.5 rounded bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-400"><TelegramIcon /> Telegram</span>
|
||||||
|
};
|
||||||
|
|
||||||
interface ListItemProps {
|
interface ListItemProps {
|
||||||
notification: Notification;
|
notification: Notification;
|
||||||
}
|
}
|
||||||
|
@ -70,8 +94,8 @@ function ListItem({ notification }: ListItemProps) {
|
||||||
<li key={notification.id} className="text-gray-500 dark:text-gray-400">
|
<li key={notification.id} className="text-gray-500 dark:text-gray-400">
|
||||||
<NotificationUpdateForm isOpen={updateFormIsOpen} toggle={toggleUpdateForm} notification={notification} />
|
<NotificationUpdateForm isOpen={updateFormIsOpen} toggle={toggleUpdateForm} notification={notification} />
|
||||||
|
|
||||||
<div className="grid grid-cols-12 gap-4 items-center py-4">
|
<div className="grid grid-cols-12 gap-4 items-center py-3">
|
||||||
<div className="col-span-1 flex items-center sm:px-6 ">
|
<div className="col-span-2 flex items-center sm:px-6">
|
||||||
<Switch
|
<Switch
|
||||||
checked={notification.enabled}
|
checked={notification.enabled}
|
||||||
onChange={toggleUpdateForm}
|
onChange={toggleUpdateForm}
|
||||||
|
@ -90,23 +114,21 @@ function ListItem({ notification }: ListItemProps) {
|
||||||
/>
|
/>
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
<div className="col-span-2 flex items-center sm:px-6 ">
|
<div className="col-span-4 flex items-center sm:px-6">
|
||||||
{notification.name}
|
{notification.name}
|
||||||
</div>
|
</div>
|
||||||
<div className="col-span-2 flex items-center sm:px-6">
|
<div className="col-span-2 flex items-center sm:px-6">
|
||||||
{notification.type}
|
{iconComponentMap[notification.type]}
|
||||||
</div>
|
</div>
|
||||||
<div className="col-span-5 flex items-center sm:px-6 ">
|
<div className="col-span-3 flex items-center sm:px-6">
|
||||||
{notification.events.map((n, idx) => (
|
|
||||||
<span
|
<span
|
||||||
key={idx}
|
className="mr-2 inline-flex items-center px-2.5 py-1 rounded-md text-sm font-medium bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-400"
|
||||||
className="mr-2 inline-flex items-center px-2.5 py-0.5 rounded-md text-sm font-medium bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-400"
|
title={notification.events.join(", ")}
|
||||||
>
|
>
|
||||||
{n}
|
{notification.events.length}
|
||||||
</span>
|
</span>
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
<div className="col-span-1 flex items-center sm:px-6 ">
|
<div className="col-span-1 flex items-center">
|
||||||
<span className="text-indigo-600 dark:text-gray-300 hover:text-indigo-900 cursor-pointer" onClick={toggleUpdateForm}>
|
<span className="text-indigo-600 dark:text-gray-300 hover:text-indigo-900 cursor-pointer" onClick={toggleUpdateForm}>
|
||||||
Edit
|
Edit
|
||||||
</span>
|
</span>
|
||||||
|
|
6
web/src/types/Notification.d.ts
vendored
6
web/src/types/Notification.d.ts
vendored
|
@ -1,4 +1,4 @@
|
||||||
type NotificationType = "DISCORD";
|
type NotificationType = "DISCORD" | "TELEGRAM";
|
||||||
|
|
||||||
interface Notification {
|
interface Notification {
|
||||||
id: number;
|
id: number;
|
||||||
|
@ -6,5 +6,7 @@ interface Notification {
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
type: NotificationType;
|
type: NotificationType;
|
||||||
events: string[];
|
events: string[];
|
||||||
webhook: string;
|
webhook?: string;
|
||||||
|
token?: string;
|
||||||
|
channel?: string;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue