aliases: fix blacklists when aliasing tags.

When aliasing a tag, update any blacklists containing the old tag to use
the new tag.
This commit is contained in:
evazion
2020-08-26 16:11:18 -05:00
parent f0299a8945
commit f4f25cf0c8
3 changed files with 48 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ class TagMover
move_artist!
move_wiki!
move_saved_searches!
move_blacklists!
move_posts!
end
end
@@ -58,6 +59,10 @@ class TagMover
SavedSearch.rewrite_queries!(old_tag.name, new_tag.name)
end
def move_blacklists!
User.rewrite_blacklists!(old_tag.name, new_tag.name)
end
def merge_artists!
old_artist.lock!
new_artist.lock!

View File

@@ -125,6 +125,8 @@ class User < ApplicationRecord
scope :undeleted, -> { where("name !~ 'user_[0-9]+~*'") }
scope :admins, -> { where(level: Levels::ADMIN) }
scope :has_blacklisted_tag, ->(name) { where_regex(:blacklisted_tags, "(^| )[~-]?#{Regexp.escape(name)}( |$)", flags: "ni") }
module BanMethods
def unban!
self.is_banned = false
@@ -312,9 +314,24 @@ class User < ApplicationRecord
end
end
module BlacklistMethods
concerning :BlacklistMethods do
class_methods do
def rewrite_blacklists!(old_name, new_name)
has_blacklisted_tag(old_name).find_each do |user|
user.lock!
user.rewrite_blacklist(old_name, new_name)
user.save!
end
end
end
def rewrite_blacklist(old_name, new_name)
self.blacklisted_tags.gsub!(/(?:^| )([-~])?#{Regexp.escape(old_name)}(?: |$)/i) { " #{$1}#{new_name} " }
end
def normalize_blacklisted_tags
self.blacklisted_tags = blacklisted_tags.downcase if blacklisted_tags.present?
return unless blacklisted_tags.present?
self.blacklisted_tags = self.blacklisted_tags.lines.map(&:strip).join("\n")
end
end
@@ -581,7 +598,6 @@ class User < ApplicationRecord
include BanMethods
include LevelMethods
include EmailMethods
include BlacklistMethods
include ForumMethods
include LimitMethods
include ApiMethods

View File

@@ -104,6 +104,30 @@ class TagAliasTest < ActiveSupport::TestCase
end
end
context "blacklists" do
should "move blacklists" do
@u1 = create(:user, blacklisted_tags: "111 ... 222")
@u2 = create(:user, blacklisted_tags: "111 -... -222")
@u3 = create(:user, blacklisted_tags: "111 ~... ~222")
@u4 = create(:user, blacklisted_tags: "... 222")
@u5 = create(:user, blacklisted_tags: "111 ...")
@u6 = create(:user, blacklisted_tags: "111 222\n\n... 333\n")
@u7 = create(:user, blacklisted_tags: "111 ...\r\n222 333\n")
@ta = create(:tag_alias, antecedent_name: "...", consequent_name: "aaa")
@ta.approve!(approver: @admin)
perform_enqueued_jobs
assert_equal("111 aaa 222", @u1.reload.blacklisted_tags)
assert_equal("111 -aaa -222", @u2.reload.blacklisted_tags)
assert_equal("111 ~aaa ~222", @u3.reload.blacklisted_tags)
assert_equal("aaa 222", @u4.reload.blacklisted_tags)
assert_equal("111 aaa", @u5.reload.blacklisted_tags)
assert_equal("111 222\n\naaa 333", @u6.reload.blacklisted_tags)
assert_equal("111 aaa\n222 333", @u7.reload.blacklisted_tags)
end
end
should "update any affected posts when saved" do
post1 = FactoryBot.create(:post, :tag_string => "aaa bbb")
post2 = FactoryBot.create(:post, :tag_string => "ccc ddd")