FIX: Recursively delete nested replies

Previously we were only deleting top level replies
This commit is contained in:
Robin Ward 2019-04-30 12:54:53 -04:00
parent 5edb5c8b03
commit 812905cbb6
2 changed files with 18 additions and 2 deletions

View File

@ -211,7 +211,8 @@ class ReviewableFlaggedPost < Reviewable
def perform_delete_and_ignore_replies(performed_by, args)
result = perform_ignore(performed_by, args)
replies = PostReply.where(post_id: post.id).includes(:reply).map(&:reply)
reply_ids = post.reply_ids(Guardian.new(performed_by), only_replies_to_single_post: false)
replies = Post.where(id: reply_ids.map { |r| r[:id] })
PostDestroyer.new(performed_by, post).destroy
replies.each { |reply| PostDestroyer.new(performed_by, reply).destroy }
@ -227,7 +228,8 @@ class ReviewableFlaggedPost < Reviewable
def perform_delete_and_agree_replies(performed_by, args)
result = agree(performed_by, args)
replies = PostReply.where(post_id: post.id).includes(:reply).map(&:reply)
reply_ids = post.reply_ids(Guardian.new(performed_by), only_replies_to_single_post: false)
replies = Post.where(id: reply_ids.map { |r| r[:id] })
PostDestroyer.new(performed_by, post).destroy
replies.each { |reply| PostDestroyer.new(performed_by, reply).destroy }

View File

@ -131,6 +131,12 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
reply_to_post_number: post.post_number,
topic_id: post.topic_id
)
nested_reply = PostCreator.create(
Fabricate(:user),
raw: 'this is the reply text2',
reply_to_post_number: reply.post_number,
topic_id: post.topic_id
)
post.reload
reviewable.perform(moderator, :delete_and_ignore_replies)
@ -138,6 +144,7 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(score.reload).to be_ignored
expect(post.reload.deleted_at).to be_present
expect(reply.reload.deleted_at).to be_present
expect(nested_reply.reload.deleted_at).to be_present
end
it "delete_and_agree agrees with the flags and deletes post" do
@ -154,6 +161,12 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
reply_to_post_number: post.post_number,
topic_id: post.topic_id
)
nested_reply = PostCreator.create(
Fabricate(:user),
raw: 'this is the reply text2',
reply_to_post_number: reply.post_number,
topic_id: post.topic_id
)
post.reload
reviewable.perform(moderator, :delete_and_agree_replies)
@ -161,6 +174,7 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(score.reload).to be_agreed
expect(post.reload.deleted_at).to be_present
expect(reply.reload.deleted_at).to be_present
expect(nested_reply.reload.deleted_at).to be_present
end
it "disagrees with the flags" do