Merge pull request #3027 from evazion/feat-flag-cooldown

Add 3 day cooldown between flags
This commit is contained in:
Albert Yi
2017-05-08 13:05:38 -07:00
committed by GitHub
4 changed files with 37 additions and 14 deletions

View File

@@ -296,17 +296,11 @@ class Post < ActiveRecord::Base
end
def flag!(reason, options = {})
if is_status_locked?
raise PostFlag::Error.new("Post is locked and cannot be flagged")
end
flag = flags.create(:reason => reason, :is_resolved => false, :is_deletion => options[:is_deletion])
if flag.errors.any?
raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
end
update_column(:is_flagged, true) unless is_flagged?
end
def appeal!(reason)

View File

@@ -7,17 +7,23 @@ class PostFlag < ActiveRecord::Base
BANNED = "Artist requested removal"
end
COOLDOWN_PERIOD = 3.days
belongs_to :creator, :class_name => "User"
belongs_to :post
validates_presence_of :reason, :creator_id, :creator_ip_addr
validate :validate_creator_is_not_limited
validate :validate_post_is_active
validate :validate_post
before_validation :initialize_creator, :on => :create
validates_uniqueness_of :creator_id, :scope => :post_id, :on => :create, :unless => :is_deletion, :message => "have already flagged this post"
before_save :update_post
attr_accessible :post, :post_id, :reason, :is_resolved, :is_deletion
attr_accessor :is_deletion
scope :by_users, lambda { where.not(creator: User.system) }
scope :by_system, lambda { where(creator: User.system) }
scope :in_cooldown, lambda { by_users.where("created_at >= ?", COOLDOWN_PERIOD.ago) }
module SearchMethods
def reason_matches(query)
if query =~ /\*/
@@ -146,15 +152,19 @@ class PostFlag < ActiveRecord::Base
end
end
def validate_post_is_active
if post.is_deleted?
errors[:post] << "is deleted"
def validate_post
errors[:post] << "is locked and cannot be flagged" if post.is_status_locked?
errors[:post] << "is deleted" if post.is_deleted?
flag = post.flags.in_cooldown.last
if flag.present?
errors[:post] << "cannot be flagged more than once every #{COOLDOWN_PERIOD.inspect} (last flagged: #{flag.created_at.to_s(:long)})"
end
end
def initialize_creator
self.creator_id = CurrentUser.id
self.creator_ip_addr = CurrentUser.ip_addr
self.creator_id ||= CurrentUser.id
self.creator_ip_addr ||= CurrentUser.ip_addr
end
def resolve!

View File

@@ -49,7 +49,9 @@ class PostApprovalTest < ActiveSupport::TestCase
@post.approve!(@approver2)
assert_not_equal(@approver.id, @post.approver_id)
CurrentUser.user = @user3
@post.flag!("blah blah")
travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do
@post.flag!("blah blah")
end
approval = @post.approve!(@approver)
assert_includes(approval.errors.full_messages, "You have previously approved this post and cannot approve it again")

View File

@@ -44,7 +44,7 @@ class PostFlagTest < ActiveSupport::TestCase
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
end
assert_equal(["You have already flagged this post"], @post_flag.errors.full_messages)
assert_equal(["have already flagged this post"], @post_flag.errors[:creator_id])
end
should "not be able to flag more than 10 posts in 24 hours" do
@@ -64,6 +64,23 @@ class PostFlagTest < ActiveSupport::TestCase
assert_equal(["Post is deleted"], @post_flag.errors.full_messages)
end
should "not be able to flag a post in the cooldown period" do
users = FactoryGirl.create_list(:user, 2, created_at: 2.weeks.ago)
flag1 = FactoryGirl.create(:post_flag, post: @post, creator: users.first)
@post.approve!
travel_to(PostFlag::COOLDOWN_PERIOD.from_now - 1.minute) do
flag2 = FactoryGirl.build(:post_flag, post: @post, creator: users.second)
assert(flag2.invalid?)
assert_match(/cannot be flagged more than once/, flag2.errors[:post].join)
end
travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do
flag3 = FactoryGirl.build(:post_flag, post: @post, creator: users.second)
assert(flag3.valid?)
end
end
should "initialize its creator" do
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
assert_equal(@alice.id, @post_flag.creator_id)