Files
danbooru/app/models/post_event.rb
evazion 054ac51d47 policies: remove current request from context.
This refactors Pundit policies to only rely on the current user, not on
the current user and the current HTTP request. In retrospect, it was a
bad idea to include the current request in the Pundit context. It bleeds
out everywhere and there are many contexts (in tests and models) where
we only have the current user, not the current request. The previous
commit got rid of the only two places where we used it.
2021-01-17 00:57:59 -06:00

80 lines
1.6 KiB
Ruby

class PostEvent
include ActiveModel::Model
include ActiveModel::Serializers::JSON
include ActiveModel::Serializers::Xml
attr_accessor :event
delegate :created_at, to: :event
def self.find_for_post(post_id)
post = Post.find(post_id)
(post.appeals + post.flags + post.approvals).sort_by(&:created_at).reverse.map { |e| new(event: e) }
end
def type_name
case event
when PostFlag
"flag"
when PostAppeal
"appeal"
when PostApproval
"approval"
end
end
def type
type_name.first
end
def reason
event.try(:reason) || ""
end
def creator_id
event.try(:creator_id) || event.try(:user_id)
end
def creator
event.try(:creator) || event.try(:user)
end
def status
if event.is_a?(PostApproval)
"approved"
elsif (event.is_a?(PostAppeal) && event.succeeded?) || (event.is_a?(PostFlag) && event.rejected?)
"approved"
elsif (event.is_a?(PostAppeal) && event.rejected?) || (event.is_a?(PostFlag) && event.succeeded?)
"deleted"
else
"pending"
end
end
def is_creator_visible?(user = CurrentUser.user)
case event
when PostAppeal, PostApproval
true
when PostFlag
flag = event
Pundit.policy!(user, flag).can_view_flagger?
end
end
def attributes
{
"creator_id": nil,
"created_at": nil,
"reason": nil,
"status": nil,
"type": nil
}
end
# XXX can't use hidden_attributes because we don't inherit from ApplicationRecord.
def serializable_hash(options = {})
hash = super
hash = hash.except(:creator_id) unless is_creator_visible?
hash
end
end