Added change_modules mod and lots of bugfixes. Now merging to master.

This commit is contained in:
Nathan Schneider
2021-12-19 23:23:23 -07:00
parent 1d6d0f21e2
commit 2365eb83d0
9 changed files with 219 additions and 18 deletions

View File

@ -12,6 +12,7 @@ dofile (localdir .. "/interactions/interactions.lua")
--modules
--TODO make this automatic and directory-based
dofile (localdir .. "/modules/add_child_org_consent.lua")
dofile (localdir .. "/modules/change_modules.lua")
dofile (localdir .. "/modules/consent.lua")
dofile (localdir .. "/modules/join_org_consent.lua")
dofile (localdir .. "/modules/leave_org.lua")

View File

@ -44,6 +44,9 @@ print (topdir)
-- OldCoder utilities
dofile (topdir .. "/util/ocutil/ocutil.lua")
-- Misc. utilities
dofile (topdir .. "/util/misc.lua")
-- ===================================================================
-- Persistent storage
-- must implement modpol.load_storage() and modpol.store_data()

View File

@ -0,0 +1,171 @@
--- change_modules
-- @module change_modules
-- Depends on consent
local change_modules = {
name = "Change modules (consent)",
slug = "change_modules",
desc = "Add or remove modules from the org with member consent",
hide = false;
}
change_modules.data = {
result = nil
}
change_modules.config = {
}
function change_modules:initiate(result)
self.data.result = result
-- Step 1: add or remove?
modpol.interactions.dropdown_query(
self.initiator, "Module change options:",
{"Add module","Remove module"},
function(input)
if input == "Add module" then
self:add_module()
elseif input == "Remove module" then
self:remove_module()
end
end
)
end
function change_modules:add_module()
-- prepare module options
local available_modules = modpol.copy_table(modpol.modules)
for k,org_mod in pairs(self.org.modules) do
if available_modules[org_mod.slug] then
available_modules[org_mod.slug] = nil
end end
-- present module options
local modules_list = {}
for k,v in pairs(available_modules) do
table.insert(modules_list,v.name)
end
if #modules_list == 0 then
modpol.interactions.message(
self.initiator, "Org has all modules")
modpol.interactions.org_dashboard(
self.initiator, self.org.id)
if self.data.result then self.data.result() end
self.org:delete_process(self.id)
return
end
table.sort(modules_list)
-- now ask which to add
modpol.interactions.dropdown_query(
self.initiator, "Choose a module to add:",
modules_list,
function(mod_choice)
-- confirm choice
modpol.interactions.binary_poll_user(
self.initiator,
"Confirm: propose to add module \"" ..
mod_choice .. "\"?",
function(input)
if input == "Yes" then
self:propose_change("add",mod_choice)
modpol.interactions.org_dashboard(
self.initiator, self.org.id)
else
self:add_module()
end
end
)
end
)
end
function change_modules:remove_module()
-- prepare module options
local available_modules = {}
for k,org_mod in pairs(self.org.modules) do
if not org_mod.hide then
available_modules[org_mod.slug] = modpol.copy_table(org_mod)
end end
local modules_list = {}
local modules_count = 0
for k,v in pairs(available_modules) do
table.insert(modules_list,v.name)
modules_count = modules_count + 1
end
-- abort if no modules to remove
if modules_count == 0 then
modpol.interactions.message(
self.initiator, "Org has no modules")
modpol.interactions.org_dashboard(
self.initiator, self.org.id)
if self.data.result then self.data.result() end
self.org:delete_process(self.id)
return
end
table.sort(modules_list)
-- now ask which to remove
modpol.interactions.dropdown_query(
self.initiator, "Choose a module to remove:",
modules_list,
function(mod_choice)
-- confirm choice
modpol.interactions.binary_poll_user(
self.initiator,
"Confirm: propose to remove module \"" .. mod_choice .. "\"?",
function(input)
if input == "Yes" then
self:propose_change("remove",mod_choice)
modpol.interactions.org_dashboard(
self.initiator, self.org.id)
else
self:remove_module()
end
end
)
end
)
end
--- propose_change
-- @field type "add" or "remove"
function change_modules:propose_change(type, mod_text)
self.org:call_module(
"consent",self.initiator,
{
prompt = "Do you consent to "..type..
" this module in org "..self.org.name..
":\n"..mod_text,
votes_required = #self.org.members
},
function()
if type == "add" then
for k,v in pairs(modpol.modules) do
if v.name == mod_text then
table.insert(self.org.modules,v)
end
end
modpol.interactions.message_org(
self.initiator,self.org.id,
"Consent reached:\nAdding \""
..mod_text.."\" to org "..self.org.name)
elseif type == "remove" then
modpol.msg("Removing!")
local i = 0
for k,v in pairs(self.org.modules) do
i = i + 1
if v.name == mod_text then
modpol.msg("got it!")
self.org.modules[k] = nil
end
end
modpol.interactions.message_org(
self.initiator,self.org.id,
"Consent reached:\nRemoving \""
..mod_text.."\" from org "..self.org.name)
end
end)
if self.data.result then self.data.result() end
self.org:delete_process(self.id)
end
--- (Required) Add to module table
modpol.modules.change_modules = change_modules

View File

@ -33,8 +33,8 @@ function remove_child_consent:initiate(result)
self.data.result = result
modpol.interactions.dropdown_query(
self.initiator,
"Which child of org "..self.org.name..
" do you want to remove?",
"Choose a child of org "..
self.org.name.." to remove:",
children,
function(input)
self.data.child_to_remove = modpol.orgs.get_org(input)

View File

@ -2,7 +2,7 @@
-- @module module_template
--- (Required): data table containing name and description of the module
-- @field name "Human-readable name"
-- @field name "Human-readable name (parens OK, no brackets)"
-- @field slug "Same as module class name"
-- @field desc "Description of the module"
-- @field hide "Whether this is a hidden utility module"

View File

@ -14,7 +14,7 @@ function temp_org()
return {
id = nil,
name = nil,
modules = modpol.modules,
modules = modpol.copy_table(modpol.modules),
processes = {},
pending = {},
members = {},

View File

@ -0,0 +1,9 @@
--- @function modpol.copy_table
-- Returns a copy of the table inputted
function modpol.copy_table(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end