change_modules.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. --- change_modules
  2. -- @module change_modules
  3. local change_modules = {
  4. name = "Change modules",
  5. slug = "change_modules",
  6. desc = "Add or remove modules from the org",
  7. hide = false;
  8. }
  9. change_modules.data = {
  10. result = nil,
  11. modules_before = {},
  12. modules_after = {},
  13. summary = "",
  14. }
  15. change_modules.config = {
  16. approval_module = false
  17. }
  18. --- Initiate change in modules.
  19. -- Either adds or removes module depending on user input
  20. -- @function change_modules:initiate
  21. -- @param result Callback if this module is embedded in other modules
  22. function change_modules:initiate(result)
  23. self.data.result = result
  24. self.data.add_modules = {}
  25. self.data.remove_modules = {}
  26. local modules_before = {}
  27. local modules_after = {}
  28. -- generate self.config.modules table
  29. for k, module in pairs(modpol.modules) do
  30. if not modpol.modules[module.slug].hide then
  31. local in_org = false
  32. if self.org.policies[module.slug] then
  33. in_org = true
  34. end
  35. table.insert(
  36. modules_before,
  37. {module.name.." ["..module.slug.."]", in_org})
  38. end
  39. end
  40. -- send query to user
  41. modpol.interactions.checkbox_query(
  42. self.initiator,
  43. "Check the modules to activate in this org:",
  44. modules_before,
  45. function(input)
  46. -- identify changes
  47. modules_after = input
  48. for i,v in ipairs(modules_after) do
  49. if v[2] ~= modules_before[i][2] then
  50. if v[2] then
  51. table.insert(self.data.add_modules, v[1])
  52. else
  53. table.insert(self.data.remove_modules, v[1])
  54. end
  55. end
  56. end
  57. -- abort if no changes
  58. if #self.data.add_modules == 0
  59. and #self.data.remove_modules == 0 then
  60. modpol.interactions.message(
  61. self.initiator, "No module changes proposed")
  62. modpol.interactions.org_dashboard(
  63. self.initiator, self.org.id)
  64. self.org:delete_process(self.id)
  65. return
  66. end
  67. -- proceed with approval
  68. local query = "Accept module changes in org "..
  69. self.org.name.."?"
  70. self.data.summary = ""
  71. if #self.data.add_modules > 0 then
  72. self.data.summary = self.data.summary.."\nAdd: "..
  73. table.concat(self.data.add_modules,", ")
  74. elseif #self.data.remove_modules > 0 then
  75. self.data.summary = "\nRemove: "..
  76. table.concat(self.data.remove_modules,", ")
  77. end
  78. self:call_module(
  79. self.config.approval_module,
  80. self.initiator,
  81. {
  82. prompt = query..self.data.summary
  83. },
  84. function()
  85. self:implement_change()
  86. end)
  87. modpol.interactions.org_dashboard(
  88. self.initiator, self.org.id)
  89. end)
  90. end
  91. function change_modules:implement_change()
  92. for i,v in ipairs(self.data.add_modules) do
  93. local slug = string.match(v,"%[(.+)%]")
  94. self.org.policies[slug] = {}
  95. table.sort(self.org.policies)
  96. end
  97. for i,v in ipairs(self.data.remove_modules) do
  98. local slug = string.match(v,"%[(.+)%]")
  99. self.org.policies[slug] = nil
  100. table.sort(self.org.policies)
  101. end
  102. -- announce and shut down
  103. modpol.interactions.message_org(
  104. self.initiator,
  105. self.org.id,
  106. "Module changes applied to org "..self.org.name..":"..
  107. self.data.summary)
  108. if self.data.result then self.data.result() end
  109. self.org:delete_process(self.id)
  110. end
  111. modpol.modules.change_modules = change_modules