From 7f56abac90a4f8e477ef5f1ac0e5ca30af9b249d Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Tue, 17 Nov 2020 07:40:36 +1100 Subject: [PATCH] FIX: remove post-action when a post is permanently deleted (#11242) Followup of https://github.com/discourse/discourse/pull/11115 When we permanently remove the post, we should remove related post-actions as well. --- lib/post_destroyer.rb | 6 ++++-- spec/components/post_destroyer_spec.rb | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index e5bff487bb0..c85fcbca2f5 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -254,7 +254,7 @@ class PostDestroyer .limit(1) .first - if last_post.present? && @post.topic.present? + if last_post.present? topic = @post.topic topic.last_posted_at = last_post.created_at topic.last_post_user_id = last_post.user_id @@ -277,7 +277,9 @@ class PostDestroyer def trash_public_post_actions if public_post_actions = PostAction.publics.where(post_id: @post.id) - public_post_actions.each { |pa| pa.trash!(@user) } + public_post_actions.each { |pa| permanent? ? pa.destroy! : pa.trash!(@user) } + + return if permanent? @post.custom_fields["deleted_public_actions"] = public_post_actions.ids @post.save_custom_fields diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index be1f3aa9c27..6ec8622ed3f 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -965,6 +965,7 @@ describe PostDestroyer do describe "permanent destroy" do fab!(:private_message_topic) { Fabricate(:private_message_topic) } fab!(:private_post) { Fabricate(:private_message_post, topic: private_message_topic) } + fab!(:post_action) { Fabricate(:post_action, post: private_post) } fab!(:reply) { Fabricate(:private_message_post, topic: private_message_topic) } it "destroys the post and topic if deleting first post" do PostDestroyer.new(reply.user, reply, permanent: true).destroy @@ -974,6 +975,7 @@ describe PostDestroyer do PostDestroyer.new(private_post.user, private_post, permanent: true).destroy expect { private_post.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { private_message_topic.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { post_action.reload }.to raise_error(ActiveRecord::RecordNotFound) end it 'soft delete if not creator of post or not private message' do