Skip to content

A platform for community discussion. Free, open, simple.

FIX: when the user is deleted anonymise category post (#11551)

Krzysztof Kotlarek32ff52dd423d
diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb
index 51ece7789ba..fee04664395 100644
--- a/app/services/user_destroyer.rb
+++ b/app/services/user_destroyer.rb
@@ -32,6 +32,8 @@ class UserDestroyer
Draft.where(user_id: user.id).delete_all
Reviewable.where(created_by_id: user.id).delete_all
+ category_topic_ids = Category.where("topic_id IS NOT NULL").pluck(:topic_id)
+
if opts[:delete_posts]
user.posts.each do |post|
@@ -49,7 +51,11 @@ class UserDestroyer
end
end
- PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
+ if post.is_first_post? && category_topic_ids.include?(post.topic_id)
+ post.update!(user: Discourse.system_user)
+ else
+ PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
+ end
if post.topic && post.is_first_post?
Topic.unscoped.where(id: post.topic_id).update_all(user_id: nil)
diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb
index 5582a05b2fe..f3791681162 100644
--- a/spec/services/user_destroyer_spec.rb
+++ b/spec/services/user_destroyer_spec.rb
@@ -212,6 +212,23 @@ describe UserDestroyer do
end
end
+ context 'user created category' do
+ let!(:topic) { Fabricate(:topic, user: @user) }
+ let!(:first_post) { Fabricate(:post, user: @user, topic: topic) }
+ let!(:second_post) { Fabricate(:post, user: @user, topic: topic) }
+ let!(:category) { Fabricate(:category, user: @user, topic_id: topic.id) }
+
+ it 'changes author of first category post to system user and still deletes second post' do
+ UserDestroyer.new(@admin).destroy(@user, delete_posts: true)
+
+ expect(first_post.reload.deleted_at).to eq(nil)
+ expect(first_post.user_id).to eq(Discourse.system_user.id)
+
+ expect(second_post.reload.deleted_at).not_to eq(nil)
+ expect(second_post.user_id).to eq(nil)
+ end
+ end
+
context 'user has no posts, but user_stats table has post_count > 0' do
before do
# out of sync user_stat data shouldn't break UserDestroyer