mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 16:50:01 +00:00
[customevent] Add scheduled events to API handler
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
bf3b800972
commit
9106bcbe3f
2 changed files with 25 additions and 23 deletions
|
@ -2,7 +2,6 @@ package customevent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
"github.com/go-irc/irc"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -27,21 +26,8 @@ func (a actor) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData
|
||||||
return false, errors.Wrap(err, "executing schedule_in template")
|
return false, errors.Wrap(err, "executing schedule_in template")
|
||||||
}
|
}
|
||||||
|
|
||||||
if delay, err := time.ParseDuration(delayRaw); err == nil && delay > 0 {
|
|
||||||
fields, err := parseEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd))
|
|
||||||
if err != nil {
|
|
||||||
return false, errors.Wrap(err, "parsing fields data")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = storeEvent(db, time.Now().Add(delay).UTC(), plugins.DeriveChannel(m, eventData), fields); err != nil {
|
|
||||||
return false, errors.Wrap(err, "storing event")
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, errors.Wrap(mc.Refresh(), "refreshing memory cache")
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, errors.Wrap(
|
return false, errors.Wrap(
|
||||||
triggerEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd)),
|
triggerOrStoreEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd), delayRaw),
|
||||||
"triggering event",
|
"triggering event",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -66,12 +67,18 @@ func Register(args plugins.RegistrationArguments) error {
|
||||||
})
|
})
|
||||||
|
|
||||||
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
||||||
Description: "Creates an `custom` event containing the fields provided in the request body",
|
Description: "Creates an `custom` event containing the fields provided in the request body",
|
||||||
HandlerFunc: handleCreateEvent,
|
HandlerFunc: handleCreateEvent,
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
Module: "customevent",
|
Module: "customevent",
|
||||||
Name: "Create custom event",
|
Name: "Create custom event",
|
||||||
Path: "/{channel}",
|
Path: "/{channel}",
|
||||||
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
||||||
|
{
|
||||||
|
Description: "Time until the event is triggered (must be valid duration like 1h, 1h1m, 10s, ...)",
|
||||||
|
Name: "schedule_in",
|
||||||
|
},
|
||||||
|
},
|
||||||
RequiresWriteAuth: true,
|
RequiresWriteAuth: true,
|
||||||
ResponseType: plugins.HTTPRouteResponseTypeNo200,
|
ResponseType: plugins.HTTPRouteResponseTypeNo200,
|
||||||
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
||||||
|
@ -103,7 +110,7 @@ func handleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
channel = "#" + strings.TrimLeft(channel, "#") // Sanitize
|
channel = "#" + strings.TrimLeft(channel, "#") // Sanitize
|
||||||
|
|
||||||
if err := triggerEvent(channel, r.Body); err != nil {
|
if err := triggerOrStoreEvent(channel, r.Body, r.FormValue("schedule_in")); err != nil {
|
||||||
http.Error(w, errors.Wrap(err, "creating event").Error(), http.StatusInternalServerError)
|
http.Error(w, errors.Wrap(err, "creating event").Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -124,12 +131,21 @@ func parseEvent(channel string, fieldData io.Reader) (*plugins.FieldCollection,
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func triggerEvent(channel string, fieldData io.Reader) error {
|
func triggerOrStoreEvent(channel string, fieldData io.Reader, rawDelay string) error {
|
||||||
fields, err := parseEvent(channel, fieldData)
|
fields, err := parseEvent(channel, fieldData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "parsing fields")
|
return errors.Wrap(err, "parsing fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if delay, err := time.ParseDuration(rawDelay); err == nil && delay > 0 {
|
||||||
|
// Delay set, store for later triggering
|
||||||
|
if err = storeEvent(db, time.Now().Add(delay).UTC(), channel, fields); err != nil {
|
||||||
|
return errors.Wrap(err, "storing event")
|
||||||
|
}
|
||||||
|
return errors.Wrap(mc.Refresh(), "refreshing memory cache")
|
||||||
|
}
|
||||||
|
|
||||||
|
// No delay, trigger instantly
|
||||||
if err := eventCreatorFunc("custom", fields); err != nil {
|
if err := eventCreatorFunc("custom", fields); err != nil {
|
||||||
return errors.Wrap(err, "creating event")
|
return errors.Wrap(err, "creating event")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue