flags: fix mods not being able to see the flagger on self-flagged posts.
Fix a bug where, when a mod searched for their own flags, they couldn't see their own self-flagged uploads. Fix a bug where, when a mod viewed their own flags, they couldn't see the flagger name for their own self-flagged uploads. This also makes it so you can do things like `/post_flags?search[creator][level]=20` to search flags by user level.
This commit is contained in:
@@ -59,17 +59,9 @@ class PostFlag < ApplicationRecord
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :post)
|
||||
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :post, :creator)
|
||||
q = q.text_attribute_matches(:reason, params[:reason_matches])
|
||||
|
||||
if params[:creator_id].present?
|
||||
flagger = User.find(params[:creator_id])
|
||||
q = q.creator_matches(flagger, CurrentUser.user)
|
||||
elsif params[:creator_name].present?
|
||||
flagger = User.find_by_name(params[:creator_name])
|
||||
q = q.creator_matches(flagger, CurrentUser.user)
|
||||
end
|
||||
|
||||
if params[:category]
|
||||
q = q.category_matches(params[:category])
|
||||
end
|
||||
|
||||
@@ -14,7 +14,7 @@ class PostFlagPolicy < ApplicationPolicy
|
||||
end
|
||||
|
||||
def can_view_flagger?
|
||||
(user.is_moderator? || record.creator_id == user.id) && (record.post&.uploader_id != user.id)
|
||||
record.creator_id == user.id || (user.is_moderator? && record.post&.uploader_id != user.id)
|
||||
end
|
||||
|
||||
def permitted_attributes_for_create
|
||||
@@ -30,4 +30,15 @@ class PostFlagPolicy < ApplicationPolicy
|
||||
attributes -= [:creator_id] unless can_view_flagger?
|
||||
attributes
|
||||
end
|
||||
|
||||
def visible_for_search(relation, attribute)
|
||||
case attribute
|
||||
in :creator | :creator_id if can_search_flagger?
|
||||
relation.where(creator: user).or(relation.where.not(post: user.posts))
|
||||
in :creator | :creator_id
|
||||
relation.where(creator: user)
|
||||
else
|
||||
relation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user