From ec79eafe4336ae6e46457c14bb98a9ddc361be11 Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:07:58 +0200 Subject: [PATCH] feat(notifications): optional Telegram sender (#1726) refactor(notifications): optional Telegram sender --- internal/database/notification.go | 12 +++++++++--- internal/notification/telegram.go | 2 +- web/src/forms/settings/NotificationForms.tsx | 13 +++++++++++-- web/src/types/Notification.d.ts | 1 + 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/internal/database/notification.go b/internal/database/notification.go index 3891355..bbe9e96 100644 --- a/internal/database/notification.go +++ b/internal/database/notification.go @@ -30,7 +30,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", "host", "created_at", "updated_at", "COUNT(*) OVER() AS total_count"). + Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "host", "username", "created_at", "updated_at", "COUNT(*) OVER() AS total_count"). From("notification"). OrderBy("name") @@ -51,9 +51,9 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ for rows.Next() { var n domain.Notification - var webhook, token, apiKey, channel, host, topic sql.NullString + var webhook, token, apiKey, channel, host, topic, username 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, &host, &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, &username, &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.Channel = channel.String n.Topic = topic.String n.Host = host.String + n.Username = username.String notifications = append(notifications, n) } @@ -185,6 +186,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi channel := toNullString(notification.Channel) topic := toNullString(notification.Topic) host := toNullString(notification.Host) + username := toNullString(notification.Username) queryBuilder := r.db.squirrel. Insert("notification"). @@ -200,6 +202,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi "priority", "topic", "host", + "username", ). Values( notification.Name, @@ -213,6 +216,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi notification.Priority, topic, host, + username, ). Suffix("RETURNING id").RunWith(r.db.handler) @@ -236,6 +240,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif channel := toNullString(notification.Channel) topic := toNullString(notification.Topic) host := toNullString(notification.Host) + username := toNullString(notification.Username) queryBuilder := r.db.squirrel. Update("notification"). @@ -250,6 +255,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif Set("priority", notification.Priority). Set("topic", topic). Set("host", host). + Set("username", username). Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")). Where(sq.Eq{"id": notification.ID}) diff --git a/internal/notification/telegram.go b/internal/notification/telegram.go index f60958a..ebd6558 100644 --- a/internal/notification/telegram.go +++ b/internal/notification/telegram.go @@ -64,7 +64,7 @@ func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain. func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error { - payload.Sender = "autobrr" + payload.Sender = s.Settings.Username message := s.builder.BuildBody(payload) m := TelegramMessage{ diff --git a/web/src/forms/settings/NotificationForms.tsx b/web/src/forms/settings/NotificationForms.tsx index 1e4eccc..7ca2d34 100644 --- a/web/src/forms/settings/NotificationForms.tsx +++ b/web/src/forms/settings/NotificationForms.tsx @@ -146,6 +146,12 @@ function FormFieldsTelegram() { help="Reverse proxy domain for api.telegram.org, only needs to be specified if the network you are using has blocked the Telegram API." placeholder="http(s)://ip:port" /> + ); } @@ -373,7 +379,8 @@ export function NotificationAddForm({ isOpen, toggle }: AddProps) { type: "", name: "", webhook: "", - events: [] + events: [], + username: "" }} onSubmit={onSubmit} validate={validate} @@ -577,6 +584,7 @@ interface InitialValues { topic?: string; host?: string; events: NotificationEvent[]; + username?: string } export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateProps) { @@ -626,7 +634,8 @@ export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateP channel: notification.channel, topic: notification.topic, host: notification.host, - events: notification.events || [] + events: notification.events || [], + username: notification.username }; return ( diff --git a/web/src/types/Notification.d.ts b/web/src/types/Notification.d.ts index 608c456..faa86cf 100644 --- a/web/src/types/Notification.d.ts +++ b/web/src/types/Notification.d.ts @@ -25,4 +25,5 @@ interface ServiceNotification { priority?: number; topic?: string; host?: string; + username?: string; }