2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
class SearchService
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
2020-01-14 13:08:31 -05:00
|
|
|
SEARCH_TERM_LIMIT = 64
|
|
|
|
SEARCH_CHAR_LIMIT = 4096
|
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-31 09:03:55 -04:00
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
|
|
|
@project =
|
|
|
|
if params[:project_id].present?
|
|
|
|
the_project = Project.find_by(id: params[:project_id])
|
2017-05-22 12:51:09 -04:00
|
|
|
can?(current_user, :read_project, the_project) ? the_project : nil
|
2017-03-31 09:03:55 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-31 09:03:55 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-31 09:03:55 -04:00
|
|
|
def group
|
|
|
|
return @group if defined?(@group)
|
|
|
|
|
|
|
|
@group =
|
|
|
|
if params[:group_id].present?
|
|
|
|
the_group = Group.find_by(id: params[:group_id])
|
|
|
|
can?(current_user, :read_group, the_group) ? the_group : nil
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-31 09:03:55 -04:00
|
|
|
|
|
|
|
def show_snippets?
|
|
|
|
return @show_snippets if defined?(@show_snippets)
|
|
|
|
|
|
|
|
@show_snippets = params[:snippets] == 'true'
|
|
|
|
end
|
|
|
|
|
2020-01-14 13:08:31 -05:00
|
|
|
def valid_query_length?
|
|
|
|
params[:search].length <= SEARCH_CHAR_LIMIT
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_terms_count?
|
|
|
|
params[:search].split.count { |word| word.length >= 3 } <= SEARCH_TERM_LIMIT
|
|
|
|
end
|
|
|
|
|
2017-03-31 09:03:55 -04:00
|
|
|
delegate :scope, to: :search_service
|
|
|
|
|
|
|
|
def search_results
|
|
|
|
@search_results ||= search_service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_objects
|
|
|
|
@search_objects ||= search_results.objects(scope, params[:page])
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def search_service
|
|
|
|
@search_service ||=
|
|
|
|
if project
|
|
|
|
Search::ProjectService.new(project, current_user, params)
|
|
|
|
elsif show_snippets?
|
|
|
|
Search::SnippetService.new(current_user, params)
|
2017-04-13 11:20:04 -04:00
|
|
|
elsif group
|
|
|
|
Search::GroupService.new(current_user, group, params)
|
2017-03-31 09:03:55 -04:00
|
|
|
else
|
|
|
|
Search::GlobalService.new(current_user, params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :current_user, :params
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
SearchService.prepend_if_ee('EE::SearchService')
|