From c4bf6a9192eeb739699fcd126cfae640e13dd613 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 25 Jul 2013 14:32:15 -0700 Subject: [PATCH] fixes for #1865 --- app/logical/current_user.rb | 8 ++++++ app/logical/moderator/tag_batch_change.rb | 10 ++++--- app/logical/post_query_builder.rb | 1 - app/models/artist.rb | 34 ++++++++++++----------- app/models/tag_alias.rb | 22 ++++++++------- app/models/tag_implication.rb | 14 ++++++---- 6 files changed, 52 insertions(+), 37 deletions(-) diff --git a/app/logical/current_user.rb b/app/logical/current_user.rb index 8fff13fdd..6bad60bf4 100644 --- a/app/logical/current_user.rb +++ b/app/logical/current_user.rb @@ -46,6 +46,14 @@ class CurrentUser Thread.current[:safe_mode] end + def self.without_safe_mode + prev = Thread.current[:safe_mode] + Thread.current[:safe_mode] = false + yield + ensure + Thread.current[:safe_mode] = prev + end + def self.set_safe_mode(req) if req.host =~ /safe/ Thread.current[:safe_mode] = true diff --git a/app/logical/moderator/tag_batch_change.rb b/app/logical/moderator/tag_batch_change.rb index 1b1ab2bdb..0a6fb7274 100644 --- a/app/logical/moderator/tag_batch_change.rb +++ b/app/logical/moderator/tag_batch_change.rb @@ -10,10 +10,12 @@ module Moderator updater = User.find(updater_id) - CurrentUser.scoped(updater, updater_ip_addr) do - ::Post.tag_match(antecedent).each do |post| - tags = (post.tag_array - normalized_antecedent + normalized_consequent).join(" ") - post.update_attributes(:tag_string => tags) + CurrentUser.without_safe_mode do + CurrentUser.scoped(updater, updater_ip_addr) do + ::Post.tag_match(antecedent).each do |post| + tags = (post.tag_array - normalized_antecedent + normalized_consequent).join(" ") + post.update_attributes(:tag_string => tags) + end end end end diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index d56fdcef0..61d52724d 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -114,7 +114,6 @@ class PostQueryBuilder if CurrentUser.safe_mode? relation = relation.where(:rating => "s") - # relation = relation.where("created_at <= ?", 3.months.ago) end relation = add_range_relation(q[:post_id], "posts.id", relation) diff --git a/app/models/artist.rb b/app/models/artist.rb index d0336800e..eafb0a7be 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -182,26 +182,28 @@ class Artist < ActiveRecord::Base module BanMethods def ban! Post.transaction do - begin - Post.tag_match(name).each do |post| - begin - post.flag!("Artist requested removal") - rescue PostFlag::Error - # swallow + CurrentUser.without_safe_mode do + begin + Post.tag_match(name).each do |post| + begin + post.flag!("Artist requested removal") + rescue PostFlag::Error + # swallow + end + post.delete!(:ban => true) end - post.delete!(:ban => true) + rescue Post::SearchError + # swallow end - rescue Post::SearchError - # swallow - end - # potential race condition but unlikely - unless TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").exists? - tag_implication = TagImplication.create(:antecedent_name => name, :consequent_name => "banned_artist") - tag_implication.delay(:queue => "default").process! - end + # potential race condition but unlikely + unless TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").exists? + tag_implication = TagImplication.create(:antecedent_name => name, :consequent_name => "banned_artist") + tag_implication.delay(:queue => "default").process! + end - update_column(:is_banned, true) + update_column(:is_banned, true) + end end end end diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 21e0dd2fb..d12575927 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -126,18 +126,20 @@ class TagAlias < ActiveRecord::Base end def update_posts - Post.raw_tag_match(antecedent_name).find_each do |post| - escaped_antecedent_name = Regexp.escape(antecedent_name) - fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{consequent_name} ").strip - CurrentUser.scoped(creator, creator_ip_addr) do - post.update_attributes( - :tag_string => fixed_tags - ) + CurrentUser.without_safe_mode do + Post.raw_tag_match(antecedent_name).find_each do |post| + escaped_antecedent_name = Regexp.escape(antecedent_name) + fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{consequent_name} ").strip + CurrentUser.scoped(creator, creator_ip_addr) do + post.update_attributes( + :tag_string => fixed_tags + ) + end end - end - antecedent_tag.fix_post_count if antecedent_tag - consequent_tag.fix_post_count if consequent_tag + antecedent_tag.fix_post_count if antecedent_tag + consequent_tag.fix_post_count if consequent_tag + end end def rename_wiki_and_artist diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index a303927ee..ad38b8e35 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -126,12 +126,14 @@ class TagImplication < ActiveRecord::Base end def update_posts - Post.raw_tag_match(antecedent_name).find_each do |post| - fixed_tags = "#{post.tag_string} #{descendant_names}".strip - CurrentUser.scoped(creator, creator_ip_addr) do - post.update_attributes( - :tag_string => fixed_tags - ) + CurrentUser.without_safe_mode do + Post.raw_tag_match(antecedent_name).find_each do |post| + fixed_tags = "#{post.tag_string} #{descendant_names}".strip + CurrentUser.scoped(creator, creator_ip_addr) do + post.update_attributes( + :tag_string => fixed_tags + ) + end end end end