change_modules-dropdown.lua 5.0 KB

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