feat(notifications): add Pushover (#598)

* feat(notifications): add pushover

* add db migration

* fix lint error

* some small corrections

* fixed README

* added missing columns to postgres_migrate.go

* use token for user_key

* refactor(notifications): change priority to int

* fix: only test selected events

---------

Co-authored-by: soup <soup@r4tio.dev>
Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
Nelson Pecora 2023-04-29 11:07:15 -04:00 committed by GitHub
parent 1b8f2fce3c
commit da5492febb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 290 additions and 37 deletions

View file

@ -28,7 +28,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", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
From("notification").
OrderBy("name")
@ -50,10 +50,8 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
var n domain.Notification
var webhook, token, apiKey, channel 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 {
//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, &token, &apiKey, &channel, &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, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
return nil, 0, errors.Wrap(err, "error scanning row")
}
@ -61,14 +59,6 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
n.Webhook = webhook.String
n.Token = token.String
n.Channel = channel.String
//n.Title = title.String
//n.Icon = icon.String
//n.Host = host.String
//n.Username = username.String
//n.Password = password.String
//n.Channel = channel.String
//n.Targets = targets.String
//n.Devices = devices.String
notifications = append(notifications, n)
}
@ -81,7 +71,7 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, error) {
rows, err := r.db.handler.QueryContext(ctx, "SELECT id, name, type, enabled, events, token, api_key, webhook, title, icon, host, username, password, channel, targets, devices, created_at, updated_at FROM notification ORDER BY name ASC")
rows, err := r.db.handler.QueryContext(ctx, "SELECT id, name, type, enabled, events, token, api_key, webhook, title, icon, host, username, password, channel, targets, devices, priority,created_at, updated_at FROM notification ORDER BY name ASC")
if err != nil {
return nil, errors.Wrap(err, "error executing query")
}
@ -94,7 +84,7 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
//var eventsSlice []string
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.Priority, &n.CreatedAt, &n.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "error scanning row")
}
@ -140,6 +130,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
"channel",
"targets",
"devices",
"priority",
"created_at",
"updated_at",
).
@ -151,7 +142,6 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
return nil, errors.Wrap(err, "error building query")
}
//row := r.db.handler.QueryRowContext(ctx, "SELECT id, name, type, enabled, events, token, api_key, webhook, title, icon, host, username, password, channel, targets, devices, created_at, updated_at FROM notification WHERE id = ?", id)
row := r.db.handler.QueryRowContext(ctx, query, args...)
if err := row.Err(); err != nil {
return nil, errors.Wrap(err, "error executing query")
@ -160,7 +150,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
var n domain.Notification
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices sql.NullString
if err := row.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 := row.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.Priority, &n.CreatedAt, &n.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "error scanning row")
}
@ -196,6 +186,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
"token",
"api_key",
"channel",
"priority",
).
Values(
notification.Name,
@ -206,14 +197,14 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
token,
apiKey,
channel,
notification.Priority,
).
Suffix("RETURNING id").RunWith(r.db.handler)
// return values
var retID int64
err := queryBuilder.QueryRowContext(ctx).Scan(&retID)
if err != nil {
if err := queryBuilder.QueryRowContext(ctx).Scan(&retID); err != nil {
return nil, errors.Wrap(err, "error executing query")
}
@ -239,6 +230,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
Set("token", token).
Set("api_key", apiKey).
Set("channel", channel).
Set("priority", notification.Priority).
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
Where(sq.Eq{"id": notification.ID})
@ -247,8 +239,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
return nil, errors.Wrap(err, "error building query")
}
_, err = r.db.handler.ExecContext(ctx, query, args...)
if err != nil {
if _, err = r.db.handler.ExecContext(ctx, query, args...); err != nil {
return nil, errors.Wrap(err, "error executing query")
}
@ -267,8 +258,7 @@ func (r *NotificationRepo) Delete(ctx context.Context, notificationID int) error
return errors.Wrap(err, "error building query")
}
_, err = r.db.handler.ExecContext(ctx, query, args...)
if err != nil {
if _, err = r.db.handler.ExecContext(ctx, query, args...); err != nil {
return errors.Wrap(err, "error executing query")
}