change_modules.lua 3.6 KB

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