2022-09-10 11:39:07 +00:00
|
|
|
package counter
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-28 15:27:24 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-03-06 00:42:40 +00:00
|
|
|
"strconv"
|
2022-09-10 11:39:07 +00:00
|
|
|
"strings"
|
2021-03-06 00:42:40 +00:00
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
"github.com/go-irc/irc"
|
2021-08-28 15:27:24 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-12-21 00:32:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/pkg/database"
|
2021-11-25 22:48:16 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2020-12-21 00:32:39 +00:00
|
|
|
)
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
var (
|
|
|
|
db database.Connector
|
|
|
|
formatMessage plugins.MsgFormatter
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
ptrStringEmpty = func(s string) *string { return &s }("")
|
|
|
|
)
|
|
|
|
|
|
|
|
//nolint:funlen // This function is a few lines too long but only contains definitions
|
|
|
|
func Register(args plugins.RegistrationArguments) error {
|
|
|
|
db = args.GetDatabaseConnector()
|
2022-10-22 22:08:02 +00:00
|
|
|
if err := db.DB().AutoMigrate(&counter{}); err != nil {
|
2022-09-10 11:39:07 +00:00
|
|
|
return errors.Wrap(err, "applying schema migration")
|
|
|
|
}
|
|
|
|
|
|
|
|
formatMessage = args.FormatMessage
|
|
|
|
|
|
|
|
args.RegisterActor("counter", func() plugins.Actor { return &ActorCounter{} })
|
|
|
|
|
|
|
|
args.RegisterActorDocumentation(plugins.ActionDocumentation{
|
2021-09-22 13:36:45 +00:00
|
|
|
Description: "Update counter values",
|
|
|
|
Name: "Modify Counter",
|
|
|
|
Type: "counter",
|
|
|
|
|
|
|
|
Fields: []plugins.ActionDocumentationField{
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Name of the counter to update",
|
|
|
|
Key: "counter",
|
|
|
|
Name: "Counter",
|
|
|
|
Optional: false,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "1",
|
|
|
|
Description: "Value to add to the counter",
|
|
|
|
Key: "counter_step",
|
|
|
|
Name: "Counter Step",
|
|
|
|
Optional: true,
|
2022-01-31 00:50:08 +00:00
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
2021-09-22 13:36:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Default: "",
|
|
|
|
Description: "Value to set the counter to",
|
|
|
|
Key: "counter_set",
|
|
|
|
Name: "Counter Set",
|
|
|
|
Optional: true,
|
|
|
|
SupportTemplate: true,
|
|
|
|
Type: plugins.ActionDocumentationFieldTypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2021-08-28 15:27:24 +00:00
|
|
|
Description: "Returns the (formatted) value as a plain string",
|
|
|
|
HandlerFunc: routeActorCounterGetValue,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: "counter",
|
|
|
|
Name: "Get Counter Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Template to apply to the value: Variations of %d sprintf template are supported once",
|
|
|
|
Name: "template",
|
|
|
|
Required: false,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the counter to query",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
args.RegisterAPIRoute(plugins.HTTPRouteRegistrationArgs{
|
2021-08-28 15:27:24 +00:00
|
|
|
Description: "Updates the value of the counter",
|
|
|
|
HandlerFunc: routeActorCounterSetValue,
|
|
|
|
Method: http.MethodPatch,
|
|
|
|
Module: "counter",
|
|
|
|
Name: "Set Counter Value",
|
|
|
|
Path: "/{name}",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "If set to `true` the given value is set instead of added",
|
|
|
|
Name: "absolute",
|
|
|
|
Required: false,
|
|
|
|
Type: "boolean",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Value to add / set for the given counter",
|
|
|
|
Name: "value",
|
|
|
|
Required: true,
|
|
|
|
Type: "int64",
|
|
|
|
},
|
|
|
|
},
|
2021-10-23 15:22:58 +00:00
|
|
|
RequiresWriteAuth: true,
|
2021-08-28 15:27:24 +00:00
|
|
|
RouteParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "Name of the counter to update",
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-09-10 11:39:07 +00:00
|
|
|
|
|
|
|
args.RegisterTemplateFunction("channelCounter", func(m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) interface{} {
|
|
|
|
return func(name string) (string, error) {
|
|
|
|
channel, err := fields.String("channel")
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "channel not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join([]string{channel, name}, ":"), nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
args.RegisterTemplateFunction("counterValue", plugins.GenericTemplateFunctionGetter(func(name string, _ ...string) (int64, error) {
|
2022-10-22 22:08:02 +00:00
|
|
|
return GetCounterValue(db, name)
|
2022-09-10 11:39:07 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
return nil
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
type ActorCounter struct{}
|
2020-12-25 23:31:49 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a ActorCounter) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, eventData *plugins.FieldCollection, attrs *plugins.FieldCollection) (preventCooldown bool, err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
counterName, err := formatMessage(attrs.MustString("counter", nil), m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "preparing response")
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if counterSet := attrs.MustString("counter_set", ptrStringEmpty); counterSet != "" {
|
|
|
|
parseValue, err := formatMessage(counterSet, m, r, eventData)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "execute counter value template")
|
2021-03-06 00:35:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
counterValue, err := strconv.ParseInt(parseValue, 10, 64)
|
2021-06-11 11:52:42 +00:00
|
|
|
if err != nil {
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(err, "parse counter value")
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2022-10-22 22:08:02 +00:00
|
|
|
UpdateCounter(db, counterName, counterValue, true),
|
2021-06-11 11:52:42 +00:00
|
|
|
"set counter",
|
2020-12-21 00:32:39 +00:00
|
|
|
)
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var counterStep int64 = 1
|
2022-01-31 00:50:08 +00:00
|
|
|
if s := attrs.MustString("counter_step", ptrStringEmpty); s != "" {
|
|
|
|
parseStep, err := formatMessage(s, m, r, eventData)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "execute counter step template")
|
|
|
|
}
|
|
|
|
|
|
|
|
counterStep, err = strconv.ParseInt(parseStep, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "parse counter step")
|
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:12:10 +00:00
|
|
|
return false, errors.Wrap(
|
2022-10-22 22:08:02 +00:00
|
|
|
UpdateCounter(db, counterName, counterStep, false),
|
2021-06-11 11:52:42 +00:00
|
|
|
"update counter",
|
|
|
|
)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2021-06-11 11:52:42 +00:00
|
|
|
|
|
|
|
func (a ActorCounter) IsAsync() bool { return false }
|
|
|
|
func (a ActorCounter) Name() string { return "counter" }
|
2021-08-28 15:27:24 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
func (a ActorCounter) Validate(attrs *plugins.FieldCollection) (err error) {
|
2021-09-22 13:36:45 +00:00
|
|
|
if cn, err := attrs.String("counter"); err != nil || cn == "" {
|
|
|
|
return errors.New("counter name must be non-empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
func routeActorCounterGetValue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
template := r.FormValue("template")
|
|
|
|
if template == "" {
|
|
|
|
template = "%d"
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
cv, err := GetCounterValue(db, mux.Vars(r)["name"])
|
2022-09-10 11:39:07 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, errors.Wrap(err, "getting value").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
w.Header().Set("Content-Type", "text-plain")
|
2022-09-10 11:39:07 +00:00
|
|
|
fmt.Fprintf(w, template, cv)
|
2021-08-28 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeActorCounterSetValue(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
absolute = r.FormValue("absolute") == "true"
|
|
|
|
err error
|
|
|
|
value int64
|
|
|
|
)
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
if value, err = strconv.ParseInt(r.FormValue("value"), 10, 64); err != nil {
|
2021-08-28 15:27:24 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "parsing value").Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:08:02 +00:00
|
|
|
if err = UpdateCounter(db, mux.Vars(r)["name"], value, absolute); err != nil {
|
2021-08-28 15:27:24 +00:00
|
|
|
http.Error(w, errors.Wrap(err, "updating value").Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|