2019-09-22 18:29:13 +02:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Converter.Comment do
|
2019-05-22 14:12:11 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Comment converter.
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
This module allows to convert events from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-05-22 14:12:11 +02:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.Comment, as: CommentModel
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
2019-12-17 09:49:08 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Visibility}
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Utils, as: ConverterUtils
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Tombstone, as: TombstoneModel
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
defimpl Convertible, for: CommentModel do
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Comment, as: CommentConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(comment), to: CommentConverter
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-05-22 14:12:11 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-10-25 17:43:37 +02:00
|
|
|
@spec as_to_model_data(map) :: {:ok, map} | {:error, any()}
|
2019-05-22 14:12:11 +02:00
|
|
|
def as_to_model_data(object) do
|
2019-10-25 17:43:37 +02:00
|
|
|
Logger.debug("We're converting raw ActivityStream data to a comment entity")
|
2019-05-22 14:12:11 +02:00
|
|
|
Logger.debug(inspect(object))
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
with author_url <- Map.get(object, "actor") || Map.get(object, "attributedTo"),
|
2019-12-17 09:49:08 +01:00
|
|
|
{:ok, %Actor{id: actor_id, domain: domain}} <-
|
|
|
|
ActivityPub.get_or_fetch_actor_by_url(author_url),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:tags, tags} <- {:tags, ConverterUtils.fetch_tags(Map.get(object, "tag", []))},
|
|
|
|
{:mentions, mentions} <-
|
|
|
|
{:mentions, ConverterUtils.fetch_mentions(Map.get(object, "tag", []))} do
|
2019-10-25 17:43:37 +02:00
|
|
|
Logger.debug("Inserting full comment")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
|
|
|
|
data = %{
|
|
|
|
text: object["content"],
|
|
|
|
url: object["id"],
|
|
|
|
actor_id: actor_id,
|
|
|
|
in_reply_to_comment_id: nil,
|
|
|
|
event_id: nil,
|
|
|
|
uuid: object["uuid"],
|
|
|
|
tags: tags,
|
2019-12-17 09:49:08 +01:00
|
|
|
mentions: mentions,
|
|
|
|
local: is_nil(domain),
|
|
|
|
visibility: if(Visibility.is_public?(object), do: :public, else: :private)
|
2019-10-25 17:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# We fetch the parent object
|
|
|
|
Logger.debug("We're fetching the parent object")
|
|
|
|
|
|
|
|
data =
|
|
|
|
if Map.has_key?(object, "inReplyTo") && object["inReplyTo"] != nil &&
|
|
|
|
object["inReplyTo"] != "" do
|
|
|
|
Logger.debug(fn -> "Object has inReplyTo #{object["inReplyTo"]}" end)
|
|
|
|
|
|
|
|
case ActivityPub.fetch_object_from_url(object["inReplyTo"]) do
|
|
|
|
# Reply to an event (Event)
|
|
|
|
{:ok, %Event{id: id}} ->
|
|
|
|
Logger.debug("Parent object is an event")
|
|
|
|
data |> Map.put(:event_id, id)
|
|
|
|
|
|
|
|
# Reply to a comment (Comment)
|
|
|
|
{:ok, %CommentModel{id: id} = comment} ->
|
|
|
|
Logger.debug("Parent object is another comment")
|
|
|
|
|
|
|
|
data
|
|
|
|
|> Map.put(:in_reply_to_comment_id, id)
|
|
|
|
|> Map.put(:origin_comment_id, comment |> CommentModel.get_thread_id())
|
2019-12-03 11:29:51 +01:00
|
|
|
|> Map.put(:event_id, comment.event_id)
|
2019-10-25 17:43:37 +02:00
|
|
|
|
|
|
|
# Anything else is kind of a MP
|
|
|
|
{:error, parent} ->
|
2019-11-15 18:36:47 +01:00
|
|
|
Logger.warn("Parent object is something we don't handle")
|
2019-10-25 17:43:37 +02:00
|
|
|
Logger.debug(inspect(parent))
|
|
|
|
data
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Logger.debug("No parent object for this comment")
|
|
|
|
|
|
|
|
data
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
{:ok, data}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make an AS comment object from an existing `Comment` structure.
|
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec model_to_as(CommentModel.t()) :: map
|
2019-11-15 18:36:47 +01:00
|
|
|
def model_to_as(%CommentModel{deleted_at: nil} = comment) do
|
2019-10-25 17:43:37 +02:00
|
|
|
to =
|
|
|
|
if comment.visibility == :public,
|
|
|
|
do: ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
else: [comment.actor.followers_url]
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
object = %{
|
|
|
|
"type" => "Note",
|
2019-10-25 17:43:37 +02:00
|
|
|
"to" => to,
|
|
|
|
"cc" => [],
|
2019-05-22 14:12:11 +02:00
|
|
|
"content" => comment.text,
|
2019-12-03 11:29:51 +01:00
|
|
|
"mediaType" => "text/html",
|
2019-05-22 14:12:11 +02:00
|
|
|
"actor" => comment.actor.url,
|
|
|
|
"attributedTo" => comment.actor.url,
|
|
|
|
"uuid" => comment.uuid,
|
2019-10-25 17:43:37 +02:00
|
|
|
"id" => comment.url,
|
|
|
|
"tag" =>
|
|
|
|
ConverterUtils.build_mentions(comment.mentions) ++ ConverterUtils.build_tags(comment.tags)
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
cond do
|
|
|
|
comment.in_reply_to_comment ->
|
|
|
|
Map.put(object, "inReplyTo", comment.in_reply_to_comment.url)
|
|
|
|
|
|
|
|
comment.event ->
|
|
|
|
Map.put(object, "inReplyTo", comment.event.url)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
object
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
@impl Converter
|
|
|
|
@spec model_to_as(CommentModel.t()) :: map
|
2019-12-03 11:29:51 +01:00
|
|
|
@doc """
|
|
|
|
A "soft-deleted" comment is a tombstone
|
|
|
|
"""
|
2019-11-15 18:36:47 +01:00
|
|
|
def model_to_as(%CommentModel{} = comment) do
|
2019-12-03 11:29:51 +01:00
|
|
|
Convertible.model_to_as(%TombstoneModel{
|
|
|
|
uri: comment.url,
|
|
|
|
inserted_at: comment.deleted_at
|
|
|
|
})
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|