favoritescontroller#destroy should work even if the post doesn't exist, remove from favorites on expunge (fixes #3222)

This commit is contained in:
r888888888
2017-07-19 13:22:24 -07:00
parent 5d013f9c88
commit d48ed95191
7 changed files with 50 additions and 21 deletions

View File

@@ -47,12 +47,19 @@ class FavoritesController < ApplicationController
end end
def destroy def destroy
@post = Post.find(params[:id]) @post = Post.find_by_id(params[:id])
@post.remove_favorite!(CurrentUser.user)
if @post
@post.remove_favorite!(CurrentUser.user)
path = mobile_post_path(@post)
else
Favorite.remove(post_id: params[:id], user: CurrentUser.user)
path = mobile_posts_path
end
respond_with(@post) do |format| respond_with(@post) do |format|
format.html do format.html do
redirect_to(mobile_post_path(@post)) redirect_to(path)
end end
format.js format.js
format.json do format.json do

View File

@@ -7,7 +7,7 @@ class UserDeletion
user = User.find(user_id) user = User.find(user_id)
Post.without_timeout do Post.without_timeout do
Post.raw_tag_match("fav:#{user_id}").where("true /* UserDeletion.remove_favorites_for */").find_each do |post| Post.raw_tag_match("fav:#{user_id}").where("true /* UserDeletion.remove_favorites_for */").find_each do |post|
Favorite.remove(post, user) Favorite.remove(post: post, user: user)
end end
end end
end end

View File

@@ -5,15 +5,19 @@ class Favorite < ApplicationRecord
attr_accessible :user_id, :post_id attr_accessible :user_id, :post_id
# this is necessary because there's no trigger for deleting favorites # this is necessary because there's no trigger for deleting favorites
def self.destroy_all(hash) def self.destroy_all(user_id: nil, post_id: nil)
if hash[:user_id] && hash[:post_id] if user_id && post_id
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}") connection.execute("delete from favorites_#{user_id % 100} where user_id = #{user_id} and post_id = #{post_id}")
elsif hash[:user_id] elsif user_id
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[: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
end end
def self.add(post, user) def self.add(post:, user:)
Favorite.transaction do Favorite.transaction do
User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first User.where(:id => user.id).select("id").lock("FOR UPDATE NOWAIT").first
@@ -27,17 +31,21 @@ class Favorite < ApplicationRecord
end end
end end
def self.remove(post, user) def self.remove(user:, post: nil, post_id: nil)
Favorite.transaction do 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 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? 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) Favorite.destroy_all(user_id: user.id, post_id: post_id)
Post.where(:id => post.id).update_all("fav_count = fav_count - 1") Post.where(:id => post_id).update_all("fav_count = fav_count - 1")
post.delete_user_from_fav_string(user.id) post.delete_user_from_fav_string(user.id) if post
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 post.fav_count -= 1 if post
end end
end end
end end

View File

@@ -982,7 +982,7 @@ class Post < ApplicationRecord
end end
def add_favorite!(user) def add_favorite!(user)
Favorite.add(self, user) Favorite.add(post: self, user: user)
vote!("up", user) if user.is_voter? vote!("up", user) if user.is_voter?
rescue PostVote::Error rescue PostVote::Error
end end
@@ -992,7 +992,7 @@ class Post < ApplicationRecord
end end
def remove_favorite!(user) def remove_favorite!(user)
Favorite.remove(self, user) Favorite.remove(post: self, user: user)
unvote!(user) if user.is_voter? unvote!(user) if user.is_voter?
rescue PostVote::Error rescue PostVote::Error
end end
@@ -1022,6 +1022,10 @@ class Post < ApplicationRecord
end end
end end
def remove_from_favorites
Favorite.destroy_all(post_id: self.id)
end
def remove_from_fav_groups def remove_from_fav_groups
FavoriteGroup.for_post(id).find_each do |group| FavoriteGroup.for_post(id).find_each do |group|
group.remove!(self) group.remove!(self)
@@ -1383,6 +1387,7 @@ class Post < ApplicationRecord
decrement_tag_post_counts decrement_tag_post_counts
remove_from_all_pools remove_from_all_pools
remove_from_fav_groups remove_from_fav_groups
remove_from_favorites
destroy destroy
update_parent_on_destroy update_parent_on_destroy
end end

View File

@@ -289,12 +289,12 @@ class User < ApplicationRecord
end end
def add_favorite!(post) def add_favorite!(post)
Favorite.add(post, self) Favorite.add(post: post, user: self)
clean_favorite_count! if clean_favorite_count? clean_favorite_count! if clean_favorite_count?
end end
def remove_favorite!(post) def remove_favorite!(post)
Favorite.remove(post, self) Favorite.remove(post: post, user: self)
end end
def favorite_groups def favorite_groups

View File

@@ -36,6 +36,7 @@ class PostTest < ActiveSupport::TestCase
@upload = FactoryGirl.create(:jpg_upload) @upload = FactoryGirl.create(:jpg_upload)
@upload.process! @upload.process!
@post = @upload.post @post = @upload.post
Favorite.add(post: @post, user: @user)
end end
should "delete the files" do should "delete the files" do
@@ -52,6 +53,14 @@ class PostTest < ActiveSupport::TestCase
assert_equal(false, File.exists?(@post.file_path)) assert_equal(false, File.exists?(@post.file_path))
end end
should "remove all favorites" do
TestAfterCommit.with_commits(true) do
@post.expunge!
end
assert_equal(0, Favorite.for_user(@user.id).where("post_id = ?", @post.id).count)
end
should "remove the post from iqdb" do should "remove the post from iqdb" do
mock_iqdb_service! mock_iqdb_service!
Post.iqdb_sqs_service.expects(:send_message).with("remove\n#{@post.id}") Post.iqdb_sqs_service.expects(:send_message).with("remove\n#{@post.id}")

View File

@@ -40,7 +40,7 @@ class UserDeletionTest < ActiveSupport::TestCase
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post) @post = FactoryGirl.create(:post)
Favorite.add(@post, @user) Favorite.add(post: @post, user: @user)
@user.update_attributes(:email => "ted@danbooru.com") @user.update_attributes(:email => "ted@danbooru.com")