change_modules.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. -- @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.modules[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 consent
  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. "consent",
  80. self.initiator,
  81. {
  82. prompt = query..self.data.summary,
  83. votes_required = #self.org.members
  84. },
  85. function()
  86. self:implement_change()
  87. end)
  88. modpol.interactions.org_dashboard(
  89. self.initiator, self.org.id)
  90. end)
  91. end
  92. function change_modules:implement_change()
  93. for i,v in ipairs(self.data.add_modules) do
  94. local slug = string.match(v,"%[(.+)%]")
  95. self.org.modules[slug] =
  96. modpol.util.copy_table(modpol.modules[slug])
  97. table.sort(self.org.modules)
  98. end
  99. for i,v in ipairs(self.data.remove_modules) do
  100. local slug = string.match(v,"%[(.+)%]")
  101. self.org.modules[slug] = nil
  102. table.sort(self.org.modules)
  103. end
  104. -- announce and shut down
  105. modpol.interactions.message_org(
  106. self.initiator,
  107. self.org.id,
  108. "Module changes applied to org "..self.org.name..":"..
  109. self.data.summary)
  110. if self.data.result then self.data.result() end
  111. self.org:delete_process(self.id)
  112. end
  113. modpol.modules.change_modules = change_modules