diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 545fe9102..d8537b029 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -14,10 +14,17 @@ class Favorite < ApplicationRecord post.append_user_to_fav_string(user.id) User.where(:id => user.id).update_all("favorite_count = favorite_count + 1") user.favorite_count += 1 - # post.fav_count += 1 # this is handled in Post#clean_fav_string! end end + def self.purge_post(post_id, user_ids) + 0.upto(99) do |uid| + Favorite.where("user_id % 100 = ?", uid).delete_all(post_id: post_id) + end + + User.where(:id => user_ids).update_all("favorite_count = favorite_count - 1") + end + def self.remove(user:, post: nil, post_id: nil) Favorite.transaction do if post && post_id.nil? diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index 833fca988..d9d847392 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -172,18 +172,24 @@ class FavoriteGroup < ApplicationRecord self.post_count = post_id_array.size end - def add!(post) - return if contains?(post.id) + def add!(post_id) + return if contains?(post_id) clear_post_id_array - update_attributes(:post_ids => add_number_to_string(post.id, post_ids)) + update_attributes(:post_ids => add_number_to_string(post_id, post_ids)) end - def remove!(post) - return unless contains?(post.id) + def self.purge_post(post_id) + for_post(post_id).find_each do |group| + group.remove!(post_id) + end + end + + def remove!(post_id) + return unless contains?(post_id) clear_post_id_array - update_attributes(:post_ids => remove_number_from_string(post.id, post_ids)) + update_attributes(:post_ids => remove_number_from_string(post_id, post_ids)) end def add_number_to_string(number, string) diff --git a/app/models/post.rb b/app/models/post.rb index 48acc5676..1c567b36a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -815,19 +815,19 @@ class Post < ApplicationRecord when /^-favgroup:(\d+)$/i favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first - favgroup.remove!(self) if favgroup + favgroup.remove!(id) if favgroup when /^-favgroup:(.+)$/i favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first - favgroup.remove!(self) if favgroup + favgroup.remove!(id) if favgroup when /^favgroup:(\d+)$/i favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first - favgroup.add!(self) if favgroup + favgroup.add!(id) if favgroup when /^favgroup:(.+)$/i favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first - favgroup.add!(self) if favgroup + favgroup.add!(id) if favgroup end end end @@ -1038,15 +1038,12 @@ class Post < ApplicationRecord end def remove_from_favorites - favorites.find_each do |fav| - remove_favorite!(fav.user) - end + Favorite.delay.purge_post(id, fav_string.scan(/\d+/)) + PostVote.where(post_id: id).delete_all end def remove_from_fav_groups - FavoriteGroup.for_post(id).find_each do |group| - group.remove!(self) - end + FavoriteGroup.delay.purge_post(id) end end @@ -1382,7 +1379,6 @@ class Post < ApplicationRecord transaction do Post.without_timeout do ModAction.log("permanently deleted post ##{id}") - #delete!("Permanently deleted post ##{id}", :without_mod_action => true) give_favorites_to_parent update_children_on_destroy diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index d2a208102..419e2bd33 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -21,6 +21,7 @@ class PostTest < ActiveSupport::TestCase CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" mock_saved_search_service! + ImageCropper.stubs(:enabled?).returns(false) end def teardown