mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-02 18:53:07 +08:00
Merge pull request #3468 from artoscinote/ma_SCI_5973
Fixed handling of activity subject parents when matching activity filters [SCI-5973]
This commit is contained in:
commit
d0f8544075
3 changed files with 31 additions and 3 deletions
app/services
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -23,7 +23,7 @@ class WebhookService
|
|||
@webhook.url,
|
||||
{
|
||||
headers: { 'Content-Type' => 'application/json' },
|
||||
body: @payload
|
||||
body: @payload.to_json
|
||||
}
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue