Skip to content

A platform for community discussion. Free, open, simple.

FIX: Support status:noreplies and status:single_user on the /filter route (#40371)

Régis Hanolf88fd19d3c5a
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d730c669c22..05f29d36220 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3031,6 +3031,8 @@ en:
status_unlisted: "Show unlisted (hidden) topics"
status_deleted: "Show deleted topics (if you have permission)"
status_public: "Show topics in public categories"
+ status_noreplies: "Show topics with no replies"
+ status_single_user: "Show topics with a single participant"
in_pinned: "Show pinned topics"
in_bookmarked: "Show topics you've bookmarked"
in_watching: "Show topics you're watching"
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index ef8a3e97193..8a5388cb43e 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -168,6 +168,10 @@ class TopicsFilter
end
when "public"
@scope = @scope.joins(:category).where("NOT categories.read_restricted")
+ when "noreplies"
+ @scope = @scope.where("topics.posts_count = 1")
+ when "single_user"
+ @scope = @scope.where("topics.participant_count = 1")
else
if custom_filter = TopicsFilter.custom_status_filters[status]
@scope = custom_filter[:block].call(@scope) if custom_filter[:enabled].call
@@ -277,6 +281,8 @@ class TopicsFilter
{ name: "status:unlisted", description: I18n.t("filter.description.status_unlisted") },
{ name: "status:deleted", description: I18n.t("filter.description.status_deleted") },
{ name: "status:public", description: I18n.t("filter.description.status_public") },
+ { name: "status:noreplies", description: I18n.t("filter.description.status_noreplies") },
+ { name: "status:single_user", description: I18n.t("filter.description.status_single_user") },
{ name: "order:", description: I18n.t("filter.description.order"), priority: 1 },
{ name: "order:activity", description: I18n.t("filter.description.order_activity") },
{ name: "order:activity-asc", description: I18n.t("filter.description.order_activity_asc") },
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index b21485a6ff5..35a88de6334 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -1206,6 +1206,36 @@ RSpec.describe TopicsFilter do
).to contain_exactly(closed_and_unlisted_topic.id)
end
end
+
+ describe "when query string is `status:noreplies`" do
+ fab!(:topic_without_replies) { Fabricate(:topic, posts_count: 1) }
+ fab!(:topic_with_replies) { Fabricate(:topic, posts_count: 2) }
+
+ it "should only return topics that have no replies" do
+ expect(
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("status:noreplies")
+ .pluck(:id),
+ ).to contain_exactly(topic_without_replies.id)
+ end
+ end
+
+ describe "when query string is `status:single_user`" do
+ fab!(:single_user_topic) { Fabricate(:topic, participant_count: 1) }
+ fab!(:multi_user_topic) { Fabricate(:topic, participant_count: 2) }
+
+ it "should only return topics that have a single participant" do
+ topic_ids =
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("status:single_user")
+ .pluck(:id)
+
+ expect(topic_ids).to include(single_user_topic.id)
+ expect(topic_ids).not_to include(multi_user_topic.id)
+ end
+ end
end
describe "when filtering by tags" do