FEATURE: Sort hashtags starting with term higher priority (#19463)

This introduces another "section" of queries to the
hashtag autocomplete search, which returns results for
each type that start with the search term. So now results
will be in this order, and within these sections ordered
by the types in priority order:

1. Exact matches sorted by type
2. "starts with" sorted by type
3. Everything else sorted by type then name within type
This commit is contained in:
Martin Brennan
2022-12-15 13:01:44 +10:00
committed by GitHub
parent 2b4009c6bc
commit ec9ec1e04e
9 changed files with 289 additions and 134 deletions

View File

@ -99,19 +99,19 @@ module Chat::ChatChannelFetcher
channels = channels.where(status: options[:status]) if options[:status].present?
if options[:filter].present?
category_filter = \
if options[:filter_on_category_name]
"OR categories.name ILIKE :filter"
else
""
end
category_filter =
(options[:filter_on_category_name] ? "OR categories.name ILIKE :filter" : "")
sql =
"chat_channels.name ILIKE :filter OR chat_channels.slug ILIKE :filter #{category_filter}"
if options[:match_filter_on_starts_with]
filter_sql = "#{options[:filter].downcase}%"
else
filter_sql = "%#{options[:filter].downcase}%"
end
channels =
channels.where(sql, filter: "%#{options[:filter].downcase}%").order(
"chat_channels.name ASC, categories.name ASC",
)
channels.where(sql, filter: filter_sql).order("chat_channels.name ASC, categories.name ASC")
end
if options.key?(:slugs)

View File

@ -27,7 +27,12 @@ class Chat::ChatChannelHashtagDataSource
end
end
def self.search(guardian, term, limit)
def self.search(
guardian,
term,
limit,
condition = HashtagAutocompleteService.search_conditions[:contains]
)
if SiteSetting.enable_experimental_hashtag_autocomplete
return [] if !guardian.can_chat?
Chat::ChatChannelFetcher
@ -36,6 +41,8 @@ class Chat::ChatChannelHashtagDataSource
filter: term,
limit: limit,
exclude_dm_channels: true,
match_filter_on_starts_with:
condition == HashtagAutocompleteService.search_conditions[:starts_with],
)
.map { |channel| channel_to_hashtag_item(guardian, channel) }
else