FIX: Use polymorphic bookmarks for in:bookmarks search (#16684)

This commit makes sure the in:bookmarks post advanced
search filter works with polymorphic bookmarks.
This commit is contained in:
Martin Brennan 2022-05-10 09:08:01 +10:00 committed by GitHub
parent 2df3c65ba9
commit 955d47bbd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -453,11 +453,22 @@ class Search
end end
end end
# TODO (martin) [POLYBOOK] Make a separate PR for advanced searched in:bookmarks # NOTE: With polymorphic bookmarks it may make sense to possibly expand
# functionality as the bookmarkables will have to define this. # 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| advanced_filter(/^in:(bookmarks)$/i) do |posts, match|
if @guardian.user 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
end end

View File

@ -1471,6 +1471,34 @@ describe Search do
end end
describe 'Advanced search' do 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 it 'supports pinned' do
Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic) Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)
_post = Fabricate(:post, raw: 'boom boom shake the room', topic: topic) _post = Fabricate(:post, raw: 'boom boom shake the room', topic: topic)