2
0

requests.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. -- THIS IS NOW DEPRECATED, TO MERGE INTO PROCESS
  2. -- REQUESTS
  3. -- Provides request functionality to org
  4. -- TODO
  5. -- include consent functionality, available to modules
  6. -- load available modules in modpol.lua
  7. -- make policies a set of options for consent functions
  8. -- create simple modules for core functions: change_policy, add_member, start_org, join_org, close_org
  9. -- CONSENT
  10. -- Provides consent functionality for modules
  11. -- define default params
  12. modpol.default_policy = {
  13. target_org = nil,
  14. time_limit = nil,
  15. vote_threshold = 1 -- a ratio of the total number of members
  16. }
  17. -- PROCESSES
  18. -- functions that enable requests to create processes
  19. -- ================================
  20. -- creates a new process linked to a request id
  21. function modpol.orgs:create_process(process_type, request_id)
  22. if not modpol.modules[process_type] then
  23. modpol.ocutil.log('Process type "' .. process_type .. '" does not exist')
  24. return
  25. end
  26. local empty_index = nil
  27. -- linear search for empty process slots (lazy deletion)
  28. for k, v in ipairs(self.processes) do
  29. if v == 'deleted' then
  30. empty_index = k
  31. break
  32. end
  33. end
  34. local index
  35. -- attempts to fill empty spots in list, otherwise appends to end
  36. if empty_index then
  37. index = empty_index
  38. else
  39. index = #self.processes + 1
  40. end
  41. -- retrieving requested module
  42. local module = modpol.modules[process_type]
  43. local new_process = module:new_process(index, request_id, self.id)
  44. self.processes[index] = new_process
  45. modpol.ocutil.log('Created process #' .. index .. ' - ' .. process_type .. ' for ' .. self.name)
  46. self:record('Created process #' .. index .. ' - ' .. process_type, 'add_process')
  47. return index
  48. end
  49. -- ===========================
  50. -- adds a new pending action to the org's table
  51. function modpol.orgs:add_pending_action(process_id, user)
  52. -- adds tables if they don't exist already
  53. self.pending[user] = self.pending[user] or {}
  54. -- flagging actual action
  55. self.pending[user][process_id] = true
  56. local message =
  57. modpol.interactions.message(user, "New pending action in " .. self.name)
  58. modpol.ocutil.log("Added pending action to " .. user .. " in process #" .. process_id .. ' from ' .. self.name)
  59. self:record('Added pending action to ' .. user .. ' in process #' .. process_id, "add_pending_action")
  60. end
  61. -- ========================
  62. -- removes a pending action from the org's table
  63. function modpol.orgs:remove_pending_action(process_id, user)
  64. if self.pending[user] then
  65. self.pending[user][process_id] = nil
  66. modpol.ocutil.log("Removed pending action from " .. user .. " in process #" .. process_id .. ' from ' .. self.name)
  67. self:record('Removed pending action from ' .. user .. ' in process #' .. process_id, "del_pending_action")
  68. else
  69. modpol.ocutil.log("Could not remove pending action from " .. user .. " in process #" .. process_id)
  70. end
  71. end
  72. -- =====================
  73. -- removes all pending actions for a given process id from all users
  74. function modpol.orgs:wipe_pending_actions(process_id)
  75. for user in pairs(self.pending) do
  76. self.pending[user][process_id] = nil
  77. end
  78. modpol.ocutil.log("Removed all pending actions for process #" .. process_id)
  79. self:record('Removed all pending actions for process #' .. process_id, "del_pending_action")
  80. end
  81. -- ======================
  82. -- returns a boolean to indicate whether a user has any active pending actions
  83. function modpol.orgs:has_pending_actions(user)
  84. -- next() will return the next pair in a table
  85. -- if next() returns nil, the table is empty
  86. if not self.pending[user] then
  87. return false
  88. else
  89. if not next(self.pending[user]) then
  90. return false
  91. else
  92. return true
  93. end
  94. end
  95. end
  96. -- ===========================
  97. -- compares to requests to see if they are identical
  98. function modpol.orgs.comp_req(req1, req2)
  99. -- compares request type
  100. if req1.type ~= req2.type then
  101. return false
  102. else
  103. -- comparing parameters
  104. -- we can assume the number of params is the same as this is checked in the make_request func
  105. for k, v in ipairs(req1.params) do
  106. if v ~= req2.params[k] then
  107. return false
  108. end
  109. end
  110. end
  111. return true
  112. end
  113. -- ===============================
  114. -- returns string of all active requests
  115. function modpol.orgs:list_request()
  116. local str
  117. for id, req in ipairs(self.requests) do
  118. if req ~= "deleted" then
  119. if str then
  120. str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
  121. else
  122. str = req.type .. ' (' .. req.user .. ') '
  123. end
  124. end
  125. end
  126. return str
  127. end
  128. -- ===============================
  129. -- if the request was approved, the associated function is called, otherwise it is deleted
  130. -- TODO Rather than hard-coding functions below, this should be given an arbitrary result function based on the request
  131. function modpol.orgs:resolve_request(request_id, approve)
  132. -- wipe actions
  133. self:wipe_pending_actions(request_id)
  134. if approve then
  135. local request = self.requests[request_id]
  136. local p = request.params
  137. -- there's probably a way to clean this up, the issue is the varying number of commands
  138. -- ex: self['add_member'](self, 'member_name')
  139. -- not sure if this is safe, more testing to do
  140. -- self[request.type](self, p[1], p[2], p[3])
  141. if request.type == "add_org" then
  142. self:add_org(request.params[1], request.user)
  143. elseif request.type == "delete" then
  144. self:delete()
  145. elseif request.type == "add_member" then
  146. self:add_member(request.params[1])
  147. elseif request.type == "remove_member" then
  148. self:remove_member(request.params[1])
  149. end
  150. end
  151. self.processes[request_id] = "deleted"
  152. modpol.ocutil.log('Deleted process #' .. request_id)
  153. self.requests[request_id] = "deleted"
  154. modpol.ocutil.log("Resolved request #" .. request_id .. ' in ' .. self.name)
  155. self:record("Resolved request #" .. request_id, "resolve_request")
  156. end
  157. -- ================================
  158. -- tries to make a request to the org
  159. function modpol.orgs:make_request(request)
  160. -- checking to see if identical request already exists
  161. for k, v in ipairs(self.requests) do
  162. if self.comp_req(request, v) == true then
  163. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
  164. return false
  165. end
  166. end
  167. -- checking to see if user is able to make request
  168. local requested_policy = self.policies[request.type]
  169. -- if not the instance org (instance's don't have parents)
  170. if self.id ~= 1 then
  171. local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
  172. -- tries to use org's policy table, defers to parent otherwise
  173. if not requested_policy then
  174. modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org')
  175. requested_policy = parent_policy
  176. if not parent_policy then
  177. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
  178. return false
  179. end
  180. end
  181. -- fails if instance policy undefined
  182. else
  183. if not requested_policy then
  184. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> policy undefined')
  185. return false
  186. end
  187. end
  188. -- make sure user is allowed to make request
  189. if requested_policy.must_be_member and not self:has_member(request.user) then
  190. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
  191. return false
  192. end
  193. local empty_index = nil
  194. -- linear search for empty process slots (lazy deletion)
  195. for k, v in ipairs(self.requests) do
  196. if v == 'deleted' then
  197. empty_index = k
  198. break
  199. end
  200. end
  201. -- attempts to fill empty spots in list, otherwise appends to end
  202. local request_id = nil
  203. if empty_index then
  204. self.requests[empty_index] = request
  205. request_id = empty_index
  206. else
  207. table.insert(self.requests, request)
  208. request_id = #self.requests
  209. end
  210. modpol.interactions.message(request.user, "Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name)
  211. modpol.ocutil.log("Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name)
  212. self:record("Request made by " .. request.user .. " to " .. request.type, "make_request")
  213. -- launching process tied to this request
  214. local process_id = self:create_process(requested_policy.process_type, request_id)
  215. -- returns process id of processes launched by this request
  216. return process_id
  217. end
  218. -- wrapper for process:interact function, ensures that user actually has a pending action for that process
  219. function modpol.orgs:interact(process_id, user)
  220. process = self.processes[process_id]
  221. if self.pending[user] then
  222. if self.pending[user][process_id] == true then
  223. process:interact(user)
  224. else
  225. modpol.ocutil.log("Cannot interact with process, user does not have a valid pending action")
  226. end
  227. else
  228. modpol.ocutil.log("Cannot interact with process, user does not have any pending actions")
  229. end
  230. end