diff --git a/app/models/user.rb b/app/models/user.rb index 904fea779..ca68e277b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb index e41f04e59..a6dd4da49 100644 --- a/app/services/user_destroyer.rb +++ b/app/services/user_destroyer.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c857c519e..a85f0600e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb index b7b685a55..eda2fac4b 100644 --- a/spec/services/user_destroyer_spec.rb +++ b/spec/services/user_destroyer_spec.rb @@ -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) }