From 86de5cb5d2989b45657236c97cb66bf2f03c0112 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 6 Apr 2022 23:48:41 -0500 Subject: [PATCH] posts: fixup `flagger:` metatag. Fix regression in 01a22930e. --- app/logical/post_query_builder.rb | 2 +- app/models/post.rb | 6 +++--- test/unit/post_query_builder_test.rb | 20 ++++++++++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 56ac71e72..5a4f7245c 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -215,7 +215,7 @@ class PostQueryBuilder when "approver" relation.approver_matches(value) when "flagger" - relation.flagger_matches(value) + relation.flagger_matches(value, current_user) when "appealer" relation.user_subquery_matches(PostAppeal.unscoped, value) when "commenter", "comm" diff --git a/app/models/post.rb b/app/models/post.rb index a3459035b..e629a9e61 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1311,7 +1311,7 @@ class Post < ApplicationRecord end end - def flagger_matches(username) + def flagger_matches(username, current_user) flags = PostFlag.unscoped.category_matches("normal") user_subquery_matches(flags, username) do |username| @@ -1323,9 +1323,9 @@ class Post < ApplicationRecord def user_subquery_matches(subquery, username, field: :creator, &block) subquery = subquery.where("post_id = posts.id").select(1) - if username == "any" + if username.downcase == "any" where("EXISTS (#{subquery.to_sql})") - elsif username == "none" + elsif username.downcase == "none" where("NOT EXISTS (#{subquery.to_sql})") elsif block.nil? user = User.find_by_name(username) diff --git a/test/unit/post_query_builder_test.rb b/test/unit/post_query_builder_test.rb index efe1f1941..fbd40d059 100644 --- a/test/unit/post_query_builder_test.rb +++ b/test/unit/post_query_builder_test.rb @@ -1,8 +1,8 @@ require 'test_helper' class PostQueryBuilderTest < ActiveSupport::TestCase - def assert_tag_match(posts, query, **options) - assert_equal(posts.map(&:id), Post.user_tag_match(query, CurrentUser.user, **options).pluck(:id)) + def assert_tag_match(posts, query, current_user: CurrentUser.user, **options) + assert_equal(posts.map(&:id), Post.user_tag_match(query, current_user, **options).pluck(:id)) end def assert_fast_count(count, query, query_options = {}, fast_count_options = {}) @@ -451,6 +451,22 @@ class PostQueryBuilderTest < ActiveSupport::TestCase assert_tag_match([], "approver:does_not_exist") end + should "return posts for the flagger: metatag" do + posts = create_list(:post, 2) + flag = create(:post_flag, post: posts[0]) + + assert_tag_match([posts[0]], "flagger:#{flag.creator.name}", current_user: flag.creator) + assert_tag_match([posts[1]], "-flagger:#{flag.creator.name}", current_user: flag.creator) + + assert_tag_match([], "flagger:#{flag.creator.name}", current_user: User.anonymous) + assert_tag_match([posts[1], posts[0]], "-flagger:#{flag.creator.name}", current_user: User.anonymous) + + assert_tag_match([posts[0]], "flagger:any") + assert_tag_match([posts[1]], "flagger:none") + assert_tag_match([posts[1]], "flagger:NONE") + assert_tag_match([], "flagger:does_not_exist") + end + should "return posts for the commenter: metatag" do users = create_list(:user, 2, created_at: 2.weeks.ago) posts = create_list(:post, 2)