agents_controller.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 remove_events
  66. @agent = current_user.agents.find(params[:id])
  67. @agent.events.delete_all
  68. respond_to do |format|
  69. format.html { redirect_back "All emitted events removed for '#{@agent.name}'" }
  70. format.json { head :ok }
  71. end
  72. end
  73. def propagate
  74. respond_to do |format|
  75. if AgentPropagateJob.can_enqueue?
  76. details = Agent.receive! # Eventually this should probably be scoped to the current_user.
  77. format.html { redirect_back "Queued propagation calls for #{details[:event_count]} event(s) on #{details[:agent_count]} agent(s)" }
  78. format.json { head :ok }
  79. else
  80. format.html { redirect_back "Event propagation is already scheduled to run." }
  81. format.json { head :locked }
  82. end
  83. end
  84. end
  85. def destroy_memory
  86. @agent = current_user.agents.find(params[:id])
  87. @agent.update!(memory: {})
  88. respond_to do |format|
  89. format.html { redirect_back "Memory erased for '#{@agent.name}'" }
  90. format.json { head :ok }
  91. end
  92. end
  93. def show
  94. @agent = current_user.agents.find(params[:id])
  95. respond_to do |format|
  96. format.html
  97. format.json { render json: @agent }
  98. end
  99. end
  100. def new
  101. agents = current_user.agents
  102. if id = params[:id]
  103. @agent = agents.build_clone(agents.find(id))
  104. else
  105. @agent = agents.build
  106. end
  107. @agent.scenario_ids = [params[:scenario_id]] if params[:scenario_id] && current_user.scenarios.find_by(id: params[:scenario_id])
  108. initialize_presenter
  109. respond_to do |format|
  110. format.html
  111. format.json { render json: @agent }
  112. end
  113. end
  114. def edit
  115. @agent = current_user.agents.find(params[:id])
  116. initialize_presenter
  117. end
  118. def create
  119. build_agent
  120. respond_to do |format|
  121. if @agent.save
  122. format.html { redirect_back "'#{@agent.name}' was successfully created.", return: agents_path }
  123. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  124. else
  125. initialize_presenter
  126. format.html { render action: "new" }
  127. format.json { render json: @agent.errors, status: :unprocessable_entity }
  128. end
  129. end
  130. end
  131. def update
  132. @agent = current_user.agents.find(params[:id])
  133. respond_to do |format|
  134. if @agent.update_attributes(agent_params)
  135. format.html { redirect_back "'#{@agent.name}' was successfully updated.", return: agents_path }
  136. format.json { render json: @agent, status: :ok, location: agent_path(@agent) }
  137. else
  138. initialize_presenter
  139. format.html { render action: "edit" }
  140. format.json { render json: @agent.errors, status: :unprocessable_entity }
  141. end
  142. end
  143. end
  144. def leave_scenario
  145. @agent = current_user.agents.find(params[:id])
  146. @scenario = current_user.scenarios.find(params[:scenario_id])
  147. @agent.scenarios.destroy(@scenario)
  148. respond_to do |format|
  149. format.html { redirect_back "'#{@agent.name}' removed from '#{@scenario.name}'" }
  150. format.json { head :no_content }
  151. end
  152. end
  153. def destroy
  154. @agent = current_user.agents.find(params[:id])
  155. @agent.destroy
  156. respond_to do |format|
  157. format.html { redirect_back "'#{@agent.name}' deleted" }
  158. format.json { head :no_content }
  159. end
  160. end
  161. def validate
  162. build_agent
  163. if @agent.validate_option(params[:attribute])
  164. render plain: 'ok'
  165. else
  166. render plain: 'error', status: 403
  167. end
  168. end
  169. def complete
  170. build_agent
  171. render json: @agent.complete_option(params[:attribute])
  172. end
  173. def destroy_undefined
  174. current_user.undefined_agents.destroy_all
  175. redirect_back "All undefined Agents have been deleted."
  176. end
  177. protected
  178. # Sanitize params[:return] to prevent open redirect attacks, a common security issue.
  179. def redirect_back(message, options = {})
  180. if path = filtered_agent_return_link(options)
  181. redirect_to path, notice: message
  182. else
  183. super agents_path, notice: message
  184. end
  185. end
  186. def build_agent
  187. @agent = Agent.build_for_type(agent_params[:type],
  188. current_user,
  189. agent_params.except(:type))
  190. end
  191. def initialize_presenter
  192. if @agent.present? && @agent.is_form_configurable?
  193. @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
  194. end
  195. end
  196. private
  197. def show_only_enabled_agents?
  198. !!cookies[:huginn_view_only_enabled_agents]
  199. end
  200. def set_only_enabled_agents_as_viewable
  201. cookies[:huginn_view_only_enabled_agents] = {
  202. value: "true",
  203. expires: 1.year.from_now
  204. }
  205. end
  206. def mark_all_agents_viewable
  207. cookies.delete(:huginn_view_only_enabled_agents)
  208. end
  209. end