consent.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. modpol.modules = modpol.modules or {}
  2. modpol.modules.consent = {}
  3. -- sets consent to its own callback
  4. modpol.modules.consent.__index = modpol.modules.consent
  5. function temp_consent_process()
  6. return {
  7. type = "consent",
  8. org_id = nil,
  9. request_id = nil,
  10. total_votes = 0,
  11. majority_to_pass = 0.51,
  12. votes_yes = {},
  13. votes_no = {}
  14. }
  15. end
  16. -- ===============================================
  17. -- function to create a new consent process to resolve a pending process
  18. function modpol.modules.consent:new_process(request_id, org_id)
  19. local process = temp_consent_process()
  20. process.request_id = request_id
  21. process.org_id = org_id
  22. setmetatable(process, modpol.modules.consent)
  23. modpol.ocutil.log('Created new process for request id #' .. request_id)
  24. return process
  25. end
  26. -- =========================================
  27. -- function to delete a process, called when process finishes
  28. function modpol.modules.consent:delete()
  29. local process_org = modpol.orgs.get_org(self.org_id)
  30. process_org.processes[self.id] = "deleted"
  31. modpol.ocutil.log('Deleted process #' .. self.id)
  32. end
  33. -- ======================================================
  34. -- function for users to vote on a pending request
  35. function modpol.modules.consent:approve(user, decision)
  36. if not modpol.orgs.get_org(self.org_id):has_member(user) then
  37. modpol.ocutil.log('Error in consent:approve -> user not a member of the org')
  38. return
  39. end
  40. if decision then
  41. table.insert(self.votes_yes, user)
  42. modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id)
  43. else
  44. table.insert(self.votes_no, user)
  45. modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id)
  46. end
  47. self.total_votes = self.total_votes + 1
  48. self:update_status()
  49. end
  50. -- ===================================================
  51. -- determines whether process has finished and resolves request if it has (unfinished)
  52. function modpol.modules.consent:update_status()
  53. local process_org = modpol.orgs.get_org(self.org_id)
  54. local eligible_voters = process_org:get_member_count()
  55. local votes_needed = math.ceil(self.majority_to_pass * eligible_voters)
  56. if #self.votes_yes >= votes_needed then
  57. modpol.ocutil.log('Request #' .. self.request_id .. ' passes')
  58. process_org:resolve_request(self.request_id, true)
  59. self:delete()
  60. elseif #self.votes_no >= votes_needed then
  61. modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass')
  62. process_org:resolve_request(self.request_id, false)
  63. self:delete()
  64. else
  65. modpol.ocutil.log('Waiting for more votes...')
  66. end
  67. end
  68. function modpol.modules.consent.pending_processes()
  69. end