remove_process.lua 3.9 KB

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