consent.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --- @module consent
  2. -- A utility module for checking 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 = 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:delete_process(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. modpol.interactions.message_org(
  40. "consent", self.org.id,
  41. member.." decided "..resp.." on: "..
  42. self.config.prompt.." ("..self.data.votes..
  43. "/"..self.config.votes_required..")"
  44. )
  45. if self.data.votes >= self.config.votes_required then
  46. if self.data.result then
  47. self.data.result() end
  48. self.org:delete_process(self.id)
  49. end
  50. modpol.interactions.org_dashboard(
  51. member, self.org.name)
  52. end
  53. )
  54. end
  55. modpol.modules.consent = consent