owncast/webserver/handlers/admin/webhooks.go

84 lines
1.9 KiB
Go

package admin
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/models"
webutils "github.com/owncast/owncast/webserver/utils"
)
type deleteWebhookRequest struct {
ID int `json:"id"`
}
type createWebhookRequest struct {
URL string `json:"url"`
Events []models.EventType `json:"events"`
}
// CreateWebhook will add a single webhook.
func CreateWebhook(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var request createWebhookRequest
if err := decoder.Decode(&request); err != nil {
webutils.BadRequestHandler(w, err)
return
}
// Verify all the scopes provided are valid
if !models.HasValidEvents(request.Events) {
webutils.BadRequestHandler(w, errors.New("one or more invalid event provided"))
return
}
newWebhookID, err := data.InsertWebhook(request.URL, request.Events)
if err != nil {
webutils.InternalErrorHandler(w, err)
return
}
webutils.WriteResponse(w, models.Webhook{
ID: newWebhookID,
URL: request.URL,
Events: request.Events,
Timestamp: time.Now(),
LastUsed: nil,
})
}
// GetWebhooks will return all webhooks.
func GetWebhooks(w http.ResponseWriter, r *http.Request) {
webhooks, err := data.GetWebhooks()
if err != nil {
webutils.InternalErrorHandler(w, err)
return
}
webutils.WriteResponse(w, webhooks)
}
// DeleteWebhook will delete a single webhook.
func DeleteWebhook(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
webutils.WriteSimpleResponse(w, false, r.Method+" not supported")
return
}
decoder := json.NewDecoder(r.Body)
var request deleteWebhookRequest
if err := decoder.Decode(&request); err != nil {
webutils.BadRequestHandler(w, err)
return
}
if err := data.DeleteWebhook(request.ID); err != nil {
webutils.InternalErrorHandler(w, err)
return
}
webutils.WriteSimpleResponse(w, true, "deleted webhook")
}