A bunch of module bugfixes

This commit is contained in:
Nathan Schneider
2021-12-18 20:41:49 -07:00
parent af6e639e27
commit e994061f38
13 changed files with 87 additions and 78 deletions

View File

@ -228,11 +228,11 @@ end
-- ====================
-- Function: modpol.interactions.message_org
-- input: initiator (string), org_id (number), message (string)
-- input: initiator (string), org (number or string), message (string)
-- output: broadcasts message to all org members
function modpol.interactions.message_org(initiator, org_id, message)
local org = modpol.orgs.get_org(org_id)
local users = org:list_members()
function modpol.interactions.message_org(initiator, org, message)
local this_org = modpol.orgs.get_org(org)
local users = this_org:list_members()
for k,v in ipairs(users) do
modpol.interactions.message(v, message)
end

View File

@ -15,12 +15,6 @@ add_child_org.config = {
-- @function initiate
function add_child_org:initiate(config, result)
self.org:add_pending_action(
self.id, self.initiator,
"name_child_org")
end
function add_child_org:name_child_org()
modpol.interactions.text_query(
self.initiator,"Child org name: ",
function(input)
@ -28,34 +22,38 @@ function add_child_org:name_child_org()
modpol.interactions.message(
self.initiator,
"No name entered for child org")
self.org:wipe_pending_actions(self.id)
self.org:delete_process(self.id)
return end
self.data.child_name = input
modpol.interactions.message(
self.initiator,
"Proposed child org: " .. input)
self.org:wipe_pending_actions(self.id)
self:propose_child_org()
-- initiate consent process
self.org:call_module(
"consent",
self.initiator,
{
prompt = "Create child org " ..
self.data.child_name .. "?",
votes_required = #self.org.members
},
function()
self:create_child_org()
end
)
modpol.interactions.org_dashboard(
self.initiator, self.org.name)
end
)
end
function add_child_org:propose_child_org()
self.org:call_module(
"consent",
self.initiator,
{
prompt = "Create child org " .. self.data.child_name .. "?",
votes_required = #self.org.members
},
function()
self.org:add_org(self.data.child_name, self.initiator)
modpol.interactions.message_org(
self.initiator,
self.org.id,
"Child org created: "..self.data.child_name)
end
)
function add_child_org:create_child_org()
self.org:add_org(self.data.child_name, self.initiator)
modpol.interactions.message_org(
self.initiator,
self.org.name,
"Child org created: "..self.data.child_name)
self.org:delete_process(self.id)
end
--- (Required) Add to module table

View File

@ -8,7 +8,7 @@ local consent = {
}
consent.data = {
votes = 0
votes = 0
}
consent.config = {
@ -16,11 +16,12 @@ consent.config = {
votes_required = 1
}
function consent:initiate(config, result)
self.result = result
function consent:initiate(result)
self.data.result = result
-- if org is empty, consent is given automatically
if self.org:get_member_count() == 0 then
self.result()
if self.data.result then
self.data.result() end
self.org:wipe_pending_actions(self.id)
else
-- otherwise, create poll
@ -39,14 +40,14 @@ function consent:callback(member)
if resp == "Yes" then
self.data.votes = self.data.votes + 1
end
if self.data.votes >= self.config.votes_required then
if self.data.result then
self.data.result() end
self.org:wipe_pending_actions(self.id)
if self.result then self.result() end
self.org:delete_process(self.id)
end
end
)
end
modpol.modules.consent = consent

View File

@ -7,19 +7,19 @@ join_org.setup = {
desc = "If consent process is passed, initiator joins this org."
}
function join_org.initiate(initiator, org, result)
function join_org.initiate(result)
modpol.interactions.binary_poll_user(
initiator,
"Would you like to join " .. org.name,
function (resp)
if resp == "Yes" then
org:add_member(initiator)
self.org:add_member(self.initiator)
end
end
)
for i, member in ipairs(org.members) do
org:add_pending_action(
for i, member in ipairs(self.org.members) do
self.org:add_pending_action(
member,
function ()
modpol.interactions.binary_poll_user(

View File

@ -14,7 +14,7 @@ join_org_consent.data = {
join_org_consent.config = {
}
function join_org_consent:initiate()
function join_org_consent:initiate(result)
self.org:call_module(
"consent",
self.initiator,
@ -26,6 +26,7 @@ function join_org_consent:initiate()
self:complete()
end
)
if result then result() end
end
function join_org_consent:complete()

View File

@ -15,13 +15,9 @@ leave_org.config = {
--- (Required): initiate function
-- Modules have access to the following instance variables:
-- <li><code>self.org</code> (the org the module was called in),</li>
-- <li><code>self.initiator</code> (the user that callced the module),</li>
-- <li><code>self.id</code> (the process id of the module instance)</li>
-- @param config (optional) If user wants to override fields in the config table
-- @param result (optional) Callback if this module is embedded in other modules
-- @function initiate
function leave_org:initiate(config, result)
function leave_org:initiate(result)
if self.org == modpol.instance then
modpol.interactions.message(
self.initiator,
@ -34,7 +30,6 @@ function leave_org:initiate(config, result)
modpol.interactions.message(
self.initiator,
"You have left org " .. self.org.name)
-- call result function
end
if result then result() end
end

View File

@ -1,5 +1,5 @@
--- @module Remove Org
-- A simple module that calls a consent process on an org to remove it.
-- A simple module that removes an org.
--- Main module table
@ -14,7 +14,7 @@ remove_org.data = {}
--- Initiate function
-- @function initiate
function remove_org:initiate(config, result)
function remove_org:initiate(result)
if self.org == modpol.instance then
modpol.interactions.message(
self.initiator,

View File

@ -14,7 +14,7 @@ remove_org_consent.data = {
remove_org_consent.config = {
}
function remove_org_consent:initiate()
function remove_org_consent:initiate(result)
self.org:call_module(
"consent",
self.initiator,
@ -26,6 +26,9 @@ function remove_org_consent:initiate()
self:complete()
end
)
modpol.interactions.org_dashboard(
self.initiator, self.org.name)
if result then result() end
end
function remove_org_consent:complete()

View File

@ -35,10 +35,9 @@ module_template.config = {
-- <li><code>self.org</code> (the org the module was called in),</li>
-- <li><code>self.initiator</code> (the user that callced the module),</li>
-- <li><code>self.id</code> (the process id of the module instance)</li>
-- @param config (optional) If user wants to override fields in the config table
-- @param result (optional) Callback if this module is embedded in other modules
-- @function initiate
function module_template:initiate(config, result)
function module_template:initiate(result)
-- call interaction functions here!
-- call result function

View File

@ -60,6 +60,8 @@ end
function modpol.orgs:add_pending_action(process_id, user, callback)
self.pending[user] = self.pending[user] or {}
self.pending[user][process_id] = callback
modpol.interactions.message(
user, "New pending action in org "..self.name)
end
function modpol.orgs:remove_pending_action(process_id, user)

View File

@ -97,7 +97,7 @@ modpol.ocutil.log = function (s)
if modpol.ocutil.logdir ~= nil and
modpol.ocutil.logname ~= nil then
s = s .. "\n" -- Add trailing newline
s = s .. "\n" -- Add trailing newline
local logpath = modpol.ocutil.logdir .. "/" .. modpol.ocutil.logname
modpol.ocutil.file_append (logpath, s)