join_org_class.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -- JOIN ORG
  2. -- Module that enables a user to join an org
  3. JoinOrg = {}
  4. JoinOrg_mt = { __index = JoinOrg }
  5. function JoinOrg.create(initiator, org, id)
  6. local inst = {
  7. name = "Join an org",
  8. desc = "Initiator chooses an org to become a member of. Nothing happens if they are already in an org.",
  9. initiator = initiator,
  10. org = org,
  11. id = id,
  12. votes_yes = 0
  13. }
  14. setmetatable(inst, JoinOrg_mt)
  15. return inst
  16. end
  17. function JoinOrg:initiate(result)
  18. modpol.interactions.binary_poll_user(
  19. self.initiator,
  20. "Would you like to join",
  21. function (resp)
  22. if resp == "Yes" then
  23. for id, member in pairs(self.org.members) do
  24. self.org:add_pending_action(self.id, member, "callback")
  25. end
  26. end
  27. end
  28. )
  29. if result then result() end
  30. end
  31. function JoinOrg:callback(member)
  32. modpol.interactions.binary_poll_user(
  33. member,
  34. "Do you want " .. self.initiator .. " to join?",
  35. function (resp)
  36. if resp == "Yes" then
  37. self.votes_yes = self.votes_yes + 1
  38. end
  39. self:evaluate_vote()
  40. end
  41. )
  42. end
  43. function JoinOrg:evaluate_vote()
  44. if self.votes_yes >= 1 then
  45. print('added user')
  46. self.org:add_member(self.initiator)
  47. self.org:wipe_pending_actions(self.id)
  48. end
  49. end
  50. -- ===================================
  51. -- When calling a module internally
  52. modpol.modules.join_org_class = JoinOrg