2020-01-22 22:40:40 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
|
2019-05-22 14:12:11 +02:00
|
|
|
@moduledoc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Actor 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
|
|
|
|
2022-03-24 14:19:24 +01:00
|
|
|
alias Mobilizon.Actors
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor, as: ActorModel
|
2021-11-06 10:09:07 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Utils
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
2021-11-06 10:09:07 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Address, as: AddressConverter
|
|
|
|
alias Mobilizon.Medias.File
|
2020-12-15 17:17:42 +01:00
|
|
|
alias Mobilizon.Service.HTTP.RemoteMediaDownloaderClient
|
|
|
|
alias Mobilizon.Service.RichMedia.Parser
|
|
|
|
alias Mobilizon.Web.Upload
|
2021-11-06 10:09:07 +01:00
|
|
|
import Mobilizon.Federation.ActivityStream.Converter.Utils, only: [get_address: 1]
|
2020-01-28 19:18:33 +01:00
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
@behaviour Converter
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
defimpl Convertible, for: ActorModel do
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Actor, as: ActorConverter
|
2019-09-22 18:29:13 +02:00
|
|
|
|
|
|
|
defdelegate model_to_as(actor), to: ActorConverter
|
|
|
|
end
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
@allowed_types ["Application", "Group", "Organization", "Person", "Service"]
|
|
|
|
|
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
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec as_to_model_data(map()) :: map() | {:error, :actor_not_allowed_type}
|
2020-07-07 15:51:42 +02:00
|
|
|
def as_to_model_data(%{"type" => type} = data) when type in @allowed_types do
|
2019-09-04 18:24:31 +02:00
|
|
|
avatar =
|
2020-12-15 17:17:42 +01:00
|
|
|
download_picture(get_in(data, ["icon", "url"]), get_in(data, ["icon", "name"]), "avatar")
|
2019-09-04 18:24:31 +02:00
|
|
|
|
|
|
|
banner =
|
2020-12-15 17:17:42 +01:00
|
|
|
download_picture(get_in(data, ["image", "url"]), get_in(data, ["image", "name"]), "banner")
|
2019-09-04 18:24:31 +02:00
|
|
|
|
2021-11-06 10:09:07 +01:00
|
|
|
address = get_address(data["location"])
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
%{
|
2019-12-03 11:29:51 +01:00
|
|
|
url: data["id"],
|
|
|
|
avatar: avatar,
|
|
|
|
banner: banner,
|
|
|
|
name: data["name"],
|
|
|
|
preferred_username: data["preferredUsername"],
|
2020-07-09 17:24:28 +02:00
|
|
|
summary: data["summary"] || "",
|
2019-12-03 11:29:51 +01:00
|
|
|
keys: data["publicKey"]["publicKeyPem"],
|
|
|
|
inbox_url: data["inbox"],
|
|
|
|
outbox_url: data["outbox"],
|
|
|
|
following_url: data["following"],
|
|
|
|
followers_url: data["followers"],
|
|
|
|
domain: URI.parse(data["id"]).host,
|
|
|
|
manually_approves_followers: data["manuallyApprovesFollowers"],
|
2020-08-11 18:00:35 +02:00
|
|
|
type: data["type"],
|
2020-11-06 11:34:32 +01:00
|
|
|
visibility: if(Map.get(data, "discoverable", false) == true, do: :public, else: :unlisted),
|
2021-11-06 10:09:07 +01:00
|
|
|
openness: data["openness"],
|
|
|
|
physical_address_id: if(address, do: address.id, else: nil)
|
2019-12-03 11:29:51 +01:00
|
|
|
}
|
2021-11-12 16:29:22 +01:00
|
|
|
|> add_endpoints_to_model(data)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
def as_to_model_data(_), do: {:error, :actor_not_allowed_type}
|
2020-07-07 15:51:42 +02:00
|
|
|
|
2021-11-12 16:29:22 +01:00
|
|
|
defp add_endpoints_to_model(actor, data) do
|
|
|
|
# TODO: Remove fallbacks in 3.0
|
|
|
|
endpoints = %{
|
|
|
|
members_url: get_in(data, ["endpoints", "members"]) || data["members"],
|
|
|
|
resources_url: get_in(data, ["endpoints", "resources"]) || data["resources"],
|
|
|
|
todos_url: get_in(data, ["endpoints", "todos"]) || data["todos"],
|
|
|
|
events_url: get_in(data, ["endpoints", "events"]) || data["events"],
|
|
|
|
posts_url: get_in(data, ["endpoints", "posts"]) || data["posts"],
|
|
|
|
discussions_url: get_in(data, ["endpoints", "discussions"]) || data["discussions"],
|
|
|
|
shared_inbox_url: data["endpoints"]["sharedInbox"]
|
|
|
|
}
|
|
|
|
|
|
|
|
Map.merge(actor, endpoints)
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Convert an actor struct to an ActivityStream representation.
|
2019-05-22 14:12:11 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec model_to_as(ActorModel.t()) :: map()
|
2019-05-22 14:12:11 +02:00
|
|
|
def model_to_as(%ActorModel{} = actor) do
|
2019-12-03 11:29:51 +01:00
|
|
|
actor_data = %{
|
|
|
|
"id" => actor.url,
|
|
|
|
"type" => actor.type,
|
|
|
|
"preferredUsername" => actor.preferred_username,
|
2019-05-22 14:12:11 +02:00
|
|
|
"name" => actor.name,
|
2020-07-09 17:24:28 +02:00
|
|
|
"summary" => actor.summary || "",
|
2019-12-03 11:29:51 +01:00
|
|
|
"following" => actor.following_url,
|
|
|
|
"followers" => actor.followers_url,
|
|
|
|
"inbox" => actor.inbox_url,
|
|
|
|
"outbox" => actor.outbox_url,
|
|
|
|
"url" => actor.url,
|
|
|
|
"endpoints" => %{
|
|
|
|
"sharedInbox" => actor.shared_inbox_url
|
|
|
|
},
|
2020-08-11 18:00:35 +02:00
|
|
|
"discoverable" => actor.visibility == :public,
|
2020-11-06 11:34:32 +01:00
|
|
|
"openness" => actor.openness,
|
2019-12-03 11:29:51 +01:00
|
|
|
"manuallyApprovesFollowers" => actor.manually_approves_followers,
|
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{actor.url}#main-key",
|
|
|
|
"owner" => actor.url,
|
|
|
|
"publicKeyPem" =>
|
|
|
|
if(is_nil(actor.domain) and not is_nil(actor.keys),
|
|
|
|
do: Utils.pem_to_public_key_pem(actor.keys),
|
|
|
|
else: actor.keys
|
|
|
|
)
|
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
2019-12-03 11:29:51 +01:00
|
|
|
|
2021-11-06 10:09:07 +01:00
|
|
|
actor_data
|
2021-11-12 16:29:22 +01:00
|
|
|
|> add_endpoints(actor)
|
2021-11-06 10:09:07 +01:00
|
|
|
|> maybe_add_members(actor)
|
|
|
|
|> maybe_add_avatar_picture(actor)
|
|
|
|
|> maybe_add_banner_picture(actor)
|
|
|
|
|> maybe_add_physical_address(actor)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
2020-12-15 17:17:42 +01:00
|
|
|
|
2021-11-12 16:29:22 +01:00
|
|
|
defp add_endpoints(%{"endpoints" => endpoints} = actor_data, %ActorModel{} = actor) do
|
|
|
|
new_endpoints = %{
|
|
|
|
"members" => actor.members_url,
|
|
|
|
"resources" => actor.resources_url,
|
|
|
|
"todos" => actor.todos_url,
|
|
|
|
"posts" => actor.posts_url,
|
|
|
|
"events" => actor.events_url,
|
|
|
|
"discussions" => actor.discussions_url
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints = Map.merge(endpoints, new_endpoints)
|
|
|
|
|
|
|
|
actor_data
|
|
|
|
|> Map.merge(new_endpoints)
|
|
|
|
|> Map.put("endpoints", endpoints)
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec download_picture(String.t() | nil, String.t(), String.t()) :: map() | nil
|
2020-12-15 17:17:42 +01:00
|
|
|
defp download_picture(nil, _name, _default_name), do: nil
|
|
|
|
|
|
|
|
defp download_picture(url, name, default_name) do
|
|
|
|
with {:ok, %{body: body, status: code, headers: response_headers}}
|
|
|
|
when code in 200..299 <- RemoteMediaDownloaderClient.get(url),
|
|
|
|
name <- name || Parser.get_filename_from_response(response_headers, url) || default_name,
|
|
|
|
{:ok, file} <- Upload.store(%{body: body, name: name}) do
|
2021-06-10 09:36:25 +02:00
|
|
|
Map.take(file, [:content_type, :name, :url, :size])
|
2021-11-16 15:48:09 +01:00
|
|
|
else
|
|
|
|
_ -> nil
|
2020-12-15 17:17:42 +01:00
|
|
|
end
|
|
|
|
end
|
2021-11-06 10:09:07 +01:00
|
|
|
|
2022-03-24 14:19:24 +01:00
|
|
|
defp maybe_add_members(actor_data, %ActorModel{type: :Group, members_url: members_url} = group) do
|
|
|
|
actor_data
|
|
|
|
|> Map.put("members", members_url)
|
|
|
|
|> Map.put("memberCount", Actors.count_members_for_group(group))
|
2021-11-06 10:09:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_members(actor_data, %ActorModel{}), do: actor_data
|
|
|
|
|
|
|
|
@spec maybe_add_avatar_picture(map(), ActorModel.t()) :: map()
|
|
|
|
defp maybe_add_avatar_picture(actor_data, %ActorModel{avatar: %File{} = avatar}) do
|
2021-11-16 15:47:14 +01:00
|
|
|
Map.put(actor_data, "icon", %{
|
2021-11-06 10:09:07 +01:00
|
|
|
"type" => "Image",
|
|
|
|
"mediaType" => avatar.content_type,
|
|
|
|
"url" => avatar.url
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_avatar_picture(res, %ActorModel{avatar: _}), do: res
|
|
|
|
|
|
|
|
@spec maybe_add_banner_picture(map(), ActorModel.t()) :: map()
|
|
|
|
defp maybe_add_banner_picture(actor_data, %ActorModel{banner: %File{} = banner}) do
|
|
|
|
Map.put(actor_data, "image", %{
|
|
|
|
"type" => "Image",
|
|
|
|
"mediaType" => banner.content_type,
|
|
|
|
"url" => banner.url
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_banner_picture(res, %ActorModel{banner: _}), do: res
|
|
|
|
|
|
|
|
@spec maybe_add_physical_address(map(), ActorModel.t()) :: map()
|
|
|
|
defp maybe_add_physical_address(res, %ActorModel{
|
|
|
|
physical_address: %Address{} = physical_address
|
|
|
|
}) do
|
|
|
|
Map.put(res, "location", AddressConverter.model_to_as(physical_address))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_physical_address(res, %ActorModel{physical_address: _}), do: res
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|