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
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
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
{: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
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}
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")
:error
end
{:ok, %Comment{} = comment} ->
# Object already exists
{:ok, nil, comment}
end
end
end
# 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
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

View File

@ -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"],

View File

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

View File

@ -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": "<p><span class=\"h-card\"><a href=\"https://framapiaf.org/users/tcit\" class=\"u-url mention\">@<span>tcit</span></a></span></p>",
"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>",
"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": {