2017-09-05 14:59:40 -04:00
|
|
|
class AutocompleteUsersFinder
|
2018-02-15 13:33:35 -05:00
|
|
|
# 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
|
|
|
|
|
2017-09-08 05:00:31 -04:00
|
|
|
attr_reader :current_user, :project, :group, :search, :skip_users,
|
2018-02-15 13:33:35 -05:00
|
|
|
:author_id, :params
|
2017-08-23 15:33:56 -04:00
|
|
|
|
2017-09-08 05:00:31 -04:00
|
|
|
def initialize(params:, current_user:, project:, group:)
|
2017-08-23 15:33:56 -04:00
|
|
|
@current_user = current_user
|
2017-09-08 05:00:31 -04:00
|
|
|
@project = project
|
|
|
|
@group = group
|
2017-08-23 15:33:56 -04:00
|
|
|
@search = params[:search]
|
|
|
|
@skip_users = params[:skip_users]
|
|
|
|
@author_id = params[:author_id]
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2017-09-08 05:00:31 -04:00
|
|
|
items = find_users
|
2017-08-23 15:33:56 -04:00
|
|
|
items = items.active
|
|
|
|
items = items.reorder(:name)
|
|
|
|
items = items.search(search) if search.present?
|
|
|
|
items = items.where.not(id: skip_users) if skip_users.present?
|
2018-02-15 13:33:35 -05:00
|
|
|
items = items.limit(LIMIT)
|
2017-08-23 15:33:56 -04:00
|
|
|
|
|
|
|
if params[:todo_filter].present? && current_user
|
|
|
|
items = items.todo_authors(current_user.id, params[:todo_state_filter])
|
|
|
|
end
|
|
|
|
|
|
|
|
if search.blank?
|
|
|
|
# Include current user if available to filter by "Me"
|
|
|
|
if params[:current_user].present? && current_user
|
|
|
|
items = [current_user, *items].uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
if author_id.present? && current_user
|
|
|
|
author = User.find_by_id(author_id)
|
|
|
|
items = [author, *items].uniq if author
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
items
|
|
|
|
end
|
2017-09-08 05:00:31 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_users
|
|
|
|
return users_from_project if project
|
2017-11-13 06:24:01 -05:00
|
|
|
return group.users_with_parents if group
|
2017-09-08 05:00:31 -04:00
|
|
|
return User.all if current_user
|
|
|
|
|
|
|
|
User.none
|
|
|
|
end
|
|
|
|
|
|
|
|
def users_from_project
|
2018-02-15 13:32:57 -05:00
|
|
|
if author_id.present?
|
|
|
|
union = Gitlab::SQL::Union
|
2018-02-15 13:55:43 -05:00
|
|
|
.new([project.authorized_users, User.where(id: author_id)])
|
2017-09-08 05:00:31 -04:00
|
|
|
|
2018-02-15 13:32:57 -05:00
|
|
|
User.from("(#{union.to_sql}) #{User.table_name}")
|
|
|
|
else
|
|
|
|
project.authorized_users
|
|
|
|
end
|
2017-09-08 05:00:31 -04:00
|
|
|
end
|
2017-08-23 15:33:56 -04:00
|
|
|
end
|