[TagImplications] Ignore tag count limit on approve

This will apply to all automated changes, though I think only implications really need it.
This commit is contained in:
Earlopain 2024-04-21 19:09:52 +02:00
parent 6dfd8d8a7e
commit 53d67da88d
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
3 changed files with 36 additions and 6 deletions

View File

@ -529,6 +529,8 @@ class Post < ApplicationRecord
end
def tag_count_not_insane
return if do_not_version_changes
max_count = Danbooru.config.max_tags_per_post
if TagQuery.scan(tag_string).size > max_count
self.errors.add(:tag_string, "tag count exceeds maximum of #{max_count}")
@ -550,12 +552,6 @@ class Post < ApplicationRecord
end
normalized_tags = TagQuery.scan(tag_string)
# Sanity check input, this is checked again on output as well to prevent bad cases where implications push post
# over the limit and posts will fail to edit later on.
if normalized_tags.size > Danbooru.config.max_tags_per_post
self.errors.add(:tag_string, "tag count exceeds maximum of #{Danbooru.config.max_tags_per_post}")
throw :abort
end
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map {|tag| tag.downcase}
normalized_tags = filter_metatags(normalized_tags)

View File

@ -1257,6 +1257,32 @@ class PostTest < ActiveSupport::TestCase
post = create(:post, tag_string: "tagme")
assert_match(/Uploads must have at least \d+ general tags/, post.warnings.full_messages.join)
end
should "error if the tagcount is above the limit" do
Danbooru.config.stubs(:max_tags_per_post).returns(5)
post = create(:post, tag_string: "1 2 3 4 5")
post.add_tag("6")
post.save
assert_match(/tag count exceeds maximum/, post.errors.full_messages.join)
end
should "error if the tagcount via implications is above the limit" do
Danbooru.config.stubs(:max_tags_per_post).returns(2)
create(:tag_implication, antecedent_name: "2", consequent_name: "3")
post = create(:post, tag_string: "1")
post.add_tag("2")
post.save
assert_match(/tag count exceeds maximum/, post.errors.full_messages.join)
end
should "allow removing tags when the post is above the limit" do
Danbooru.config.stubs(:max_tags_per_post).returns(2)
post = build(:post, tag_string: "1 2 3")
post.save(validate: false)
post.remove_tag("3")
post.save
assert_no_match(/tag count exceeds maximum/, post.errors.full_messages.join)
end
end
end
end

View File

@ -240,6 +240,14 @@ class TagImplicationTest < ActiveSupport::TestCase
assert_match "error", ti.reload.status
end
should "ignore tag count limits on approve" do
Danbooru.config.stubs(:max_tags_per_post).returns(5)
ti = create(:tag_implication, antecedent_name: "5", consequent_name: "6", status: "pending")
post = create(:post, tag_string: "1 2 3 4 5")
with_inline_jobs { ti.approve!(approver: @user) }
assert_equal("1 2 3 4 5 6", post.reload.tag_string)
end
context "with an associated forum topic" do
setup do
@admin = create(:admin_user)