Use PostDestroyer when deleting all of a user's posts; deleting a post removes its flags and resets its flag counts

This commit is contained in:
Neil Lalonde 2013-06-05 16:00:45 -04:00
parent 93cdb97b46
commit b55182b983
5 changed files with 31 additions and 16 deletions

View file

@ -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={})

View file

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

View file

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

View file

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

View file

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