2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Discussion do
|
|
|
|
@moduledoc """
|
|
|
|
Comment converter.
|
|
|
|
|
|
|
|
This module allows to convert events from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Discussions.Discussion
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Discussion, as: DiscussionConverter
|
|
|
|
alias Mobilizon.Storage.Repo
|
2021-09-10 11:27:59 +02:00
|
|
|
import Mobilizon.Service.Guards, only: [is_valid_string: 1]
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
|
|
|
defimpl Convertible, for: Discussion do
|
|
|
|
defdelegate model_to_as(comment), to: DiscussionConverter
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make an AS comment object from an existing `discussion` structure.
|
|
|
|
"""
|
|
|
|
@impl Converter
|
|
|
|
@spec model_to_as(Discussion.t()) :: map
|
|
|
|
def model_to_as(%Discussion{} = discussion) do
|
|
|
|
discussion = Repo.preload(discussion, [:last_comment, :actor, :creator])
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Note",
|
2021-04-16 16:01:10 +02:00
|
|
|
"to" => [discussion.actor.members_url],
|
2020-07-09 17:24:28 +02:00
|
|
|
"cc" => [],
|
|
|
|
"name" => discussion.title,
|
|
|
|
"content" => discussion.last_comment.text,
|
|
|
|
"mediaType" => "text/html",
|
|
|
|
"actor" => discussion.creator.url,
|
|
|
|
"attributedTo" => discussion.actor.url,
|
|
|
|
"id" => discussion.url,
|
2020-08-27 11:53:24 +02:00
|
|
|
"publishedAt" => discussion.inserted_at,
|
2020-07-09 17:24:28 +02:00
|
|
|
"context" => discussion.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Converter
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, atom()}
|
2021-09-10 11:27:59 +02:00
|
|
|
def as_to_model_data(%{"type" => "Note", "name" => name} = object) when is_valid_string(name) do
|
|
|
|
case extract_actors(object) do
|
|
|
|
%{actor_id: actor_id, creator_id: creator_id} ->
|
|
|
|
%{actor_id: actor_id, creator_id: creator_id, title: name, url: object["id"]}
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec extract_actors(map()) ::
|
|
|
|
%{actor_id: String.t(), creator_id: String.t()} | {:error, atom()}
|
2021-09-10 11:27:59 +02:00
|
|
|
defp extract_actors(%{"actor" => creator_url, "attributedTo" => actor_url} = _object)
|
|
|
|
when is_valid_string(creator_url) and is_valid_string(actor_url) do
|
|
|
|
with {:ok, %Actor{id: creator_id, suspended: false}} <-
|
2021-04-22 12:17:56 +02:00
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(creator_url),
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, %Actor{id: actor_id, suspended: false}} <-
|
2021-04-22 12:17:56 +02:00
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(actor_url) do
|
2021-09-10 11:27:59 +02:00
|
|
|
%{actor_id: actor_id, creator_id: creator_id}
|
|
|
|
else
|
|
|
|
{:error, error} -> {:error, error}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|