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) def delete_all_posts!(guardian)
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self 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| posts.order("post_number desc").each do |p|
PostDestroyer.new(guardian.user, p).destroy PostDestroyer.new(guardian.user, p).destroy
end end

View file

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

View file

@ -105,11 +105,13 @@ describe User do
@post3 = Fabricate(:post, user: @user) @post3 = Fabricate(:post, user: @user)
@posts = [@post1, @post2, @post3] @posts = [@post1, @post2, @post3]
@guardian = Guardian.new(Fabricate(:admin)) @guardian = Guardian.new(Fabricate(:admin))
@queued_post = Fabricate(:queued_post, user: @user)
end end
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))).to be_empty 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| @posts.each do |p|
if p.is_first_post? if p.is_first_post?
expect(Topic.find_by(id: p.topic_id)).to be_nil 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" include_examples "successfully destroy a user"
end 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 context 'user has posts' do
let!(:topic_starter) { Fabricate(:user) } let!(:topic_starter) { Fabricate(:user) }
let!(:topic) { Fabricate(:topic, user: topic_starter) } let!(:topic) { Fabricate(:topic, user: topic_starter) }