DEV: Change HashtagAutocompleteService to use DiscoursePluginRegistry (#19491)

Follow up to a review in #18937, this commit changes the HashtagAutocompleteService to no longer use class variables to register hashtag data sources or types in context priority order. This is to address multisite concerns, where one site could e.g. have chat disabled and another might not. The filtered plugin registers I added will not be included if the plugin is disabled.
This commit is contained in:
Martin Brennan
2022-12-19 13:46:17 +10:00
committed by GitHub
parent 68d5bdefdd
commit 6b9c0ee554
13 changed files with 230 additions and 124 deletions

View File

@ -1097,13 +1097,21 @@ class Plugin::Instance
# Used to register data sources for HashtagAutocompleteService to look
# up results based on a #hashtag string.
#
# @param {String} type - Roughly corresponding to a model, this is used as a unique
# key for the datasource and is also used when allowing different
# contexts to search for and lookup these types. The `category`
# and `tag` types are registered by default.
# @param {Class} klass - Must be a class that implements methods with the following
# signatures:
#
# Roughly corresponding to a model, this is used as a unique
# key for the datasource and is also used when allowing different
# contexts to search for and lookup these types. The `category`
# and `tag` types are registered by default.
# def self.type
# end
#
# The FontAwesome icon to use for the data source in the search results
# and cooked markdown.
# def self.icon
# end
#
# @param {Guardian} guardian - Current user's guardian, used for permission-based filtering
# @param {Array} slugs - An array of strings that represent slugs to search this type for,
# e.g. category slugs.
@ -1117,8 +1125,20 @@ class Plugin::Instance
# @returns {Array} An Array of HashtagAutocompleteService::HashtagItem
# def self.search(guardian, term, limit)
# end
def register_hashtag_data_source(type, klass)
HashtagAutocompleteService.register_data_source(type, klass)
#
# @param {Array} search_results - An array of HashtagAutocompleteService::HashtagItem to sort
# @param {String} term - The search term which was used, which may help with sorting.
# @returns {Array} An Array of HashtagAutocompleteService::HashtagItem
# def self.search_sort(search_results, term)
# end
#
# @param {Guardian} guardian - Current user's guardian, used for permission-based filtering
# @param {Integer} limit - The number of search results that should be returned by the query
# @returns {Array} An Array of HashtagAutocompleteService::HashtagItem
# def self.search_without_term(guardian, limit)
# end
def register_hashtag_data_source(klass)
DiscoursePluginRegistry.register_hashtag_autocomplete_data_source(klass, self)
end
##
@ -1134,8 +1154,10 @@ class Plugin::Instance
# for certain types of hashtag result.
# @param {Integer} priority - A number value for ordering type results when hashtag searches
# or lookups occur. Priority is ordered by DESCENDING order.
def register_hashtag_type_in_context(type, context, priority)
HashtagAutocompleteService.register_type_in_context(type, context, priority)
def register_hashtag_type_priority_for_context(type, context, priority)
DiscoursePluginRegistry.register_hashtag_autocomplete_contextual_type_priority(
{ type: type, context: context, priority: priority }, self
)
end
##