refactor: to lower cyclomatic complexity

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2023-12-05 08:40:01 +01:00
parent f81472e081
commit 147096cc3d
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 43 additions and 30 deletions

View File

@ -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

View File

@ -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