added consent module, nesting mdoules now functional!
This commit is contained in:
@ -15,5 +15,7 @@ dofile (localdir .. "/interactions/interactions.lua")
|
||||
--modules
|
||||
dofile (localdir .. "/modules/join_org.lua")
|
||||
dofile (localdir .. "/modules/join_org_class.lua")
|
||||
dofile (localdir .. "/modules/join_org_consent.lua")
|
||||
dofile (localdir .. "/modules/consent.lua")
|
||||
dofile (localdir .. "/modules/remove_org.lua")
|
||||
dofile (localdir .. "/modules/child_org.lua")
|
||||
|
42
modpol/modules/consent.lua
Normal file
42
modpol/modules/consent.lua
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
|
||||
Consent = {}
|
||||
|
||||
Consent.setup = {
|
||||
name = "Consent",
|
||||
slug = "consent",
|
||||
desc = "Other modules can use to implement consent based decision making",
|
||||
votes = 0
|
||||
}
|
||||
|
||||
Consent.config = {
|
||||
prompt = "Would you like to approve this action?",
|
||||
votes_required = 1
|
||||
}
|
||||
|
||||
function Consent:initiate(result)
|
||||
self.result = result
|
||||
for id, member in pairs(self.org.members) do
|
||||
self.org:add_pending_action(self.id, member, "callback")
|
||||
end
|
||||
end
|
||||
|
||||
function Consent:callback(member)
|
||||
modpol.interactions.binary_poll_user(
|
||||
member,
|
||||
self.config.prompt,
|
||||
function (resp)
|
||||
if resp == "Yes" then
|
||||
self.votes = self.votes + 1
|
||||
end
|
||||
|
||||
if self.votes >= self.config.votes_required then
|
||||
self.result()
|
||||
self.org:wipe_pending_actions(self.id)
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
modpol.modules.consent = Consent
|
30
modpol/modules/join_org_consent.lua
Normal file
30
modpol/modules/join_org_consent.lua
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
|
||||
JoinOrg = {}
|
||||
|
||||
JoinOrg.setup = {
|
||||
name = "Join an org",
|
||||
slug = "join_org_consent",
|
||||
desc = "Consent based join org module"
|
||||
}
|
||||
|
||||
function JoinOrg:initiate()
|
||||
self.org:call_module(
|
||||
"consent",
|
||||
self.initiator,
|
||||
{
|
||||
prompt = "Allow " .. self.initiator .. " to join?",
|
||||
votes_required = 1
|
||||
},
|
||||
function ()
|
||||
self:complete()
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
function JoinOrg:complete()
|
||||
self.org:add_member(self.initiator)
|
||||
print("Added " .. self.initiator .. " to the org!")
|
||||
end
|
||||
|
||||
modpol.modules.join_org_consent = JoinOrg
|
@ -1,6 +1,6 @@
|
||||
function modpol.orgs:call_module(module_name, initiator, config, result)
|
||||
if not modpol.modules[module_name] then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. name .. '" not found')
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_name .. '" not found')
|
||||
return
|
||||
end
|
||||
|
||||
@ -23,12 +23,22 @@ function modpol.orgs:call_module(module_name, initiator, config, result)
|
||||
|
||||
local module = modpol.modules[module_name]
|
||||
|
||||
-- sets default values for undeclared config variables
|
||||
if module.config then
|
||||
for k, v in pairs(module.config) do
|
||||
if config[k] == nil then
|
||||
config[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- setting default params
|
||||
local new_process = {
|
||||
metatable = {__index = module},
|
||||
initiator = initiator,
|
||||
org = self,
|
||||
id = index
|
||||
id = index,
|
||||
config = config
|
||||
}
|
||||
|
||||
-- copying default fields from setup
|
||||
@ -39,15 +49,7 @@ function modpol.orgs:call_module(module_name, initiator, config, result)
|
||||
setmetatable(new_process, new_process.metatable)
|
||||
|
||||
self.processes[index] = new_process
|
||||
|
||||
-- sets default values for undeclared config variables
|
||||
for k, v in pairs(module.config) do
|
||||
if config[k] == nil then
|
||||
config[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
self.processes[index]:initiate(config, result))
|
||||
self.processes[index]:initiate(result)
|
||||
|
||||
return index
|
||||
end
|
||||
|
@ -1,31 +0,0 @@
|
||||
dofile("../modpol.lua")
|
||||
|
||||
print('\nRemoving existing orgs')
|
||||
modpol.orgs.reset()
|
||||
|
||||
print('\nCreating an org called "test_org"')
|
||||
test_org = modpol.instance:add_org('test_org', 'luke')
|
||||
|
||||
print('\nAdding user "nathan" to test_org')
|
||||
test_org:add_member('nathan')
|
||||
|
||||
print("\nOkay, let's start with requests. Setting up an org...")
|
||||
modpol.instance:add_org('test_org', 'luke')
|
||||
test_org:add_member('nathan')
|
||||
|
||||
print('\nMaking consent the add_member policy')
|
||||
test_org:set_policy("add_member", "consent", false);
|
||||
|
||||
print('\nMaking a request to add Josh')
|
||||
add_josh = {
|
||||
user = "josh",
|
||||
type = "add_member",
|
||||
params = {"josh"}
|
||||
}
|
||||
request_id = test_org:make_request(add_josh)
|
||||
|
||||
|
||||
print('\nHave the two members vote on it')
|
||||
modpol.interactions.org_dashboard("nathan","test_org")
|
||||
modpol.interactions.org_dashboard("luke","test_org")
|
||||
|
13
modpol/tests/nested_module_test.lua
Normal file
13
modpol/tests/nested_module_test.lua
Normal file
@ -0,0 +1,13 @@
|
||||
dofile('../modpol.lua');
|
||||
|
||||
modpol.orgs.reset()
|
||||
|
||||
test_org = modpol.instance:add_org('test_org', 'luke')
|
||||
test_org:add_member('nathan')
|
||||
|
||||
print(table.concat(test_org:list_members(), ", "))
|
||||
|
||||
test_org:call_module(
|
||||
"join_org_consent",
|
||||
"paul"
|
||||
)
|
Reference in New Issue
Block a user