change_modules-dropdown.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  13. change_modules.config = {
  14. }
  15. --- Change modules initiate
  16. -- @function change_modules:initiate
  17. -- @param result Callback if this module is embedded in other modules
  18. function change_modules:initiate(result)
  19. self.data.result = result
  20. -- Step 1: add or remove?
  21. modpol.interactions.dropdown_query(
  22. self.initiator, "Module change options:",
  23. {"Add module","Remove module"},
  24. function(input)
  25. if input == "Add module" then
  26. self:add_module()
  27. elseif input == "Remove module" then
  28. self:remove_module()
  29. end
  30. end
  31. )
  32. end
  33. --- Add a new module
  34. -- @function change_modules:add_module
  35. function change_modules:add_module()
  36. -- prepare module options
  37. local available_modules = modpol.util.copy_table(modpol.modules)
  38. for k,org_mod in pairs(self.org.modules) do
  39. if available_modules[org_mod.slug] then
  40. available_modules[org_mod.slug] = nil
  41. end end
  42. -- present module options
  43. local modules_list = {}
  44. for k,v in pairs(available_modules) do
  45. table.insert(modules_list,v.name)
  46. end
  47. if #modules_list == 0 then
  48. modpol.interactions.message(
  49. self.initiator, "Org has all modules")
  50. modpol.interactions.org_dashboard(
  51. self.initiator, self.org.id)
  52. if self.data.result then self.data.result() end
  53. self.org:delete_process(self.id)
  54. return
  55. end
  56. table.sort(modules_list)
  57. -- now ask which to add
  58. modpol.interactions.dropdown_query(
  59. self.initiator, "Choose a module to add:",
  60. modules_list,
  61. function(mod_choice)
  62. -- confirm choice
  63. modpol.interactions.binary_poll_user(
  64. self.initiator,
  65. "Confirm: propose to add module \"" ..
  66. mod_choice .. "\"?",
  67. function(input)
  68. if input == "Yes" then
  69. self:propose_change("add",mod_choice)
  70. modpol.interactions.org_dashboard(
  71. self.initiator, self.org.id)
  72. else
  73. self:add_module()
  74. end
  75. end
  76. )
  77. end
  78. )
  79. end
  80. --- Remove a module
  81. -- @function change_modules:remove_module
  82. function change_modules:remove_module()
  83. -- prepare module options
  84. local available_modules = {}
  85. for k,org_mod in pairs(self.org.modules) do
  86. if not org_mod.hide then
  87. available_modules[org_mod.slug] = modpol.util.copy_table(org_mod)
  88. end end
  89. local modules_list = {}
  90. local modules_count = 0
  91. for k,v in pairs(available_modules) do
  92. table.insert(modules_list,v.name)
  93. modules_count = modules_count + 1
  94. end
  95. -- abort if no modules to remove
  96. if modules_count == 0 then
  97. modpol.interactions.message(
  98. self.initiator, "Org has no modules")
  99. modpol.interactions.org_dashboard(
  100. self.initiator, self.org.id)
  101. if self.data.result then self.data.result() end
  102. self.org:delete_process(self.id)
  103. return
  104. end
  105. table.sort(modules_list)
  106. -- now ask which to remove
  107. modpol.interactions.dropdown_query(
  108. self.initiator, "Choose a module to remove:",
  109. modules_list,
  110. function(mod_choice)
  111. -- confirm choice
  112. modpol.interactions.binary_poll_user(
  113. self.initiator,
  114. "Confirm: propose to remove module \"" .. mod_choice .. "\"?",
  115. function(input)
  116. if input == "Yes" then
  117. self:propose_change("remove",mod_choice)
  118. modpol.interactions.org_dashboard(
  119. self.initiator, self.org.id)
  120. else
  121. self:remove_module()
  122. end
  123. end
  124. )
  125. end
  126. )
  127. end
  128. --- Propose a change.
  129. -- Type "add" or "remove"
  130. -- @function change_modules:propose_change
  131. -- @param type
  132. -- @param mod_text
  133. function change_modules:propose_change(type, mod_text)
  134. self:call_module(
  135. "consent",self.initiator,
  136. {
  137. prompt = "Do you consent to "..type..
  138. " this module in org "..self.org.name..
  139. ":\n"..mod_text,
  140. votes_required = #self.org.members
  141. },
  142. function()
  143. if type == "add" then
  144. for k,v in pairs(modpol.modules) do
  145. if v.name == mod_text then
  146. table.insert(self.org.modules,v)
  147. end
  148. end
  149. modpol.interactions.message_org(
  150. self.initiator,self.org.id,
  151. "Consent reached:\nAdding \""
  152. ..mod_text.."\" to org "..self.org.name)
  153. elseif type == "remove" then
  154. local i = 0
  155. for k,v in pairs(self.org.modules) do
  156. i = i + 1
  157. if v.name == mod_text then
  158. self.org.modules[k] = nil
  159. end
  160. end
  161. modpol.interactions.message_org(
  162. self.initiator,self.org.id,
  163. "Consent reached:\nRemoving \""
  164. ..mod_text.."\" from org "..self.org.name)
  165. end
  166. end)
  167. if self.data.result then self.data.result() end
  168. self.org:delete_process(self.id)
  169. end
  170. modpol.modules.change_modules = change_modules