Allow deletion of a post belonging to a deleted topic, so that users can be destroyed

This commit is contained in:
Neil Lalonde 2013-07-23 11:50:58 -04:00
parent adf7c9ad06
commit cf102be953
2 changed files with 48 additions and 20 deletions

View file

@ -53,26 +53,25 @@ class PostDestroyer
# When a post is properly deleted. Well, it's still soft deleted, but it will no longer # When a post is properly deleted. Well, it's still soft deleted, but it will no longer
# show up in the topic # show up in the topic
def staff_destroyed def staff_destroyed
# the topic may be trashed, skip it
return unless @post.topic
Post.transaction do Post.transaction do
# Update the last post id to the previous post if it exists if @post.topic
last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first # Update the last post id to the previous post if it exists
if last_post.present? last_post = Post.where("topic_id = ? and id <> ?", @post.topic_id, @post.id).order('created_at desc').limit(1).first
@post.topic.update_attributes(last_posted_at: last_post.created_at, if last_post.present?
last_post_user_id: last_post.user_id, @post.topic.update_attributes(last_posted_at: last_post.created_at,
highest_post_number: last_post.post_number) last_post_user_id: last_post.user_id,
highest_post_number: last_post.post_number)
# If the poster doesn't have any other posts in the topic, clear their posted flag # If the poster doesn't have any other posts in the topic, clear their posted flag
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id]) unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false' TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false'
end
end end
end
# 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)
end
@post.post_actions.each do |pa| @post.post_actions.each do |pa|
pa.trash!(@user) pa.trash!(@user)
@ -83,7 +82,7 @@ class PostDestroyer
@post.trash!(@user) @post.trash!(@user)
Topic.reset_highest(@post.topic_id) Topic.reset_highest(@post.topic_id) if @post.topic
@post.update_flagged_posts_count @post.update_flagged_posts_count
@ -98,7 +97,7 @@ 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!(@user) if @post.post_number == 1 @post.topic.trash!(@user) if @post.topic and @post.post_number == 1
end end
end end

View file

@ -8,6 +8,7 @@ describe PostDestroyer do
end end
let(:moderator) { Fabricate(:moderator) } let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
let(:post) { create_post } let(:post) { create_post }
describe 'destroy_old_stubs' do describe 'destroy_old_stubs' do
@ -56,9 +57,6 @@ describe PostDestroyer do
describe 'basic destroying' do describe 'basic destroying' do
let(:moderator) { Fabricate(:moderator) }
let(:admin) { Fabricate(:admin) }
context "as the creator of the post" do context "as the creator of the post" do
before do before do
@orig = post.cooked @orig = post.cooked
@ -146,6 +144,37 @@ describe PostDestroyer do
end end
context "deleting a post belonging to a deleted topic" do
let!(:topic) { post.topic }
before do
topic.trash!(admin)
post.reload
end
context "as a moderator" do
before do
PostDestroyer.new(moderator, post).destroy
end
it "deletes the post" do
post.deleted_at.should be_present
post.deleted_by.should == moderator
end
end
context "as an admin" do
before do
PostDestroyer.new(admin, post).destroy
end
it "deletes the post" do
post.deleted_at.should be_present
post.deleted_by.should == admin
end
end
end
describe 'after delete' do describe 'after delete' do
let!(:coding_horror) { Fabricate(:coding_horror) } let!(:coding_horror) { Fabricate(:coding_horror) }