ignore incoming federated private comments

This commit is contained in:
Mayel 2021-10-19 17:57:38 +13:00 committed by Thomas Citharel
parent 8cb7df16d0
commit 475c72597e
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
4 changed files with 41 additions and 37 deletions

View File

@ -67,30 +67,31 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object}) do def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object}) do
Logger.info("Handle incoming to create notes") Logger.info("Handle incoming to create notes")
with object_data when is_map(object_data) <- case Converter.Comment.as_to_model_data(object) do
object |> Converter.Comment.as_to_model_data(), %{visibility: visibility, event_id: event_id}
{:existing_comment, {:error, :comment_not_found}} <- when visibility != :public and event_id != nil ->
{:existing_comment, Discussions.get_comment_from_url_with_preload(object_data.url)}, Logger.info("Tried to reply to an event with a private comment - ignore")
object_data <- transform_object_data_for_discussion(object_data), :error
# Check should be better
{:ok, %Activity{} = activity, entity} <- object_data when is_map(object_data) ->
(if is_data_for_comment_or_discussion?(object_data) do case Discussions.get_comment_from_url_with_preload(object_data.url) do
Logger.debug("Chosing to create a regular comment") {:error, :comment_not_found} ->
Actions.Create.create(:comment, object_data, false) object_data = transform_object_data_for_discussion(object_data)
else
Logger.debug("Chosing to initialize or add a comment to a conversation") case create_comment_or_discussion(object_data) do
Actions.Create.create(:discussion, object_data, false) {:ok, %Activity{} = activity, entity} ->
end) do
{:ok, activity, entity} {:ok, activity, entity}
else
{:existing_comment, {:ok, %Comment{} = comment}} ->
{:ok, nil, comment}
{:error, :event_not_allow_commenting} -> {:error, :event_not_allow_commenting} ->
Logger.debug("Tried to reply to an event for which comments are closed") Logger.debug("Tried to reply to an event for which comments are closed")
:error :error
end end
{:ok, %Comment{} = comment} ->
# Object already exists
{:ok, nil, comment}
end
end
end end
# Handles a `Create` activity for `Event` objects # Handles a `Create` activity for `Event` objects
@ -1177,4 +1178,16 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
defp can_manage_event?(%Actor{} = _actor, %Event{} = _event) do defp can_manage_event?(%Actor{} = _actor, %Event{} = _event) do
false false
end 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 end

View File

@ -50,6 +50,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Comment do
Logger.debug("Inserting full comment") Logger.debug("Inserting full comment")
Logger.debug(inspect(object)) Logger.debug(inspect(object))
data = %{ data = %{
text: object["content"], text: object["content"],
url: object["id"], url: object["id"],

View File

@ -230,8 +230,6 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
end end
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) defp get_visibility(object), do: if(@ap_public in object["to"], do: :public, else: :unlisted)
@spec date_to_string(DateTime.t() | nil) :: String.t() @spec date_to_string(DateTime.t() | nil) :: String.t()

View File

@ -16,23 +16,15 @@
} }
], ],
"actor": "https://framapiaf.org/users/admin", "actor": "https://framapiaf.org/users/admin",
"cc": [ "id": "https://framapiaf.org/users/admin/statuses/19512778738411823/activity",
"https://framapiaf.org/users/admin/followers",
"http://mobilizon.com/@tcit"
],
"id": "https://framapiaf.org/users/admin/statuses/99512778738411822/activity",
"nickname": "lain", "nickname": "lain",
"object": { "object": {
"atomUri": "https://framapiaf.org/users/admin/statuses/99512778738411822", "atomUri": "https://framapiaf.org/users/admin/statuses/19512778738411823",
"attachment": [], "attachment": [],
"attributedTo": "https://framapiaf.org/users/admin", "attributedTo": "https://framapiaf.org/users/admin",
"cc": [ "content": "<p><span class=\"h-card\"><a href=\"https://framapiaf.org/users/tcit\" class=\"u-url mention\">@<span>tcit</span></a></span> here is a private message</p>",
"https://framapiaf.org/users/admin/followers",
"https://framapiaf.org/users/tcit"
],
"content": "<p><span class=\"h-card\"><a href=\"https://framapiaf.org/users/tcit\" class=\"u-url mention\">@<span>tcit</span></a></span></p>",
"conversation": "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation", "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, "inReplyTo": null,
"inReplyToAtomUri": null, "inReplyToAtomUri": null,
"published": "2018-02-12T14:08:20Z", "published": "2018-02-12T14:08:20Z",
@ -49,7 +41,7 @@
"https://framapiaf.org/users/tcit" "https://framapiaf.org/users/tcit"
], ],
"type": "Note", "type": "Note",
"url": "https://framapiaf.org/@admin/99512778738411822" "url": "https://framapiaf.org/@admin/19512778738411823"
}, },
"published": "2018-02-12T14:08:20Z", "published": "2018-02-12T14:08:20Z",
"signature": { "signature": {