Fixed handling of activity subject parents when matching activity filters [SCI-5973]

This commit is contained in:
Martin Artnik 2021-08-06 13:43:30 +02:00
parent 04e58740d8
commit 8f3f39f7c7
3 changed files with 31 additions and 3 deletions

View file

@ -2,6 +2,11 @@
module Activities
class ActivityFilterMatchingService
# invert the children hash to get a hash defining parents
ACTIVITY_SUBJECT_PARENTS = Extends::ACTIVITY_SUBJECT_CHILDREN.invert.map do |k, v|
k&.map { |s| [s.to_s.singularize.camelize, v] }
end.compact.sum.to_h.freeze
def initialize(activity)
@activity = activity
@activity_filters = ActivityFilter.all
@ -46,10 +51,29 @@ module Activities
end
def filter_subjects!
parents = activity_subject_parents
@activity_filters = @activity_filters.where(
"NOT(filter ? 'subjects') OR "\
"filter -> 'subjects' -> '#{@activity.subject_type}' @> '\"#{@activity.subject_id}\"'"
"filter -> 'subjects' -> 'Project' @> '\"#{@activity.project_id}\"' OR "\
"filter -> 'subjects' -> '#{@activity.subject_type}' @> '\"#{@activity.subject_id}\"'" +
(parents.any? ? ' OR ' : '') +
parents.map do |subject|
"filter -> 'subjects' -> '#{subject.class}' @> '\"#{subject.id}\"'"
end.join(' OR ')
)
end
def activity_subject_parents
subject_parents(@activity.subject, [])
end
def subject_parents(subject, parents)
parent_model_name = ACTIVITY_SUBJECT_PARENTS[subject.class.name]
return parents if parent_model_name.nil?
parent = subject.public_send(parent_model_name)
subject_parents(parent, parents << parent)
end
end
end

View file

@ -14,7 +14,11 @@ module Activities
def activity_payload
@activity.values.merge(
type: @activity.type_of,
created_at: @activity.created_at
created_at: @activity.created_at,
subject: {
type: @activity.subject_type,
id: @activity.subject_id
}
)
end
end

View file

@ -23,7 +23,7 @@ class WebhookService
@webhook.url,
{
headers: { 'Content-Type' => 'application/json' },
body: @payload
body: @payload.to_json
}
)