consent.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. --- @module consent
  2. -- A utility module for checking consent
  3. local consent = {
  4. name = "Consent",
  5. slug = "consent",
  6. desc = "Other modules can use to implement consent based decision making",
  7. }
  8. consent.data = {
  9. votes = 0
  10. }
  11. consent.config = {
  12. prompt = "Do you consent?",
  13. votes_required = 1
  14. }
  15. function consent:initiate(result)
  16. self.data.result = result
  17. -- if org is empty, consent is given automatically
  18. if self.org:get_member_count() == 0 then
  19. if self.data.result then
  20. self.data.result() end
  21. self.org:wipe_pending_actions(self.id)
  22. else
  23. -- otherwise, create poll
  24. for id, member in pairs(self.org.members) do
  25. self.org:add_pending_action(self.id, member, "callback")
  26. end
  27. end
  28. end
  29. function consent:callback(member)
  30. modpol.interactions.binary_poll_user(
  31. member,
  32. self.config.prompt,
  33. function (resp)
  34. self.org:remove_pending_action(self.id,member)
  35. if resp == "Yes" then
  36. self.data.votes = self.data.votes + 1
  37. end
  38. if self.data.votes >= self.config.votes_required then
  39. if self.data.result then
  40. self.data.result() end
  41. self.org:wipe_pending_actions(self.id)
  42. self.org:delete_process(self.id)
  43. end
  44. end
  45. )
  46. end
  47. modpol.modules.consent = consent