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 537736a98e
commit 04e342c897
3 changed files with 30 additions and 3 deletions

View File

@ -40,4 +40,11 @@ defmodule Mobilizon.Events.Comment do
|> put_change(:url, url) |> put_change(:url, url)
|> validate_required([:text, :actor_id, :url]) |> validate_required([:text, :actor_id, :url])
end 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 end

View File

@ -50,6 +50,8 @@ defmodule Mobilizon.Service.ActivityPub do
@spec fetch_object_from_url(String.t()) :: tuple() @spec fetch_object_from_url(String.t()) :: tuple()
def fetch_object_from_url(url) do def fetch_object_from_url(url) do
with true <- String.starts_with?(url, "http"), 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 <- {:ok, %{body: body, status_code: code}} when code in 200..299 <-
HTTPoison.get( HTTPoison.get(
url, url,
@ -59,8 +61,6 @@ defmodule Mobilizon.Service.ActivityPub do
recv_timeout: 20_000 recv_timeout: 20_000
), ),
{:ok, data} <- Jason.decode(body), {:ok, data} <- Jason.decode(body),
nil <- Events.get_event_by_url(data["id"]),
nil <- Events.get_comment_from_url(data["id"]),
params <- %{ params <- %{
"type" => "Create", "type" => "Create",
"to" => data["to"], "to" => data["to"],

View File

@ -14,6 +14,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
alias MobilizonWeb alias MobilizonWeb
alias MobilizonWeb.Router.Helpers alias MobilizonWeb.Router.Helpers
alias MobilizonWeb.Endpoint alias MobilizonWeb.Endpoint
alias Mobilizon.Service.ActivityPub
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
import Ecto.Query import Ecto.Query
@ -137,11 +138,30 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
"text" => object_data["content"], "text" => object_data["content"],
"url" => object_data["id"], "url" => object_data["id"],
"actor_id" => actor_id, "actor_id" => actor_id,
"in_reply_to_comment_id" => object_data["inReplyTo"], "in_reply_to_comment_id" => nil,
"event_id" => nil,
# probably # probably
"local" => false "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 require Logger
Logger.info("comment data ready to be inserted") Logger.info("comment data ready to be inserted")
Logger.info(inspect(data)) Logger.info(inspect(data))