FEATURE: add option to sort topic query result via plugin. (#25349)

Previously, it was not possible to modify the sorting order of the `TopicQuery` result from a plugin. This feature adds support to specify custom sorting functionality in a plugin. We're using the `apply_modifier` method in the `DiscoursePluginRegistry` module to achieve it.

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
Vinoth Kannan
2024-01-24 12:58:12 +05:30
committed by GitHub
parent 32e2a1fd4a
commit ecb7fb0481
3 changed files with 54 additions and 3 deletions

View File

@ -664,9 +664,21 @@ class TopicQuery
end
def apply_ordering(result, options = {})
sort_column = SORTABLE_MAPPING[options[:order]] || "default"
order_option = options[:order]
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
new_result =
DiscoursePluginRegistry.apply_modifier(
:topic_query_apply_ordering_result,
result,
order_option,
sort_dir,
options,
self,
)
return new_result if !new_result.nil? && new_result != result
sort_column = SORTABLE_MAPPING[order_option] || "default"
# If we are sorting in the default order desc, we should consider including pinned
# topics. Otherwise, just use bumped_at.
if sort_column == "default"