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