owncast/webserver/handlers/admin/webhooks.go
2024-07-02 20:37:58 -07:00

81 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"
"github.com/owncast/owncast/webserver/handlers/generated"
webutils "github.com/owncast/owncast/webserver/utils"
)
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 generated.DeleteWebhookJSONBody
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")
}