Fix things with in_reply_to_comment_id

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-08 15:57:46 +01:00
parent d23c0762e3
commit fb03e040aa
3 changed files with 30 additions and 3 deletions

View File

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

View File

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

View File

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