Skip to content

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

DEV: Support `in:bookmarked` filter for the `/filter` route (#21000)

Alan Guo Xiang Tanb2a951e4a5b8
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index c9222168406..e1544be24cc 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -93,6 +93,13 @@ class TopicsFilter
"topics.pinned_at IS NOT NULL AND topics.pinned_until > topics.pinned_at AND ? < topics.pinned_until",
Time.zone.now,
)
+ when "bookmarked"
+ return @scope.none unless @guardian.user
+
+ @scope.joins(:topic_users).where(
+ "topic_users.bookmarked AND topic_users.user_id = ?",
+ @guardian.user.id,
+ )
else
@scope
end
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index 7471669077b..8de12dcb601 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -56,6 +56,40 @@ RSpec.describe TopicsFilter do
end
end
end
+
+ describe "when query string is `in:bookmarked`" do
+ fab!(:bookmark) do
+ BookmarkManager.new(user).create_for(
+ bookmarkable_id: topic.id,
+ bookmarkable_type: "Topic",
+ )
+ end
+
+ fab!(:bookmark2) do
+ BookmarkManager.new(admin).create_for(
+ bookmarkable_id: topic.id,
+ bookmarkable_type: "Topic",
+ )
+ end
+
+ it "should not return any topics when user is anonymous" do
+ expect(
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("in:bookmarked")
+ .pluck(:id),
+ ).to eq([])
+ end
+
+ it "should return topics that are bookmarked" do
+ expect(
+ TopicsFilter
+ .new(guardian: Guardian.new(user))
+ .filter_from_query_string("in:bookmarked")
+ .pluck(:id),
+ ).to contain_exactly(topic.id)
+ end
+ end
end
describe "when filtering by categories" do