change_modules.lua 3.5 KB

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