From 099b85e9a95e8218b2bb9747c21901636cf4d6b3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 30 Jul 2021 17:24:57 +0200 Subject: [PATCH] Refactor audience to introduce maybe_add_followers/2 Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/audience.ex | 19 +++++++++++++++---- .../federation/activity_pub/audience_test.exs | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/federation/activity_pub/audience.ex b/lib/federation/activity_pub/audience.ex index a2ccaf5ca..fb98f1e23 100644 --- a/lib/federation/activity_pub/audience.ex +++ b/lib/federation/activity_pub/audience.ex @@ -66,16 +66,20 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do end def get_audience(%Participant{} = participant) do - event = Events.get_event_with_preload!(participant.event_id) + %Event{} = event = Events.get_event_with_preload!(participant.event_id) + %Actor{} = organizer = group_or_organizer_event(event) - actor_participants_urls = + cc = event.id |> Mobilizon.Events.list_actors_participants_for_event() |> Enum.map(& &1.url) + |> Enum.filter(&(&1 != participant.actor.url)) + |> maybe_add_group_members(organizer) + |> maybe_add_followers(organizer) %{ - "to" => [participant.actor.url, group_or_organizer_event(event).url], - "cc" => actor_participants_urls + "to" => [participant.actor.url, organizer.url], + "cc" => cc } end @@ -156,6 +160,13 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do defp maybe_add_group_members(collection, %Actor{type: _}), do: collection + @spec maybe_add_followers(List.t(), Actor.t()) :: List.t() + defp maybe_add_followers(collection, %Actor{type: :Group, followers_url: followers_url}) do + [followers_url | collection] + end + + defp maybe_add_followers(collection, %Actor{type: _}), do: collection + def get_addressed_actors(mentioned_users, _), do: mentioned_users defp add_in_reply_to(%Comment{actor: %Actor{url: url}} = _comment), do: [url] diff --git a/test/federation/activity_pub/audience_test.exs b/test/federation/activity_pub/audience_test.exs index f00ced414..bf16bc0da 100644 --- a/test/federation/activity_pub/audience_test.exs +++ b/test/federation/activity_pub/audience_test.exs @@ -257,7 +257,7 @@ defmodule Mobilizon.Federation.ActivityPub.AudienceTest do assert %{ "to" => [participant.actor.url, participant.event.organizer_actor.url], - "cc" => [participant2.actor.url, participant.actor.url] + "cc" => [participant2.actor.url] } == Audience.get_audience(participant) end @@ -271,7 +271,7 @@ defmodule Mobilizon.Federation.ActivityPub.AudienceTest do assert %{ "to" => [participant.actor.url, participant.event.attributed_to.url], - "cc" => [participant2.actor.url, participant.actor.url] + "cc" => [group.followers_url, group.members_url, participant2.actor.url] } == Audience.get_audience(participant) end end