agents_controller.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. class AgentsController < ApplicationController
  2. include DotHelper
  3. include ActionView::Helpers::TextHelper
  4. include SortableTable
  5. def index
  6. set_table_sort sorts: %w[name created_at last_check_at last_event_at last_receive_at], default: { created_at: :desc }
  7. @agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
  8. if show_only_enabled_agents?
  9. @agents = @agents.where(disabled: false)
  10. end
  11. respond_to do |format|
  12. format.html
  13. format.json { render json: @agents }
  14. end
  15. end
  16. def toggle_visibility
  17. if show_only_enabled_agents?
  18. mark_all_agents_viewable
  19. else
  20. set_only_enabled_agents_as_viewable
  21. end
  22. redirect_to agents_path
  23. end
  24. def handle_details_post
  25. @agent = current_user.agents.find(params[:id])
  26. if @agent.respond_to?(:handle_details_post)
  27. render :json => @agent.handle_details_post(params) || {}
  28. else
  29. @agent.error "#handle_details_post called on an instance of #{@agent.class} that does not define it."
  30. head 500
  31. end
  32. end
  33. def run
  34. @agent = current_user.agents.find(params[:id])
  35. Agent.async_check(@agent.id)
  36. respond_to do |format|
  37. format.html { redirect_back "Agent run queued for '#{@agent.name}'" }
  38. format.json { head :ok }
  39. end
  40. end
  41. def type_details
  42. @agent = Agent.build_for_type(params[:type], current_user, {})
  43. initialize_presenter
  44. render json: {
  45. can_be_scheduled: @agent.can_be_scheduled?,
  46. default_schedule: @agent.default_schedule,
  47. can_receive_events: @agent.can_receive_events?,
  48. can_create_events: @agent.can_create_events?,
  49. can_control_other_agents: @agent.can_control_other_agents?,
  50. can_dry_run: @agent.can_dry_run?,
  51. options: @agent.default_options,
  52. description_html: @agent.html_description,
  53. oauthable: render_to_string(partial: 'oauth_dropdown', locals: { agent: @agent }),
  54. form_options: render_to_string(partial: 'options', locals: { agent: @agent })
  55. }
  56. end
  57. def event_descriptions
  58. html = current_user.agents.find(params[:ids].split(",")).group_by(&:type).map { |type, agents|
  59. agents.map(&:html_event_description).uniq.map { |desc|
  60. "<p><strong>#{type}</strong><br />" + desc + "</p>"
  61. }
  62. }.flatten.join()
  63. render :json => { :description_html => html }
  64. end
  65. def reemit_events
  66. @agent = current_user.agents.find(params[:id])
  67. AgentReemitJob.perform_later(@agent, @agent.most_recent_event.id,
  68. params[:delete_old_events] == '1') if @agent.most_recent_event
  69. respond_to do |format|
  70. format.html { redirect_back "Enqueued job to re-emit all events for '#{@agent.name}'" }
  71. format.json { head :ok }
  72. end
  73. end
  74. def remove_events
  75. @agent = current_user.agents.find(params[:id])
  76. @agent.events.delete_all
  77. respond_to do |format|
  78. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  79. format.json { head :ok }
  80. end
  81. end
  82. def propagate
  83. respond_to do |format|
  84. if AgentPropagateJob.can_enqueue?
  85. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  86. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  87. format.json { head :ok }
  88. else
  89. format.html { redirect_back "Event propagation is already scheduled to run." }
  90. format.json { head :locked }
  91. end
  92. end
  93. end
  94. def destroy_memory
  95. @agent = current_user.agents.find(params[:id])
  96. @agent.update!(memory: {})
  97. respond_to do |format|
  98. format.html { redirect_back "Memory erased for '#{@agent.name}'" }
  99. format.json { head :ok }
  100. end
  101. end
  102. def show
  103. @agent = current_user.agents.find(params[:id])
  104. respond_to do |format|
  105. format.html
  106. format.json { render json: @agent }
  107. end
  108. end
  109. def new
  110. agents = current_user.agents
  111. if id = params[:id]
  112. @agent = agents.build_clone(agents.find(id))
  113. else
  114. @agent = agents.build
  115. end
  116. @agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])
  117. initialize_presenter
  118. respond_to do |format|
  119. format.html
  120. format.json { render json: @agent }
  121. end
  122. end
  123. def edit
  124. @agent = current_user.agents.find(params[:id])
  125. initialize_presenter
  126. end
  127. def create
  128. build_agent
  129. respond_to do |format|
  130. if @agent.save
  131. format.html { redirect_back "'#{@agent.name}' was successfully created.", return: agents_path }
  132. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  133. else
  134. initialize_presenter
  135. format.html { render action: "new" }
  136. format.json { render json: @agent.errors, status: :unprocessable_entity }
  137. end
  138. end
  139. end
  140. def update
  141. @agent = current_user.agents.find(params[:id])
  142. respond_to do |format|
  143. if @agent.update(agent_params)
  144. format.html { redirect_back "'#{@agent.name}' was successfully updated.", return: agents_path }
  145. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  146. else
  147. initialize_presenter
  148. format.html { render action: "edit" }
  149. format.json { render json: @agent.errors, status: :unprocessable_entity }
  150. end
  151. end
  152. end
  153. def leave_scenario
  154. @agent = current_user.agents.find(params[:id])
  155. @scenario = current_user.scenarios.find(params[:scenario_id])
  156. @agent.scenarios.destroy(@scenario)
  157. respond_to do |format|
  158. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  159. format.json { head :no_content }
  160. end
  161. end
  162. def destroy
  163. @agent = current_user.agents.find(params[:id])
  164. @agent.destroy
  165. respond_to do |format|
  166. format.html { redirect_back "'#{@agent.name}' deleted" }
  167. format.json { head :no_content }
  168. end
  169. end
  170. def validate
  171. build_agent
  172. if @agent.validate_option(params[:attribute])
  173. render plain: 'ok'
  174. else
  175. render plain: 'error', status: 403
  176. end
  177. end
  178. def complete
  179. build_agent
  180. render json: @agent.complete_option(params[:attribute])
  181. end
  182. def destroy_undefined
  183. current_user.undefined_agents.destroy_all
  184. redirect_back "All undefined Agents have been deleted."
  185. end
  186. protected
  187. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  188. def redirect_back(message, options = {})
  189. if path = filtered_agent_return_link(options)
  190. redirect_to path, notice: message
  191. else
  192. super agents_path, notice: message
  193. end
  194. end
  195. def build_agent
  196. @agent = Agent.build_for_type(agent_params[:type],
  197. current_user,
  198. agent_params.except(:type))
  199. end
  200. def initialize_presenter
  201. if @agent.present? && @agent.is_form_configurable?
  202. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  203. end
  204. end
  205. private
  206. def show_only_enabled_agents?
  207. !!cookies[:huginn_view_only_enabled_agents]
  208. end
  209. def set_only_enabled_agents_as_viewable
  210. cookies[:huginn_view_only_enabled_agents] = {
  211. value: "true",
  212. expires: 1.year.from_now
  213. }
  214. end
  215. def mark_all_agents_viewable
  216. cookies.delete(:huginn_view_only_enabled_agents)
  217. end
  218. end