Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants committed Jun 11, 2021
1 parent a79334e commit 5d20bb0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 53 deletions.
10 changes: 3 additions & 7 deletions pkg/plugin/common/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@ func (o *PluginOutput) SetError(err error) {
// HookContext is passed as a PluginArgValue and indicates what hook triggered
// this plugin task.
type HookContext struct {
Type string `json:"type"`
Input interface{} `json:"input"`
}

type PostHookInput struct {
ID int `json:"id"`
Input interface{} `json:"input,omitempty"`
ID int `json:"id,omitempty"`
Type string `json:"type"`
Input interface{} `json:"input"`
InputFields []string `json:"inputFields,omitempty"`
}
7 changes: 2 additions & 5 deletions pkg/plugin/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ func (e HookTriggerEnum) String() string {
return string(e)
}

func addHookContext(argsMap common.ArgsMap, hookType HookTriggerEnum, input interface{}) {
argsMap[common.HookContextKey] = common.HookContext{
Type: string(hookType),
Input: input,
}
func addHookContext(argsMap common.ArgsMap, hookContext common.HookContext) {
argsMap[common.HookContextKey] = hookContext
}
12 changes: 7 additions & 5 deletions pkg/plugin/js/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/plugin/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,17 @@ func (c Cache) CreateTask(ctx context.Context, pluginID string, operationName st
}

func (c Cache) ExecutePostHooks(ctx context.Context, id int, hookType HookTriggerEnum, input interface{}, inputFields []string) {
if err := c.executePostHooks(ctx, hookType, common.PostHookInput{
if err := c.executePostHooks(ctx, hookType, common.HookContext{
ID: id,
Type: hookType.String(),
Input: input,
InputFields: inputFields,
}); err != nil {
logger.Errorf("error executing post hooks: %s", err.Error())
}
}

func (c Cache) executePostHooks(ctx context.Context, hookType HookTriggerEnum, input common.PostHookInput) error {
func (c Cache) executePostHooks(ctx context.Context, hookType HookTriggerEnum, hookContext common.HookContext) error {
visitedPlugins := session.GetVisitedPlugins(ctx)

for _, p := range c.plugins {
Expand All @@ -194,7 +195,7 @@ func (c Cache) executePostHooks(ctx context.Context, hookType HookTriggerEnum, i
serverConnection := c.makeServerConnection(newCtx)

pluginInput := buildPluginInput(&p, &h.OperationConfig, serverConnection, nil)
addHookContext(pluginInput.Args, hookType, input)
addHookContext(pluginInput.Args, hookContext)

pt := pluginTask{
plugin: &p,
Expand Down
56 changes: 23 additions & 33 deletions ui/v2.5/src/docs/en/Plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,11 @@ Currently, only `Post` hook types are supported. These are executed after the op

Plugin tasks triggered by a hook include an argument named `hookContext` in the `args` object structure. The `hookContext` is structured as follows:

```
{
type: <trigger type>
input: <hook input>
}
```

For `Post` hooks, `input` uses the following structure:

```
{
"id": <object id>,
"input": <operation input>
"type": <trigger type>,
"input": <operation input>,
"inputFields": <fields included in input>
}
```
Expand All @@ -157,30 +149,28 @@ For example, here is the `args` values for a Scene update operation:
{
"hookContext": {
"type":"Scene.Update.Post",
"id":45,
"input":{
"id":45,
"input":{
"clientMutationId":null,
"id":"45",
"title":null,
"details":null,
"url":null,
"date":null,
"rating":null,
"organized":null,
"studio_id":null,
"gallery_ids":null,
"performer_ids":null,
"movies":null,
"tag_ids":["21"],
"cover_image":null,
"stash_ids":null
},
"inputFields":[
"tag_ids",
"id"
]
}
"clientMutationId":null,
"id":"45",
"title":null,
"details":null,
"url":null,
"date":null,
"rating":null,
"organized":null,
"studio_id":null,
"gallery_ids":null,
"performer_ids":null,
"movies":null,
"tag_ids":["21"],
"cover_image":null,
"stash_ids":null
},
"inputFields":[
"tag_ids",
"id"
]
}
}
```

0 comments on commit 5d20bb0

Please sign in to comment.