consent.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.result = result
  17. -- if org is empty, consent is given automatically
  18. if self.org:get_member_count() == 0 then
  19. self.result()
  20. self.org:wipe_pending_actions(self.id)
  21. else
  22. -- otherwise, create poll
  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. function consent:callback(member)
  29. modpol.interactions.binary_poll_user(
  30. member,
  31. self.config.prompt,
  32. function (resp)
  33. self.org:remove_pending_action(self.id,member)
  34. if resp == "Yes" then
  35. self.data.votes = self.data.votes + 1
  36. end
  37. if self.data.votes >= self.config.votes_required then
  38. if self.result then self.result() end
  39. self.org:wipe_pending_actions(self.id)
  40. end
  41. end
  42. )
  43. end
  44. modpol.modules.consent = consent