* Removed unapprovals, added post flags and post appeals (still need to update tests)

* Restyled text
This commit is contained in:
albert
2011-03-28 18:48:02 -04:00
parent 42627be1d3
commit f9c961cdc6
66 changed files with 642 additions and 403 deletions

View File

@@ -10,8 +10,12 @@ class Comment < ActiveRecord::Base
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)}
scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)}
search_method :body_matches
search_method :post_tag_match
def initialize_creator
self.creator_id = CurrentUser.user.id

View File

@@ -15,8 +15,9 @@ class Post < ActiveRecord::Base
belongs_to :updater, :class_name => "User"
belongs_to :approver, :class_name => "User"
belongs_to :parent, :class_name => "Post"
has_one :unapproval, :dependent => :destroy
has_one :upload, :dependent => :destroy
has_many :flags, :class_name => "PostFlag", :dependent => :destroy
has_many :appeals, :class_name => "PostAppeal", :dependent => :destroy
has_many :versions, :class_name => "PostVersion", :dependent => :destroy, :order => "post_versions.id ASC"
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
@@ -30,8 +31,10 @@ class Post < ActiveRecord::Base
scope :pending, where(["is_pending = ?", true])
scope :pending_or_flagged, where(["(is_pending = ? OR is_flagged = ?)", true, true])
scope :undeleted, where(["is_deleted = ?", false])
scope :deleted, where(["is_deleted = ?", true])
scope :visible, lambda {|user| Danbooru.config.can_user_see_post_conditions(user)}
scope :commented_before, lambda {|date| where("last_commented_at < ?", date).order("last_commented_at DESC")}
scope :for_user, lambda {|user_id| where(["uploader_string = ?", "uploader:#{user_id}"])}
scope :available_for_moderation, lambda {where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
scope :before_id, lambda {|id| id.present? ? where(["posts.id < ?", id]) : where("TRUE")}
@@ -212,31 +215,27 @@ class Post < ActiveRecord::Base
end
module ApprovalMethods
def is_unapprovable?
is_pending == false && is_flagged == false && unapproval.nil?
end
def is_approvable?
(is_pending? || is_flagged?) && approver_string != "approver:#{CurrentUser.name}"
end
def unapprove!(reason)
raise Unapproval::Error.new("This post is still pending approval") if is_pending?
raise Unapproval::Error.new("This post has already been flagged") if is_flagged?
raise Unapproval::Error.new("This post has already been unapproved once") unless unapproval.nil?
def flag!(reason)
flag = create_flag(:reason => reason)
unapproval = create_unapproval(
:unapprover_id => CurrentUser.user.id,
:unapprover_ip_addr => CurrentUser.ip_addr,
:reason => reason
)
if unapproval.errors.any?
raise Unapproval::Error.new(unapproval.errors.full_messages.join("; "))
if flag.errors.any?
raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
end
update_attribute(:is_flagged, true)
end
def appeal!(reason)
appeal = create_appeal(:reason => reason)
if appeal.errors.any?
raise PostAppeal::Error.new(appeal.errors.full_messages.join("; "))
end
end
def approve!
raise ApprovalError.new("You have previously approved this post and cannot approve it again") if approver_string == "approver:#{CurrentUser.name}"

36
app/models/post_appeal.rb Normal file
View File

@@ -0,0 +1,36 @@
class PostAppeal < ActiveRecord::Base
class Error < Exception ; end
belongs_to :creator, :class_name => "User"
belongs_to :post
validates_presence_of :reason, :creator_id, :creator_ip_addr
validate :validate_post_is_inactive
validate :creator_is_not_limited
before_validation :initialize_creator, :on => :create
validates_uniqueness_of :creator_id, :scope => :post_id
scope :for_user, lambda {|user_id| where(["creator_id = ?", user_id])}
scope :recent, lambda {where(["created_at >= ?", 1.day.ago])}
def validate_creator_is_not_limited
if PostAppeal.for_user(creator_id).recent.count >= 5
errors[:creator] << "can appeal 5 posts a day"
false
else
true
end
end
def validate_post_is_inactive
if !post.is_deleted? && !post.is_flagged?
errors[:post] << "is inactive"
false
else
true
end
end
def initialize_creator
self.creator_id = CurrentUser.id
self.creator_ip_addr = CurrentUser.ip_addr
end
end

34
app/models/post_flag.rb Normal file
View File

@@ -0,0 +1,34 @@
class PostFlag < ActiveRecord::Base
class Error < Exception ; end
belongs_to :creator, :class_name => "User"
belongs_to :post
validates_presence_of :reason, :creator_id, :creator_ip_addr
validate :creator_is_not_limited
validate :validate_post_is_active
before_validation :initialize_creator, :on => :create
validates_uniqueness_of :creator_id, :scope => :post_id
def validate_creator_is_not_limited
if PostAppeal.for_user(creator_id).recent.count >= 10
errors[:creator] << "can flag 10 posts a day"
false
else
true
end
end
def validate_post_is_active
if post.is_deleted?
errors[:post] << "is deleted"
false
else
true
end
end
def initialize_creator
self.creator_id = CurrentUser.id
self.creator_ip_addr = CurrentUser.ip_addr
end
end

View File

@@ -1,28 +0,0 @@
class Unapproval < ActiveRecord::Base
class Error < Exception ; end
belongs_to :unapprover, :class_name => "User"
belongs_to :post
validates_presence_of :reason, :unapprover_id, :unapprover_ip_addr
validate :validate_post_is_active
before_validation :initialize_unapprover, :on => :create
before_save :flag_post
def validate_post_is_active
if post.is_pending? || post.is_flagged? || post.is_deleted?
errors[:post] << "is inactive"
false
else
true
end
end
def flag_post
post.update_attribute(:is_flagged, true)
end
def initialize_unapprover
self.unapprover_id = CurrentUser.id
self.unapprover_ip_addr = CurrentUser.ip_addr
end
end

View File

@@ -228,11 +228,15 @@ class User < ActiveRecord::Base
end
def upload_limit
deleted_count = RemovedPost.where("user_id = ?", id).count
unapproved_count = Post.where("is_pending = true and user_id = ?", id).count
deleted_count = Post.for_user(id).deleted.count
pending_count = Post.for_user(id).pending.count
approved_count = Post.where("is_flagged = false and is_pending = false and user_id = ?", id).count
limit = base_upload_limit + (approved_count / 10) - (deleted_count / 4) - unapproved_count
if base_upload_limit
limit = base_upload_limit - pending_count
else
limit = 10 + (approved_count / 10) - (deleted_count / 4) - pending_count
end
if limit > 20
limit = 20