flags: fix flagger:<name> not returning self-flagged uploads
Fix the search `flagger:evazion user:evazion` not returning the user's own self-flagged uploads.
Followup to a6e0872ce.
Fixes #4690: user profile 'flags' count links to /post_flags with different search criteria
This commit is contained in:
@@ -170,7 +170,7 @@ class PostQueryBuilder
|
||||
when "approver"
|
||||
relation.approver_matches(value)
|
||||
when "flagger"
|
||||
relation.flagger_matches(value, current_user)
|
||||
relation.user_subquery_matches(PostFlag.unscoped.category_matches("normal"), value, current_user)
|
||||
when "appealer"
|
||||
relation.user_subquery_matches(PostAppeal.unscoped, value, current_user)
|
||||
when "commenter", "comm"
|
||||
|
||||
@@ -1350,31 +1350,18 @@ class Post < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def flagger_matches(username, current_user)
|
||||
flags = PostFlag.unscoped.category_matches("normal")
|
||||
|
||||
user_subquery_matches(flags, username) do |username|
|
||||
flagger = User.find_by_name(username)
|
||||
PostFlag.unscoped.creator_matches(flagger, current_user)
|
||||
end
|
||||
end
|
||||
|
||||
def user_subquery_matches(subquery, username, current_user, field: :creator, &block)
|
||||
def user_subquery_matches(subquery, username, current_user, field: :creator)
|
||||
subquery = subquery.where("post_id = posts.id").select(1)
|
||||
|
||||
if username.downcase == "any"
|
||||
where("EXISTS (#{subquery.to_sql})")
|
||||
elsif username.downcase == "none"
|
||||
where("NOT EXISTS (#{subquery.to_sql})")
|
||||
elsif block.nil?
|
||||
else
|
||||
user = User.find_by_name(username)
|
||||
return none if user.nil?
|
||||
subquery = subquery.visible_for_search(field, current_user).where(field => user)
|
||||
where("EXISTS (#{subquery.to_sql})")
|
||||
else
|
||||
subquery = subquery.merge(block.call(username))
|
||||
return none if subquery.to_sql.blank?
|
||||
where("EXISTS (#{subquery.to_sql})")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -31,18 +31,6 @@ class PostFlag < ApplicationRecord
|
||||
scope :active, -> { pending.or(rejected.in_cooldown) }
|
||||
|
||||
module SearchMethods
|
||||
def creator_matches(creator, searcher)
|
||||
return none if creator.nil?
|
||||
|
||||
policy = Pundit.policy!(searcher, PostFlag.unscoped.new(creator: creator))
|
||||
|
||||
if policy.can_view_flagger?
|
||||
where(creator: creator).where.not(post: searcher.posts)
|
||||
else
|
||||
none
|
||||
end
|
||||
end
|
||||
|
||||
def category_matches(category)
|
||||
case category
|
||||
when "normal"
|
||||
|
||||
@@ -220,7 +220,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
|
||||
get_auth profile_path, @user, as: :json
|
||||
assert_response :success
|
||||
|
||||
assert_equal(@user.comment_count, response.parsed_body["comment_count"])
|
||||
assert_equal(@user.comments.count, response.parsed_body["comment_count"])
|
||||
end
|
||||
|
||||
should "redirect anonymous users to the sign in page" do
|
||||
|
||||
@@ -465,6 +465,16 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
|
||||
assert_tag_match([], "flagger:does_not_exist")
|
||||
end
|
||||
|
||||
should "return self-flagged posts for the flagger:<name> metatag" do
|
||||
flagger = create(:user)
|
||||
posts = create_list(:post, 2, uploader: flagger)
|
||||
flag = create(:post_flag, post: posts[0], creator: flagger)
|
||||
|
||||
assert_tag_match([], "flagger:#{flagger.name} user:#{flagger.name}", current_user: User.anonymous)
|
||||
assert_tag_match([posts[0]], "flagger:#{flagger.name} user:#{flagger.name}", current_user: flagger)
|
||||
assert_tag_match([posts[0]], "flagger:#{flagger.name} user:#{flagger.name}", current_user: create(:mod_user))
|
||||
end
|
||||
|
||||
should "return posts for the commenter:<name> metatag" do
|
||||
users = create_list(:user, 2, created_at: 2.weeks.ago)
|
||||
posts = create_list(:post, 2)
|
||||
|
||||
Reference in New Issue
Block a user