Fix #5335: Queries with "ordfav:<username>" and geometry attributes (e.g. "ratio:", "height:") crashes the api/site.

Fix `Relation passed to #and must be structurally compatible. Incompatible values: [:joins] (ArgumentError)`
exception in `ordfav:evazion ratio:4:3` search. Broken by e849d8f1c.

We were effectively doing this:

    q1 = Post.joins(:favorites, :media_asset).where("favorites.user_id = ?", 52664).order("favorites.id DESC")
    q2 = Post.joins(:media_asset, :favorites).where("ROUND(media_assets.image_width::numeric / media_assets.image_height::numeric, 2) = 1.33")
    q3 = q1.and(q2)

This failed because Rails didn't like the fact that the joins were in a different order when the
queries were `and`-ed together.
This commit is contained in:
evazion
2022-11-06 21:08:55 -06:00
parent c133866cb7
commit 174c8e0067
2 changed files with 19 additions and 2 deletions

View File

@@ -244,12 +244,12 @@ class PostQueryBuilder
in :and
joins = children.flat_map(&:joins_values)
orders = children.flat_map(&:order_values)
nodes = children.map { |child| child.joins(joins).order(orders) }
nodes = children.map { |child| child.except(:joins).joins(joins).order(orders) }
nodes.reduce(&:and)
in :or
joins = children.flat_map(&:joins_values)
orders = children.flat_map(&:order_values)
nodes = children.map { |child| child.joins(joins).order(orders) }
nodes = children.map { |child| child.except(:joins).joins(joins).order(orders) }
nodes.reduce(&:or)
end
end

View File

@@ -315,6 +315,23 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
assert_tag_match([post2, post1], "ordfav:#{CurrentUser.user.name} -has:comments")
end
should "allow the ordfav:<name> metatag to be combined with other metatags" do
post1 = create(:post, tag_string: "fav:#{CurrentUser.user.name}", media_asset: build(:media_asset, image_width: 800, image_height: 600, file_size: 1234, file_ext: "jpg"))
post2 = create(:post, tag_string: "fav:#{CurrentUser.user.name}", media_asset: build(:media_asset, image_width: 1920, image_height: 1080, file_size: 4567, file_ext: "png"))
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} ratio:4/3")
assert_tag_match([post1], "ratio:4/3 ordfav:#{CurrentUser.user.name}")
assert_tag_match([post2], "ordfav:#{CurrentUser.user.name} ratio:16/9")
assert_tag_match([post2], "ratio:16/9 ordfav:#{CurrentUser.user.name}")
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} width:800")
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} height:600")
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} mpixels:0.48")
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} filesize:1234")
assert_tag_match([post1], "ordfav:#{CurrentUser.user.name} filetype:jpg")
end
should "return posts for the pool:<name> metatag" do
SqsService.any_instance.stubs(:send_message)