mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(notifications): Add Gotify (#1180)
feat(notifications): Add Gotify (resolves #513)
This commit is contained in:
parent
525861074b
commit
77948d7654
8 changed files with 233 additions and 6 deletions
|
@ -31,7 +31,7 @@ func NewNotificationRepo(log logger.Logger, db *DB) domain.NotificationRepo {
|
|||
func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
|
||||
Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "host", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
|
||||
From("notification").
|
||||
OrderBy("name")
|
||||
|
||||
|
@ -52,9 +52,9 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
for rows.Next() {
|
||||
var n domain.Notification
|
||||
|
||||
var webhook, token, apiKey, channel, topic sql.NullString
|
||||
var webhook, token, apiKey, channel, host, topic sql.NullString
|
||||
|
||||
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &token, &apiKey, &channel, &n.Priority, &topic, &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, &apiKey, &channel, &n.Priority, &topic, &host, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
|
||||
return nil, 0, errors.Wrap(err, "error scanning row")
|
||||
}
|
||||
|
||||
|
@ -63,6 +63,7 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
|
|||
n.Token = token.String
|
||||
n.Channel = channel.String
|
||||
n.Topic = topic.String
|
||||
n.Host = host.String
|
||||
|
||||
notifications = append(notifications, n)
|
||||
}
|
||||
|
@ -182,6 +183,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
|||
apiKey := toNullString(notification.APIKey)
|
||||
channel := toNullString(notification.Channel)
|
||||
topic := toNullString(notification.Topic)
|
||||
host := toNullString(notification.Host)
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
Insert("notification").
|
||||
|
@ -196,6 +198,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
|||
"channel",
|
||||
"priority",
|
||||
"topic",
|
||||
"host",
|
||||
).
|
||||
Values(
|
||||
notification.Name,
|
||||
|
@ -208,6 +211,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
|
|||
channel,
|
||||
notification.Priority,
|
||||
topic,
|
||||
host,
|
||||
).
|
||||
Suffix("RETURNING id").RunWith(r.db.handler)
|
||||
|
||||
|
@ -230,6 +234,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
|
|||
apiKey := toNullString(notification.APIKey)
|
||||
channel := toNullString(notification.Channel)
|
||||
topic := toNullString(notification.Topic)
|
||||
host := toNullString(notification.Host)
|
||||
|
||||
queryBuilder := r.db.squirrel.
|
||||
Update("notification").
|
||||
|
@ -243,6 +248,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
|
|||
Set("channel", channel).
|
||||
Set("priority", notification.Priority).
|
||||
Set("topic", topic).
|
||||
Set("host", host).
|
||||
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
|
||||
Where(sq.Eq{"id": notification.ID})
|
||||
|
||||
|
|
|
@ -79,6 +79,7 @@ const (
|
|||
NotificationTypeRocketChat NotificationType = "ROCKETCHAT"
|
||||
NotificationTypeSlack NotificationType = "SLACK"
|
||||
NotificationTypeTelegram NotificationType = "TELEGRAM"
|
||||
NotificationTypeGotify NotificationType = "GOTIFY"
|
||||
)
|
||||
|
||||
type NotificationEvent string
|
||||
|
|
176
internal/notification/gotify.go
Normal file
176
internal/notification/gotify.go
Normal file
|
@ -0,0 +1,176 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package notification
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type gotifyMessage struct {
|
||||
Message string `json:"message"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
type gotifySender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
}
|
||||
|
||||
func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
return &gotifySender{
|
||||
log: log.With().Str("sender", "gotify").Logger(),
|
||||
Settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *gotifySender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := gotifyMessage{
|
||||
Message: s.buildMessage(payload),
|
||||
Title: s.buildTitle(event),
|
||||
}
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("message", m.Message)
|
||||
data.Set("title", m.Title)
|
||||
|
||||
url := fmt.Sprintf("%v/message?token=%v", s.Settings.Host, s.Settings.Token);
|
||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("gotify client request error: %v", event)
|
||||
return errors.Wrap(err, "could not create request")
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("gotify 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("gotify client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
s.log.Trace().Msgf("gotify status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
s.log.Error().Err(err).Msgf("gotify 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 gotify")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *gotifySender) CanSend(event domain.NotificationEvent) bool {
|
||||
if s.isEnabled() && s.isEnabledEvent(event) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *gotifySender) isEnabled() bool {
|
||||
if s.Settings.Enabled {
|
||||
if s.Settings.Host == "" {
|
||||
s.log.Warn().Msg("gotify missing host")
|
||||
return false
|
||||
}
|
||||
|
||||
if s.Settings.Token == "" {
|
||||
s.log.Warn().Msg("gotify missing application token")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *gotifySender) isEnabledEvent(event domain.NotificationEvent) bool {
|
||||
for _, e := range s.Settings.Events {
|
||||
if e == string(event) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *gotifySender) buildMessage(payload domain.NotificationPayload) string {
|
||||
msg := ""
|
||||
|
||||
if payload.Subject != "" && payload.Message != "" {
|
||||
msg += fmt.Sprintf("%v\n%v", payload.Subject, payload.Message)
|
||||
}
|
||||
if payload.ReleaseName != "" {
|
||||
msg += fmt.Sprintf("\nNew release: %v", payload.ReleaseName)
|
||||
}
|
||||
if payload.Size > 0 {
|
||||
msg += fmt.Sprintf("\nSize: %v", humanize.Bytes(payload.Size))
|
||||
}
|
||||
if payload.Status != "" {
|
||||
msg += fmt.Sprintf("\nStatus: %v", payload.Status.String())
|
||||
}
|
||||
if payload.Indexer != "" {
|
||||
msg += fmt.Sprintf("\nIndexer: %v", payload.Indexer)
|
||||
}
|
||||
if payload.Filter != "" {
|
||||
msg += fmt.Sprintf("\nFilter: %v", payload.Filter)
|
||||
}
|
||||
if payload.Action != "" {
|
||||
action := fmt.Sprintf("\nAction: %v Type: %v", payload.Action, payload.ActionType)
|
||||
if payload.ActionClient != "" {
|
||||
action += fmt.Sprintf(" Client: %v", payload.ActionClient)
|
||||
}
|
||||
msg += action
|
||||
}
|
||||
if len(payload.Rejections) > 0 {
|
||||
msg += fmt.Sprintf("\nRejections: %v", strings.Join(payload.Rejections, ", "))
|
||||
}
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
func (s *gotifySender) buildTitle(event domain.NotificationEvent) string {
|
||||
title := ""
|
||||
|
||||
switch event {
|
||||
case domain.NotificationEventAppUpdateAvailable:
|
||||
title = "Autobrr update available"
|
||||
case domain.NotificationEventPushApproved:
|
||||
title = "Push Approved"
|
||||
case domain.NotificationEventPushRejected:
|
||||
title = "Push Rejected"
|
||||
case domain.NotificationEventPushError:
|
||||
title = "Error"
|
||||
case domain.NotificationEventIRCDisconnected:
|
||||
title = "IRC Disconnected"
|
||||
case domain.NotificationEventIRCReconnected:
|
||||
title = "IRC Reconnected"
|
||||
case domain.NotificationEventTest:
|
||||
title = "Test"
|
||||
}
|
||||
|
||||
return title
|
||||
}
|
|
@ -130,6 +130,8 @@ func (s *service) registerSenders() {
|
|||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -243,6 +245,8 @@ func (s *service) Test(ctx context.Context, notification domain.Notification) er
|
|||
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")
|
||||
|
|
|
@ -395,6 +395,10 @@ export const NotificationTypeOptions: OptionBasicTyped<NotificationType>[] = [
|
|||
{
|
||||
label: "Pushover",
|
||||
value: "PUSHOVER"
|
||||
},
|
||||
{
|
||||
label: "Gotify",
|
||||
value: "GOTIFY"
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
@ -182,11 +182,36 @@ function FormFieldsPushover() {
|
|||
);
|
||||
}
|
||||
|
||||
function FormFieldsGotify() {
|
||||
return (
|
||||
<div className="border-t border-gray-200 dark:border-gray-700 py-4">
|
||||
<div className="px-4 space-y-1">
|
||||
<Dialog.Title className="text-lg font-medium text-gray-900 dark:text-white">Settings</Dialog.Title>
|
||||
</div>
|
||||
|
||||
<TextFieldWide
|
||||
name="host"
|
||||
label="Gotify URL"
|
||||
help="Gotify URL"
|
||||
placeholder="https://some.gotify.server.com"
|
||||
required={true}
|
||||
/>
|
||||
<PasswordFieldWide
|
||||
name="token"
|
||||
label="Application Token"
|
||||
help="Application Token"
|
||||
required={true}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const componentMap: componentMapType = {
|
||||
DISCORD: <FormFieldsDiscord />,
|
||||
NOTIFIARR: <FormFieldsNotifiarr />,
|
||||
TELEGRAM: <FormFieldsTelegram />,
|
||||
PUSHOVER: <FormFieldsPushover />
|
||||
PUSHOVER: <FormFieldsPushover />,
|
||||
GOTIFY: <FormFieldsGotify />
|
||||
};
|
||||
|
||||
interface NotificationAddFormValues {
|
||||
|
@ -464,6 +489,7 @@ interface InitialValues {
|
|||
priority?: number;
|
||||
channel?: string;
|
||||
topic?: string;
|
||||
host?: string;
|
||||
events: NotificationEvent[];
|
||||
}
|
||||
|
||||
|
@ -513,6 +539,7 @@ export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateP
|
|||
priority: notification.priority,
|
||||
channel: notification.channel,
|
||||
topic: notification.topic,
|
||||
host: notification.host,
|
||||
events: notification.events || []
|
||||
};
|
||||
|
||||
|
|
|
@ -99,12 +99,20 @@ const PushoverIcon = () => (
|
|||
</svg>
|
||||
);
|
||||
|
||||
const GotifyIcon = () => (
|
||||
<svg viewBox="0 0 140 140" xmlns="http://www.w3.org/2000/svg" className="mr-2 h-4">
|
||||
<path d="m 114.5,21.4 c -11.7,0 -47.3,5.9 -54.3,7.1 -47.3,8.0 -48.4,9.9 -50.1,12.8 -1.2,2.1 -2.4,4.0 2.6,29.4 2.3,11.5 5.8,26.9 8.8,35.8 1.8,5.4 3.6,8.8 6.9,10.1 0.8,0.3 1.7,0.5 2.7,0.6 0.2,0.0 0.3,0.0 0.5,0.0 12.8,0 89.1,-19.5 89.9,-19.7 1.4,-0.4 4.0,-1.5 5.3,-5.1 1.8,-4.7 1.9,-16.7 0.5,-35.7 -2.1,-28.0 -4.1,-31.0 -4.8,-32.0 -2.0,-3.1 -5.6,-3.3 -6.7,-3.3 -0.4,-0.0 -0.9,-0.0 -1.4,-0.0 z m -1.9,6.6 c -9.3,12.0 -18.9,24.0 -25.9,32.4 -2.3,2.8 -4.3,5.1 -6.0,7.0 -1.7,1.9 -2.9,3.2 -3.8,4.0 l -0.3,0.3 -0.4,-0.1 c -1.0,-0.3 -2.5,-0.9 -4.4,-1.7 -2.3,-1.0 -5.2,-2.3 -8.8,-3.9 C 51.6,60.7 34.4,52.2 18.0,43.6 30.3,39.7 95.0,28.7 112.6,27.9 Z m 5.7,5.0 c 2.0,11.8 4.5,42.6 3.1,54.0 -1.8,-1.4 -10.1,-8.0 -19.8,-15.2 -3.0,-2.3 -5.9,-4.3 -8.4,-6.1 l -0.7,-0.5 0.5,-0.6 C 99.5,56.9 108.0,46.2 118.3,32.9 Z M 16.1,51.1 c 3.0,1.5 14.3,7.4 27.4,13.8 5.3,2.6 9.9,4.8 13.9,6.7 l 0.9,0.4 -0.7,0.8 C 50.3,81.2 40.6,92.8 28.8,107.2 24.5,96.7 17.9,65.0 16.1,51.1 Z m 71.5,19.7 0.6,0.4 c 7.8,5.5 18.1,13.2 27.9,21.0 C 104.9,95.1 53.2,107.9 36.0,110.3 46.6,97.4 57.3,84.7 65.1,75.8 l 0.4,-0.4 0.5,0.2 c 5.7,2.5 9.3,3.7 11.1,3.8 0.1,0.0 0.2,0.0 0.3,0.0 0.6,0 1.0,-0.1 1.4,-0.3 0.6,-0.2 2.0,-0.7 8.3,-7.7 z"
|
||||
clipRule="evenodd" fill="currentColor" fillRule="evenodd"/>
|
||||
</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>,
|
||||
NOTIFIARR: <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 /> Notifiarr</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>,
|
||||
PUSHOVER: <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"><PushoverIcon /> Pushover</span>
|
||||
PUSHOVER: <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"><PushoverIcon /> Pushover</span>,
|
||||
GOTIFY: <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"><GotifyIcon /> Gotify</span>
|
||||
};
|
||||
|
||||
interface ListItemProps {
|
||||
|
|
3
web/src/types/Notification.d.ts
vendored
3
web/src/types/Notification.d.ts
vendored
|
@ -3,7 +3,7 @@
|
|||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
type NotificationType = "DISCORD" | "NOTIFIARR" | "TELEGRAM" | "PUSHOVER";
|
||||
type NotificationType = "DISCORD" | "NOTIFIARR" | "TELEGRAM" | "PUSHOVER" | "GOTIFY";
|
||||
type NotificationEvent =
|
||||
"PUSH_APPROVED"
|
||||
| "PUSH_REJECTED"
|
||||
|
@ -24,4 +24,5 @@ interface ServiceNotification {
|
|||
channel?: string;
|
||||
priority?: number;
|
||||
topic?: string;
|
||||
host?: string;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue