consent.lua 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --- A utility module for checking consent
  2. -- @module consent
  3. local consent = {
  4. name = "Consent process",
  5. slug = "consent",
  6. desc = "A utility 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 = false
  15. }
  16. --- Initiate consent
  17. -- @function consent:initiate
  18. -- @param result Callback if this module is embedded in other modules
  19. function consent:initiate(result)
  20. self.data.result = result
  21. -- if org is empty or no votes required, consent given
  22. if self.org:get_member_count() == 0
  23. or self.config.votes_required == 0 then
  24. modpol.interactions.message_org(
  25. self.initiator,
  26. self.org.name,
  27. "Consent reached: " .. self.config.prompt)
  28. if self.data.result then
  29. self.data.result() end
  30. self.org:delete_process(self.id)
  31. else
  32. -- otherwise, create poll
  33. -- set default votes_required
  34. if not self.config.votes_required then
  35. self.config.votes_required = self.org:get_member_count()
  36. end
  37. for id, member in pairs(self.org.members) do
  38. self.org:add_pending_action(self.id, member, "callback")
  39. end
  40. end
  41. end
  42. --- Callback
  43. -- @function consent:callback
  44. -- @param member
  45. function consent:callback(member)
  46. modpol.interactions.binary_poll_user(
  47. member,
  48. self.config.prompt,
  49. function (resp)
  50. self.org:remove_pending_action(self.id,member)
  51. if resp == "Yes" then
  52. self.data.votes = self.data.votes + 1
  53. end
  54. modpol.interactions.message_org(
  55. "consent", self.org.id,
  56. member.." decided "..resp.." on: "..
  57. self.config.prompt.." ("..self.data.votes..
  58. "/"..self.config.votes_required..")"
  59. )
  60. if self.data.votes >= self.config.votes_required then
  61. modpol.interactions.message_org(
  62. self.initiator,
  63. self.org.name,
  64. "Consent reached: " .. self.config.prompt)
  65. if self.data.result then
  66. self.data.result() end
  67. self.org:delete_process(self.id)
  68. end
  69. modpol.interactions.org_dashboard(
  70. member, self.org.id)
  71. end
  72. )
  73. end
  74. modpol.modules.consent = consent