remove_process.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. --- Remove a process
  2. -- @module remove_process
  3. local remove_process = {
  4. name = "Remove process",
  5. slug = "remove_process",
  6. desc = "User can remove own processes, consent required for those of others",
  7. hide = false;
  8. }
  9. remove_process.data = {
  10. }
  11. remove_process.config = {
  12. }
  13. --- Initiate function
  14. -- @function remove_process:initiate
  15. -- @param result Callback if this module is embedded in other modules
  16. function remove_process:initiate(result)
  17. -- prepare process options
  18. local available_processes = {}
  19. for k,process in pairs(self.org.processes) do
  20. if process ~= "deleted" then
  21. available_processes[process.id] = process
  22. end
  23. end
  24. local process_list = {}
  25. local process_count = 0
  26. for k,v in pairs(available_processes) do
  27. local mine = ""
  28. if v.initiator == self.initiator then mine = "*" end
  29. table.insert(process_list,"["..v.id.."] "..v.slug..mine)
  30. process_count = process_count + 1
  31. end
  32. -- abort if no processes to remove
  33. if process_count == 0 then
  34. modpol.interactions.message(
  35. self.initiator, "Org has no modules")
  36. modpol.interactions.org_dashboard(
  37. self.initiator, self.org.id)
  38. if result then result() end
  39. self.org:delete_process(self.id)
  40. return
  41. end
  42. table.sort(process_list)
  43. -- now ask which to remove
  44. modpol.interactions.dropdown_query(
  45. self.initiator, "Choose a process to remove (* marks yours, no consent required):",
  46. process_list,
  47. function(process_choice)
  48. -- confirm choice
  49. local process_id = tonumber(
  50. string.match(process_choice, "%d+"))
  51. local process_mine = string.match(process_choice,
  52. "%*")
  53. modpol.interactions.binary_poll_user(
  54. self.initiator,
  55. "Confirm: Remove process \""..
  56. process_choice .. "\"?",
  57. function(input)
  58. if input == "Yes" then
  59. if process_mine then
  60. self.org:delete_process_tree(process_id)
  61. modpol.interactions.message(
  62. self.initiator,
  63. "Removed process: "..process_choice)
  64. modpol.interactions.org_dashboard(
  65. self.initiator, self.org.id)
  66. if result then result() end
  67. self.org:delete_process(self.id)
  68. else
  69. self:call_module(
  70. "consent",
  71. self.initiator,
  72. {
  73. prompt = "Approve removal of process "..process_choice.."?",
  74. votes_required = #self.org.members
  75. },
  76. function(input)
  77. modpol.interactions.message_org(
  78. self.initiator,
  79. self.org.id,
  80. "Removing process: "..
  81. process_choice)
  82. self.org:delete_process_tree(process_id)
  83. modpol.interactions.org_dashboard(
  84. self.initiator, self.org.id)
  85. if result then result() end
  86. self.org:delete_process(self.id)
  87. end
  88. )
  89. end
  90. modpol.interactions.org_dashboard(
  91. self.initiator, self.org.id)
  92. else
  93. modpol.interactions.org_dashboard(
  94. self.initiator, self.org.id)
  95. if result then result() end
  96. self.org:delete_process(self.id)
  97. end
  98. end
  99. )
  100. end
  101. )
  102. end
  103. modpol.modules.remove_process = remove_process