diff --git a/lib/user_destroyer.rb b/lib/user_destroyer.rb index 8f27154a9f3..dcc2bb0bd20 100644 --- a/lib/user_destroyer.rb +++ b/lib/user_destroyer.rb @@ -42,6 +42,19 @@ class UserDestroyer b.record_match! if b end Post.with_deleted.where(user_id: user.id).update_all("user_id = NULL") + + # If this user created categories, fix those up: + categories = Category.where(user_id: user.id).all + categories.each do |c| + c.user_id = Discourse.system_user.id + c.save! + if topic = Topic.with_deleted.where(id: c.topic_id).first + topic.try(:recover!) + topic.user_id = Discourse.system_user.id + topic.save! + end + end + StaffActionLogger.new(@staff).log_user_deletion(user, opts.slice(:context)) DiscourseHub.unregister_nickname(user.username) if SiteSetting.call_discourse_hub? MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id] diff --git a/spec/components/user_destroyer_spec.rb b/spec/components/user_destroyer_spec.rb index bb5a48986cb..a0462957949 100644 --- a/spec/components/user_destroyer_spec.rb +++ b/spec/components/user_destroyer_spec.rb @@ -238,6 +238,17 @@ describe UserDestroyer do UserDestroyer.new(@admin).destroy(@user, {block_ip: true}) end end + + context 'user created a category' do + let!(:category) { Fabricate(:category, user: @user) } + + it "assigns the system user to the categories" do + UserDestroyer.new(@admin).destroy(@user, {delete_posts: true}) + category.reload.user_id.should == Discourse.system_user.id + category.topic.should be_present + category.topic.user_id.should == Discourse.system_user.id + end + end end end