DEV: Support posts-min:<count> and posts-max:<count> on /filter (#21090)

This commit adds support for the `posts-min:<count>` and
`posts-max:<count>` filters for the topics filtering query language.
`posts-min:1` will filter for topics with at least a one post while
`posts-max:3` will filter foor topics with a maximum of 3 posts.

If the filter has an invalid value, i.e string that cannot be converted
into an integer, the filter will be ignored.

If either of each filter is specify multiple times, only the last
occurence of each filter will be taken into consideration.
This commit is contained in:
Alan Guo Xiang Tan
2023-04-14 06:05:55 +08:00
committed by GitHub
parent 9b3408223b
commit bc4a9c50f2
2 changed files with 94 additions and 0 deletions

View File

@@ -40,6 +40,10 @@ class TopicsFilter
filter_created_by_user(usernames: values.flat_map { |value| value.split(",") })
when "in"
filter_in(values: values)
when "posts-min"
filter_by_number_of_posts(min: values.last)
when "posts-max"
filter_by_number_of_posts(max: values.last)
when "status"
values.each { |status| @scope = filter_status(status: status) }
when "tags"
@@ -77,6 +81,13 @@ class TopicsFilter
private
def filter_by_number_of_posts(min: nil, max: nil)
{ min => ">=", max => "<=" }.each do |value, operator|
next if !value || value !~ /^\d+$/
@scope = @scope.where("topics.posts_count #{operator} ?", value)
end
end
def filter_categories(values:)
exclude_subcategories_category_slugs = []
include_subcategories_category_slugs = []