From 6c3c5bbbc3ea0c70413f8d2ac655d6035ddbff75 Mon Sep 17 00:00:00 2001
From: Claire <claire.github-309c@sitedethib.com>
Date: Thu, 27 Jul 2023 16:11:56 +0200
Subject: [PATCH] Fix crash when processing Flag activity with no status
 (#26189)

---
 app/lib/activitypub/activity/flag.rb       |  2 +-
 spec/lib/activitypub/activity/flag_spec.rb | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/app/lib/activitypub/activity/flag.rb b/app/lib/activitypub/activity/flag.rb
index 304cf0ad2..68ee43d0e 100644
--- a/app/lib/activitypub/activity/flag.rb
+++ b/app/lib/activitypub/activity/flag.rb
@@ -9,7 +9,7 @@ class ActivityPub::Activity::Flag < ActivityPub::Activity
 
     target_accounts.each do |target_account|
       target_statuses     = target_statuses_by_account[target_account.id]
-      replied_to_accounts = Account.local.where(id: target_statuses.filter_map(&:in_reply_to_account_id))
+      replied_to_accounts = target_statuses.nil? ? [] : Account.local.where(id: target_statuses.filter_map(&:in_reply_to_account_id))
 
       next if target_account.suspended? || (!target_account.local? && replied_to_accounts.none?)
 
diff --git a/spec/lib/activitypub/activity/flag_spec.rb b/spec/lib/activitypub/activity/flag_spec.rb
index 601473069..8593d567f 100644
--- a/spec/lib/activitypub/activity/flag_spec.rb
+++ b/spec/lib/activitypub/activity/flag_spec.rb
@@ -139,6 +139,35 @@ RSpec.describe ActivityPub::Activity::Flag do
         expect(report.status_ids).to eq []
       end
     end
+
+    context 'when an account is passed but no status' do
+      let(:mentioned) { Fabricate(:account) }
+
+      let(:json) do
+        {
+          '@context': 'https://www.w3.org/ns/activitystreams',
+          id: flag_id,
+          type: 'Flag',
+          content: 'Boo!!',
+          actor: ActivityPub::TagManager.instance.uri_for(sender),
+          object: [
+            ActivityPub::TagManager.instance.uri_for(flagged),
+          ],
+        }.with_indifferent_access
+      end
+
+      before do
+        subject.perform
+      end
+
+      it 'creates a report with no attached status' do
+        report = Report.find_by(account: sender, target_account: flagged)
+
+        expect(report).to_not be_nil
+        expect(report.comment).to eq 'Boo!!'
+        expect(report.status_ids).to eq []
+      end
+    end
   end
 
   describe '#perform with a defined uri' do