From b55182b9834ef612dd6ea02b2528cc3ef5833a9e Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 5 Jun 2013 16:00:45 -0400 Subject: [PATCH] Use PostDestroyer when deleting all of a user's posts; deleting a post removes its flags and resets its flag counts --- app/models/post_action.rb | 1 - app/models/user.rb | 9 ++------- lib/post_destroyer.rb | 8 ++++++++ spec/components/post_destroyer_spec.rb | 21 ++++++++++++++++++--- spec/models/user_spec.rb | 8 +++----- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 4ac3a9ec6..80244c4fd 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -71,7 +71,6 @@ class PostAction < ActiveRecord::Base f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]} Post.with_deleted.update_all(Hash[*f.flatten], id: post.id) update_flagged_posts_count - # TODO: SpamRulesEnforcer.enforce!(post.user) end def self.act(user, post, post_action_type_id, opts={}) diff --git a/app/models/user.rb b/app/models/user.rb index da28f739f..64d8c779b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,7 @@ require_dependency 'trust_level' require_dependency 'pbkdf2' require_dependency 'summarize' require_dependency 'discourse' +require_dependency 'post_destroyer' class User < ActiveRecord::Base attr_accessible :name, :username, :password, :email, :bio_raw, :website @@ -445,13 +446,7 @@ class User < ActiveRecord::Base raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self posts.order("post_number desc").each do |p| - if p.post_number == 1 - p.topic.trash! - # TODO: But the post is not destroyed. Why? - else - # TODO: This should be using the PostDestroyer! - p.trash! - end + PostDestroyer.new(guardian.user, p).destroy end end diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 4675b4879..dd89b2728 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -37,9 +37,15 @@ class PostDestroyer # Feature users in the topic Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id, except_post_id: @post.id) + @post.post_actions.map(&:trash!) + + f = PostActionType.types.map{|k,v| ["#{k}_count", 0]} + Post.with_deleted.update_all(Hash[*f.flatten], id: @post.id) + @post.trash! Topic.reset_highest(@post.topic_id) + @post.update_flagged_posts_count # Remove any reply records that point to deleted posts @@ -52,6 +58,8 @@ class PostDestroyer # Remove any notifications that point to this deleted post Notification.delete_all topic_id: @post.topic_id, post_number: @post.post_number + + @post.topic.trash! if @post.post_number == 1 end end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 2362eaa61..b18edb7d6 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -140,14 +140,29 @@ describe PostDestroyer do end end - describe "flag counts" do + describe "post actions" do let(:codinghorror) { Fabricate(:coding_horror) } let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) } + let(:second_post) { Fabricate(:post, topic_id: post.topic_id) } it "should reset counts when a post is deleted" do - second_post = Fabricate(:post, topic_id: post.topic_id) PostAction.act(codinghorror, second_post, PostActionType.types[:off_topic]) - -> { PostDestroyer.new(moderator, second_post).destroy }.should change(PostAction, :flagged_posts_count).by(-1) + expect { PostDestroyer.new(moderator, second_post).destroy }.to change(PostAction, :flagged_posts_count).by(-1) + end + + it "should delete the post actions" do + flag = PostAction.act(codinghorror, second_post, PostActionType.types[:off_topic]) + PostDestroyer.new(moderator, second_post).destroy + expect(PostAction.where(id: flag.id).first).to be_nil + expect(PostAction.where(id: bookmark.id).first).to be_nil + end + + it 'should update flag counts on the post' do + PostAction.act(codinghorror, second_post, PostActionType.types[:off_topic]) + PostDestroyer.new(moderator, second_post.reload).destroy + second_post.reload + expect(second_post.off_topic_count).to eq(0) + expect(second_post.bookmark_count).to eq(0) end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 261525c46..e7b4aa534 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -208,12 +208,10 @@ describe User do it 'allows moderator to delete all posts' do @user.delete_all_posts!(@guardian) + expect(Post.where(id: @posts.map(&:id)).all).to be_empty @posts.each do |p| - p.reload - if p - p.topic.should be_nil - else - p.should be_nil + if p.post_number == 1 + expect(Topic.where(id: p.topic_id).first).to be_nil end end end