Fix #4027: Search for posts favorited by the fav group order.
This commit is contained in:
@@ -143,6 +143,7 @@ Autocomplete.initialize_tag_autocomplete = function() {
|
||||
results = await Autocomplete.pool_source(term, metatag + ":");
|
||||
break;
|
||||
case "favgroup":
|
||||
case "ordfavgroup":
|
||||
results = await Autocomplete.favorite_group_source(term, metatag + ":", CurrentUser.data("id"));
|
||||
break;
|
||||
case "search":
|
||||
|
||||
@@ -32,7 +32,7 @@ class PostQueryBuilder
|
||||
-downvote downvote
|
||||
-fav fav
|
||||
-ordfav ordfav
|
||||
-favgroup favgroup
|
||||
-favgroup favgroup ordfavgroup
|
||||
-pool pool ordpool
|
||||
-commentary commentary
|
||||
-id id
|
||||
@@ -207,6 +207,8 @@ class PostQueryBuilder
|
||||
favgroup_matches(value)
|
||||
when "-favgroup"
|
||||
favgroup_matches(value).negate
|
||||
when "ordfavgroup"
|
||||
ordfavgroup_matches(value)
|
||||
when "fav"
|
||||
favorites_include(value)
|
||||
when "-fav"
|
||||
@@ -429,6 +431,13 @@ class PostQueryBuilder
|
||||
Post.joins("JOIN (#{pool_posts.to_sql}) pool_posts ON pool_posts.post_id = posts.id").order("pool_posts.pool_index ASC")
|
||||
end
|
||||
|
||||
def ordfavgroup_matches(query)
|
||||
# XXX unify with FavoriteGroup#posts
|
||||
favgroup = FavoriteGroup.visible(CurrentUser.user).name_or_id_matches(query, CurrentUser.user)
|
||||
favgroup_posts = favgroup.joins("CROSS JOIN unnest(favorite_groups.post_ids) WITH ORDINALITY AS row(post_id, favgroup_index)").select(:post_id, :favgroup_index)
|
||||
Post.joins("JOIN (#{favgroup_posts.to_sql}) favgroup_posts ON favgroup_posts.post_id = posts.id").order("favgroup_posts.favgroup_index ASC")
|
||||
end
|
||||
|
||||
def favgroup_matches(query)
|
||||
favgroup = FavoriteGroup.visible(CurrentUser.user).name_or_id_matches(query, CurrentUser.user)
|
||||
Post.where(id: favgroup.select("unnest(post_ids)"))
|
||||
|
||||
@@ -299,6 +299,27 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
should "return posts for the ordfavgroup:<name> metatag" do
|
||||
post1 = create(:post)
|
||||
post2 = create(:post)
|
||||
post3 = create(:post)
|
||||
|
||||
favgroup1 = create(:favorite_group, creator: CurrentUser.user, post_ids: [post1.id, post2.id])
|
||||
favgroup2 = create(:favorite_group, creator: create(:user), post_ids: [post2.id, post3.id], is_public: false)
|
||||
|
||||
assert_tag_match([post1, post2], "ordfavgroup:#{favgroup1.id}")
|
||||
assert_tag_match([post1, post2], "ordfavgroup:#{favgroup1.name}")
|
||||
assert_tag_match([], "ordfavgroup:#{favgroup2.id}")
|
||||
assert_tag_match([], "ordfavgroup:#{favgroup2.name}")
|
||||
|
||||
as(favgroup2.creator) do
|
||||
assert_tag_match([post2, post3], "ordfavgroup:#{favgroup2.id}")
|
||||
assert_tag_match([post2, post3], "ordfavgroup:#{favgroup2.name}")
|
||||
assert_tag_match([post1, post2], "ordfavgroup:#{favgroup1.id}")
|
||||
assert_tag_match([], "ordfavgroup:#{favgroup1.name}")
|
||||
end
|
||||
end
|
||||
|
||||
should "return posts for the user:<name> metatag" do
|
||||
users = create_list(:user, 2, created_at: 2.weeks.ago)
|
||||
posts = users.map { |u| create(:post, uploader: u) }
|
||||
|
||||
Reference in New Issue
Block a user