From f53fa2adb4356868960b578edcae4728b373dc36 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 14 Aug 2020 21:51:42 -0500 Subject: [PATCH] Fix #4587: "Max 5 flags per 3 days" should account for reapproved flags. --- app/models/post_flag.rb | 1 + app/models/user.rb | 2 +- test/unit/post_flag_test.rb | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index f5355e3e9..852de1809 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -25,6 +25,7 @@ class PostFlag < ApplicationRecord scope :by_system, -> { where(creator: User.system) } scope :in_cooldown, -> { by_users.where("created_at >= ?", Danbooru.config.moderation_period.ago) } scope :expired, -> { pending.where("post_flags.created_at < ?", Danbooru.config.moderation_period.ago) } + scope :active, -> { pending.or(rejected.in_cooldown) } module SearchMethods def creator_matches(creator, searcher) diff --git a/app/models/user.rb b/app/models/user.rb index 577361a4d..afee4db2d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -356,7 +356,7 @@ class User < ApplicationRecord def is_flag_limited? return false if has_unlimited_flags? - post_flags.pending.count >= 5 + post_flags.active.count >= 5 end # Flags are unlimited if you're an approver or you have at least 30 flags diff --git a/test/unit/post_flag_test.rb b/test/unit/post_flag_test.rb index 88f4979f9..de30861ce 100644 --- a/test/unit/post_flag_test.rb +++ b/test/unit/post_flag_test.rb @@ -27,7 +27,7 @@ class PostFlagTest < ActiveSupport::TestCase end context "a basic user" do - should "be able to flag up to 5 posts" do + should "be able to flag up to 5 posts at once" do @user = create(:user) @flags = create_list(:post_flag, 5, creator: @user, status: :pending) @flag = build(:post_flag, creator: @user, status: :pending) @@ -35,6 +35,22 @@ class PostFlagTest < ActiveSupport::TestCase assert_equal(false, @flag.valid?) assert_equal(["have reached your flag limit"], @flag.errors[:creator]) end + + should "have early rejected flags count against their flag limit" do + @user = create(:user) + + create(:post_flag, creator: @user, status: :pending) + assert_equal(1, @user.post_flags.active.count) + + create(:post_flag, creator: @user, status: :rejected) + assert_equal(2, @user.post_flags.active.count) + + create(:post_flag, creator: @user, status: :succeeded) + assert_equal(2, @user.post_flags.active.count) + + create(:post_flag, creator: @user, status: :rejected, created_at: 4.days.ago) + assert_equal(2, @user.post_flags.active.count) + end end context "a user" do