[BURs] Fix mass updates when the tag has been aliased away

Resolving aliases here is not great. Just mass update nothing in these cases, that's much safer.

Using tag_string_diff also wasn't a very bright idea, this too can result in both tags being removed
This commit is contained in:
Earlopain 2024-02-26 16:48:25 +01:00
parent e0b52c13af
commit ca56ef3529
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
2 changed files with 30 additions and 13 deletions

View File

@ -9,35 +9,34 @@ class TagBatchJob < ApplicationJob
updater_id = args[2]
updater_ip_addr = args[3]
scanned_antecedent = TagQuery.scan(antecedent.downcase)
scanned_consequent = TagQuery.scan(consequent.downcase)
raise JobError, "#{antecedent} or #{consequent} has unexpected format" if scanned_antecedent.count != 1 || scanned_consequent.count != 1
from, *from_remaining = TagQuery.scan(antecedent.downcase)
to, *to_remaining = TagQuery.scan(consequent.downcase)
raise JobError, "#{antecedent} or #{consequent} has unexpected format" if from_remaining.any? || to_remaining.any?
normalized_antecedent = TagAlias.to_aliased(scanned_antecedent).first
normalized_consequent = TagAlias.to_aliased(scanned_consequent).first
updater = User.find(updater_id)
CurrentUser.scoped(updater, updater_ip_addr) do
migrate_posts(normalized_antecedent, normalized_consequent)
migrate_blacklists(normalized_antecedent, normalized_consequent)
migrate_posts(from, to)
migrate_blacklists(from, to)
ModAction.log(:mass_update, { antecedent: antecedent, consequent: consequent })
end
end
def migrate_posts(normalized_antecedent, normalized_consequent)
Post.sql_raw_tag_match(normalized_antecedent).find_each do |post|
def migrate_posts(from, to)
Post.sql_raw_tag_match(from).find_each do |post|
post.with_lock do
post.do_not_version_changes = true
post.tag_string_diff = "-#{normalized_antecedent} #{normalized_consequent}"
post.remove_tag(from)
post.add_tag(to)
post.save
end
end
end
def migrate_blacklists(normalized_antecedent, normalized_consequent)
def migrate_blacklists(from, to)
User.without_timeout do
User.where_ilike(:blacklisted_tags, "*#{normalized_antecedent}*").find_each(batch_size: 50) do |user|
fixed_blacklist = TagAlias.to_aliased_query(user.blacklisted_tags, overrides: { normalized_antecedent => normalized_consequent })
User.where_ilike(:blacklisted_tags, "*#{from}*").find_each(batch_size: 50) do |user|
fixed_blacklist = TagAlias.to_aliased_query(user.blacklisted_tags, overrides: { from => to })
user.update_column(:blacklisted_tags, fixed_blacklist)
end
end

View File

@ -13,6 +13,24 @@ class TagBatchJobTest < ActiveJob::TestCase
assert_equal("d dd y z", p2.tag_string)
end
should "ignore aliases when the job tries to replace the old with the new tag" do
CurrentUser.user = create(:user)
post = create(:post, tag_string: "extra new_tag")
create(:tag_alias, antecedent_name: "old_tag", consequent_name: "new_tag", status: "active")
TagBatchJob.perform_now("old_tag", "new_tag", CurrentUser.user.id)
assert_equal("extra new_tag", post.reload.tag_string)
end
should "ignore aliases when the job tries to replace the new with the old tag" do
CurrentUser.user = create(:user)
post = create(:post, tag_string: "extra new_tag")
create(:tag_alias, antecedent_name: "old_tag", consequent_name: "new_tag", status: "active")
TagBatchJob.perform_now("new_tag", "old_tag", CurrentUser.user.id)
assert_equal("extra new_tag", post.reload.tag_string)
end
should "migrate users blacklists" do
initial_blacklist = <<~BLACKLIST.chomp
tag