forked from potsda.mn/mobilizon
refactor: to lower cyclomatic complexity
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
f81472e081
commit
147096cc3d
|
@ -47,26 +47,7 @@ defmodule Mobilizon.Federation.ActivityPub.Fetcher do
|
||||||
case fetch(url, options) do
|
case fetch(url, options) do
|
||||||
{:ok, data} when is_map(data) ->
|
{:ok, data} when is_map(data) ->
|
||||||
if origin_check?(url, data) do
|
if origin_check?(url, data) do
|
||||||
case Transmogrifier.handle_incoming(%{
|
pass_to_transmogrifier(data)
|
||||||
"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
|
|
||||||
else
|
else
|
||||||
Logger.warning("Object origin check failed")
|
Logger.warning("Object origin check failed")
|
||||||
{:error, :object_origin_check_failed}
|
{:error, :object_origin_check_failed}
|
||||||
|
@ -101,6 +82,34 @@ defmodule Mobilizon.Federation.ActivityPub.Fetcher do
|
||||||
end
|
end
|
||||||
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 ::
|
@type fetch_actor_errors ::
|
||||||
:json_decode_error | :actor_deleted | :http_error | :actor_not_allowed_type
|
:json_decode_error | :actor_deleted | :http_error | :actor_not_allowed_type
|
||||||
|
|
||||||
|
|
|
@ -76,16 +76,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
|
||||||
Actions.Create.create(:conversation, object_data, false)
|
Actions.Create.create(:conversation, object_data, false)
|
||||||
|
|
||||||
object_data when is_map(object_data) ->
|
object_data when is_map(object_data) ->
|
||||||
case Discussions.get_comment_from_url_with_preload(object_data.url) do
|
handle_comment_or_discussion(object_data)
|
||||||
{: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
|
|
||||||
|
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
{:error, err}
|
{:error, err}
|
||||||
|
@ -1026,6 +1017,19 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
|
||||||
is_nil(object_data.title) or object_data.title == ""
|
is_nil(object_data.title) or object_data.title == ""
|
||||||
end
|
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
|
# Comment and conversations have different attributes for actor and groups
|
||||||
@spec transform_object_data_for_discussion(map()) :: map()
|
@spec transform_object_data_for_discussion(map()) :: map()
|
||||||
defp transform_object_data_for_discussion(object_data) do
|
defp transform_object_data_for_discussion(object_data) do
|
||||||
|
|
Loading…
Reference in a new issue