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.
45 lines
940 B
Ruby
45 lines
940 B
Ruby
# frozen_string_literal: true
|
|
|
|
class PostFlagPolicy < ApplicationPolicy
|
|
def edit?
|
|
update?
|
|
end
|
|
|
|
def update?
|
|
unbanned? && record.pending? && record.creator_id == user.id
|
|
end
|
|
|
|
def can_search_flagger?
|
|
user.is_moderator?
|
|
end
|
|
|
|
def can_view_flagger?
|
|
record.creator_id == user.id || (user.is_moderator? && record.post&.uploader_id != user.id)
|
|
end
|
|
|
|
def permitted_attributes_for_create
|
|
[:post_id, :reason]
|
|
end
|
|
|
|
def permitted_attributes_for_update
|
|
[:reason]
|
|
end
|
|
|
|
def api_attributes
|
|
attributes = super + [:category]
|
|
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
|