2019-05-22 12:12:11 +00:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Converters.Actor do
|
|
|
|
@moduledoc """
|
|
|
|
Actor converter
|
|
|
|
|
|
|
|
This module allows to convert events from ActivityStream format to our own internal one, and back
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors.Actor, as: ActorModel
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter
|
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts an AP object data to our internal data structure
|
|
|
|
"""
|
|
|
|
@impl Converter
|
|
|
|
@spec as_to_model_data(map()) :: map()
|
|
|
|
def as_to_model_data(object) do
|
2019-09-04 16:24:31 +00: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 12:12:11 +00:00
|
|
|
%{
|
|
|
|
"type" => String.to_existing_atom(object["type"]),
|
2019-09-04 16:24:31 +00:00
|
|
|
"preferred_username" => object["preferredUsername"],
|
2019-05-22 12:12:11 +00:00
|
|
|
"summary" => object["summary"],
|
2019-09-09 07:55:08 +00:00
|
|
|
"url" => object["id"],
|
2019-09-04 16:24:31 +00:00
|
|
|
"name" => object["name"],
|
|
|
|
"avatar" => avatar,
|
|
|
|
"banner" => banner,
|
|
|
|
"keys" => object["publicKey"]["publicKeyPem"],
|
|
|
|
"manually_approves_followers" => object["manuallyApprovesFollowers"]
|
2019-05-22 12:12:11 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Convert an actor struct to an ActivityStream representation
|
|
|
|
"""
|
|
|
|
@impl Converter
|
|
|
|
@spec model_to_as(ActorModel.t()) :: map()
|
|
|
|
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
|
2019-07-30 14:40:59 +00:00
|
|
|
|
|
|
|
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Actors.Actor do
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(actor), to: ActorConverter
|
|
|
|
end
|