mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 13:31:18 +08:00
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:
@ -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"
|
||||
|
Reference in New Issue
Block a user