From 04e342c897e4d967f0699d4725f4559fac2db7da Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 8 Nov 2018 15:57:46 +0100 Subject: [PATCH] Fix things with in_reply_to_comment_id Signed-off-by: Thomas Citharel --- lib/mobilizon/events/comment.ex | 7 +++++++ lib/service/activity_pub/activity_pub.ex | 4 ++-- lib/service/activity_pub/utils.ex | 22 +++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/mobilizon/events/comment.ex b/lib/mobilizon/events/comment.ex index cbc9a6f57..adf137baa 100644 --- a/lib/mobilizon/events/comment.ex +++ b/lib/mobilizon/events/comment.ex @@ -40,4 +40,11 @@ defmodule Mobilizon.Events.Comment do |> put_change(:url, url) |> validate_required([:text, :actor_id, :url]) end + + @doc """ + Returns the id of the first comment in the conversation + """ + def get_thread_id(%Comment{id: id, origin_comment_id: origin_comment_id}) do + origin_comment_id || id + end end diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 9f2a3e4a7..f140d7484 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -50,6 +50,8 @@ defmodule Mobilizon.Service.ActivityPub do @spec fetch_object_from_url(String.t()) :: tuple() def fetch_object_from_url(url) do with true <- String.starts_with?(url, "http"), + nil <- Events.get_event_by_url(url), + nil <- Events.get_comment_from_url(url), {:ok, %{body: body, status_code: code}} when code in 200..299 <- HTTPoison.get( url, @@ -59,8 +61,6 @@ defmodule Mobilizon.Service.ActivityPub do recv_timeout: 20_000 ), {:ok, data} <- Jason.decode(body), - nil <- Events.get_event_by_url(data["id"]), - nil <- Events.get_comment_from_url(data["id"]), params <- %{ "type" => "Create", "to" => data["to"], diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index d52b62c4a..60745349f 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -14,6 +14,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do alias MobilizonWeb alias MobilizonWeb.Router.Helpers alias MobilizonWeb.Endpoint + alias Mobilizon.Service.ActivityPub alias Ecto.{Changeset, UUID} import Ecto.Query @@ -137,11 +138,30 @@ defmodule Mobilizon.Service.ActivityPub.Utils do "text" => object_data["content"], "url" => object_data["id"], "actor_id" => actor_id, - "in_reply_to_comment_id" => object_data["inReplyTo"], + "in_reply_to_comment_id" => nil, + "event_id" => nil, # probably "local" => false } + # We fetch the parent object + data = + case ActivityPub.fetch_object_from_url(object_data["inReplyTo"]) do + # Reply to an event (Comment) + {:ok, %Event{id: id}} -> + data |> Map.put("event_id", id) + + # Reply to a comment (Comment) + {:ok, %Comment{id: id} = comment} -> + data + |> Map.put("in_reply_to_comment_id", id) + |> Map.put("origin_comment_id", comment |> Comment.get_thread_id()) + + # Anthing else is kind of a MP + _ -> + data + end + require Logger Logger.info("comment data ready to be inserted") Logger.info(inspect(data))