consent.lua 1.4 KB

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