notifications.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/owncast/owncast/core/user"
  6. "github.com/owncast/owncast/notifications"
  7. "github.com/owncast/owncast/utils"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. // RegisterForLiveNotifications will register a channel + destination to be
  11. // notified when a stream goes live.
  12. func RegisterForLiveNotifications(u user.User, w http.ResponseWriter, r *http.Request) {
  13. if r.Method != POST {
  14. WriteSimpleResponse(w, false, r.Method+" not supported")
  15. return
  16. }
  17. type request struct {
  18. // Channel is the notification channel (browser, sms, etc)
  19. Channel string `json:"channel"`
  20. // Destination is the target of the notification in the above channel.
  21. Destination string `json:"destination"`
  22. }
  23. decoder := json.NewDecoder(r.Body)
  24. var req request
  25. if err := decoder.Decode(&req); err != nil {
  26. log.Errorln(err)
  27. WriteSimpleResponse(w, false, "unable to register for notifications")
  28. return
  29. }
  30. // Make sure the requested channel is one we want to handle.
  31. validTypes := []string{notifications.BrowserPushNotification}
  32. _, validChannel := utils.FindInSlice(validTypes, req.Channel)
  33. if !validChannel {
  34. WriteSimpleResponse(w, false, "invalid notification channel: "+req.Channel)
  35. return
  36. }
  37. if err := notifications.AddNotification(req.Channel, req.Destination); err != nil {
  38. log.Errorln(err)
  39. WriteSimpleResponse(w, false, "unable to save notification")
  40. return
  41. }
  42. }