FIX: Handle deleted categories in post export (#10567)

FIX: Handle deleted categories in post export (#10567)

Fixes a crash when exporting my own archive on Meta.

diff --git a/app/jobs/regular/export_user_archive.rb b/app/jobs/regular/export_user_archive.rb
index 75b8e4a..366aa87 100644
--- a/app/jobs/regular/export_user_archive.rb
+++ b/app/jobs/regular/export_user_archive.rb
@@ -207,7 +207,8 @@ module Jobs
 
     def piped_category_name(category_id)
       return "-" unless category_id
-      category = Category.find(category_id)
+      category = Category.find_by(id: category_id)
+      return "#{category_id}" unless category
       categories = [category.name]
       while category.parent_category_id && category = category.parent_category
         categories << category.name
diff --git a/spec/jobs/export_user_archive_spec.rb b/spec/jobs/export_user_archive_spec.rb
index 8519f9d..79f3b50 100644
--- a/spec/jobs/export_user_archive_spec.rb
+++ b/spec/jobs/export_user_archive_spec.rb
@@ -130,6 +130,18 @@ describe Jobs::ExportUserArchive do
       expect(post2['reply_count']).to eq(0)
     end
 
+    it 'can export a post from a deleted category' do
+      cat2 = Fabricate(:category)
+      topic2 = Fabricate(:topic, category: cat2, user: user)
+      post2 = Fabricate(:post, topic: topic2, user: user)
+
+      cat2_id = cat2.id
+      cat2.destroy!
+
+      _, csv_out = make_component_csv
+      expect(csv_out).to match cat2_id.to_s
+      puts csv_out
+    end
   end
 
   context 'user_archive_profile' do

GitHub sha: a1dd761b

This commit appears in #10567 which was merged by riking.