change_modules.lua 5.2 KB

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