mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: allow to extend topic_eager_loads in Search (#10625)
This additional interface is required by encrypt plugin
This commit is contained in:
committed by
GitHub
parent
15f7fa801d
commit
cb58cbbc2c
@@ -206,6 +206,17 @@ class Plugin::Instance
|
||||
Search.advanced_filter(trigger, &block)
|
||||
end
|
||||
|
||||
# Allow to eager load additional tables in Search. Useful to avoid N+1 performance problems.
|
||||
# Example usage:
|
||||
# register_search_topic_eager_load do |opts|
|
||||
# %i(example_table)
|
||||
# end
|
||||
# OR
|
||||
# register_search_topic_eager_load(%i(example_table))
|
||||
def register_search_topic_eager_load(tables = nil, &block)
|
||||
Search.custom_topic_eager_load(tables, &block)
|
||||
end
|
||||
|
||||
# Request a new size for topic thumbnails
|
||||
# Will respect plugin enabled setting is enabled
|
||||
# Size should be an array with two elements [max_width, max_height]
|
||||
|
||||
@@ -297,6 +297,14 @@ class Search
|
||||
@advanced_filters
|
||||
end
|
||||
|
||||
def self.custom_topic_eager_load(tables = nil, &block)
|
||||
(@custom_topic_eager_loads ||= []) << (tables || block)
|
||||
end
|
||||
|
||||
def self.custom_topic_eager_loads
|
||||
Array.wrap(@custom_topic_eager_loads)
|
||||
end
|
||||
|
||||
advanced_filter(/^in:personal-direct$/) do |posts|
|
||||
if @guardian.user
|
||||
posts
|
||||
@@ -1194,11 +1202,13 @@ class Search
|
||||
topic_eager_loads << :tags
|
||||
end
|
||||
|
||||
Search.custom_topic_eager_loads.each do |custom_loads|
|
||||
topic_eager_loads.concat(custom_loads.is_a?(Array) ? custom_loads : custom_loads.call(search_pms: @search_pms).to_a)
|
||||
end
|
||||
|
||||
query.includes(topic: topic_eager_loads)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Limited for performance reasons since `TS_HEADLINE` is slow when the text
|
||||
# document is too long.
|
||||
MAX_LENGTH_FOR_HEADLINE = 2500
|
||||
|
||||
Reference in New Issue
Block a user