2019-09-22 18:29:13 +02:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.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
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Converter, Convertible}
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
defimpl Convertible, for: ActorModel do
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Actor, as: ActorConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(actor), to: ActorConverter
|
|
|
|
end
|
|
|
|
|
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
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec as_to_model_data(map) :: map
|
2019-05-22 14:12:11 +02:00
|
|
|
def as_to_model_data(object) do
|
2019-09-04 18:24:31 +02:00
|
|
|
avatar =
|
|
|
|
object["icon"]["url"] &&
|
|
|
|
%{
|
|
|
|
"name" => object["icon"]["name"] || "avatar",
|
|
|
|
"url" => object["icon"]["url"]
|
|
|
|
}
|
|
|
|
|
|
|
|
banner =
|
|
|
|
object["image"]["url"] &&
|
|
|
|
%{
|
|
|
|
"name" => object["image"]["name"] || "banner",
|
|
|
|
"url" => object["image"]["url"]
|
|
|
|
}
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
%{
|
|
|
|
"type" => String.to_existing_atom(object["type"]),
|
2019-09-04 18:24:31 +02:00
|
|
|
"preferred_username" => object["preferredUsername"],
|
2019-05-22 14:12:11 +02:00
|
|
|
"summary" => object["summary"],
|
2019-09-09 09:55:08 +02:00
|
|
|
"url" => object["id"],
|
2019-09-04 18:24:31 +02:00
|
|
|
"name" => object["name"],
|
|
|
|
"avatar" => avatar,
|
|
|
|
"banner" => banner,
|
|
|
|
"keys" => object["publicKey"]["publicKeyPem"],
|
|
|
|
"manually_approves_followers" => object["manuallyApprovesFollowers"]
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec model_to_as(ActorModel.t()) :: map
|
2019-05-22 14:12:11 +02:00
|
|
|
def model_to_as(%ActorModel{} = actor) do
|
|
|
|
%{
|
|
|
|
"type" => Atom.to_string(actor.type),
|
|
|
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"preferred_username" => actor.preferred_username,
|
|
|
|
"name" => actor.name,
|
|
|
|
"summary" => actor.summary,
|
|
|
|
"following" => ActorModel.build_url(actor.preferred_username, :following),
|
|
|
|
"followers" => ActorModel.build_url(actor.preferred_username, :followers),
|
|
|
|
"inbox" => ActorModel.build_url(actor.preferred_username, :inbox),
|
|
|
|
"outbox" => ActorModel.build_url(actor.preferred_username, :outbox),
|
|
|
|
"id" => ActorModel.build_url(actor.preferred_username, :page),
|
|
|
|
"url" => actor.url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|