mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 08:40:01 +00:00
[customevent] Enforce channel to be set in event
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
47a4390a83
commit
4c5fcc9e91
1 changed files with 22 additions and 3 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue