diff --git a/lib/federation/activity_pub/fetcher.ex b/lib/federation/activity_pub/fetcher.ex index 288c4a39..b017256d 100644 --- a/lib/federation/activity_pub/fetcher.ex +++ b/lib/federation/activity_pub/fetcher.ex @@ -47,26 +47,7 @@ defmodule Mobilizon.Federation.ActivityPub.Fetcher do case fetch(url, options) do {:ok, data} when is_map(data) -> if origin_check?(url, data) do - case Transmogrifier.handle_incoming(%{ - "type" => "Create", - "to" => data["to"], - "cc" => data["cc"], - "actor" => data["actor"] || data["attributedTo"], - "attributedTo" => data["attributedTo"] || data["actor"], - "object" => data - }) do - {:ok, entity, structure} -> - {:ok, entity, structure} - - {:error, error} when is_atom(error) -> - {:error, error} - - {:error, %Ecto.Changeset{} = err} -> - {:error, err} - - :error -> - {:error, :transmogrifier_error} - end + pass_to_transmogrifier(data) else Logger.warning("Object origin check failed") {:error, :object_origin_check_failed} @@ -101,6 +82,34 @@ defmodule Mobilizon.Federation.ActivityPub.Fetcher do end end + @spec pass_to_transmogrifier(map()) :: + {:ok, map(), struct()} + | {:error, atom()} + | {:error, Ecto.Changeset.t()} + | {:error, :transmogrifier_error} + defp pass_to_transmogrifier(data) do + case Transmogrifier.handle_incoming(%{ + "type" => "Create", + "to" => data["to"], + "cc" => data["cc"], + "actor" => data["actor"] || data["attributedTo"], + "attributedTo" => data["attributedTo"] || data["actor"], + "object" => data + }) do + {:ok, entity, structure} -> + {:ok, entity, structure} + + {:error, error} when is_atom(error) -> + {:error, error} + + {:error, %Ecto.Changeset{} = err} -> + {:error, err} + + :error -> + {:error, :transmogrifier_error} + end + end + @type fetch_actor_errors :: :json_decode_error | :actor_deleted | :http_error | :actor_not_allowed_type diff --git a/lib/federation/activity_pub/transmogrifier.ex b/lib/federation/activity_pub/transmogrifier.ex index 121d6a1b..4b64483b 100644 --- a/lib/federation/activity_pub/transmogrifier.ex +++ b/lib/federation/activity_pub/transmogrifier.ex @@ -76,16 +76,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do Actions.Create.create(:conversation, object_data, false) object_data when is_map(object_data) -> - case Discussions.get_comment_from_url_with_preload(object_data.url) do - {:error, :comment_not_found} -> - object_data - |> transform_object_data_for_discussion() - |> save_comment_or_discussion() - - {:ok, %Comment{} = comment} -> - # Object already exists - {:ok, nil, comment} - end + handle_comment_or_discussion(object_data) {:error, err} -> {:error, err} @@ -1026,6 +1017,19 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do is_nil(object_data.title) or object_data.title == "" end + defp handle_comment_or_discussion(object_data) do + case Discussions.get_comment_from_url_with_preload(object_data.url) do + {:error, :comment_not_found} -> + object_data + |> transform_object_data_for_discussion() + |> save_comment_or_discussion() + + {:ok, %Comment{} = comment} -> + # Object already exists + {:ok, nil, comment} + end + end + # Comment and conversations have different attributes for actor and groups @spec transform_object_data_for_discussion(map()) :: map() defp transform_object_data_for_discussion(object_data) do