consent.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. --- A utility module for checking consent
  2. -- @module 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. --- Initiate consent
  17. -- @function consent:initiate
  18. -- @param result
  19. function consent:initiate(result)
  20. self.data.result = result
  21. -- if org is empty, consent is given automatically
  22. if self.org:get_member_count() == 0 then
  23. if self.data.result then
  24. self.data.result() end
  25. self.org:wipe_pending_actions(self.id)
  26. else
  27. -- otherwise, create poll
  28. for id, member in pairs(self.org.members) do
  29. self.org:add_pending_action(self.id, member, "callback")
  30. end
  31. end
  32. end
  33. --- Callback
  34. -- @function consent:callback
  35. -- @param member
  36. function consent:callback(member)
  37. modpol.interactions.binary_poll_user(
  38. member,
  39. self.config.prompt,
  40. function (resp)
  41. self.org:remove_pending_action(self.id,member)
  42. if resp == "Yes" then
  43. self.data.votes = self.data.votes + 1
  44. end
  45. if self.data.votes >= self.config.votes_required then
  46. if self.data.result then
  47. self.data.result() end
  48. self.org:wipe_pending_actions(self.id)
  49. self.org:delete_process(self.id)
  50. end
  51. modpol.interactions.org_dashboard(
  52. member, self.org.name)
  53. end
  54. )
  55. end
  56. modpol.modules.consent = consent