-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
423 lines (345 loc) · 12.6 KB
/
plugin.rb
File metadata and controls
423 lines (345 loc) · 12.6 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# frozen_string_literal: true
# name: discourse-workflow
# about: A topic-based workflow engine for Discourse
# version: 0.4.1
# authors: Robert Barrow
# contact_emails: robert@pavilion.tech
# url: https://github.com/merefield/discourse-workflow
gem "event_stream_parser", "1.0.0", { require: false }
gem "ruby-openai", "8.1.0", { require: false }
enabled_site_setting :workflow_enabled
register_asset "stylesheets/common/workflow_common.scss"
register_asset "stylesheets/desktop/workflow_desktop.scss", :desktop
register_asset "stylesheets/mobile/workflow_mobile.scss", :mobile
module ::DiscourseWorkflow
PLUGIN_NAME = "discourse-workflow"
end
require_relative "lib/discourse_workflow/engine"
register_svg_icon "right-left" if respond_to?(:register_svg_icon)
after_initialize do
reloadable_patch do
ListController.prepend(DiscourseWorkflow::ListControllerExtension)
TopicQuery.prepend(DiscourseWorkflow::TopicQueryExtension)
Topic.prepend(DiscourseWorkflow::TopicExtension)
Notification.singleton_class.prepend(DiscourseWorkflow::NotificationExtension)
end
register_topic_preloader_associations({ workflow_state: %i[workflow workflow_step] }) do
SiteSetting.workflow_enabled
end
Discourse::Application.routes.prepend { get "/workflow/charts" => "list#workflow_charts" }
Discourse.top_menu_items.push(:workflow)
Discourse.anonymous_top_menu_items.push(:workflow)
Discourse.filters.push(:workflow)
Discourse.anonymous_filters.push(:workflow)
SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-workflow", "db", "fixtures").to_s
add_admin_route(
"admin.discourse_workflow.title",
"discourse-workflow",
{ use_new_show_route: true },
)
add_to_class(:category, :workflow_enabled) do
WorkflowStep.find_by(category_id: self.id)&.step_id == 1 || false
end
add_to_class(:category, :workflow_slug) do
Workflow.joins(:workflow_steps).where(workflow_steps: { category_id: self.id }).first&.slug
end
# prevent non-staff from changing category on a workflow topic
PostRevisor.track_topic_field(:category_id) do |tc, category_id|
if tc.guardian.is_staff?
tc.record_change("category_id", tc.topic.category_id, category_id)
tc.topic.category_id = category_id
else
if ::DiscourseWorkflow::WorkflowState.find_by(topic_id: tc.topic.id).present?
# TODO get this to work and add a translation
tc.topic.errors.add(
:base,
:workflow,
message: "you can't change category on a workflow topic unless you are staff",
)
next
else
tc.record_change("category_id", tc.topic.category_id, category_id)
tc.topic.category_id = category_id
end
end
end
add_to_class(:topic, :workflow_slug) { workflow_state&.workflow&.slug }
add_to_class(:topic, :workflow_name) { workflow_state&.workflow&.name }
add_to_class(:topic, :workflow_step_slug) { workflow_state&.workflow_step&.slug }
add_to_class(:topic, :workflow_step_name) { workflow_state&.workflow_step&.name }
add_to_class(:topic, :workflow_step_position) { workflow_state&.workflow_step&.position }
add_to_class(:topic, :workflow_step_options) do
step = workflow_state&.workflow_step
return [] unless step
step
.workflow_step_options
.includes(:workflow_option)
.order(:position)
.map { |wso| wso.workflow_option.slug }
end
add_to_class(:topic, :workflow_step_actions) do
step = workflow_state&.workflow_step
return [] unless step
step_options = step.workflow_step_options.includes(:workflow_option).order(:position)
target_steps =
DiscourseWorkflow::WorkflowStep.where(
id: step_options.map(&:target_step_id).compact.uniq,
).index_by(&:id)
step_options.map do |workflow_step_option|
option = workflow_step_option.workflow_option
target_step = target_steps[workflow_step_option.target_step_id]
{
slug: option&.slug,
option_name: option&.name,
target_step_name: target_step&.name,
target_step_position: target_step&.position,
}
end
end
add_to_class(:topic, :workflow_step_entered_at) { workflow_state&.updated_at }
add_to_class(:topic, :workflow_overdue_days_threshold) do
state = workflow_state
return nil if state.blank?
step_overdue_days = state.workflow_step&.overdue_days
workflow_overdue_days = state.workflow&.overdue_days
if !step_overdue_days.nil?
step_overdue_days.to_i
elsif !workflow_overdue_days.nil?
workflow_overdue_days.to_i
else
SiteSetting.workflow_overdue_days_default.to_i
end
end
add_to_class(:topic, :workflow_overdue) do
threshold_days = workflow_overdue_days_threshold
return false if threshold_days.blank? || threshold_days <= 0
entered_at = workflow_step_entered_at
return false if entered_at.blank?
entered_at <= threshold_days.days.ago
end
add_to_class(:topic_list, :workflow_kanban_workflow) do
return @workflow_kanban_workflow if defined?(@workflow_kanban_workflow)
workflow_ids = topics.map { |topic| topic.workflow_state&.workflow_id }.compact.uniq
@workflow_kanban_workflow =
if workflow_ids.length == 1
DiscourseWorkflow::Workflow.includes(
workflow_steps: [
{ category: :parent_category },
{ workflow_step_options: :workflow_option },
],
).find_by(id: workflow_ids.first)
end
end
add_to_class(:topic_list, :has_workflow_topics?) do
return @has_workflow_topics if defined?(@has_workflow_topics)
@has_workflow_topics = topics.any? { |topic| topic.workflow_state.present? }
end
add_to_class(:topic_list, :workflow_kanban_compatible) do
workflow = workflow_kanban_workflow
workflow.present? && workflow.kanban_compatible?
end
add_to_class(:topic_list, :workflow_kanban_show_tags) do
workflow = workflow_kanban_workflow
workflow.present? && workflow.show_kanban_tags != false
end
add_to_class(:topic_list, :workflow_single_workflow_id) { workflow_kanban_workflow&.id }
add_to_class(:topic_list, :workflow_single_workflow_name) { workflow_kanban_workflow&.name }
add_to_class(:topic_list, :workflow_kanban_steps) do
return [] if !workflow_kanban_compatible
workflow_kanban_workflow
.workflow_steps
.order(:position)
.map do |step|
category = step.category
{
id: step.id,
position: step.position,
name: step.name,
category_color: category&.color || category&.parent_category&.color,
}
end
end
add_to_class(:topic_list, :workflow_kanban_transitions) do
return [] if !workflow_kanban_compatible
workflow = workflow_kanban_workflow
steps = workflow.workflow_steps.to_a
steps_by_id = steps.index_by(&:id)
first_option_for_edge = {}
steps.each do |step|
step
.workflow_step_options
.sort_by { |option| option.position.to_i }
.each do |step_option|
target_step = steps_by_id[step_option.target_step_id]
next if target_step.blank?
option_slug = step_option.workflow_option&.slug
next if option_slug.blank?
edge_key = [step.position.to_i, target_step.position.to_i]
first_option_for_edge[edge_key] ||= option_slug
end
end
first_option_for_edge.map do |(from_position, to_position), option_slug|
{ from_position: from_position, to_position: to_position, option_slug: option_slug }
end
end
add_to_serializer(
:topic_view,
:workflow_slug,
include_condition: -> { object.topic.workflow_slug.present? },
) { object.topic.workflow_slug }
add_to_serializer(
:topic_view,
:workflow_name,
include_condition: -> { object.topic.workflow_name.present? },
) { object.topic.workflow_name }
add_to_serializer(
:topic_view,
:workflow_step_slug,
include_condition: -> { object.topic.workflow_step_slug.present? },
) { object.topic.workflow_step_slug }
add_to_serializer(
:topic_view,
:workflow_step_name,
include_condition: -> { object.topic.workflow_step_name.present? },
) { object.topic.workflow_step_name }
add_to_serializer(
:topic_view,
:workflow_step_position,
include_condition: -> { object.topic.workflow_step_position.present? },
) { object.topic.workflow_step_position }
add_to_serializer(
:topic_view,
:workflow_step_options,
include_condition: -> do
@workflow_step_options ||= object.topic.workflow_step_options
@workflow_step_options.present?
end,
) do
@workflow_step_options ||= object.topic.workflow_step_options
@workflow_step_options
end
add_to_serializer(
:topic_view,
:workflow_step_actions,
include_condition: -> do
@workflow_step_actions ||= object.topic.workflow_step_actions
@workflow_step_actions.present?
end,
) { @workflow_step_actions ||= object.topic.workflow_step_actions }
add_to_serializer(
:topic_view,
:workflow_can_act,
include_condition: -> { object.topic.workflow_name.present? },
) do
begin
scope.ensure_can_create_topic_on_category!(object.topic.category_id)
true
rescue Discourse::InvalidAccess
false
end
end
add_to_serializer(
:topic_view,
:workflow_step_entered_at,
include_condition: -> { object.topic.workflow_step_entered_at.present? },
) { object.topic.workflow_step_entered_at }
add_to_serializer(
:topic_list_item,
:workflow_name,
include_condition: -> { object.workflow_name.present? },
) { object.workflow_name }
add_to_serializer(
:topic_list_item,
:workflow_step_position,
include_condition: -> { object.workflow_step_position.present? },
) { object.workflow_step_position.to_i }
add_to_serializer(
:topic_list_item,
:workflow_step_name,
include_condition: -> { object.workflow_step_name.present? },
) { object.workflow_step_name }
add_to_serializer(
:topic_list_item,
:workflow_overdue,
include_condition: -> { object.workflow_name.present? },
) { object.workflow_overdue }
add_to_serializer(
:topic_list_item,
:workflow_can_act,
include_condition: -> { object.workflow_name.present? },
) do
# Cache permission checks per category on the scope to avoid repeated work
permissions_cache =
scope.instance_variable_get(:@workflow_can_act_category_permissions) ||
scope.instance_variable_set(:@workflow_can_act_category_permissions, {})
category_id = object.category_id
unless permissions_cache.key?(category_id)
begin
scope.ensure_can_create_topic_on_category!(category_id)
permissions_cache[category_id] = true
rescue Discourse::InvalidAccess
permissions_cache[category_id] = false
end
end
permissions_cache[category_id]
end
add_to_serializer(
:topic_list,
:workflow_kanban_compatible,
include_condition: -> { object.has_workflow_topics? },
) { object.workflow_kanban_compatible }
add_to_serializer(
:topic_list,
:workflow_kanban_workflow_name,
include_condition: -> { object.workflow_single_workflow_name.present? },
) { object.workflow_kanban_workflow.name }
add_to_serializer(
:topic_list,
:workflow_single_workflow_id,
include_condition: -> { object.has_workflow_topics? },
) { object.workflow_single_workflow_id }
add_to_serializer(
:topic_list,
:workflow_can_view_charts,
include_condition: -> { object.has_workflow_topics? },
) { DiscourseWorkflow::ChartsPermissions.can_view?(scope.user) }
add_to_serializer(
:topic_list,
:workflow_kanban_show_tags,
include_condition: -> { object.has_workflow_topics? },
) { object.workflow_kanban_show_tags }
add_to_serializer(
:topic_list,
:workflow_kanban_steps,
include_condition: -> { object.has_workflow_topics? },
) { object.workflow_kanban_steps }
add_to_serializer(
:topic_list,
:workflow_kanban_transitions,
include_condition: -> { object.has_workflow_topics? },
) { object.workflow_kanban_transitions }
on(:topic_created) do |*params|
topic, opts = params
if SiteSetting.workflow_enabled
workflow_step =
DiscourseWorkflow::WorkflowStep.joins(:workflow).find_by(
category_id: topic.category_id,
position: 1,
workflows: {
enabled: true,
},
)
if workflow_step
DiscourseWorkflow::WorkflowState.create!(
topic_id: topic.id,
workflow_id: workflow_step.workflow_id,
workflow_step_id: workflow_step.id,
)
end
end
end
on(:post_alerter_after_save_post) do |post, new_record, notified|
next if !new_record
DiscourseWorkflow::PostNotificationHandler.new(post, notified).handle
end
end