2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
# Get user activity feed for projects common for a user and a logged in user
|
|
|
|
#
|
|
|
|
# - current_user: The user viewing the events
|
2018-11-08 19:23:39 +05:30
|
|
|
# WARNING: does not consider project feature visibility!
|
2018-03-27 19:54:05 +05:30
|
|
|
# - user: The user for which to load the events
|
|
|
|
# - params:
|
2020-09-03 11:15:55 +05:30
|
|
|
# - limit: Number of items that to be returned. Defaults to 20 and limited to 100.
|
2018-03-27 19:54:05 +05:30
|
|
|
# - offset: The page of events to return
|
|
|
|
class UserRecentEventsFinder
|
|
|
|
prepend FinderWithCrossProjectAccess
|
|
|
|
include FinderMethods
|
2018-11-18 11:00:15 +05:30
|
|
|
include Gitlab::Allowable
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
requires_cross_project_access
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
attr_reader :current_user, :target_user, :params, :event_filter
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
DEFAULT_LIMIT = 20
|
|
|
|
MAX_LIMIT = 100
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def initialize(current_user, target_user, event_filter, params = {})
|
2018-03-27 19:54:05 +05:30
|
|
|
@current_user = current_user
|
|
|
|
@target_user = target_user
|
|
|
|
@params = params
|
2021-03-11 19:13:27 +05:30
|
|
|
@event_filter = event_filter || EventFilter.new(EventFilter::ALL)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-03-11 19:13:27 +05:30
|
|
|
if target_user.is_a? User
|
|
|
|
execute_single
|
|
|
|
else
|
|
|
|
execute_multi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_single
|
2018-11-18 11:00:15 +05:30
|
|
|
return Event.none unless can?(current_user, :read_user_profile, target_user)
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
event_filter.apply_filter(target_events
|
2018-03-27 19:54:05 +05:30
|
|
|
.with_associations
|
2020-09-03 11:15:55 +05:30
|
|
|
.limit_recent(limit, params[:offset])
|
2021-03-11 19:13:27 +05:30
|
|
|
.order_created_desc)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute_optimized_multi(users)
|
|
|
|
Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder.new(
|
|
|
|
scope: Event.reorder(id: :desc),
|
|
|
|
array_scope: User.select(:id).where(id: users),
|
|
|
|
# Event model has a default scope { reorder(nil) }
|
|
|
|
# When a relation is rordered and used as a target when merging scope,
|
|
|
|
# its order takes a precedence and _overwrites_ the original scope's order.
|
|
|
|
# Thus we have to explicitly provide `reorder` for array_mapping_scope here.
|
|
|
|
array_mapping_scope: -> (author_id_expression) { Event.where(Event.arel_table[:author_id].eq(author_id_expression)).reorder(id: :desc) },
|
|
|
|
finder_query: -> (id_expression) { Event.where(Event.arel_table[:id].eq(id_expression)) }
|
|
|
|
)
|
|
|
|
.execute
|
|
|
|
.limit(limit)
|
|
|
|
.offset(params[:offset] || 0)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-03-11 19:13:27 +05:30
|
|
|
def execute_multi
|
|
|
|
users = []
|
|
|
|
@target_user.each do |user|
|
|
|
|
users.append(user.id) if can?(current_user, :read_user_profile, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
return Event.none if users.empty?
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
if Feature.enabled?(:optimized_followed_users_queries, current_user)
|
|
|
|
query_builder_params = event_filter.in_operator_query_builder_params(users)
|
|
|
|
|
|
|
|
Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder
|
|
|
|
.new(**query_builder_params)
|
|
|
|
.execute
|
|
|
|
.limit(limit)
|
|
|
|
.offset(params[:offset] || 0)
|
2022-03-02 08:16:31 +05:30
|
|
|
else
|
2022-06-21 17:19:12 +05:30
|
|
|
if event_filter.filter == EventFilter::ALL
|
|
|
|
execute_optimized_multi(users)
|
|
|
|
else
|
|
|
|
event_filter.apply_filter(Event.where(author: users).limit_recent(limit, params[:offset] || 0))
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-27 19:54:05 +05:30
|
|
|
def target_events
|
|
|
|
Event.where(author: target_user)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-09-03 11:15:55 +05:30
|
|
|
def limit
|
|
|
|
return DEFAULT_LIMIT unless params[:limit].present?
|
|
|
|
|
|
|
|
[params[:limit].to_i, MAX_LIMIT].min
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|