--- Process functions for orgs -- @module modpol.orgs.process --- Call modules -- @function modpol.orgs.call_module -- @param module_slug Same as module name -- @param intiator Initiator for module -- @param config Config for module -- @param result function modpol.orgs:call_module(module_slug, initiator, config, result) if not modpol.modules[module_slug] then modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found') return end local empty_index = nil -- linear search for empty process slots (lazy deletion) for k, v in ipairs(self.processes) do if v == 'deleted' then empty_index = k break end end local index -- attempts to fill empty spots in list, otherwise appends to end if empty_index then index = empty_index else index = #self.processes + 1 end local module = modpol.modules[module_slug] -- sets default values for undeclared config variables if #module.config > 0 then for k, v in pairs(module.config) do if config[k] == nil then config[k] = v end end end -- setting default params local new_process = { metatable = {__index = module}, initiator = initiator, org = self, id = index, config = config, data = module.data, slug = module_slug } setmetatable(new_process, new_process.metatable) self.processes[index] = new_process self.processes[index]:initiate(result) return index end --- Delete process by id -- @function modpol.orgs:delete_process -- @param id Id of process function modpol.orgs:delete_process(id) self.processes[id] = 'deleted' end --- Add a new pending action -- @function modpol.orgs:add_pending_action -- @param process_id Process id -- @param user User adding the action -- @param callback function modpol.orgs:add_pending_action(process_id, user, callback) self.pending[user] = self.pending[user] or {} self.pending[user][process_id] = callback modpol.interactions.message( user, "New pending action in org "..self.name) end --- Remove a pending action -- @function modpol.orgs:remove_pending_action -- @param process_id Process id to be removed -- @param user function modpol.orgs:remove_pending_action(process_id, user) if self.pending[user] then self.pending[user][process_id] = nil end end --- Wipe all pending actions for process -- @function modpol.orgs:wipe_pending_actions -- @param process_id function modpol.orgs:wipe_pending_actions(process_id) for user in pairs(self.pending) do self.pending[user][process_id] = nil end end --- Check if there are pending actions for user -- @function modpol.orgs:has_pending_actions -- @param user User -- @return True if there are pending actions for a user, false if not function modpol.orgs:has_pending_actions(user) -- next() will return the next pair in a table -- if next() returns nil, the table is empty if not self.pending[user] then return false else if not next(self.pending[user]) then return false else return true end end end --- Interact a user with given process -- @function modpol.orgs:interact -- @param process_id -- @param user function modpol.orgs:interact(process_id, user) local process = self.processes[process_id] if self.pending[user] then local callback = self.pending[user][process_id] if callback then process[callback](process, user) end end end