Fix audience for comments under a remote event from group

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-10 11:35:12 +02:00
parent d3a05b5568
commit 56861d6483
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 46 additions and 11 deletions

View File

@ -52,16 +52,14 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
}) do }) do
with {to, cc} <- with {to, cc} <-
extract_actors_from_mentions(mentions, actor, visibility), extract_actors_from_mentions(mentions, actor, visibility),
{to, cc} <- {Enum.uniq(to ++ add_in_reply_to(in_reply_to_comment)), cc}, {to, cc} <- {to ++ add_in_reply_to(in_reply_to_comment), cc},
{to, cc} <- {Enum.uniq(to ++ add_event_author(event)), cc}, {to, cc} <- add_event_organizers(event, to, cc),
{to, cc} <- {to, cc} <-
{to, {to,
Enum.uniq(
cc ++ cc ++
add_comments_authors([origin_comment]) ++ add_comments_authors([origin_comment]) ++
add_shares_actors_followers(url) add_shares_actors_followers(url)} do
)} do %{"to" => Enum.uniq(to), "cc" => Enum.uniq(cc)}
%{"to" => to, "cc" => cc}
end end
end end
@ -173,11 +171,22 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
defp add_in_reply_to(%Event{organizer_actor: %Actor{url: url}} = _event), do: [url] defp add_in_reply_to(%Event{organizer_actor: %Actor{url: url}} = _event), do: [url]
defp add_in_reply_to(_), do: [] defp add_in_reply_to(_), do: []
defp add_event_author(%Event{} = event) do defp add_event_organizers(%Event{} = event, to, cc) do
[Repo.preload(event, [:organizer_actor]).organizer_actor.url] event = Repo.preload(event, [:organizer_actor, :attributed_to])
case event do
%Event{
attributed_to: %Actor{members_url: members_url, followers_url: followers_url},
organizer_actor: %Actor{url: organizer_actor_url}
} ->
{to ++ [organizer_actor_url, members_url], cc ++ [followers_url]}
%Event{organizer_actor: %Actor{url: organizer_actor_url}} ->
{to ++ [organizer_actor_url], cc}
end
end end
defp add_event_author(_), do: [] defp add_event_organizers(_, to, cc), do: {to, cc}
defp add_comment_author(%Comment{} = comment) do defp add_comment_author(%Comment{} = comment) do
case Repo.preload(comment, [:actor]) do case Repo.preload(comment, [:actor]) do

View File

@ -247,6 +247,32 @@ defmodule Mobilizon.Federation.ActivityPub.AudienceTest do
assert %{"to" => [members_url], "cc" => []} == assert %{"to" => [members_url], "cc" => []} ==
Audience.get_audience(comment) Audience.get_audience(comment)
end end
test "reply to a remote comment" do
%Actor{id: remote_actor_id, url: remote_actor_url} =
remote_actor =
insert(:actor, domain: "somewhere.else", url: "https://somewhere.else/@someone")
%Actor{id: remote_group_id, url: remote_group_url} =
remote_group =
insert(:group, domain: "somewhere.else", url: "https://somewhere.else/@somegroup")
%Event{} =
event =
insert(:event, local: false, organizer_actor: remote_actor, attributed_to: remote_group)
%Comment{} = comment = insert(:comment, event: event)
assert %{
"cc" => [comment.actor.followers_url, comment.event.attributed_to.followers_url],
"to" => [
@ap_public,
comment.event.organizer_actor.url,
comment.event.attributed_to.members_url
]
} ==
Audience.get_audience(comment)
end
end end
describe "participant" do describe "participant" do