delegate removal from favorites and updating of user fav counts to delayed job

This commit is contained in:
r888888888
2017-09-13 14:19:54 -07:00
parent 5a6cc84817
commit 1f3bafc061
4 changed files with 28 additions and 18 deletions

View File

@@ -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?

View File

@@ -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)

View File

@@ -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

View File

@@ -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