From 3b8b150d48fe7b86572b8adae7c3329a39ace608 Mon Sep 17 00:00:00 2001
From: Thomas Citharel <tcit@tcit.fr>
Date: Fri, 6 May 2022 17:47:23 +0200
Subject: [PATCH] Make sure activity recipient can't be nil

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
---
 lib/federation/activity_pub/publisher.ex | 42 ++++++++++++++----------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/lib/federation/activity_pub/publisher.ex b/lib/federation/activity_pub/publisher.ex
index f276cb741..19d645d43 100644
--- a/lib/federation/activity_pub/publisher.ex
+++ b/lib/federation/activity_pub/publisher.ex
@@ -110,13 +110,17 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do
   @spec convert_followers_in_recipients(list(String.t())) :: {list(String.t()), list(String.t())}
   defp convert_followers_in_recipients(recipients) do
     Enum.reduce(recipients, {recipients, []}, fn recipient, {recipients, follower_actors} = acc ->
-      case Actors.get_actor_by_followers_url(recipient) do
-        %Actor{} = group ->
-          {Enum.filter(recipients, fn recipient -> recipient != group.followers_url end),
-           follower_actors ++ Actors.list_external_followers_for_actor(group)}
+      if is_nil(recipient) do
+        acc
+      else
+        case Actors.get_actor_by_followers_url(recipient) do
+          %Actor{} = group ->
+            {Enum.filter(recipients, fn recipient -> recipient != group.followers_url end),
+             follower_actors ++ Actors.list_external_followers_for_actor(group)}
 
-        nil ->
-          acc
+          nil ->
+            acc
+        end
       end
     end)
   end
@@ -128,19 +132,23 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do
   @spec convert_members_in_recipients(list(String.t())) :: {list(String.t()), list(Actor.t())}
   defp convert_members_in_recipients(recipients) do
     Enum.reduce(recipients, {recipients, []}, fn recipient, {recipients, member_actors} = acc ->
-      case Actors.get_group_by_members_url(recipient) do
-        # If the group is local just add external members
-        %Actor{domain: domain} = group when is_nil(domain) ->
-          {Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
-           member_actors ++ Actors.list_external_actors_members_for_group(group)}
+      if is_nil(recipient) do
+        acc
+      else
+        case Actors.get_group_by_members_url(recipient) do
+          # If the group is local just add external members
+          %Actor{domain: domain} = group when is_nil(domain) ->
+            {Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
+             member_actors ++ Actors.list_external_actors_members_for_group(group)}
 
-        # If it's remote add the remote group actor as well
-        %Actor{} = group ->
-          {Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
-           member_actors ++ Actors.list_external_actors_members_for_group(group) ++ [group]}
+          # If it's remote add the remote group actor as well
+          %Actor{} = group ->
+            {Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
+             member_actors ++ Actors.list_external_actors_members_for_group(group) ++ [group]}
 
-        _ ->
-          acc
+          _ ->
+            acc
+        end
       end
     end)
   end