notifications.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package notifications
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/owncast/owncast/config"
  6. "github.com/owncast/owncast/core/data"
  7. "github.com/owncast/owncast/db"
  8. "github.com/owncast/owncast/models"
  9. "github.com/owncast/owncast/persistence/configrepository"
  10. "github.com/owncast/owncast/persistence/tables"
  11. "github.com/owncast/owncast/notifications/browser"
  12. "github.com/owncast/owncast/notifications/discord"
  13. "github.com/pkg/errors"
  14. log "github.com/sirupsen/logrus"
  15. )
  16. // Notifier is an instance of the live stream notifier.
  17. type Notifier struct {
  18. datastore *data.Datastore
  19. browser *browser.Browser
  20. discord *discord.Discord
  21. configRepository configrepository.ConfigRepository
  22. }
  23. // Setup will perform any pre-use setup for the notifier.
  24. func Setup(datastore *data.Datastore) {
  25. tables.CreateNotificationsTable(datastore.DB)
  26. initializeBrowserPushIfNeeded()
  27. }
  28. func initializeBrowserPushIfNeeded() {
  29. configRepository := configrepository.Get()
  30. pubKey, _ := configRepository.GetBrowserPushPublicKey()
  31. privKey, _ := configRepository.GetBrowserPushPrivateKey()
  32. // We need browser push keys so people can register for pushes.
  33. if pubKey == "" || privKey == "" {
  34. browserPrivateKey, browserPublicKey, err := browser.GenerateBrowserPushKeys()
  35. if err != nil {
  36. log.Errorln("unable to initialize browser push notification keys", err)
  37. }
  38. if err := configRepository.SetBrowserPushPrivateKey(browserPrivateKey); err != nil {
  39. log.Errorln("unable to set browser push private key", err)
  40. }
  41. if err := configRepository.SetBrowserPushPublicKey(browserPublicKey); err != nil {
  42. log.Errorln("unable to set browser push public key", err)
  43. }
  44. }
  45. // Enable browser push notifications by default.
  46. if !configRepository.GetHasPerformedInitialNotificationsConfig() {
  47. _ = configRepository.SetBrowserPushConfig(models.BrowserNotificationConfiguration{Enabled: true, GoLiveMessage: config.GetDefaults().FederationGoLiveMessage})
  48. _ = configRepository.SetHasPerformedInitialNotificationsConfig(true)
  49. }
  50. }
  51. // New creates a new instance of the Notifier.
  52. func New(datastore *data.Datastore) (*Notifier, error) {
  53. notifier := Notifier{
  54. datastore: datastore,
  55. configRepository: configrepository.Get(),
  56. }
  57. if err := notifier.setupBrowserPush(); err != nil {
  58. log.Error(err)
  59. }
  60. if err := notifier.setupDiscord(); err != nil {
  61. log.Error(err)
  62. }
  63. return &notifier, nil
  64. }
  65. func (n *Notifier) setupBrowserPush() error {
  66. if n.configRepository.GetBrowserPushConfig().Enabled {
  67. publicKey, err := n.configRepository.GetBrowserPushPublicKey()
  68. if err != nil || publicKey == "" {
  69. return errors.Wrap(err, "browser notifier disabled, failed to get browser push public key")
  70. }
  71. privateKey, err := n.configRepository.GetBrowserPushPrivateKey()
  72. if err != nil || privateKey == "" {
  73. return errors.Wrap(err, "browser notifier disabled, failed to get browser push private key")
  74. }
  75. browserNotifier, err := browser.New(n.datastore, publicKey, privateKey)
  76. if err != nil {
  77. return errors.Wrap(err, "error creating browser notifier")
  78. }
  79. n.browser = browserNotifier
  80. }
  81. return nil
  82. }
  83. func (n *Notifier) notifyBrowserPush() {
  84. destinations, err := GetNotificationDestinationsForChannel(BrowserPushNotification)
  85. if err != nil {
  86. log.Errorln("error getting browser push notification destinations", err)
  87. }
  88. for _, destination := range destinations {
  89. unsubscribed, err := n.browser.Send(destination, n.configRepository.GetServerName(), n.configRepository.GetBrowserPushConfig().GoLiveMessage)
  90. if unsubscribed {
  91. // If the error is "unsubscribed", then remove the destination from the database.
  92. if err := RemoveNotificationForChannel(BrowserPushNotification, destination); err != nil {
  93. log.Errorln(err)
  94. }
  95. } else if err != nil {
  96. log.Errorln(err)
  97. }
  98. }
  99. }
  100. func (n *Notifier) setupDiscord() error {
  101. discordConfig := n.configRepository.GetDiscordConfig()
  102. if discordConfig.Enabled && discordConfig.Webhook != "" {
  103. var image string
  104. if serverURL := n.configRepository.GetServerURL(); serverURL != "" {
  105. image = serverURL + "/logo"
  106. }
  107. discordNotifier, err := discord.New(
  108. n.configRepository.GetServerName(),
  109. image,
  110. discordConfig.Webhook,
  111. )
  112. if err != nil {
  113. return errors.Wrap(err, "error creating discord notifier")
  114. }
  115. n.discord = discordNotifier
  116. }
  117. return nil
  118. }
  119. func (n *Notifier) notifyDiscord() {
  120. goLiveMessage := n.configRepository.GetDiscordConfig().GoLiveMessage
  121. streamTitle := n.configRepository.GetStreamTitle()
  122. if streamTitle != "" {
  123. goLiveMessage += "\n" + streamTitle
  124. }
  125. message := fmt.Sprintf("%s\n\n%s", goLiveMessage, n.configRepository.GetServerURL())
  126. if err := n.discord.Send(message); err != nil {
  127. log.Errorln("error sending discord message", err)
  128. }
  129. }
  130. // Notify will fire the different notification channels.
  131. func (n *Notifier) Notify() {
  132. if n.browser != nil {
  133. n.notifyBrowserPush()
  134. }
  135. if n.discord != nil {
  136. n.notifyDiscord()
  137. }
  138. }
  139. // RemoveNotificationForChannel removes a notification destination.
  140. func RemoveNotificationForChannel(channel, destination string) error {
  141. log.Debugln("Removing notification for channel", channel)
  142. return data.GetDatastore().GetQueries().RemoveNotificationDestinationForChannel(context.Background(), db.RemoveNotificationDestinationForChannelParams{
  143. Channel: channel,
  144. Destination: destination,
  145. })
  146. }
  147. // GetNotificationDestinationsForChannel will return a collection of
  148. // destinations to notify for a given channel.
  149. func GetNotificationDestinationsForChannel(channel string) ([]string, error) {
  150. result, err := data.GetDatastore().GetQueries().GetNotificationDestinationsForChannel(context.Background(), channel)
  151. if err != nil {
  152. return nil, errors.Wrap(err, "unable to query notification destinations for channel "+channel)
  153. }
  154. return result, nil
  155. }
  156. // AddNotification saves a new user notification destination.
  157. func AddNotification(channel, destination string) error {
  158. return data.GetDatastore().GetQueries().AddNotification(context.Background(), db.AddNotificationParams{
  159. Channel: channel,
  160. Destination: destination,
  161. })
  162. }