favoritescontroller#destroy should work even if the post doesn't exist, remove from favorites on expunge (fixes #3222)
This commit is contained in:
@@ -5,15 +5,19 @@ class Favorite < ApplicationRecord
|
||||
attr_accessible :user_id, :post_id
|
||||
|
||||
# this is necessary because there's no trigger for deleting favorites
|
||||
def self.destroy_all(hash)
|
||||
if hash[:user_id] && hash[:post_id]
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
||||
elsif hash[:user_id]
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]}")
|
||||
def self.destroy_all(user_id: nil, post_id: nil)
|
||||
if user_id && post_id
|
||||
connection.execute("delete from favorites_#{user_id % 100} where user_id = #{user_id} and post_id = #{post_id}")
|
||||
elsif user_id
|
||||
connection.execute("delete from favorites_#{user_id % 100} where user_id = #{user_id}")
|
||||
elsif post_id
|
||||
0.upto(99) do |uid|
|
||||
connection.execute("delete from favorites_#{uid} where post_id = #{post_id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.add(post, user)
|
||||
def self.add(post:, user:)
|
||||
Favorite.transaction do
|
||||
User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first
|
||||
|
||||
@@ -27,17 +31,21 @@ class Favorite < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.remove(post, user)
|
||||
def self.remove(user:, post: nil, post_id: nil)
|
||||
Favorite.transaction do
|
||||
if post && post_id.nil?
|
||||
post_id = post.id
|
||||
end
|
||||
|
||||
User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first
|
||||
|
||||
return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post.id).exists?
|
||||
Favorite.destroy_all(user_id: user.id, post_id: post.id)
|
||||
Post.where(:id => post.id).update_all("fav_count = fav_count - 1")
|
||||
post.delete_user_from_fav_string(user.id)
|
||||
return unless Favorite.for_user(user.id).where(:user_id => user.id, :post_id => post_id).exists?
|
||||
Favorite.destroy_all(user_id: user.id, post_id: post_id)
|
||||
Post.where(:id => post_id).update_all("fav_count = fav_count - 1")
|
||||
post.delete_user_from_fav_string(user.id) if post
|
||||
User.where(:id => user.id).update_all("favorite_count = favorite_count - 1")
|
||||
user.favorite_count -= 1
|
||||
post.fav_count -= 1
|
||||
post.fav_count -= 1 if post
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -982,7 +982,7 @@ class Post < ApplicationRecord
|
||||
end
|
||||
|
||||
def add_favorite!(user)
|
||||
Favorite.add(self, user)
|
||||
Favorite.add(post: self, user: user)
|
||||
vote!("up", user) if user.is_voter?
|
||||
rescue PostVote::Error
|
||||
end
|
||||
@@ -992,7 +992,7 @@ class Post < ApplicationRecord
|
||||
end
|
||||
|
||||
def remove_favorite!(user)
|
||||
Favorite.remove(self, user)
|
||||
Favorite.remove(post: self, user: user)
|
||||
unvote!(user) if user.is_voter?
|
||||
rescue PostVote::Error
|
||||
end
|
||||
@@ -1022,6 +1022,10 @@ class Post < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def remove_from_favorites
|
||||
Favorite.destroy_all(post_id: self.id)
|
||||
end
|
||||
|
||||
def remove_from_fav_groups
|
||||
FavoriteGroup.for_post(id).find_each do |group|
|
||||
group.remove!(self)
|
||||
@@ -1383,6 +1387,7 @@ class Post < ApplicationRecord
|
||||
decrement_tag_post_counts
|
||||
remove_from_all_pools
|
||||
remove_from_fav_groups
|
||||
remove_from_favorites
|
||||
destroy
|
||||
update_parent_on_destroy
|
||||
end
|
||||
|
||||
@@ -289,12 +289,12 @@ class User < ApplicationRecord
|
||||
end
|
||||
|
||||
def add_favorite!(post)
|
||||
Favorite.add(post, self)
|
||||
Favorite.add(post: post, user: self)
|
||||
clean_favorite_count! if clean_favorite_count?
|
||||
end
|
||||
|
||||
def remove_favorite!(post)
|
||||
Favorite.remove(post, self)
|
||||
Favorite.remove(post: post, user: self)
|
||||
end
|
||||
|
||||
def favorite_groups
|
||||
|
||||
Reference in New Issue
Block a user