2024-07-02 07:44:37 +03:00
|
|
|
package handlers
|
2022-03-18 23:33:23 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2024-07-02 04:58:50 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2022-03-18 23:33:23 +03:00
|
|
|
"github.com/owncast/owncast/notifications"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils "github.com/owncast/owncast/webserver/utils"
|
2022-03-18 23:33:23 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterForLiveNotifications will register a channel + destination to be
|
|
|
|
// notified when a stream goes live.
|
2024-07-02 04:58:50 +03:00
|
|
|
func RegisterForLiveNotifications(u models.User, w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, r.Method+" not supported")
|
2022-03-18 23:33:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
// Channel is the notification channel (browser, sms, etc)
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
// Destination is the target of the notification in the above channel.
|
|
|
|
Destination string `json:"destination"`
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
var req request
|
|
|
|
if err := decoder.Decode(&req); err != nil {
|
|
|
|
log.Errorln(err)
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, "unable to register for notifications")
|
2022-03-18 23:33:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the requested channel is one we want to handle.
|
|
|
|
validTypes := []string{notifications.BrowserPushNotification}
|
|
|
|
_, validChannel := utils.FindInSlice(validTypes, req.Channel)
|
|
|
|
if !validChannel {
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, "invalid notification channel: "+req.Channel)
|
2022-03-18 23:33:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := notifications.AddNotification(req.Channel, req.Destination); err != nil {
|
|
|
|
log.Errorln(err)
|
2024-07-02 07:44:37 +03:00
|
|
|
webutils.WriteSimpleResponse(w, false, "unable to save notification")
|
2022-03-18 23:33:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|