DEV: Remove experimental support for query string on /filter route (#20632)

This commit is contained in:
Alan Guo Xiang Tan
2023-03-22 10:04:57 +08:00
committed by GitHub
parent 520d4f504b
commit b06e31f8e7
11 changed files with 92 additions and 100 deletions

View File

@@ -848,17 +848,12 @@ class TopicQuery
end
if status = options[:status]
options[:q] ||= +""
options[:q] << " status:#{status}"
end
if options[:q].present?
result =
TopicsFilter.new(
scope: result,
guardian: @guardian,
category_id: options[:category],
).filter(options[:q])
).filter(status: options[:status])
end
if (filter = (options[:filter] || options[:f])) && @user

View File

@@ -1,51 +1,31 @@
# frozen_string_literal: true
class TopicsFilter
def self.register_filter(matcher, &block)
self.filters[matcher] = block
end
def self.filters
@@filters ||= {}
end
register_filter(/\Astatus:([a-zA-Z]+)\z/i) do |topics, match|
case match
when "open"
topics.where("NOT topics.closed AND NOT topics.archived")
when "closed"
topics.where("topics.closed")
when "archived"
topics.where("topics.archived")
when "deleted"
if @guardian.can_see_deleted_topics?(@category)
topics.unscope(where: :deleted_at).where("topics.deleted_at IS NOT NULL")
end
end
end
def initialize(guardian:, scope: Topic, category_id: nil)
@guardian = guardian
@scope = scope
@category = category_id.present? ? Category.find_by(id: category_id) : nil
end
def filter(input)
input
.to_s
.scan(/(([^" \t\n\x0B\f\r]+)?(("[^"]+")?))/)
.to_a
.map do |(word, _)|
next if word.blank?
self.class.filters.each do |matcher, block|
cleaned = word.gsub(/["']/, "")
new_scope = instance_exec(@scope, $1, &block) if cleaned =~ matcher
@scope = new_scope if !new_scope.nil?
end
end
def filter(status: nil)
filter_status(@scope, status) if status
@scope
end
private
def filter_status(scope, status)
case status
when "open"
@scope = @scope.where("NOT topics.closed AND NOT topics.archived")
when "closed"
@scope = @scope.where("topics.closed")
when "archived"
@scope = @scope.where("topics.archived")
when "deleted"
if @guardian.can_see_deleted_topics?(@category)
@scope = @scope.unscope(where: :deleted_at).where("topics.deleted_at IS NOT NULL")
end
end
end
end