Plugin hooks (#1452)

* Refactor session and plugin code
* Add context to job tasks
* Show hooks in plugins page
* Refactor session management
This commit is contained in:
WithoutPants
2021-06-11 17:24:58 +10:00
committed by GitHub
parent dde361f9f3
commit 46bbede9a0
48 changed files with 1289 additions and 338 deletions

View File

@@ -33,7 +33,7 @@ func throw(vm *otto.Otto, str string) {
panic(value)
}
func gqlRequestFunc(vm *otto.Otto, gqlHandler http.HandlerFunc) func(call otto.FunctionCall) otto.Value {
func gqlRequestFunc(vm *otto.Otto, cookie *http.Cookie, gqlHandler http.Handler) func(call otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) == 0 {
throw(vm, "missing argument")
@@ -67,11 +67,15 @@ func gqlRequestFunc(vm *otto.Otto, gqlHandler http.HandlerFunc) func(call otto.F
}
r.Header.Set("Content-Type", "application/json")
if cookie != nil {
r.AddCookie(cookie)
}
w := &responseWriter{
header: make(http.Header),
}
gqlHandler(w, r)
gqlHandler.ServeHTTP(w, r)
if w.statusCode != http.StatusOK && w.statusCode != 0 {
throw(vm, fmt.Sprintf("graphQL query failed: %d - %s. Query: %s. Variables: %v", w.statusCode, w.r.String(), in.Query, in.Variables))
@@ -99,9 +103,9 @@ func gqlRequestFunc(vm *otto.Otto, gqlHandler http.HandlerFunc) func(call otto.F
}
}
func AddGQLAPI(vm *otto.Otto, gqlHandler http.HandlerFunc) {
func AddGQLAPI(vm *otto.Otto, cookie *http.Cookie, gqlHandler http.Handler) {
gql, _ := vm.Object("({})")
gql.Set("Do", gqlRequestFunc(vm, gqlHandler))
gql.Set("Do", gqlRequestFunc(vm, cookie, gqlHandler))
vm.Set("gql", gql)
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/stashapp/stash/pkg/logger"
)
const pluginPrefix = "[Plugin] "
func argToString(call otto.FunctionCall) string {
arg := call.Argument(0)
if arg.IsObject() {
@@ -20,27 +22,27 @@ func argToString(call otto.FunctionCall) string {
}
func logTrace(call otto.FunctionCall) otto.Value {
logger.Trace(argToString(call))
logger.Trace(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logDebug(call otto.FunctionCall) otto.Value {
logger.Debug(argToString(call))
logger.Debug(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logInfo(call otto.FunctionCall) otto.Value {
logger.Info(argToString(call))
logger.Info(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logWarn(call otto.FunctionCall) otto.Value {
logger.Warn(argToString(call))
logger.Warn(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}
func logError(call otto.FunctionCall) otto.Value {
logger.Error(argToString(call))
logger.Error(pluginPrefix + argToString(call))
return otto.UndefinedValue()
}