[customevent] Add scheduled events to API handler

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-12-26 18:48:46 +01:00
parent bf3b800972
commit 9106bcbe3f
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 25 additions and 23 deletions

View file

@ -2,7 +2,6 @@ package customevent
import (
"strings"
"time"
"github.com/go-irc/irc"
"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")
}
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(
triggerEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd)),
triggerOrStoreEvent(plugins.DeriveChannel(m, eventData), strings.NewReader(fd), delayRaw),
"triggering event",
)
}

View file

@ -6,6 +6,7 @@ import (
"io"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
@ -66,12 +67,18 @@ func Register(args plugins.RegistrationArguments) error {
})
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
Description: "Creates an `custom` event containing the fields provided in the request body",
HandlerFunc: handleCreateEvent,
Method: http.MethodPost,
Module: "customevent",
Name: "Create custom event",
Path: "/{channel}",
Description: "Creates an `custom` event containing the fields provided in the request body",
HandlerFunc: handleCreateEvent,
Method: http.MethodPost,
Module: "customevent",
Name: "Create custom event",
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,
ResponseType: plugins.HTTPRouteResponseTypeNo200,
RouteParams: []plugins.HTTPRouteParamDocumentation{
@ -103,7 +110,7 @@ func handleCreateEvent(w http.ResponseWriter, r *http.Request) {
}
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)
return
}
@ -124,12 +131,21 @@ func parseEvent(channel string, fieldData io.Reader) (*plugins.FieldCollection,
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)
if err != nil {
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 {
return errors.Wrap(err, "creating event")
}