2
0

consent.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = 1
  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, 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:delete_process(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. modpol.interactions.message_org(
  46. "consent", self.org.id,
  47. member.." decided "..resp.." on: "..
  48. self.config.prompt.." ("..self.data.votes..
  49. "/"..self.config.votes_required..")"
  50. )
  51. if self.data.votes >= self.config.votes_required then
  52. if self.data.result then
  53. self.data.result() end
  54. self.org:delete_process(self.id)
  55. end
  56. modpol.interactions.org_dashboard(
  57. member, self.org.id)
  58. end
  59. )
  60. end
  61. modpol.modules.consent = consent