change_modules.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. modpol.msg("add-insert: "..v[1])
  49. else
  50. table.insert(self.data.remove_modules, v[1])
  51. modpol.msg("remove-insert: "..v[1])
  52. end
  53. end
  54. end
  55. -- abort if no changes
  56. if self.data.add_modules == {}
  57. and self.data.remove_modules == {} then
  58. modpol.interactions.message(
  59. self.initiator, "No module changes proposed")
  60. modpol.interactions.org_dashboard(
  61. self.initiator, self.org.id)
  62. self.org:delete_process(self.id)
  63. return
  64. end
  65. -- proceed with consent
  66. local query = "Accept module changes in org "..
  67. self.org.name.."?"
  68. self.data.summary = ""
  69. if #self.data.add_modules > 0 then
  70. self.data.summary = self.data.summary.."\nAdd: "..
  71. table.concat(self.data.add_modules,", ")
  72. elseif #self.data.remove_modules > 0 then
  73. summary = "\nRemove: "..
  74. table.concat(self.data.remove_modules,", ")
  75. end
  76. self.org:call_module(
  77. "consent",
  78. self.initiator,
  79. {
  80. prompt = query..self.data.summary,
  81. votes_required = #self.org.members
  82. },
  83. function()
  84. self:implement_change()
  85. end)
  86. modpol.interactions.org_dashboard(
  87. self.initiator, self.org.id)
  88. end)
  89. end
  90. function change_modules:implement_change()
  91. for i,v in ipairs(self.data.add_modules) do
  92. local slug = string.match(v,"%[(.+)%]")
  93. self.org.modules[slug] =
  94. modpol.util.copy_table(modpol.modules[slug])
  95. table.sort(self.org.modules)
  96. end
  97. for i,v in ipairs(self.data.remove_modules) do
  98. local slug = string.match(v,"%[(.+)%]")
  99. self.org.modules[slug] = nil
  100. table.sort(self.org.modules)
  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. --- (Required) Add to module table
  112. modpol.modules.change_modules = change_modules