Files
danbooru/app/models/post_appeal.rb
evazion 0a0a85ee70 Fix #4568: Send appealed posts back to the mod queue
* Include appealed posts in the modqueue.

* Add `status` field to appeals. Appeals start out as `pending`, then
  become `rejected` if the post isn't approved within three days. If the
  post is approved, the appeal's status becomes `succeeded`.

* Add `status` field to flags. Flags start out as `pending` then become
  `rejected` if the post is approved within three days. If the post
  isn't approved, the flag's status becomes `succeeded`.

* Leave behind a "Unapproved in three days" dummy flag when an appeal
  goes unapproved, just like when a pending post is unapproved.

* Only allow deleted posts to be appealed. Don't allow flagged posts to be appealed.

* Add `status:appealed` metatag. `status:appealed` is separate from `status:pending`.

* Include appealed posts in `status:modqueue`. Search `status:modqueue order:modqueue`
  to view the modqueue as a normal search.

* Retroactively set old flags and appeals as succeeded or rejected. This
  may not be correct for posts that were appealed or flagged multiple
  times. This is difficult to set correctly because we don't have
  approval records for old posts, so we can't tell the actual outcome of
  old flags and appeals.

* Deprecate the `is_resolved` field on post flags. A resolved flag is a
  flag that isn't pending.

* Known bug: appealed posts have a black border instead of a blue
  border. Checking whether a post has been appealed would require either
  an extra query on the posts/index page, or an is_appealed flag on
  posts, neither of which are very desirable.

* Known bug: you can't use `status:appealed` in blacklists, for the same
  reason as above.
2020-08-06 20:55:45 -05:00

66 lines
1.7 KiB
Ruby

class PostAppeal < ApplicationRecord
class Error < StandardError; end
MAX_APPEALS_PER_DAY = 1
belongs_to :creator, :class_name => "User"
belongs_to :post
validates :reason, presence: true, length: { in: 1..140 }
validate :validate_post_is_appealable, on: :create
validate :validate_creator_is_not_limited, on: :create
validates :creator, uniqueness: { scope: :post, message: "have already appealed this post" }, on: :create
enum status: {
pending: 0,
succeeded: 1,
rejected: 2
}
scope :resolved, -> { where(post: Post.undeleted.unflagged) }
scope :unresolved, -> { where(post: Post.deleted.or(Post.flagged)) }
scope :recent, -> { where("post_appeals.created_at >= ?", 1.day.ago) }
scope :expired, -> { pending.where("post_appeals.created_at <= ?", 3.days.ago) }
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :creator, :post, :reason, :status)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q = q.resolved if params[:is_resolved].to_s.truthy?
q = q.unresolved if params[:is_resolved].to_s.falsy?
q.apply_default_order(params)
end
end
extend SearchMethods
def resolved?
post.present? && !post.is_deleted? && !post.is_flagged?
end
def is_resolved
resolved?
end
def validate_creator_is_not_limited
if appeal_count_for_creator >= MAX_APPEALS_PER_DAY
errors[:creator] << "can appeal at most #{MAX_APPEALS_PER_DAY} post a day"
end
end
def validate_post_is_appealable
errors[:post] << "cannot be appealed" if post.is_status_locked? || !post.is_appealable?
end
def appeal_count_for_creator
creator.post_appeals.recent.count
end
def self.available_includes
[:creator, :post]
end
end