2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
# Get user activity feed for projects common for a user and a logged in user
|
|
|
|
#
|
|
|
|
# - current_user: The user viewing the events
|
2018-09-21 04:44:34 -04:00
|
|
|
# WARNING: does not consider project feature visibility!
|
2017-12-11 09:21:06 -05:00
|
|
|
# - user: The user for which to load the events
|
|
|
|
# - params:
|
2020-09-02 11:10:54 -04:00
|
|
|
# - limit: Number of items that to be returned. Defaults to 20 and limited to 100.
|
2017-12-11 09:21:06 -05:00
|
|
|
# - offset: The page of events to return
|
|
|
|
class UserRecentEventsFinder
|
|
|
|
prepend FinderWithCrossProjectAccess
|
|
|
|
include FinderMethods
|
2018-07-24 08:46:19 -04:00
|
|
|
include Gitlab::Allowable
|
2017-12-11 09:21:06 -05:00
|
|
|
|
|
|
|
requires_cross_project_access
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
attr_reader :current_user, :target_user, :params, :event_filter
|
2017-12-11 09:21:06 -05:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
DEFAULT_LIMIT = 20
|
|
|
|
MAX_LIMIT = 100
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
def initialize(current_user, target_user, event_filter, params = {})
|
2017-12-11 09:21:06 -05:00
|
|
|
@current_user = current_user
|
|
|
|
@target_user = target_user
|
|
|
|
@params = params
|
2021-02-15 16:08:59 -05:00
|
|
|
@event_filter = event_filter || EventFilter.new(EventFilter::ALL)
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-02-15 16:08:59 -05:00
|
|
|
if target_user.is_a? User
|
|
|
|
execute_single
|
|
|
|
else
|
|
|
|
execute_multi
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_single
|
2018-07-24 08:46:19 -04:00
|
|
|
return Event.none unless can?(current_user, :read_user_profile, target_user)
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
event_filter.apply_filter(target_events
|
2017-12-11 09:21:06 -05:00
|
|
|
.with_associations
|
2020-09-02 11:10:54 -04:00
|
|
|
.limit_recent(limit, params[:offset])
|
2021-02-15 16:08:59 -05:00
|
|
|
.order_created_desc)
|
2018-03-01 06:26:45 -05:00
|
|
|
end
|
|
|
|
|
2021-12-28 13:15:23 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def execute_optimized_multi(users)
|
|
|
|
Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder.new(
|
2022-01-07 07:10:47 -05:00
|
|
|
scope: Event.reorder(id: :desc),
|
2021-12-28 13:15:23 -05:00
|
|
|
array_scope: User.select(:id).where(id: users),
|
2022-01-07 07:10:47 -05:00
|
|
|
# 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) },
|
2021-12-28 13:15:23 -05:00
|
|
|
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
|
|
|
|
|
2021-02-15 16:08:59 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
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?
|
|
|
|
|
2022-05-06 11:09:03 -04:00
|
|
|
if Feature.enabled?(:optimized_followed_users_queries, current_user)
|
2022-05-13 08:08:49 -04:00
|
|
|
array_data = {
|
|
|
|
scope_ids: users,
|
|
|
|
scope_model: User,
|
|
|
|
mapping_column: :author_id
|
|
|
|
}
|
|
|
|
query_builder_params = event_filter.in_operator_query_builder_params(array_data)
|
2022-04-19 11:08:32 -04:00
|
|
|
|
|
|
|
Gitlab::Pagination::Keyset::InOperatorOptimization::QueryBuilder
|
|
|
|
.new(**query_builder_params)
|
|
|
|
.execute
|
|
|
|
.limit(limit)
|
|
|
|
.offset(params[:offset] || 0)
|
2021-12-28 13:15:23 -05:00
|
|
|
else
|
2022-04-19 11:08:32 -04:00
|
|
|
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
|
2021-12-28 13:15:23 -05:00
|
|
|
end
|
2021-02-15 16:08:59 -05:00
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
def target_events
|
|
|
|
Event.where(author: target_user)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-01 06:26:45 -05:00
|
|
|
|
2020-09-02 11:10:54 -04:00
|
|
|
def limit
|
|
|
|
return DEFAULT_LIMIT unless params[:limit].present?
|
|
|
|
|
|
|
|
[params[:limit].to_i, MAX_LIMIT].min
|
|
|
|
end
|
2017-12-11 09:21:06 -05:00
|
|
|
end
|