2022-03-18 23:33:23 +03:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/core/data"
|
|
|
|
"github.com/owncast/owncast/db"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createNotificationsTable(db *sql.DB) {
|
|
|
|
log.Traceln("Creating federation followers table...")
|
|
|
|
|
|
|
|
createTableSQL := `CREATE TABLE IF NOT EXISTS notifications (
|
|
|
|
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
|
|
"channel" TEXT NOT NULL,
|
|
|
|
"destination" TEXT NOT NULL,
|
2022-08-03 08:45:15 +03:00
|
|
|
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
|
2022-03-18 23:33:23 +03:00
|
|
|
|
2022-08-03 08:45:15 +03:00
|
|
|
data.MustExec(createTableSQL, db)
|
|
|
|
data.MustExec(`CREATE INDEX IF NOT EXISTS idx_channel ON notifications (channel);`, db)
|
2022-03-18 23:33:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddNotification saves a new user notification destination.
|
|
|
|
func AddNotification(channel, destination string) error {
|
|
|
|
return data.GetDatastore().GetQueries().AddNotification(context.Background(), db.AddNotificationParams{
|
|
|
|
Channel: channel,
|
|
|
|
Destination: destination,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-27 23:10:42 +03:00
|
|
|
// RemoveNotificationForChannel removes a notification destination.
|
2022-03-18 23:33:23 +03:00
|
|
|
func RemoveNotificationForChannel(channel, destination string) error {
|
2022-12-27 23:10:42 +03:00
|
|
|
log.Debugln("Removing notification for channel", channel)
|
2022-03-18 23:33:23 +03:00
|
|
|
return data.GetDatastore().GetQueries().RemoveNotificationDestinationForChannel(context.Background(), db.RemoveNotificationDestinationForChannelParams{
|
|
|
|
Channel: channel,
|
|
|
|
Destination: destination,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotificationDestinationsForChannel will return a collection of
|
|
|
|
// destinations to notify for a given channel.
|
|
|
|
func GetNotificationDestinationsForChannel(channel string) ([]string, error) {
|
|
|
|
result, err := data.GetDatastore().GetQueries().GetNotificationDestinationsForChannel(context.Background(), channel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to query notification destinations for channel "+channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|