diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb index 25c46de3c5d..60936e7480b 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -187,7 +187,7 @@ module PostGuardian # Recovery Method def can_recover_post?(post) if is_staff? - post.deleted_at && post.user + !!post.deleted_at else is_my_own?(post) && post.user_deleted && !post.deleted_at end diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb index 938560b09b4..e4df0a1cf20 100644 --- a/lib/guardian/topic_guardian.rb +++ b/lib/guardian/topic_guardian.rb @@ -113,7 +113,7 @@ module TopicGuardian # Recovery Method def can_recover_topic?(topic) if is_staff? - !!(topic && topic.deleted_at && topic.user) + !!(topic && topic.deleted_at) else topic && can_recover_post?(topic.ordered_posts.first) end diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index d53e33915ed..1247346f265 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -91,6 +91,7 @@ class PostDestroyer user_recovered end topic = Topic.with_deleted.find @post.topic_id + topic.update_column(:user_id, Discourse::SYSTEM_USER_ID) if !topic.user_id topic.recover!(@user) if @post.is_first_post? topic.update_statistics UserActionManager.post_created(@post) @@ -103,6 +104,7 @@ class PostDestroyer end def staff_recovered + @post.update_column(:user_id, Discourse::SYSTEM_USER_ID) if !@post.user_id @post.recover! mark_topic_changed diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index 8f0a454be45..14d528fa7c3 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -1159,7 +1159,7 @@ describe Guardian do PostDestroyer.new(moderator, topic.first_post).destroy topic.first_post.user.destroy! - expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_falsey + expect(Guardian.new(moderator).can_recover_topic?(topic.reload)).to be_truthy end end end @@ -1199,7 +1199,7 @@ describe Guardian do PostDestroyer.new(moderator, post).destroy post.user.destroy! - expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_falsey + expect(Guardian.new(moderator).can_recover_post?(post.reload)).to be_truthy end end end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index 3b370e4dc30..a7c622fa51f 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -180,6 +180,15 @@ describe PostDestroyer do expect(post_action).to be_present end + it "works with topics and posts with no user" do + post = Fabricate(:post) + UserDestroyer.new(Discourse.system_user).destroy(post.user, delete_posts: true) + + expect { PostDestroyer.new(Fabricate(:admin), post.reload).recover } + .to change { post.reload.user_id }.to(Discourse.system_user.id) + .and change { post.topic.user_id }.to(Discourse.system_user.id) + end + describe "post_count recovery" do before do post