2020-01-22 01:14:42 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Audience do
|
2019-10-25 15:43:37 +00:00
|
|
|
@moduledoc """
|
|
|
|
Tools for calculating content audience
|
|
|
|
"""
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2021-11-13 10:46:25 +00:00
|
|
|
alias Mobilizon.{Actors, Discussions, Events, Share}
|
2020-11-06 10:34:32 +00:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Discussions.{Comment, Discussion}
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Types.Entity
|
2021-02-04 11:28:53 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Storage.Repo
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
require Logger
|
2019-10-25 15:43:37 +00:00
|
|
|
|
|
|
|
@ap_public "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
2021-07-29 15:48:28 +00:00
|
|
|
@type audience :: %{required(String.t()) => list(String.t())}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Get audience for an entity
|
|
|
|
"""
|
|
|
|
@spec get_audience(Entity.t()) :: audience()
|
|
|
|
def get_audience(%Event{} = event) do
|
|
|
|
extract_actors_from_event(event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Post{draft: true} = post) do
|
|
|
|
get_audience(%Post{post | visibility: :private, draft: false})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Post{attributed_to: %Actor{} = group, visibility: visibility}) do
|
|
|
|
{to, cc} = get_to_and_cc(group, [], visibility)
|
|
|
|
%{"to" => to, "cc" => cc}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Discussion{actor: actor}) do
|
|
|
|
%{"to" => maybe_add_group_members([], actor), "cc" => []}
|
|
|
|
end
|
|
|
|
|
2021-08-10 18:49:17 +00:00
|
|
|
# Deleted comments are just like tombstones
|
|
|
|
def get_audience(%Comment{deleted_at: deleted_at}) when not is_nil(deleted_at) do
|
|
|
|
%{"to" => [@ap_public], "cc" => []}
|
|
|
|
end
|
|
|
|
|
2021-11-13 10:46:25 +00:00
|
|
|
def get_audience(%Comment{discussion: %Discussion{id: discussion_id}}) do
|
|
|
|
discussion_id
|
|
|
|
|> Discussions.get_discussion()
|
|
|
|
|> get_audience()
|
2021-07-29 15:48:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Comment{
|
|
|
|
mentions: mentions,
|
|
|
|
actor: %Actor{} = actor,
|
|
|
|
visibility: visibility,
|
|
|
|
in_reply_to_comment: in_reply_to_comment,
|
|
|
|
event: event,
|
|
|
|
origin_comment: origin_comment,
|
|
|
|
url: url
|
|
|
|
}) do
|
|
|
|
with {to, cc} <-
|
|
|
|
extract_actors_from_mentions(mentions, actor, visibility),
|
2021-08-10 09:35:12 +00:00
|
|
|
{to, cc} <- {to ++ add_in_reply_to(in_reply_to_comment), cc},
|
|
|
|
{to, cc} <- add_event_organizers(event, to, cc),
|
2021-07-29 15:48:28 +00:00
|
|
|
{to, cc} <-
|
|
|
|
{to,
|
2021-08-10 09:35:12 +00:00
|
|
|
cc ++
|
|
|
|
add_comments_authors([origin_comment]) ++
|
|
|
|
add_shares_actors_followers(url)} do
|
|
|
|
%{"to" => Enum.uniq(to), "cc" => Enum.uniq(cc)}
|
2021-07-29 15:48:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Participant{} = participant) do
|
2021-07-30 15:24:57 +00:00
|
|
|
%Event{} = event = Events.get_event_with_preload!(participant.event_id)
|
|
|
|
%Actor{} = organizer = group_or_organizer_event(event)
|
2021-07-29 15:48:28 +00:00
|
|
|
|
2021-07-30 15:24:57 +00:00
|
|
|
cc =
|
2021-07-29 15:48:28 +00:00
|
|
|
event.id
|
|
|
|
|> Mobilizon.Events.list_actors_participants_for_event()
|
|
|
|
|> Enum.map(& &1.url)
|
2021-07-30 15:24:57 +00:00
|
|
|
|> Enum.filter(&(&1 != participant.actor.url))
|
|
|
|
|> maybe_add_group_members(organizer)
|
|
|
|
|> maybe_add_followers(organizer)
|
2021-07-29 15:48:28 +00:00
|
|
|
|
|
|
|
%{
|
2021-07-30 15:24:57 +00:00
|
|
|
"to" => [participant.actor.url, organizer.url],
|
|
|
|
"cc" => cc
|
2021-07-29 15:48:28 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Member{} = member) do
|
|
|
|
%{"to" => [member.parent.url, member.parent.members_url], "cc" => []}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_audience(%Actor{} = actor) do
|
|
|
|
%{
|
|
|
|
"to" => [@ap_public],
|
|
|
|
"cc" =>
|
|
|
|
maybe_add_group_members([actor.followers_url], actor) ++
|
|
|
|
add_actors_that_had_our_content(actor.id)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec get_to_and_cc(Actor.t(), list(), :direct | :private | :public | :unlisted | {:list, any}) ::
|
|
|
|
{list(), list()}
|
2019-10-25 15:43:37 +00:00
|
|
|
@doc """
|
2020-11-06 14:43:38 +00:00
|
|
|
Determines the full audience based on mentions for an audience
|
2019-10-25 15:43:37 +00:00
|
|
|
|
2020-11-06 14:43:38 +00:00
|
|
|
For a public audience:
|
2019-10-25 15:43:37 +00:00
|
|
|
* `to` : the mentioned actors, the eventual actor we're replying to and the public
|
|
|
|
* `cc` : the actor's followers
|
2020-11-06 14:43:38 +00:00
|
|
|
|
|
|
|
For an unlisted audience:
|
|
|
|
* `to` : the mentioned actors, actor's followers and the eventual actor we're replying to
|
|
|
|
* `cc` : public
|
|
|
|
|
|
|
|
For a private audience:
|
|
|
|
* `to` : the mentioned actors, actor's followers and the eventual actor we're replying to
|
|
|
|
* `cc` : none
|
|
|
|
|
|
|
|
For a direct audience:
|
|
|
|
* `to` : the mentioned actors and the eventual actor we're replying to
|
|
|
|
* `cc` : none
|
2019-10-25 15:43:37 +00:00
|
|
|
"""
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, :public) do
|
2019-10-25 15:43:37 +00:00
|
|
|
to = [@ap_public | mentions]
|
|
|
|
cc = [actor.followers_url]
|
|
|
|
|
2021-07-29 15:48:28 +00:00
|
|
|
cc = maybe_add_group_members(cc, actor)
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
{to, cc}
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, :unlisted) do
|
2019-10-25 15:43:37 +00:00
|
|
|
to = [actor.followers_url | mentions]
|
|
|
|
cc = [@ap_public]
|
|
|
|
|
2021-07-29 15:48:28 +00:00
|
|
|
to = maybe_add_group_members(to, actor)
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
{to, cc}
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_to_and_cc(%Actor{} = actor, mentions, :private) do
|
|
|
|
{to, cc} = get_to_and_cc(actor, mentions, :direct)
|
2021-07-29 15:48:28 +00:00
|
|
|
|
|
|
|
to = maybe_add_group_members(to, actor)
|
|
|
|
|
|
|
|
{to, cc}
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_to_and_cc(_actor, mentions, :direct) do
|
|
|
|
{mentions, []}
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
def get_to_and_cc(_actor, mentions, {:list, _}) do
|
2019-10-25 15:43:37 +00:00
|
|
|
{mentions, []}
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec maybe_add_group_members(list(String.t()), Actor.t()) :: list(String.t())
|
2021-07-29 15:48:28 +00:00
|
|
|
defp maybe_add_group_members(collection, %Actor{type: :Group, members_url: members_url}) do
|
|
|
|
[members_url | collection]
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
|
2021-07-29 15:48:28 +00:00
|
|
|
defp maybe_add_group_members(collection, %Actor{type: _}), do: collection
|
2021-02-04 11:28:53 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec maybe_add_followers(list(String.t()), Actor.t()) :: list(String.t())
|
2021-07-30 15:24:57 +00:00
|
|
|
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
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
defp add_in_reply_to(%Comment{actor: %Actor{url: url}} = _comment), do: [url]
|
|
|
|
defp add_in_reply_to(%Event{organizer_actor: %Actor{url: url}} = _event), do: [url]
|
|
|
|
defp add_in_reply_to(_), do: []
|
|
|
|
|
2021-08-10 09:35:12 +00:00
|
|
|
defp add_event_organizers(%Event{} = event, to, cc) do
|
|
|
|
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
|
2019-12-03 10:29:51 +00:00
|
|
|
end
|
|
|
|
|
2021-08-10 09:35:12 +00:00
|
|
|
defp add_event_organizers(_, to, cc), do: {to, cc}
|
2019-12-03 10:29:51 +00:00
|
|
|
|
|
|
|
defp add_comment_author(%Comment{} = comment) do
|
2020-01-28 18:18:33 +00:00
|
|
|
case Repo.preload(comment, [:actor]) do
|
2019-12-03 10:29:51 +00:00
|
|
|
%Comment{actor: %Actor{url: url}} ->
|
|
|
|
url
|
|
|
|
|
|
|
|
_err ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-29 15:48:28 +00:00
|
|
|
defp add_comment_author(_), do: nil
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
defp add_comments_authors(comments) do
|
|
|
|
authors =
|
|
|
|
comments
|
|
|
|
|> Enum.map(&add_comment_author/1)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|
|
|
|
authors
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec add_shares_actors_followers(String.t()) :: list(String.t())
|
|
|
|
defp add_shares_actors_followers(uri) do
|
|
|
|
uri
|
|
|
|
|> Share.get_actors_by_share_uri()
|
|
|
|
|> Enum.map(& &1.url)
|
|
|
|
|> Enum.uniq()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_actors_that_had_our_content(actor_id) do
|
|
|
|
actor_id
|
|
|
|
|> Share.get_actors_by_owner_actor_id()
|
|
|
|
|> Enum.map(& &1.url)
|
|
|
|
|> Enum.uniq()
|
|
|
|
end
|
|
|
|
|
2021-11-16 14:45:05 +00:00
|
|
|
defp add_event_contacts(%Event{contacts: contacts}) do
|
|
|
|
contacts
|
|
|
|
|> Enum.map(& &1.url)
|
|
|
|
|> Enum.uniq()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_event_contacts(%Event{}), do: []
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
defp process_mention({_, mentioned_actor}), do: mentioned_actor.url
|
|
|
|
|
|
|
|
defp process_mention(%{actor_id: actor_id}) do
|
|
|
|
with %Actor{url: url} <- Actors.get_actor(actor_id) do
|
|
|
|
url
|
|
|
|
end
|
|
|
|
end
|
2021-02-04 11:28:53 +00:00
|
|
|
|
|
|
|
@spec extract_actors_from_mentions(list(), Actor.t(), atom()) :: {list(), list()}
|
|
|
|
defp extract_actors_from_mentions(mentions, actor, visibility) do
|
2021-09-28 17:40:37 +00:00
|
|
|
get_to_and_cc(actor, Enum.map(mentions, &process_mention/1), visibility)
|
2021-02-04 11:28:53 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec extract_actors_from_event(Event.t()) :: %{
|
|
|
|
String.t() => list(String.t())
|
|
|
|
}
|
2021-02-04 11:28:53 +00:00
|
|
|
defp extract_actors_from_event(%Event{} = event) do
|
2021-09-28 17:40:37 +00:00
|
|
|
{to, cc} =
|
|
|
|
extract_actors_from_mentions(
|
|
|
|
event.mentions,
|
|
|
|
group_or_organizer_event(event),
|
|
|
|
event.visibility
|
|
|
|
)
|
|
|
|
|
|
|
|
{to, cc} =
|
|
|
|
{to,
|
|
|
|
Enum.uniq(
|
2021-11-16 14:45:05 +00:00
|
|
|
cc ++
|
|
|
|
add_comments_authors(event.comments) ++
|
|
|
|
add_shares_actors_followers(event.url) ++ add_event_contacts(event)
|
2021-09-28 17:40:37 +00:00
|
|
|
)}
|
|
|
|
|
|
|
|
%{"to" => to, "cc" => cc}
|
2021-02-04 11:28:53 +00:00
|
|
|
end
|
2021-07-29 15:48:28 +00:00
|
|
|
|
|
|
|
@spec group_or_organizer_event(Event.t()) :: Actor.t()
|
|
|
|
defp group_or_organizer_event(%Event{attributed_to: %Actor{} = group}), do: group
|
|
|
|
defp group_or_organizer_event(%Event{organizer_actor: %Actor{} = actor}), do: actor
|
2019-10-25 15:43:37 +00:00
|
|
|
end
|