From 955d47bbd037ac470605eebcc1016f5c7bae7137 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Tue, 10 May 2022 09:08:01 +1000 Subject: [PATCH] FIX: Use polymorphic bookmarks for in:bookmarks search (#16684) This commit makes sure the in:bookmarks post advanced search filter works with polymorphic bookmarks. --- lib/search.rb | 17 ++++++++++++++--- spec/lib/search_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index a9974d48564..8a92641d399 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -453,11 +453,22 @@ class Search end end - # TODO (martin) [POLYBOOK] Make a separate PR for advanced searched in:bookmarks - # functionality as the bookmarkables will have to define this. + # NOTE: With polymorphic bookmarks it may make sense to possibly expand + # this at some point, as it only acts on posts at the moment. On the other + # hand, this may not be necessary, as the user bookmark list has advanced + # search based on a RegisteredBookmarkable's #search_query method. advanced_filter(/^in:(bookmarks)$/i) do |posts, match| if @guardian.user - posts.where("posts.id IN (SELECT post_id FROM bookmarks WHERE bookmarks.user_id = #{@guardian.user.id})") + if SiteSetting.use_polymorphic_bookmarks + posts.where(<<~SQL) + posts.id IN ( + SELECT bookmarkable_id FROM bookmarks + WHERE bookmarks.user_id = #{@guardian.user.id} AND bookmarks.bookmarkable_type = 'Post' + ) + SQL + else + posts.where("posts.id IN (SELECT post_id FROM bookmarks WHERE bookmarks.user_id = #{@guardian.user.id})") + end end end diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb index 5ead4116c76..ddf15b55a02 100644 --- a/spec/lib/search_spec.rb +++ b/spec/lib/search_spec.rb @@ -1471,6 +1471,34 @@ describe Search do end describe 'Advanced search' do + describe "bookmarks" do + fab!(:user) { Fabricate(:user) } + let!(:bookmark_post1) { Fabricate(:post, raw: 'boom this is a bookmarked post') } + let!(:bookmark_post2) { Fabricate(:post, raw: 'wow some other cool thing') } + + def search_with_bookmarks + Search.execute('boom in:bookmarks', guardian: Guardian.new(user)) + end + + it "can filter by posts in the user's bookmarks" do + expect(search_with_bookmarks.posts.map(&:id)).to eq([]) + Fabricate(:bookmark, user: user, post: bookmark_post1) + expect(search_with_bookmarks.posts.map(&:id)).to match_array([bookmark_post1.id]) + end + + context "using polymorphic bookmarks" do + before do + SiteSetting.use_polymorphic_bookmarks = true + end + + it "can filter by posts in the user's bookmarks" do + expect(search_with_bookmarks.posts.map(&:id)).to eq([]) + bm = Fabricate(:bookmark, user: user, bookmarkable: bookmark_post1) + expect(search_with_bookmarks.posts.map(&:id)).to match_array([bookmark_post1.id]) + end + end + end + it 'supports pinned' do Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic) _post = Fabricate(:post, raw: 'boom boom shake the room', topic: topic)