2019-02-26 18:01:15 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivitiesService
|
2019-03-18 22:38:19 +08:00
|
|
|
def self.load_activities(user, teams, filters = {})
|
|
|
|
# Create condition for view permissions checking first
|
2019-03-26 21:54:34 +08:00
|
|
|
visible_teams = user.teams.where(id: teams)
|
|
|
|
visible_projects = Project.viewable_by_user(user, visible_teams)
|
2021-12-10 02:28:27 +08:00
|
|
|
visible_by_teams = Activity.where(project: nil, team_id: visible_teams.select(:id))
|
2020-02-07 20:53:25 +08:00
|
|
|
.order(created_at: :desc)
|
2021-12-10 02:28:27 +08:00
|
|
|
visible_by_projects = Activity.where(project_id: visible_projects.select(:id))
|
2020-02-07 20:53:25 +08:00
|
|
|
.order(created_at: :desc)
|
|
|
|
|
|
|
|
query = Activity.from("((#{visible_by_teams.to_sql}) UNION ALL (#{visible_by_projects.to_sql})) AS activities")
|
2019-04-12 21:37:45 +08:00
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
if filters[:subjects].present?
|
2019-03-29 16:26:27 +08:00
|
|
|
subjects_with_children = load_subjects_children(filters[:subjects])
|
2021-02-15 17:23:32 +08:00
|
|
|
where_condition = subjects_with_children.to_h.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR ')
|
|
|
|
where_arguments = subjects_with_children.to_h.flatten
|
2020-02-07 20:53:25 +08:00
|
|
|
if subjects_with_children[:my_module]
|
2019-04-03 00:37:55 +08:00
|
|
|
where_condition = where_condition.concat(' OR (my_module_id IN(?))')
|
2020-02-07 20:53:25 +08:00
|
|
|
where_arguments << subjects_with_children[:my_module]
|
2019-04-03 00:37:55 +08:00
|
|
|
end
|
|
|
|
query = query.where(where_condition, *where_arguments)
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
query = query.where(owner_id: filters[:users]) if filters[:users]
|
2021-03-10 19:58:05 +08:00
|
|
|
query = query.where(type_of: filters[:types].map(&:to_i)) if filters[:types]
|
|
|
|
|
2019-03-26 21:54:34 +08:00
|
|
|
activities =
|
|
|
|
if filters[:from_date].present? && filters[:to_date].present?
|
|
|
|
query.where('created_at <= :from AND created_at >= :to',
|
|
|
|
from: Time.zone.parse(filters[:from_date]).end_of_day.utc,
|
|
|
|
to: Time.zone.parse(filters[:to_date]).beginning_of_day.utc)
|
|
|
|
elsif filters[:from_date].present? && filters[:to_date].blank?
|
|
|
|
query.where('created_at <= :from', from: Time.zone.parse(filters[:from_date]).end_of_day.utc)
|
2023-12-18 23:28:41 +08:00
|
|
|
elsif filters[:from_date].blank? && filters[:to_date].present?
|
|
|
|
query.where('created_at >= :to', to: Time.zone.parse(filters[:to_date]).beginning_of_day.utc)
|
2019-03-26 21:54:34 +08:00
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
2019-02-26 18:01:15 +08:00
|
|
|
|
2019-04-12 21:37:45 +08:00
|
|
|
activities.order(created_at: :desc)
|
|
|
|
.page(filters[:page])
|
|
|
|
.per(Constants::ACTIVITY_AND_NOTIF_SEARCH_LIMIT)
|
|
|
|
.without_count
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
2019-03-29 16:26:27 +08:00
|
|
|
|
|
|
|
def self.load_subjects_children(subjects = {})
|
2020-02-07 20:53:25 +08:00
|
|
|
Extends::ACTIVITY_SUBJECT_CHILDREN.each do |subject_name, children|
|
2020-07-20 21:01:00 +08:00
|
|
|
subject_name = subject_name.to_s.camelize
|
2019-03-29 16:26:27 +08:00
|
|
|
next unless children && subjects[subject_name]
|
|
|
|
|
|
|
|
children.each do |child|
|
2020-07-20 21:01:00 +08:00
|
|
|
parent_model = subject_name.constantize
|
2019-03-29 16:26:27 +08:00
|
|
|
child_model = parent_model.reflect_on_association(child).class_name.to_sym
|
2019-11-27 20:13:19 +08:00
|
|
|
next if subjects[child_model]
|
|
|
|
|
|
|
|
subjects[child_model] = parent_model.where(id: subjects[subject_name])
|
|
|
|
.joins(child)
|
2021-11-04 21:20:51 +08:00
|
|
|
.pluck("#{child.to_s.pluralize}.id")
|
2019-03-29 16:26:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subjects.each { |_sub, children| children.uniq! }
|
|
|
|
end
|
2019-04-01 17:26:51 +08:00
|
|
|
|
|
|
|
def self.my_module_activities(my_module)
|
2022-03-01 19:28:45 +08:00
|
|
|
subjects_with_children = load_subjects_children('MyModule' => [my_module.id])
|
2019-04-01 17:26:51 +08:00
|
|
|
query = Activity.where(project: my_module.experiment.project)
|
|
|
|
query.where(
|
2021-02-15 17:23:32 +08:00
|
|
|
subjects_with_children.to_h.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR '),
|
|
|
|
*subjects_with_children.to_h.flatten
|
2019-04-01 17:26:51 +08:00
|
|
|
)
|
|
|
|
end
|
2021-06-15 22:13:28 +08:00
|
|
|
|
|
|
|
def self.activity_matches_filter?(user, teams, activity, activity_filter)
|
|
|
|
load_activities(user, teams, activity_filter.filter).where(id: activity.id).any?
|
|
|
|
end
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|