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:
parent
93cdb97b46
commit
b55182b983
|
@ -71,7 +71,6 @@ class PostAction < ActiveRecord::Base
|
||||||
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
|
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
|
||||||
Post.with_deleted.update_all(Hash[*f.flatten], id: post.id)
|
Post.with_deleted.update_all(Hash[*f.flatten], id: post.id)
|
||||||
update_flagged_posts_count
|
update_flagged_posts_count
|
||||||
# TODO: SpamRulesEnforcer.enforce!(post.user)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.act(user, post, post_action_type_id, opts={})
|
def self.act(user, post, post_action_type_id, opts={})
|
||||||
|
|
|
@ -4,6 +4,7 @@ require_dependency 'trust_level'
|
||||||
require_dependency 'pbkdf2'
|
require_dependency 'pbkdf2'
|
||||||
require_dependency 'summarize'
|
require_dependency 'summarize'
|
||||||
require_dependency 'discourse'
|
require_dependency 'discourse'
|
||||||
|
require_dependency 'post_destroyer'
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
attr_accessible :name, :username, :password, :email, :bio_raw, :website
|
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
|
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
|
||||||
|
|
||||||
posts.order("post_number desc").each do |p|
|
posts.order("post_number desc").each do |p|
|
||||||
if p.post_number == 1
|
PostDestroyer.new(guardian.user, p).destroy
|
||||||
p.topic.trash!
|
|
||||||
# TODO: But the post is not destroyed. Why?
|
|
||||||
else
|
|
||||||
# TODO: This should be using the PostDestroyer!
|
|
||||||
p.trash!
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -37,9 +37,15 @@ class PostDestroyer
|
||||||
# Feature users in the topic
|
# Feature users in the topic
|
||||||
Jobs.enqueue(:feature_topic_users, topic_id: @post.topic_id, except_post_id: @post.id)
|
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!
|
@post.trash!
|
||||||
|
|
||||||
Topic.reset_highest(@post.topic_id)
|
Topic.reset_highest(@post.topic_id)
|
||||||
|
|
||||||
@post.update_flagged_posts_count
|
@post.update_flagged_posts_count
|
||||||
|
|
||||||
# Remove any reply records that point to deleted posts
|
# Remove any reply records that point to deleted posts
|
||||||
|
@ -52,6 +58,8 @@ class PostDestroyer
|
||||||
|
|
||||||
# Remove any notifications that point to this deleted post
|
# Remove any notifications that point to this deleted post
|
||||||
Notification.delete_all topic_id: @post.topic_id, post_number: @post.post_number
|
Notification.delete_all topic_id: @post.topic_id, post_number: @post.post_number
|
||||||
|
|
||||||
|
@post.topic.trash! if @post.post_number == 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -140,14 +140,29 @@ describe PostDestroyer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "flag counts" do
|
describe "post actions" do
|
||||||
let(:codinghorror) { Fabricate(:coding_horror) }
|
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(: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
|
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])
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -208,12 +208,10 @@ describe User do
|
||||||
|
|
||||||
it 'allows moderator to delete all posts' do
|
it 'allows moderator to delete all posts' do
|
||||||
@user.delete_all_posts!(@guardian)
|
@user.delete_all_posts!(@guardian)
|
||||||
|
expect(Post.where(id: @posts.map(&:id)).all).to be_empty
|
||||||
@posts.each do |p|
|
@posts.each do |p|
|
||||||
p.reload
|
if p.post_number == 1
|
||||||
if p
|
expect(Topic.where(id: p.topic_id).first).to be_nil
|
||||||
p.topic.should be_nil
|
|
||||||
else
|
|
||||||
p.should be_nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue