2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Autocomplete
|
|
|
|
class UsersFinder
|
2019-07-31 22:56:46 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
# The number of users to display in the results is hardcoded to 20, and
|
|
|
|
# pagination is not supported. This ensures that performance remains
|
|
|
|
# consistent and removes the need for implementing keyset pagination to
|
|
|
|
# ensure good performance.
|
|
|
|
LIMIT = 20
|
|
|
|
|
|
|
|
attr_reader :current_user, :project, :group, :search, :skip_users,
|
2023-06-20 00:43:36 +05:30
|
|
|
:author_id, :todo_filter, :todo_state_filter,
|
|
|
|
:filter_by_current_user, :states
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
def initialize(params:, current_user:, project:, group:)
|
|
|
|
@current_user = current_user
|
|
|
|
@project = project
|
|
|
|
@group = group
|
|
|
|
@search = params[:search]
|
|
|
|
@skip_users = params[:skip_users]
|
|
|
|
@author_id = params[:author_id]
|
|
|
|
@todo_filter = params[:todo_filter]
|
|
|
|
@todo_state_filter = params[:todo_state_filter]
|
|
|
|
@filter_by_current_user = params[:current_user]
|
2023-01-13 00:05:48 +05:30
|
|
|
@states = params[:states] || ['active']
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = limited_users
|
|
|
|
|
|
|
|
if search.blank?
|
|
|
|
# Include current user if available to filter by "Me"
|
|
|
|
items.unshift(current_user) if prepend_current_user?
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
if prepend_author? && author&.active?
|
2018-11-20 20:47:30 +05:30
|
|
|
items.unshift(author)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
items.uniq.tap do |unique_items|
|
|
|
|
preload_associations(unique_items)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def author
|
|
|
|
strong_memoize(:author) do
|
|
|
|
User.find_by_id(author_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
# Returns the users based on the input parameters, as an Array.
|
|
|
|
#
|
|
|
|
# This method is separate so it is easier to extend in EE.
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-11-20 20:47:30 +05:30
|
|
|
def limited_users
|
|
|
|
# When changing the order of these method calls, make sure that
|
|
|
|
# reorder_by_name() is called _before_ optionally_search(), otherwise
|
|
|
|
# reorder_by_name will break the ORDER BY applied in optionally_search().
|
|
|
|
find_users
|
2023-01-13 00:05:48 +05:30
|
|
|
.where(state: states)
|
|
|
|
.non_internal
|
2018-11-20 20:47:30 +05:30
|
|
|
.reorder_by_name
|
2022-04-04 11:22:00 +05:30
|
|
|
.optionally_search(search, use_minimum_char_limit: use_minimum_char_limit)
|
2018-11-20 20:47:30 +05:30
|
|
|
.where_not_in(skip_users)
|
|
|
|
.limit_to_todo_authors(
|
|
|
|
user: current_user,
|
|
|
|
with_todos: todo_filter,
|
|
|
|
todo_state: todo_state_filter
|
|
|
|
)
|
|
|
|
.limit(LIMIT)
|
|
|
|
.to_a
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
def prepend_current_user?
|
|
|
|
filter_by_current_user.present? && current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepend_author?
|
|
|
|
author_id.present? && current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users
|
|
|
|
if project
|
|
|
|
project.authorized_users.union_with_user(author_id)
|
|
|
|
elsif group
|
|
|
|
group.users_with_parents
|
|
|
|
elsif current_user
|
|
|
|
User.all
|
|
|
|
else
|
|
|
|
User.none
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def preload_associations(items)
|
2023-05-27 22:25:52 +05:30
|
|
|
ActiveRecord::Associations::Preloader.new(records: items, associations: :status).call
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def use_minimum_char_limit
|
|
|
|
return if project.blank? && group.blank? # We return nil so that we use the default defined in the User model
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Autocomplete::UsersFinder.prepend_mod_with('Autocomplete::UsersFinder')
|