2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Member do
|
|
|
|
@moduledoc """
|
|
|
|
Member converter.
|
|
|
|
|
|
|
|
This module allows to convert members from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Actors.Member, as: MemberModel
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Utils
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
|
|
|
|
|
|
|
defimpl Convertible, for: MemberModel do
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Member, as: MemberConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(member), to: MemberConverter
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2020-11-06 10:34:32 +00:00
|
|
|
Convert an member struct to an ActivityStream representation.
|
2020-02-18 07:57:00 +00:00
|
|
|
"""
|
|
|
|
@spec model_to_as(MemberModel.t()) :: map
|
|
|
|
def model_to_as(%MemberModel{} = member) do
|
|
|
|
%{
|
|
|
|
"type" => "Member",
|
|
|
|
"id" => member.url,
|
|
|
|
"actor" => member.actor.url,
|
|
|
|
"object" => member.parent.url,
|
2020-08-19 09:28:23 +00:00
|
|
|
"role" => to_string(member.role)
|
2020-02-18 07:57:00 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_to_model_data(%{
|
|
|
|
"type" => "Member",
|
|
|
|
"actor" => actor,
|
|
|
|
"object" => group,
|
|
|
|
"role" => role,
|
|
|
|
"id" => url
|
|
|
|
}) do
|
|
|
|
with {:ok, %Actor{id: group_id}} <- get_actor(group),
|
|
|
|
{:ok, %Actor{id: actor_id}} <- get_actor(actor) do
|
|
|
|
%{
|
|
|
|
url: url,
|
|
|
|
actor_id: actor_id,
|
|
|
|
parent_id: group_id,
|
|
|
|
role: role
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_actor(String.t() | map() | nil) :: {:ok, Actor.t()} | {:error, String.t()}
|
|
|
|
defp get_actor(nil), do: {:error, "nil property found for actor data"}
|
2021-04-22 10:17:56 +00:00
|
|
|
|
|
|
|
defp get_actor(actor),
|
|
|
|
do: actor |> Utils.get_url() |> ActivityPubActor.get_or_fetch_actor_by_url()
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|