Merge pull request #3027 from evazion/feat-flag-cooldown
Add 3 day cooldown between flags
This commit is contained in:
@@ -296,17 +296,11 @@ class Post < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def flag!(reason, options = {})
|
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])
|
flag = flags.create(:reason => reason, :is_resolved => false, :is_deletion => options[:is_deletion])
|
||||||
|
|
||||||
if flag.errors.any?
|
if flag.errors.any?
|
||||||
raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
|
raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
|
||||||
end
|
end
|
||||||
|
|
||||||
update_column(:is_flagged, true) unless is_flagged?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def appeal!(reason)
|
def appeal!(reason)
|
||||||
|
|||||||
@@ -7,17 +7,23 @@ class PostFlag < ActiveRecord::Base
|
|||||||
BANNED = "Artist requested removal"
|
BANNED = "Artist requested removal"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
COOLDOWN_PERIOD = 3.days
|
||||||
|
|
||||||
belongs_to :creator, :class_name => "User"
|
belongs_to :creator, :class_name => "User"
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
validates_presence_of :reason, :creator_id, :creator_ip_addr
|
validates_presence_of :reason, :creator_id, :creator_ip_addr
|
||||||
validate :validate_creator_is_not_limited
|
validate :validate_creator_is_not_limited
|
||||||
validate :validate_post_is_active
|
validate :validate_post
|
||||||
before_validation :initialize_creator, :on => :create
|
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"
|
validates_uniqueness_of :creator_id, :scope => :post_id, :on => :create, :unless => :is_deletion, :message => "have already flagged this post"
|
||||||
before_save :update_post
|
before_save :update_post
|
||||||
attr_accessible :post, :post_id, :reason, :is_resolved, :is_deletion
|
attr_accessible :post, :post_id, :reason, :is_resolved, :is_deletion
|
||||||
attr_accessor :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
|
module SearchMethods
|
||||||
def reason_matches(query)
|
def reason_matches(query)
|
||||||
if query =~ /\*/
|
if query =~ /\*/
|
||||||
@@ -146,15 +152,19 @@ class PostFlag < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_post_is_active
|
def validate_post
|
||||||
if post.is_deleted?
|
errors[:post] << "is locked and cannot be flagged" if post.is_status_locked?
|
||||||
errors[:post] << "is deleted"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize_creator
|
def initialize_creator
|
||||||
self.creator_id = CurrentUser.id
|
self.creator_id ||= CurrentUser.id
|
||||||
self.creator_ip_addr = CurrentUser.ip_addr
|
self.creator_ip_addr ||= CurrentUser.ip_addr
|
||||||
end
|
end
|
||||||
|
|
||||||
def resolve!
|
def resolve!
|
||||||
|
|||||||
@@ -49,7 +49,9 @@ class PostApprovalTest < ActiveSupport::TestCase
|
|||||||
@post.approve!(@approver2)
|
@post.approve!(@approver2)
|
||||||
assert_not_equal(@approver.id, @post.approver_id)
|
assert_not_equal(@approver.id, @post.approver_id)
|
||||||
CurrentUser.user = @user3
|
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)
|
approval = @post.approve!(@approver)
|
||||||
assert_includes(approval.errors.full_messages, "You have previously approved this post and cannot approve it again")
|
assert_includes(approval.errors.full_messages, "You have previously approved this post and cannot approve it again")
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ class PostFlagTest < ActiveSupport::TestCase
|
|||||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
|
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
should "not be able to flag more than 10 posts in 24 hours" do
|
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)
|
assert_equal(["Post is deleted"], @post_flag.errors.full_messages)
|
||||||
end
|
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
|
should "initialize its creator" do
|
||||||
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
|
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
|
||||||
assert_equal(@alice.id, @post_flag.creator_id)
|
assert_equal(@alice.id, @post_flag.creator_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user