[customevent] Enforce channel to be set in event

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-03-31 13:26:13 +02:00
parent 47a4390a83
commit 4c5fcc9e91
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -3,7 +3,9 @@ package customevent
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/plugins"
@ -20,23 +22,40 @@ func Register(args plugins.RegistrationArguments) error {
Method: http.MethodPost,
Module: "customevent",
Name: "Create custom event",
Path: "/create",
Path: "/{channel}",
RequiresWriteAuth: true,
ResponseType: plugins.HTTPRouteResponseTypeNo200,
RouteParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "Channel to create the event in",
Name: "channel",
},
},
})
return nil
}
func handleCreateEvent(w http.ResponseWriter, r *http.Request) {
payload := make(map[string]any)
var (
channel = mux.Vars(r)["channel"]
payload = make(map[string]any)
)
if channel == "" {
http.Error(w, errors.New("missing channel").Error(), http.StatusBadRequest)
return
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, errors.Wrap(err, "parsing event payload").Error(), http.StatusBadRequest)
return
}
if err := eventCreatorFunc("custom", plugins.FieldCollectionFromData(payload)); err != nil {
fields := plugins.FieldCollectionFromData(payload)
fields.Set("channel", "#"+strings.TrimLeft(channel, "#"))
if err := eventCreatorFunc("custom", fields); err != nil {
http.Error(w, errors.Wrap(err, "creating event").Error(), http.StatusInternalServerError)
return
}