From fa7a0f82f64b2c4beba6e7f201deb63ee899db7b Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 6 Dec 2021 16:45:52 -0500 Subject: [PATCH] functional test of call_module and new pending action/interact version --- modpol/api.lua | 4 +- modpol/modules/join_org_class.lua | 66 ++++++++----------------------- modpol/orgs/example.lua | 48 ++++++++++++++++++++++ modpol/orgs/process.lua | 6 ++- modpol/tests/new_module_test.lua | 12 +++--- 5 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 modpol/orgs/example.lua diff --git a/modpol/api.lua b/modpol/api.lua index 5e278ec..e1daaec 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -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") diff --git a/modpol/modules/join_org_class.lua b/modpol/modules/join_org_class.lua index cda1647..0533e8a 100644 --- a/modpol/modules/join_org_class.lua +++ b/modpol/modules/join_org_class.lua @@ -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 -- =================================== diff --git a/modpol/orgs/example.lua b/modpol/orgs/example.lua new file mode 100644 index 0000000..b16333b --- /dev/null +++ b/modpol/orgs/example.lua @@ -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) +} \ No newline at end of file diff --git a/modpol/orgs/process.lua b/modpol/orgs/process.lua index 1c66c16..d514f5b 100644 --- a/modpol/orgs/process.lua +++ b/modpol/orgs/process.lua @@ -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 \ No newline at end of file diff --git a/modpol/tests/new_module_test.lua b/modpol/tests/new_module_test.lua index 5dcf4a4..48bc292 100644 --- a/modpol/tests/new_module_test.lua +++ b/modpol/tests/new_module_test.lua @@ -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) \ No newline at end of file +test_org:call_module( + "join_org_class", + "paul" +) \ No newline at end of file