process.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --- Process functions for orgs
  2. -- @module modpol.orgs.process
  3. --- Call modules
  4. -- @function modpol.orgs.call_module
  5. -- @param module_slug Same as module name
  6. -- @param intiator Initiator for module
  7. -- @param config Config for module
  8. -- @param result
  9. function modpol.orgs:call_module(module_slug, initiator, config, result)
  10. if not modpol.modules[module_slug] then
  11. modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
  12. return
  13. end
  14. local empty_index = nil
  15. -- linear search for empty process slots (lazy deletion)
  16. for k, v in ipairs(self.processes) do
  17. if v == 'deleted' then
  18. empty_index = k
  19. break
  20. end
  21. end
  22. local index
  23. -- attempts to fill empty spots in list, otherwise appends to end
  24. if empty_index then
  25. index = empty_index
  26. else
  27. index = #self.processes + 1
  28. end
  29. local module = modpol.modules[module_slug]
  30. -- sets default values for undeclared config variables
  31. if #module.config > 0 then
  32. for k, v in pairs(module.config) do
  33. if config[k] == nil then
  34. config[k] = v
  35. end
  36. end
  37. end
  38. -- setting default params
  39. local new_process = {
  40. metatable = {__index = module},
  41. initiator = initiator,
  42. org = self,
  43. id = index,
  44. config = config,
  45. data = module.data,
  46. slug = module_slug
  47. }
  48. setmetatable(new_process, new_process.metatable)
  49. self.processes[index] = new_process
  50. self.processes[index]:initiate(result)
  51. return index
  52. end
  53. --- Delete process by id
  54. -- @function modpol.orgs:delete_process
  55. -- @param id Id of process
  56. function modpol.orgs:delete_process(id)
  57. self.processes[id] = 'deleted'
  58. end
  59. --- Add a new pending action
  60. -- @function modpol.orgs:add_pending_action
  61. -- @param process_id Process id
  62. -- @param user User adding the action
  63. -- @param callback
  64. function modpol.orgs:add_pending_action(process_id, user, callback)
  65. self.pending[user] = self.pending[user] or {}
  66. self.pending[user][process_id] = callback
  67. modpol.interactions.message(
  68. user, "New pending action in org "..self.name)
  69. end
  70. --- Remove a pending action
  71. -- @function modpol.orgs:remove_pending_action
  72. -- @param process_id Process id to be removed
  73. -- @param user
  74. function modpol.orgs:remove_pending_action(process_id, user)
  75. if self.pending[user] then
  76. self.pending[user][process_id] = nil
  77. end
  78. end
  79. --- Wipe all pending actions for process
  80. -- @function modpol.orgs:wipe_pending_actions
  81. -- @param process_id
  82. function modpol.orgs:wipe_pending_actions(process_id)
  83. for user in pairs(self.pending) do
  84. self.pending[user][process_id] = nil
  85. end
  86. end
  87. --- Check if there are pending actions for user
  88. -- @function modpol.orgs:has_pending_actions
  89. -- @param user User
  90. -- @return True if there are pending actions for a user, false if not
  91. function modpol.orgs:has_pending_actions(user)
  92. -- next() will return the next pair in a table
  93. -- if next() returns nil, the table is empty
  94. if not self.pending[user] then
  95. return false
  96. else
  97. if not next(self.pending[user]) then
  98. return false
  99. else
  100. return true
  101. end
  102. end
  103. end
  104. --- Interact a user with given process
  105. -- @function modpol.orgs:interact
  106. -- @param process_id
  107. -- @param user
  108. function modpol.orgs:interact(process_id, user)
  109. local process = self.processes[process_id]
  110. if self.pending[user] then
  111. local callback = self.pending[user][process_id]
  112. if callback then
  113. process[callback](process, user)
  114. end
  115. end
  116. end