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