feat(notification): Telegram add support for topics in groups (#894)

* feat(notification): send Telegram messages to a specific topic of a group

* Convert settings.Topic to integer once and reuse it as part of the
telegramSender struct.

* feat(notifications): add migrations for topic

* fix(notifications): find null string

* fix(notifications): form initial values

---------

Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
Yuchen Ying 2023-05-07 08:30:07 -07:00 committed by GitHub
parent e5692fefc7
commit fdc957c571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 16 deletions

View file

@ -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", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "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 sql.NullString
var webhook, token, apiKey, channel, 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, &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, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
return nil, 0, errors.Wrap(err, "error scanning row")
}
@ -62,6 +62,7 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
n.Webhook = webhook.String
n.Token = token.String
n.Channel = channel.String
n.Topic = topic.String
notifications = append(notifications, n)
}
@ -74,7 +75,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, priority,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, topic, created_at, updated_at FROM notification ORDER BY name ASC")
if err != nil {
return nil, errors.Wrap(err, "error executing query")
}
@ -86,8 +87,8 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
var n domain.Notification
//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.Priority, &n.CreatedAt, &n.UpdatedAt); err != nil {
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic 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.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "error scanning row")
}
@ -103,6 +104,7 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
n.Channel = channel.String
n.Targets = targets.String
n.Devices = devices.String
n.Topic = topic.String
notifications = append(notifications, n)
}
@ -134,6 +136,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
"targets",
"devices",
"priority",
"topic",
"created_at",
"updated_at",
).
@ -152,8 +155,8 @@ 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.Priority, &n.CreatedAt, &n.UpdatedAt); err != nil {
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic 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.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "error scanning row")
}
@ -168,6 +171,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
n.Channel = channel.String
n.Targets = targets.String
n.Devices = devices.String
n.Topic = topic.String
return &n, nil
}
@ -177,6 +181,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
token := toNullString(notification.Token)
apiKey := toNullString(notification.APIKey)
channel := toNullString(notification.Channel)
topic := toNullString(notification.Topic)
queryBuilder := r.db.squirrel.
Insert("notification").
@ -190,6 +195,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
"api_key",
"channel",
"priority",
"topic",
).
Values(
notification.Name,
@ -201,6 +207,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
apiKey,
channel,
notification.Priority,
topic,
).
Suffix("RETURNING id").RunWith(r.db.handler)
@ -222,6 +229,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
token := toNullString(notification.Token)
apiKey := toNullString(notification.APIKey)
channel := toNullString(notification.Channel)
topic := toNullString(notification.Topic)
queryBuilder := r.db.squirrel.
Update("notification").
@ -234,6 +242,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notif
Set("api_key", apiKey).
Set("channel", channel).
Set("priority", notification.Priority).
Set("topic", topic).
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
Where(sq.Eq{"id": notification.ID})

View file

@ -303,6 +303,7 @@ CREATE TABLE notification
rooms TEXT,
targets TEXT,
devices TEXT,
topic TEXT,
priority INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
@ -677,4 +678,6 @@ ADD COLUMN download_url TEXT;
`,
`ALTER TABLE notification
ADD COLUMN priority INTEGER DEFAULT 0;`,
`ALTER TABLE notification
ADD COLUMN topic text;`,
}

View file

@ -295,6 +295,7 @@ CREATE TABLE notification
rooms TEXT,
targets TEXT,
devices TEXT,
topic TEXT,
priority INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
@ -1070,4 +1071,6 @@ ADD COLUMN download_url TEXT;
`,
`ALTER TABLE notification
ADD COLUMN priority INTEGER DEFAULT 0;`,
`ALTER TABLE notification
ADD COLUMN topic text;`,
}