2020-07-09 15:24:28 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Post do
|
|
|
|
@moduledoc """
|
|
|
|
Post converter.
|
|
|
|
|
|
|
|
This module allows to convert posts from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2021-03-08 10:43:07 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Audience, Utils}
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
2020-12-16 08:54:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Media, as: MediaConverter
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
require Logger
|
|
|
|
|
2020-12-16 08:54:56 +00:00
|
|
|
import Mobilizon.Federation.ActivityStream.Converter.Utils,
|
|
|
|
only: [
|
2024-02-08 16:51:14 +00:00
|
|
|
process_pictures: 2,
|
|
|
|
visibility_public?: 1
|
2020-12-16 08:54:56 +00:00
|
|
|
]
|
|
|
|
|
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
|
|
|
@behaviour Converter
|
|
|
|
|
|
|
|
defimpl Convertible, for: Post do
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Post, as: PostConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(post), to: PostConverter
|
|
|
|
end
|
|
|
|
|
2020-12-16 08:54:56 +00:00
|
|
|
@banner_picture_name "Banner"
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
@doc """
|
|
|
|
Convert an post struct to an ActivityStream representation
|
|
|
|
"""
|
|
|
|
@impl Converter
|
|
|
|
@spec model_to_as(Post.t()) :: map
|
|
|
|
def model_to_as(
|
2021-02-04 11:28:53 +00:00
|
|
|
%Post{
|
|
|
|
author: %Actor{url: actor_url},
|
2021-03-08 10:43:07 +00:00
|
|
|
attributed_to: %Actor{
|
|
|
|
url: creator_url
|
|
|
|
}
|
2021-02-04 11:28:53 +00:00
|
|
|
} = post
|
2020-07-09 15:24:28 +00:00
|
|
|
) do
|
2021-07-29 15:48:28 +00:00
|
|
|
audience = Audience.get_audience(post)
|
2021-02-04 11:28:53 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
%{
|
|
|
|
"type" => "Article",
|
|
|
|
"actor" => actor_url,
|
|
|
|
"id" => post.url,
|
|
|
|
"name" => post.title,
|
|
|
|
"content" => post.body,
|
|
|
|
"attributedTo" => creator_url,
|
2020-12-16 08:54:56 +00:00
|
|
|
"published" => (post.publish_at || post.inserted_at) |> to_date(),
|
2021-03-08 14:58:25 +00:00
|
|
|
"attachment" => [],
|
|
|
|
"draft" => post.draft
|
2020-07-09 15:24:28 +00:00
|
|
|
}
|
2021-03-08 10:43:07 +00:00
|
|
|
|> Map.merge(audience)
|
2020-12-16 08:54:56 +00:00
|
|
|
|> maybe_add_post_picture(post)
|
|
|
|
|> maybe_add_inline_media(post)
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts an AP object data to our internal data structure.
|
|
|
|
"""
|
|
|
|
@impl Converter
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, any()}
|
2020-07-09 15:24:28 +00:00
|
|
|
def as_to_model_data(
|
2021-07-23 09:34:58 +00:00
|
|
|
%{"type" => "Article", "actor" => creator, "attributedTo" => group_uri} = object
|
2020-07-09 15:24:28 +00:00
|
|
|
) do
|
2021-07-23 09:34:58 +00:00
|
|
|
with {:ok, %Actor{id: attributed_to_id} = group} <- get_actor(group_uri),
|
2021-09-10 09:27:59 +00:00
|
|
|
{:ok, %Actor{id: author_id}} <- get_actor(creator) do
|
|
|
|
[description: description, picture_id: picture_id, medias: medias] =
|
|
|
|
process_pictures(object, attributed_to_id)
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
%{
|
|
|
|
title: object["name"],
|
2020-12-16 08:54:56 +00:00
|
|
|
body: description,
|
2020-07-09 15:24:28 +00:00
|
|
|
url: object["id"],
|
|
|
|
attributed_to_id: attributed_to_id,
|
|
|
|
author_id: author_id,
|
|
|
|
local: false,
|
2020-12-16 08:54:56 +00:00
|
|
|
publish_at: object["published"],
|
|
|
|
picture_id: picture_id,
|
2021-03-08 14:58:25 +00:00
|
|
|
medias: medias,
|
2021-09-10 09:27:59 +00:00
|
|
|
visibility: get_visibility(object, group),
|
2021-03-08 14:58:25 +00:00
|
|
|
draft: object["draft"] == true
|
2020-07-09 15:24:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{:error, err} -> {:error, err}
|
|
|
|
err -> {:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_actor(String.t() | map() | nil) :: {:ok, Actor.t()} | {:error, String.t()}
|
2021-09-10 09:27:59 +00:00
|
|
|
defp get_actor(actor) when is_valid_string(actor),
|
2021-04-22 10:17:56 +00:00
|
|
|
do: actor |> Utils.get_url() |> ActivityPubActor.get_or_fetch_actor_by_url()
|
2020-10-01 13:07:15 +00:00
|
|
|
|
2021-09-10 09:27:59 +00:00
|
|
|
defp get_actor(_), do: {:error, "nil property found for actor data"}
|
|
|
|
|
|
|
|
@spec to_date(DateTime.t() | NaiveDateTime.t() | nil) :: String.t() | nil
|
2021-03-08 14:58:25 +00:00
|
|
|
defp to_date(nil), do: nil
|
2020-10-01 13:07:15 +00:00
|
|
|
defp to_date(%DateTime{} = date), do: DateTime.to_iso8601(date)
|
|
|
|
defp to_date(%NaiveDateTime{} = date), do: NaiveDateTime.to_iso8601(date)
|
2020-12-16 08:54:56 +00:00
|
|
|
|
|
|
|
@spec maybe_add_post_picture(map(), Post.t()) :: map()
|
|
|
|
defp maybe_add_post_picture(res, post) do
|
|
|
|
if is_nil(post.picture),
|
|
|
|
do: res,
|
|
|
|
else:
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++
|
|
|
|
[
|
|
|
|
post.picture
|
|
|
|
|> MediaConverter.model_to_as()
|
|
|
|
|> Map.put("name", @banner_picture_name)
|
|
|
|
])
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_add_inline_media(map(), Post.t()) :: map()
|
|
|
|
defp maybe_add_inline_media(res, post) do
|
|
|
|
medias = Enum.map(post.media, &MediaConverter.model_to_as/1)
|
|
|
|
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++ medias)
|
|
|
|
)
|
|
|
|
end
|
2021-07-23 09:34:58 +00:00
|
|
|
|
|
|
|
defp get_visibility(%{"to" => to}, %Actor{
|
|
|
|
followers_url: followers_url,
|
|
|
|
members_url: members_url
|
|
|
|
}) do
|
|
|
|
cond do
|
2024-02-08 16:51:14 +00:00
|
|
|
visibility_public?(to) -> :public
|
2021-07-23 09:34:58 +00:00
|
|
|
followers_url in to -> :unlisted
|
|
|
|
members_url in to -> :private
|
|
|
|
end
|
|
|
|
end
|
2020-07-09 15:24:28 +00:00
|
|
|
end
|