functional test of call_module and new pending action/interact version

This commit is contained in:
Luke Miller 2021-12-06 16:45:52 -05:00
parent 08151ad9d9
commit fa7a0f82f6
5 changed files with 75 additions and 61 deletions

View File

@ -7,9 +7,7 @@ dofile (localdir .. "/users/users.lua")
--orgs
dofile (localdir .. "/orgs/base.lua")
dofile (localdir .. "/orgs/requests.lua")
dofile (localdir .. "/orgs/consent.lua")
dofile (localdir .. "/orgs/defer.lua")
dofile (localdir .. "/orgs/process.lua")
--interactions
dofile (localdir .. "/interactions/interactions.lua")

View File

@ -11,7 +11,8 @@ function JoinOrg.create(initiator, org, id)
desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.",
initiator = initiator,
org = org,
id = id
id = id,
votes_yes = 0
}
setmetatable(inst, JoinOrg_mt)
return inst
@ -24,63 +25,27 @@ function JoinOrg:initiate(result)
"Would you like to join",
function (resp)
if resp == "Yes" then
modpol.call_module(
"consent",
"",
org,
result
)
modpol.modules.consent(
"Let " .. initiator .. " join " .. org.name .. "?",
org,
params,
self:on_failure,
self:on_success
)
-- for i, member in ipairs(org.members) do
-- org:add_pending_action(
-- member,
-- function ()
-- modpol.interactions.binary_poll_user(
-- member,
-- "Let " .. initiator .. " join " .. org.name .. "?",
-- function (resp)
-- self:vote()
-- end
-- )
-- end
-- )
-- end
for id, member in pairs(self.org.members) do
self.org:add_pending_action(self.id, member, "callback")
end
end
end
)
for user in pairs(self.org.users) {
self.org:add_pending_action(self.id, user, "callback")
org.pending.user[2348] = "callback"
self:callback(user)
}
if result then result() end
end
function JoinOrg:callback(user)
function JoinOrg:callback(member)
modpol.interactions.binary_poll_user(
self.initiator,
"Do you want this" .. self.user_to_add .. "to join?",
member,
"Do you want " .. self.initiator .. " to join?",
function (resp)
if resp == "yes" then
self.votes_yes += 1
if resp == "Yes" then
self.votes_yes = self.votes_yes + 1
end
self:evaluate_vote()
@ -91,9 +56,12 @@ end
function JoinOrg:on_success()
self.org:add_member(self.initiator)
self.org:delete_process(self.id)
function JoinOrg:evaluate_vote()
if self.votes_yes >= 1 then
print('added user')
self.org:add_member(self.initiator)
self.org:wipe_pending_actions(self.id)
end
end
-- ===================================

48
modpol/orgs/example.lua Normal file
View File

@ -0,0 +1,48 @@
--[[
modpol calls this function when someone starts a new request for this module
--]]
function module:initiate(org) {
form = {
{
"name": "to_remove",
"display": "Which user should be removed?",
"type": "drop-down",
"values": org:list_members()
},
{
"name": "reason",
"type": "text-box",
"prompt": "Reason for removing member:"
}
}
return form
}
--[[
modpol prompts the user with this form, and after receiving the data asynchronously
the returned form would look like:
{
"to_remove": luke,
"reason": stealing food
}
based on provided "name" fields and the input given by the user
now module:request is called
--]]
function module:request(form) {
self.data = form
}
--[[
after the module request function runs, modpol will initiate the consent process
if consent is approved, the implement function is called
--]]
function module:implement() {
org:remove_member(self.data.to_remove)
}

View File

@ -26,6 +26,8 @@ function modpol.orgs:call_module(module_name, initiator)
self.processes[index] = new_process
self.processes[index]:initiate()
return index
end
@ -38,7 +40,7 @@ function modpol.orgs:add_pending_action(process_id, user, callback)
self.pending[user][process_id] = callback
end
function mopdol.orgs:remove_pending_action(process_id, user)
function modpol.orgs:remove_pending_action(process_id, user)
if self.pending[user] then
self.pending[user][process_id] = nil
end
@ -69,7 +71,7 @@ function modpol.orgs:interact(process_id, user)
if self.pending[user] then
local callback = self.pending[user][process_id]
if callback then
process[callback](process)
process[callback](process, user)
end
end
end

View File

@ -7,11 +7,9 @@ test_org:add_member('nathan')
print(table.concat(test_org:list_members(), ", "))
function completion()
print("completed")
end
-- modpol.modules.join_org.initiate("paul", test_org)
modpol.modules.join_org.initiate("paul", test_org, completion)
-- process = modpol.modules.join_org_class.create("paul", test_org)
-- process:initiate(completion)
test_org:call_module(
"join_org_class",
"paul"
)