Propertly handle other errors when receiving a comment

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-11-03 18:11:40 +01:00
parent 4bb90ebb3f
commit 6058da0d44
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -77,20 +77,9 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
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 case Discussions.get_comment_from_url_with_preload(object_data.url) do
{:error, :comment_not_found} -> {:error, :comment_not_found} ->
object_data = transform_object_data_for_discussion(object_data) object_data
|> transform_object_data_for_discussion()
case create_comment_or_discussion(object_data) do |> save_comment_or_discussion()
{:ok, %Activity{} = activity, entity} ->
{:ok, activity, entity}
{:error, :entity_tombstoned} ->
Logger.debug("Tried to reply to an event that has been tombstoned")
:error
{:error, :event_not_allow_commenting} ->
Logger.debug("Tried to reply to an event for which comments are closed")
:error
end
{:ok, %Comment{} = comment} -> {:ok, %Comment{} = comment} ->
# Object already exists # Object already exists
@ -1230,4 +1219,28 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
object_id = Utils.get_url(object) object_id = Utils.get_url(object)
Utils.get_actor(data) == object_id and not Utils.are_same_origin?(object_id, Endpoint.url()) Utils.get_actor(data) == object_id and not Utils.are_same_origin?(object_id, Endpoint.url())
end end
@spec save_comment_or_discussion(map()) :: {:ok, Activity.t(), struct()} | :error
defp save_comment_or_discussion(object_data) do
case create_comment_or_discussion(object_data) do
{:ok, %Activity{} = activity, entity} ->
{:ok, activity, entity}
{:error, :entity_tombstoned} ->
Logger.debug("Tried to reply to an event that has been tombstoned")
:error
{:error, :event_not_allow_commenting} ->
Logger.debug("Tried to reply to an event for which comments are closed")
:error
{:error, %Ecto.Changeset{} = _changeset} ->
Logger.debug("Error when saving external comment")
:error
{:error, err} ->
Logger.debug("Generic error when saving external comment", err: inspect(err))
:error
end
end
end end