delegate removal from favorites and updating of user fav counts to delayed job
This commit is contained in:
@@ -14,10 +14,17 @@ class Favorite < ApplicationRecord
|
|||||||
post.append_user_to_fav_string(user.id)
|
post.append_user_to_fav_string(user.id)
|
||||||
User.where(:id => user.id).update_all("favorite_count = favorite_count + 1")
|
User.where(:id => user.id).update_all("favorite_count = favorite_count + 1")
|
||||||
user.favorite_count += 1
|
user.favorite_count += 1
|
||||||
# post.fav_count += 1 # this is handled in Post#clean_fav_string!
|
|
||||||
end
|
end
|
||||||
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)
|
def self.remove(user:, post: nil, post_id: nil)
|
||||||
Favorite.transaction do
|
Favorite.transaction do
|
||||||
if post && post_id.nil?
|
if post && post_id.nil?
|
||||||
|
|||||||
@@ -172,18 +172,24 @@ class FavoriteGroup < ApplicationRecord
|
|||||||
self.post_count = post_id_array.size
|
self.post_count = post_id_array.size
|
||||||
end
|
end
|
||||||
|
|
||||||
def add!(post)
|
def add!(post_id)
|
||||||
return if contains?(post.id)
|
return if contains?(post_id)
|
||||||
|
|
||||||
clear_post_id_array
|
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
|
end
|
||||||
|
|
||||||
def remove!(post)
|
def self.purge_post(post_id)
|
||||||
return unless contains?(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
|
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
|
end
|
||||||
|
|
||||||
def add_number_to_string(number, string)
|
def add_number_to_string(number, string)
|
||||||
|
|||||||
@@ -815,19 +815,19 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
when /^-favgroup:(\d+)$/i
|
when /^-favgroup:(\d+)$/i
|
||||||
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
|
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
|
when /^-favgroup:(.+)$/i
|
||||||
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
||||||
favgroup.remove!(self) if favgroup
|
favgroup.remove!(id) if favgroup
|
||||||
|
|
||||||
when /^favgroup:(\d+)$/i
|
when /^favgroup:(\d+)$/i
|
||||||
favgroup = FavoriteGroup.where("id = ?", $1.to_i).for_creator(CurrentUser.user.id).first
|
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
|
when /^favgroup:(.+)$/i
|
||||||
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
favgroup = FavoriteGroup.named($1).for_creator(CurrentUser.user.id).first
|
||||||
favgroup.add!(self) if favgroup
|
favgroup.add!(id) if favgroup
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -1038,15 +1038,12 @@ class Post < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def remove_from_favorites
|
def remove_from_favorites
|
||||||
favorites.find_each do |fav|
|
Favorite.delay.purge_post(id, fav_string.scan(/\d+/))
|
||||||
remove_favorite!(fav.user)
|
PostVote.where(post_id: id).delete_all
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_from_fav_groups
|
def remove_from_fav_groups
|
||||||
FavoriteGroup.for_post(id).find_each do |group|
|
FavoriteGroup.delay.purge_post(id)
|
||||||
group.remove!(self)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1382,7 +1379,6 @@ class Post < ApplicationRecord
|
|||||||
transaction do
|
transaction do
|
||||||
Post.without_timeout do
|
Post.without_timeout do
|
||||||
ModAction.log("permanently deleted post ##{id}")
|
ModAction.log("permanently deleted post ##{id}")
|
||||||
#delete!("Permanently deleted post ##{id}", :without_mod_action => true)
|
|
||||||
|
|
||||||
give_favorites_to_parent
|
give_favorites_to_parent
|
||||||
update_children_on_destroy
|
update_children_on_destroy
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
CurrentUser.user = @user
|
CurrentUser.user = @user
|
||||||
CurrentUser.ip_addr = "127.0.0.1"
|
CurrentUser.ip_addr = "127.0.0.1"
|
||||||
mock_saved_search_service!
|
mock_saved_search_service!
|
||||||
|
ImageCropper.stubs(:enabled?).returns(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
|||||||
Reference in New Issue
Block a user