persistence.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package notifications
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/owncast/owncast/core/data"
  6. "github.com/owncast/owncast/db"
  7. "github.com/pkg/errors"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. func createNotificationsTable(db *sql.DB) {
  11. log.Traceln("Creating federation followers table...")
  12. createTableSQL := `CREATE TABLE IF NOT EXISTS notifications (
  13. "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  14. "channel" TEXT NOT NULL,
  15. "destination" TEXT NOT NULL,
  16. "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
  17. data.MustExec(createTableSQL, db)
  18. data.MustExec(`CREATE INDEX IF NOT EXISTS idx_channel ON notifications (channel);`, db)
  19. }
  20. // AddNotification saves a new user notification destination.
  21. func AddNotification(channel, destination string) error {
  22. return data.GetDatastore().GetQueries().AddNotification(context.Background(), db.AddNotificationParams{
  23. Channel: channel,
  24. Destination: destination,
  25. })
  26. }
  27. // RemoveNotificationForChannel removes a notification destination..
  28. func RemoveNotificationForChannel(channel, destination string) error {
  29. log.Println("Removing notification for channel", channel)
  30. return data.GetDatastore().GetQueries().RemoveNotificationDestinationForChannel(context.Background(), db.RemoveNotificationDestinationForChannelParams{
  31. Channel: channel,
  32. Destination: destination,
  33. })
  34. }
  35. // GetNotificationDestinationsForChannel will return a collection of
  36. // destinations to notify for a given channel.
  37. func GetNotificationDestinationsForChannel(channel string) ([]string, error) {
  38. result, err := data.GetDatastore().GetQueries().GetNotificationDestinationsForChannel(context.Background(), channel)
  39. if err != nil {
  40. return nil, errors.Wrap(err, "unable to query notification destinations for channel "+channel)
  41. }
  42. return result, nil
  43. }