2020-07-09 15:24:28 +00: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 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Discussion, as: DiscussionConverter
|
|
|
|
alias Mobilizon.Storage.Repo
|
2021-09-10 09:27:59 +00:00
|
|
|
import Mobilizon.Service.Guards, only: [is_valid_string: 1]
|
2020-07-09 15:24:28 +00: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 14:01:10 +00:00
|
|
|
"to" => [discussion.actor.members_url],
|
2020-07-09 15:24:28 +00: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 09:53:24 +00:00
|
|
|
"publishedAt" => discussion.inserted_at,
|
2020-07-09 15:24:28 +00:00
|
|
|
"context" => discussion.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Converter
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, atom()}
|
2021-09-10 09:27:59 +00: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 17:40:37 +00:00
|
|
|
@spec extract_actors(map()) ::
|
|
|
|
%{actor_id: String.t(), creator_id: String.t()} | {:error, atom()}
|
2021-09-10 09:27:59 +00: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 10:17:56 +00:00
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(creator_url),
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, %Actor{id: actor_id, suspended: false}} <-
|
2021-04-22 10:17:56 +00:00
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(actor_url) do
|
2021-09-10 09:27:59 +00:00
|
|
|
%{actor_id: actor_id, creator_id: creator_id}
|
|
|
|
else
|
|
|
|
{:error, error} -> {:error, error}
|
2021-10-05 15:43:45 +00:00
|
|
|
{:ok, %Actor{url: ^creator_url}} -> {:error, :creator_suspended}
|
|
|
|
{:ok, %Actor{url: ^actor_url}} -> {:error, :actor_suspended}
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|