4
0

executor_actions.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package executor
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. config "github.com/OliveTin/OliveTin/internal/config"
  6. sv "github.com/OliveTin/OliveTin/internal/stringvariables"
  7. log "github.com/sirupsen/logrus"
  8. "strconv"
  9. )
  10. func (e *Executor) FindActionBindingByID(id string) *config.Action {
  11. e.MapActionIdToBindingLock.RLock()
  12. pair, found := e.MapActionIdToBinding[id]
  13. e.MapActionIdToBindingLock.RUnlock()
  14. if found {
  15. log.Infof("findActionBinding %v, %v", id, pair.Action.ID)
  16. return pair.Action
  17. }
  18. return nil
  19. }
  20. func (e *Executor) RebuildActionMap() {
  21. e.MapActionIdToBindingLock.Lock()
  22. clear(e.MapActionIdToBinding)
  23. for configOrder, action := range e.Cfg.Actions {
  24. if action.Entity != "" {
  25. registerActionsFromEntities(e, configOrder, action.Entity, action)
  26. } else {
  27. registerAction(e, configOrder, action)
  28. }
  29. }
  30. e.MapActionIdToBindingLock.Unlock()
  31. for _, l := range e.listeners {
  32. l.OnActionMapRebuilt()
  33. }
  34. }
  35. func registerAction(e *Executor, configOrder int, action *config.Action) {
  36. actionId := hashActionToID(action, "")
  37. e.MapActionIdToBinding[actionId] = &ActionBinding{
  38. Action: action,
  39. EntityPrefix: "noent",
  40. ConfigOrder: configOrder,
  41. }
  42. }
  43. func registerActionsFromEntities(e *Executor, configOrder int, entityTitle string, tpl *config.Action) {
  44. entityCount, _ := strconv.Atoi(sv.Get("entities." + entityTitle + ".count"))
  45. for i := 0; i < entityCount; i++ {
  46. registerActionFromEntity(e, configOrder, tpl, entityTitle, i)
  47. }
  48. }
  49. func registerActionFromEntity(e *Executor, configOrder int, tpl *config.Action, entityTitle string, entityIndex int) {
  50. prefix := sv.GetEntityPrefix(entityTitle, entityIndex)
  51. virtualActionId := hashActionToID(tpl, prefix)
  52. e.MapActionIdToBinding[virtualActionId] = &ActionBinding{
  53. Action: tpl,
  54. EntityPrefix: prefix,
  55. ConfigOrder: configOrder,
  56. }
  57. }
  58. func hashActionToID(action *config.Action, entityPrefix string) string {
  59. if action.ID != "" && entityPrefix == "" {
  60. return action.ID
  61. }
  62. h := sha256.New()
  63. if entityPrefix == "" {
  64. h.Write([]byte(action.Title))
  65. } else {
  66. h.Write([]byte(action.ID + "." + entityPrefix))
  67. }
  68. return fmt.Sprintf("%x", h.Sum(nil))
  69. }