[BURs] Add tag nuke option for admins

This commit is contained in:
Earlopain 2022-10-01 21:37:00 +02:00
parent 5ba3408ce2
commit 72460f1254
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
7 changed files with 83 additions and 16 deletions

View File

@ -223,6 +223,8 @@ class ModActionDecorator < ApplicationDecorator
when "mass_update"
"Mass updated [[#{vals['antecedent']}]] -> [[#{vals['consequent']}]]"
when "nuke_tag"
"Nuked tag [[#{vals['tag_name']}]]"
### Flag Reason ###

View File

@ -46,7 +46,7 @@ module BulkUpdateRequestsHelper
when :create_alias, :create_implication, :remove_alias, :remove_implication
names.add(arg1)
names.add(arg2)
when :change_category
when :change_category, :nuke_tag
names.add(arg1)
end
end
@ -54,19 +54,20 @@ module BulkUpdateRequestsHelper
end
def script_tag_links(cmd, arg1, arg2, script_tags)
arg1_count = script_tags[arg1].try(:post_count).to_i
arg2_count = script_tags[arg2].try(:post_count).to_i
case cmd
when :create_alias, :create_implication, :remove_alias, :remove_implication
arg1_count = script_tags[arg1].try(:post_count).to_i
arg2_count = script_tags[arg2].try(:post_count).to_i
"[[#{arg1}]] (#{arg1_count}) -> [[#{arg2}]] (#{arg2_count})"
when :mass_update
"[[#{arg1}]] -> [[#{arg2}]]"
when :change_category
arg1_count = script_tags[arg1].try(:post_count).to_i
when :nuke_tag
"[[#{arg1}]] (#{arg1_count})"
when :change_category
"[[#{arg1}]] (#{arg1_count}) -> #{arg2}"
end
end

View File

@ -21,10 +21,6 @@ class TagBatchJob < ApplicationJob
end
end
def estimate_update_count
::Post.tag_match(@antecedent).count
end
def migrate_posts(normalized_antecedent, normalized_consequent)
::PostQueryBuilder.new(normalized_antecedent.join(" ")).build.reorder('').find_each do |post|
post.with_lock do

54
app/jobs/tag_nuke_job.rb Normal file
View File

@ -0,0 +1,54 @@
class TagNukeJob < ApplicationJob
queue_as :tags
def perform(*args)
tag_name = args[0]
tag = Tag.find_by_normalized_name(tag_name)
updater_id = args[1]
updater_ip_addr = args[2]
return if tag.nil?
updater = User.find(updater_id)
CurrentUser.without_safe_mode do
CurrentUser.scoped(updater, updater_ip_addr) do
create_undo_information(tag)
migrate_posts(tag.name)
ModAction.log(:nuke_tag, { tag_name: tag_name })
end
end
end
def migrate_posts(tag_name)
PostQueryBuilder.new(tag_name).build.reorder('').find_each do |post|
post.with_lock do
post.do_not_version_changes = true
post.tag_string_diff = "-#{tag_name}"
post.save
end
end
end
def create_undo_information(tag)
Post.transaction do
Post.without_timeout do
post_ids = []
Post.sql_raw_tag_match(tag.name).find_each do |post|
post_ids << post.id
end
TagRelUndo.create!(tag_rel: tag, undo_data: post_ids)
end
end
end
def self.process_undo!(tag)
TagRelUndo.where(tag_rel: tag, applied: false).find_each do |tag_rel_undo|
Post.where(id: tag_rel_undo.undo_data).find_each do |post|
post.do_not_version_changes = true
post.tag_string_diff = tag.name
post.save
end
tag_rel_undo.update(applied: true)
end
end
end

View File

@ -44,6 +44,9 @@ class AliasAndImplicationImporter
elsif line =~ /^(?:mass update|updating|update|change) (\S+) -> (\S+)( #.*)?$/i
[:mass_update, $1, $2, $3]
elsif line =~ /^(?:nuke tag|nuke) (\S+)( #.*)?$/i
[:nuke_tag, $1, nil, $2]
elsif line =~ /^category (\S+) -> (#{Tag.categories.regexp})( #.*)?$/i
[:change_category, $1, $2, $3]
@ -77,6 +80,9 @@ class AliasAndImplicationImporter
when :mass_update
comment = "# missing" if token[3] == false
"update #{token[1]} -> #{token[2]} #{comment}".strip
when :nuke_tag
comment = "# missing" if token[3] == false
"nuke tag #{token[1]} #{comment}".strip
else
raise Error.new("Unknown token to reverse")
end
@ -140,6 +146,11 @@ class AliasAndImplicationImporter
token[3] = existing
token
when :nuke_tag
errors << "Only adminds can nuke tags" unless user.is_admin?
existing = Tag.find_by(name: token[1]).present?
token[3] = existing
token
else
errors << "Unknown token: #{token[0]}"
end
@ -158,11 +169,8 @@ class AliasAndImplicationImporter
when :create_implication
sum + TagImplication.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count
when :mass_update
sum + ::Post.tag_match(token[1]).count
when :change_category
sum + (Tag.find_by_name(token[1]).try(:post_count) || 0)
when :change_category, :mass_update, :nuke_tag
sum + (Tag.find_by(name: token[1]).try(:post_count) || 0)
else
sum + 0
@ -186,7 +194,6 @@ class AliasAndImplicationImporter
end
end
tag_alias.rename_artist if rename_aliased_pages?
raise Error, "Error: Alias would modify other aliases or implications through transitive relationships. (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})" if tag_alias.has_transitives
tag_alias.approve!(approver: approver, update_topic: false, deny_transitives: true)
@ -233,6 +240,9 @@ class AliasAndImplicationImporter
when :mass_update
TagBatchJob.perform_later(token[1], token[2], CurrentUser.id, CurrentUser.ip_addr)
when :nuke_tag
TagNukeJob.perform_later(token[1], CurrentUser.id, CurrentUser.ip_addr)
when :change_category
tag = Tag.find_by(name: token[1])
raise Error, "Tag for #{token[1]} not found" if tag.nil?

View File

@ -74,6 +74,7 @@ class ModAction < ApplicationRecord
:wiki_page_unlock,
:mass_update,
:nuke_tag,
:takedown_process
]

View File

@ -16,6 +16,9 @@
<li>imply aaa -> bbb</li>
<li>update aaa -> bbb</li>
<li>category tag_name -> category_name</li>
<% if CurrentUser.is_admin? %>
<li>nuke tag_name</li>
<% end %>
</ul>
</div>