Expose and fetch event contacts

Close #829

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-16 15:45:05 +01:00
parent d7ef8f3280
commit 88067bd217
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 36 additions and 5 deletions

View file

@ -228,6 +228,14 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
|> Enum.uniq() |> Enum.uniq()
end end
defp add_event_contacts(%Event{contacts: contacts}) do
contacts
|> Enum.map(& &1.url)
|> Enum.uniq()
end
defp add_event_contacts(%Event{}), do: []
defp process_mention({_, mentioned_actor}), do: mentioned_actor.url defp process_mention({_, mentioned_actor}), do: mentioned_actor.url
defp process_mention(%{actor_id: actor_id}) do defp process_mention(%{actor_id: actor_id}) do
@ -255,7 +263,9 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
{to, cc} = {to, cc} =
{to, {to,
Enum.uniq( Enum.uniq(
cc ++ add_comments_authors(event.comments) ++ add_shares_actors_followers(event.url) cc ++
add_comments_authors(event.comments) ++
add_shares_actors_followers(event.url) ++ add_event_contacts(event)
)} )}
%{"to" => to, "cc" => cc} %{"to" => to, "cc" => cc}

View file

@ -18,6 +18,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
alias Mobilizon.Service.TimezoneDetector alias Mobilizon.Service.TimezoneDetector
alias Mobilizon.Web.Endpoint alias Mobilizon.Web.Endpoint
import Mobilizon.Federation.ActivityPub.Utils, only: [get_url: 1]
import Mobilizon.Federation.ActivityStream.Converter.Utils, import Mobilizon.Federation.ActivityStream.Converter.Utils,
only: [ only: [
fetch_tags: 1, fetch_tags: 1,
@ -25,7 +27,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
build_tags: 1, build_tags: 1,
maybe_fetch_actor_and_attributed_to_id: 1, maybe_fetch_actor_and_attributed_to_id: 1,
process_pictures: 2, process_pictures: 2,
get_address: 1 get_address: 1,
fetch_actor: 1
] ]
require Logger require Logger
@ -56,6 +59,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
visibility = get_visibility(object) visibility = get_visibility(object)
options = get_options(object, address) options = get_options(object, address)
metadata = get_metdata(object) metadata = get_metdata(object)
contacts = get_contacts(object)
[description: description, picture_id: picture_id, medias: medias] = [description: description, picture_id: picture_id, medias: medias] =
process_pictures(object, actor_id) process_pictures(object, actor_id)
@ -86,7 +90,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
physical_address_id: if(address, do: address.id, else: nil), physical_address_id: if(address, do: address.id, else: nil),
updated_at: object["updated"], updated_at: object["updated"],
publish_at: object["published"], publish_at: object["published"],
language: object["inLanguage"] language: object["inLanguage"],
contacts: contacts
} }
{:error, err} -> {:error, err} ->
@ -133,7 +138,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
"id" => event.url, "id" => event.url,
"url" => event.url, "url" => event.url,
"inLanguage" => event.language, "inLanguage" => event.language,
"timezone" => event.options.timezone "timezone" => event.options.timezone,
"contacts" => Enum.map(event.contacts, & &1.url)
} }
|> maybe_add_physical_address(event) |> maybe_add_physical_address(event)
|> maybe_add_event_picture(event) |> maybe_add_event_picture(event)
@ -286,4 +292,19 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
%URI{host: local_domain} = URI.parse(Endpoint.url()) %URI{host: local_domain} = URI.parse(Endpoint.url())
url_domain == local_domain url_domain == local_domain
end end
@spec get_contacts(map()) :: list(Actor.t())
defp get_contacts(object) do
object
|> Map.get("contacts", [])
|> Enum.map(&get_contact/1)
|> Enum.filter(&match?({:ok, _}, &1))
|> Enum.map(fn {:ok, contact} -> contact end)
end
defp get_contact(contact) do
contact
|> get_url()
|> fetch_actor()
end
end end

View file

@ -179,7 +179,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Utils do
def maybe_fetch_actor_and_attributed_to_id(_), do: {:error, :no_actor_found} def maybe_fetch_actor_and_attributed_to_id(_), do: {:error, :no_actor_found}
@spec fetch_actor(String.t()) :: {:ok, Actor.t()} | {:error, atom()} @spec fetch_actor(String.t()) :: {:ok, Actor.t()} | {:error, atom()}
defp fetch_actor(actor_url) do def fetch_actor(actor_url) do
case ActivityPubActor.get_or_fetch_actor_by_url(actor_url) do case ActivityPubActor.get_or_fetch_actor_by_url(actor_url) do
{:ok, %Actor{suspended: false} = actor} -> {:ok, %Actor{suspended: false} = actor} ->
{:ok, actor} {:ok, actor}