FIX: If you delete a user, delete their queued posts as well.

This commit is contained in:
Robin Ward 2015-04-24 16:04:44 -04:00
parent 15dbce5886
commit 151b559e4c
4 changed files with 19 additions and 0 deletions

View file

@ -474,6 +474,8 @@ class User < ActiveRecord::Base
def delete_all_posts!(guardian)
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
QueuedPost.where(user_id: id).delete_all
posts.order("post_number desc").each do |p|
PostDestroyer.new(guardian.user, p).destroy
end

View file

@ -19,6 +19,9 @@ class UserDestroyer
raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
User.transaction do
QueuedPost.where(user_id: user.id).delete_all
if opts[:delete_posts]
user.posts.each do |post|
# agree with flags

View file

@ -105,11 +105,13 @@ describe User do
@post3 = Fabricate(:post, user: @user)
@posts = [@post1, @post2, @post3]
@guardian = Guardian.new(Fabricate(:admin))
@queued_post = Fabricate(:queued_post, user: @user)
end
it 'allows moderator to delete all posts' do
@user.delete_all_posts!(@guardian)
expect(Post.where(id: @posts.map(&:id))).to be_empty
expect(QueuedPost.where(user_id: @user.id).count).to eq(0)
@posts.each do |p|
if p.is_first_post?
expect(Topic.find_by(id: p.topic_id)).to be_nil

View file

@ -73,6 +73,18 @@ describe UserDestroyer do
include_examples "successfully destroy a user"
end
context "with a queued post" do
let(:user) { Fabricate(:user) }
let(:admin) { Fabricate(:admin) }
let!(:qp) { Fabricate(:queued_post, user: user) }
it "removes the queued post" do
UserDestroyer.new(admin).destroy(user)
expect(QueuedPost.where(user_id: user.id).count).to eq(0)
end
end
context 'user has posts' do
let!(:topic_starter) { Fabricate(:user) }
let!(:topic) { Fabricate(:topic, user: topic_starter) }