consent.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. if self.data.votes >= self.config.votes_required then
  40. if self.data.result then
  41. self.data.result() end
  42. self.org:delete_process(self.id)
  43. end
  44. modpol.interactions.org_dashboard(
  45. member, self.org.name)
  46. end
  47. )
  48. end
  49. modpol.modules.consent = consent