diff --git a/lib/federation/activity_pub/transmogrifier.ex b/lib/federation/activity_pub/transmogrifier.ex index ca14b42d0..e0c28c5d2 100644 --- a/lib/federation/activity_pub/transmogrifier.ex +++ b/lib/federation/activity_pub/transmogrifier.ex @@ -67,29 +67,30 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object}) do Logger.info("Handle incoming to create notes") - with object_data when is_map(object_data) <- - object |> Converter.Comment.as_to_model_data(), - {:existing_comment, {:error, :comment_not_found}} <- - {:existing_comment, Discussions.get_comment_from_url_with_preload(object_data.url)}, - object_data <- transform_object_data_for_discussion(object_data), - # Check should be better - - {:ok, %Activity{} = activity, entity} <- - (if is_data_for_comment_or_discussion?(object_data) do - Logger.debug("Chosing to create a regular comment") - Actions.Create.create(:comment, object_data, false) - else - Logger.debug("Chosing to initialize or add a comment to a conversation") - Actions.Create.create(:discussion, object_data, false) - end) do - {:ok, activity, entity} - else - {:existing_comment, {:ok, %Comment{} = comment}} -> - {:ok, nil, comment} - - {:error, :event_not_allow_commenting} -> - Logger.debug("Tried to reply to an event for which comments are closed") + case Converter.Comment.as_to_model_data(object) do + %{visibility: visibility, event_id: event_id} + when visibility != :public and event_id != nil -> + Logger.info("Tried to reply to an event with a private comment - ignore") :error + + 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(object_data) + + case create_comment_or_discussion(object_data) do + {:ok, %Activity{} = activity, entity} -> + {:ok, activity, entity} + + {:error, :event_not_allow_commenting} -> + Logger.debug("Tried to reply to an event for which comments are closed") + :error + end + + {:ok, %Comment{} = comment} -> + # Object already exists + {:ok, nil, comment} + end end end @@ -1177,4 +1178,16 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do defp can_manage_event?(%Actor{} = _actor, %Event{} = _event) do false end + + @spec create_comment_or_discussion(map()) :: + {:ok, Activity.t(), struct()} | {:error, atom() | Ecto.Changeset.t()} + defp create_comment_or_discussion(object_data) do + if is_data_for_comment_or_discussion?(object_data) do + Logger.debug("Chosing to create a regular comment") + Actions.Create.create(:comment, object_data, false) + else + Logger.debug("Chosing to initialize or add a comment to a conversation") + Actions.Create.create(:discussion, object_data, false) + end + end end diff --git a/lib/federation/activity_stream/converter/comment.ex b/lib/federation/activity_stream/converter/comment.ex index eaa15ac01..209e04c79 100644 --- a/lib/federation/activity_stream/converter/comment.ex +++ b/lib/federation/activity_stream/converter/comment.ex @@ -50,6 +50,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Comment do Logger.debug("Inserting full comment") Logger.debug(inspect(object)) + data = %{ text: object["content"], url: object["id"], diff --git a/lib/federation/activity_stream/converter/event.ex b/lib/federation/activity_stream/converter/event.ex index 9521a7fc7..78d6ccb55 100644 --- a/lib/federation/activity_stream/converter/event.ex +++ b/lib/federation/activity_stream/converter/event.ex @@ -230,8 +230,6 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do end end - @ap_public "https://www.w3.org/ns/activitystreams#Public" - defp get_visibility(object), do: if(@ap_public in object["to"], do: :public, else: :unlisted) @spec date_to_string(DateTime.t() | nil) :: String.t() diff --git a/test/fixtures/mastodon-post-activity-private.json b/test/fixtures/mastodon-post-activity-private.json index 1c3153ce7..3f4c8647c 100644 --- a/test/fixtures/mastodon-post-activity-private.json +++ b/test/fixtures/mastodon-post-activity-private.json @@ -16,23 +16,15 @@ } ], "actor": "https://framapiaf.org/users/admin", - "cc": [ - "https://framapiaf.org/users/admin/followers", - "http://mobilizon.com/@tcit" - ], - "id": "https://framapiaf.org/users/admin/statuses/99512778738411822/activity", + "id": "https://framapiaf.org/users/admin/statuses/19512778738411823/activity", "nickname": "lain", "object": { - "atomUri": "https://framapiaf.org/users/admin/statuses/99512778738411822", + "atomUri": "https://framapiaf.org/users/admin/statuses/19512778738411823", "attachment": [], "attributedTo": "https://framapiaf.org/users/admin", - "cc": [ - "https://framapiaf.org/users/admin/followers", - "https://framapiaf.org/users/tcit" - ], - "content": "

@tcit

", + "content": "

@tcit here is a private message

", "conversation": "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation", - "id": "https://framapiaf.org/users/admin/statuses/99512778738411822", + "id": "https://framapiaf.org/users/admin/statuses/19512778738411823", "inReplyTo": null, "inReplyToAtomUri": null, "published": "2018-02-12T14:08:20Z", @@ -49,7 +41,7 @@ "https://framapiaf.org/users/tcit" ], "type": "Note", - "url": "https://framapiaf.org/@admin/99512778738411822" + "url": "https://framapiaf.org/@admin/19512778738411823" }, "published": "2018-02-12T14:08:20Z", "signature": {