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
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor, as: ActorModel
|
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}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.MediaProxy
|
|
|
|
|
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
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec as_to_model_data(map()) :: {:ok, map()}
|
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 =
|
2019-12-03 11:29:51 +01:00
|
|
|
data["icon"]["url"] &&
|
2019-09-04 18:24:31 +02:00
|
|
|
%{
|
2019-12-03 11:29:51 +01:00
|
|
|
"name" => data["icon"]["name"] || "avatar",
|
2020-01-28 19:18:33 +01:00
|
|
|
"url" => MediaProxy.url(data["icon"]["url"])
|
2019-09-04 18:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
banner =
|
2019-12-03 11:29:51 +01:00
|
|
|
data["image"]["url"] &&
|
2019-09-04 18:24:31 +02:00
|
|
|
%{
|
2019-12-03 11:29:51 +01:00
|
|
|
"name" => data["image"]["name"] || "banner",
|
2020-01-28 19:18:33 +01:00
|
|
|
"url" => MediaProxy.url(data["image"]["url"])
|
2019-09-04 18:24:31 +02:00
|
|
|
}
|
|
|
|
|
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"],
|
2020-02-18 08:57:00 +01:00
|
|
|
members_url: data["members"],
|
|
|
|
resources_url: data["resources"],
|
2020-07-09 17:24:28 +02:00
|
|
|
todos_url: data["todos"],
|
|
|
|
events_url: data["events"],
|
|
|
|
posts_url: data["posts"],
|
|
|
|
discussions_url: data["discussions"],
|
2019-12-03 11:29:51 +01:00
|
|
|
shared_inbox_url: data["endpoints"]["sharedInbox"],
|
|
|
|
domain: URI.parse(data["id"]).host,
|
|
|
|
manually_approves_followers: data["manuallyApprovesFollowers"],
|
|
|
|
type: data["type"]
|
|
|
|
}
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
def as_to_model_data(_), do: :error
|
|
|
|
|
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,
|
2020-02-18 08:57:00 +01:00
|
|
|
"members" => actor.members_url,
|
|
|
|
"resources" => actor.resources_url,
|
|
|
|
"todos" => actor.todos_url,
|
2020-07-09 17:24:28 +02:00
|
|
|
"posts" => actor.posts_url,
|
|
|
|
"events" => actor.events_url,
|
|
|
|
"discussions" => actor.discussions_url,
|
2019-12-03 11:29:51 +01:00
|
|
|
"inbox" => actor.inbox_url,
|
|
|
|
"outbox" => actor.outbox_url,
|
|
|
|
"url" => actor.url,
|
|
|
|
"endpoints" => %{
|
|
|
|
"sharedInbox" => actor.shared_inbox_url
|
|
|
|
},
|
|
|
|
"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
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
actor_data =
|
|
|
|
if actor.type == :Group do
|
|
|
|
Map.put(actor_data, "members", actor.members_url)
|
|
|
|
else
|
|
|
|
actor_data
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
actor_data =
|
|
|
|
if is_nil(actor.avatar) do
|
|
|
|
actor_data
|
|
|
|
else
|
|
|
|
Map.put(actor_data, "icon", %{
|
|
|
|
"type" => "Image",
|
|
|
|
"mediaType" => actor.avatar.content_type,
|
|
|
|
"url" => actor.avatar.url
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
if is_nil(actor.banner) do
|
|
|
|
actor_data
|
|
|
|
else
|
|
|
|
Map.put(actor_data, "image", %{
|
|
|
|
"type" => "Image",
|
|
|
|
"mediaType" => actor.banner.content_type,
|
|
|
|
"url" => actor.banner.url
|
|
|
|
})
|
|
|
|
end
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|