From e2f3474bc3b60e7256cb78666db2289bd9318222 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Tue, 24 Sep 2024 12:26:31 +0300 Subject: [PATCH] FIX: permanent delete of posts by deleted users (#28992) Permanently deleting posts that no longer have a user associated was not working as expected because of UserAction.log which expected user_id to be present. --- app/services/user_action_manager.rb | 4 ++-- spec/lib/post_destroyer_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/services/user_action_manager.rb b/app/services/user_action_manager.rb index 97c370438bb..23d357a8ff2 100644 --- a/app/services/user_action_manager.rb +++ b/app/services/user_action_manager.rb @@ -60,8 +60,8 @@ class UserActionManager end def self.post_rows(post) - # first post gets nada - return [] if post.is_first_post? || post.topic.blank? + # first post gets nada or if the author has been deleted + return [] if post.is_first_post? || post.topic.blank? || post.user.blank? row = { action_type: UserAction::REPLY, diff --git a/spec/lib/post_destroyer_spec.rb b/spec/lib/post_destroyer_spec.rb index 1d8705fc1ca..daedd7630ef 100644 --- a/spec/lib/post_destroyer_spec.rb +++ b/spec/lib/post_destroyer_spec.rb @@ -1145,6 +1145,15 @@ RSpec.describe PostDestroyer do expect { regular_post.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { topic.reload }.to raise_error(ActiveRecord::RecordNotFound) end + + it "destroys the post when force_destroy is true for posts by deleted users" do + regular_post = Fabricate(:post, post_number: 2) + UserDestroyer.new(admin).destroy(regular_post.user, delete_posts: true) + regular_post.reload + + PostDestroyer.new(moderator, regular_post, force_destroy: true).destroy + expect { regular_post.reload }.to raise_error(ActiveRecord::RecordNotFound) + end end describe "publishes messages to subscribers" do