2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module SearchHelper
def search_autocomplete_opts ( term )
return unless current_user
resources_results = [
groups_autocomplete ( term ) ,
projects_autocomplete ( term )
] . flatten
2016-09-29 09:46:39 +05:30
search_pattern = Regexp . new ( Regexp . escape ( term ) , " i " )
2014-09-02 18:07:02 +05:30
generic_results = project_autocomplete + default_autocomplete + help_autocomplete
2018-03-17 18:26:18 +05:30
generic_results . concat ( default_autocomplete_admin ) if current_user . admin?
2016-09-29 09:46:39 +05:30
generic_results . select! { | result | result [ :label ] =~ search_pattern }
2014-09-02 18:07:02 +05:30
[
resources_results ,
generic_results
] . flatten . uniq do | item |
item [ :label ]
end
end
2016-06-02 11:05:42 +05:30
def search_entries_info ( collection , scope , term )
2019-02-15 15:39:39 +05:30
return if collection . to_a . empty?
2016-06-02 11:05:42 +05:30
from = collection . offset_value + 1
2019-02-15 15:39:39 +05:30
to = collection . offset_value + collection . to_a . size
2016-06-02 11:05:42 +05:30
count = collection . total_count
2019-05-18 00:54:41 +05:30
s_ ( " SearchResults|Showing %{from} - %{to} of %{count} %{scope} for \" %{term} \" " ) % { from : from , to : to , count : count , scope : scope . humanize ( capitalize : false ) , term : term }
2016-06-02 11:05:42 +05:30
end
2019-05-18 00:54:41 +05:30
def find_project_for_result_blob ( projects , result )
2018-11-08 19:23:39 +05:30
@project
end
2019-05-18 00:54:41 +05:30
# Used in EE
def blob_projects ( results )
nil
end
2016-09-29 09:46:39 +05:30
def parse_search_result ( result )
2018-11-08 19:23:39 +05:30
result
end
def search_blob_title ( project , filename )
filename
2016-09-29 09:46:39 +05:30
end
2019-05-18 00:54:41 +05:30
def search_service
@search_service || = :: SearchService . new ( current_user , params )
end
2014-09-02 18:07:02 +05:30
private
# Autocomplete results for various settings pages
def default_autocomplete
[
2019-05-18 00:54:41 +05:30
{ category : " Settings " , label : _ ( " User settings " ) , url : profile_path } ,
{ category : " Settings " , label : _ ( " SSH Keys " ) , url : profile_keys_path } ,
{ category : " Settings " , label : _ ( " Dashboard " ) , url : root_path }
2018-03-17 18:26:18 +05:30
]
end
# Autocomplete results for settings pages, for admins
def default_autocomplete_admin
[
2019-05-18 00:54:41 +05:30
{ category : " Settings " , label : _ ( " Admin Section " ) , url : admin_root_path }
2014-09-02 18:07:02 +05:30
]
end
# Autocomplete results for internal help pages
def help_autocomplete
[
2019-05-18 00:54:41 +05:30
{ category : " Help " , label : _ ( " API Help " ) , url : help_page_path ( " api/README " ) } ,
{ category : " Help " , label : _ ( " Markdown Help " ) , url : help_page_path ( " user/markdown " ) } ,
{ category : " Help " , label : _ ( " Permissions Help " ) , url : help_page_path ( " user/permissions " ) } ,
{ category : " Help " , label : _ ( " Public Access Help " ) , url : help_page_path ( " public_access/public_access " ) } ,
{ category : " Help " , label : _ ( " Rake Tasks Help " ) , url : help_page_path ( " raketasks/README " ) } ,
{ category : " Help " , label : _ ( " SSH Keys Help " ) , url : help_page_path ( " ssh/README " ) } ,
{ category : " Help " , label : _ ( " System Hooks Help " ) , url : help_page_path ( " system_hooks/system_hooks " ) } ,
{ category : " Help " , label : _ ( " Webhooks Help " ) , url : help_page_path ( " user/project/integrations/webhooks " ) } ,
{ category : " Help " , label : _ ( " Workflow Help " ) , url : help_page_path ( " workflow/README " ) }
2014-09-02 18:07:02 +05:30
]
end
# Autocomplete results for the current project, if it's defined
def project_autocomplete
if @project && @project . repository . exists? && @project . repository . root_ref
2016-06-02 11:05:42 +05:30
ref = @ref || @project . repository . root_ref
2014-09-02 18:07:02 +05:30
[
2019-05-18 00:54:41 +05:30
{ category : " In this project " , label : _ ( " Files " ) , url : project_tree_path ( @project , ref ) } ,
{ category : " In this project " , label : _ ( " Commits " ) , url : project_commits_path ( @project , ref ) } ,
{ category : " In this project " , label : _ ( " Network " ) , url : project_network_path ( @project , ref ) } ,
{ category : " In this project " , label : _ ( " Graph " ) , url : project_graph_path ( @project , ref ) } ,
{ category : " In this project " , label : _ ( " Issues " ) , url : project_issues_path ( @project ) } ,
{ category : " In this project " , label : _ ( " Merge Requests " ) , url : project_merge_requests_path ( @project ) } ,
{ category : " In this project " , label : _ ( " Milestones " ) , url : project_milestones_path ( @project ) } ,
{ category : " In this project " , label : _ ( " Snippets " ) , url : project_snippets_path ( @project ) } ,
{ category : " In this project " , label : _ ( " Members " ) , url : project_project_members_path ( @project ) } ,
{ category : " In this project " , label : _ ( " Wiki " ) , url : project_wikis_path ( @project ) }
2014-09-02 18:07:02 +05:30
]
else
[ ]
end
end
# Autocomplete results for the current user's groups
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def groups_autocomplete ( term , limit = 5 )
2018-03-17 18:26:18 +05:30
current_user . authorized_groups . order_id_desc . search ( term ) . limit ( limit ) . map do | group |
2014-09-02 18:07:02 +05:30
{
2016-06-02 11:05:42 +05:30
category : " Groups " ,
id : group . id ,
2017-08-17 22:00:37 +05:30
label : " #{ search_result_sanitize ( group . full_name ) } " ,
2018-11-18 11:00:15 +05:30
url : group_path ( group ) ,
avatar_url : group . avatar_url || ''
2014-09-02 18:07:02 +05:30
}
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
# Autocomplete results for the current user's projects
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def projects_autocomplete ( term , limit = 5 )
2018-03-17 18:26:18 +05:30
current_user . authorized_projects . order_id_desc . search_by_title ( term )
2017-09-10 17:25:29 +05:30
. sorted_by_stars . non_archived . limit ( limit ) . map do | p |
2014-09-02 18:07:02 +05:30
{
2016-06-02 11:05:42 +05:30
category : " Projects " ,
id : p . id ,
value : " #{ search_result_sanitize ( p . name ) } " ,
2018-03-27 19:54:05 +05:30
label : " #{ search_result_sanitize ( p . full_name ) } " ,
2018-11-18 11:00:15 +05:30
url : project_path ( p ) ,
avatar_url : p . avatar_url || ''
2014-09-02 18:07:02 +05:30
}
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def search_result_sanitize ( str )
Sanitize . clean ( str )
end
2015-04-26 12:48:37 +05:30
2016-09-13 17:45:13 +05:30
def search_filter_path ( options = { } )
2015-04-26 12:48:37 +05:30
exist_opts = {
search : params [ :search ] ,
project_id : params [ :project_id ] ,
group_id : params [ :group_id ] ,
2016-09-13 17:45:13 +05:30
scope : params [ :scope ] ,
repository_ref : params [ :repository_ref ]
2015-04-26 12:48:37 +05:30
}
options = exist_opts . merge ( options )
search_path ( options )
end
2017-09-10 17:25:29 +05:30
def search_filter_input_options ( type )
2018-03-17 18:26:18 +05:30
opts =
{
id : " filtered-search- #{ type } " ,
2019-05-18 00:54:41 +05:30
placeholder : _ ( 'Search or filter results...' ) ,
2018-03-17 18:26:18 +05:30
data : {
'username-params' = > UserSerializer . new . represent ( @users )
} ,
autocomplete : 'off'
2017-09-10 17:25:29 +05:30
}
if @project . present?
opts [ :data ] [ 'project-id' ] = @project . id
opts [ :data ] [ 'base-endpoint' ] = project_path ( @project )
2019-02-15 15:39:39 +05:30
elsif @group . present?
2018-03-17 18:26:18 +05:30
opts [ :data ] [ 'group-id' ] = @group . id
2017-09-10 17:25:29 +05:30
opts [ :data ] [ 'base-endpoint' ] = group_canonical_path ( @group )
2019-02-15 15:39:39 +05:30
else
opts [ :data ] [ 'base-endpoint' ] = root_dashboard_path
2017-09-10 17:25:29 +05:30
end
opts
end
2019-02-15 15:39:39 +05:30
def search_history_storage_prefix
if @project . present?
@project . full_path
elsif @group . present?
@group . full_path
else
'dashboard'
end
end
2016-11-03 12:29:30 +05:30
# Sanitize a HTML field for search display. Most tags are stripped out and the
# maximum length is set to 200 characters.
def search_md_sanitize ( object , field )
html = markdown_field ( object , field )
html = Truncato . truncate (
html ,
count_tags : false ,
count_tail : false ,
max_length : 200
)
# Truncato's filtered_tags and filtered_attributes are not quite the same
2015-04-26 12:48:37 +05:30
sanitize ( html , tags : %w( a p ol ul li pre code ) )
end
2018-03-17 18:26:18 +05:30
def limited_count ( count , limit = 1000 )
count > limit ? " #{ limit } + " : count
end
2019-05-18 00:54:41 +05:30
def search_tabs? ( tab )
return false if Feature . disabled? ( :users_search , default_enabled : true )
if @project
project_search_tabs? ( :members )
else
can? ( current_user , :read_users_list )
end
end
2014-09-02 18:07:02 +05:30
end