debianize-mobilizon/lib/federation/activity_stream/converter/member.ex
Thomas Citharel 7c11807c14
Allow to join an open group
Also:

* Refactor interacting with a remote event so that you can interact with
  a remote group as well
* Add a setting for group admins to pick between an open and invite-only
  group
* Fix new groups without posts/todos/resources/events/conversations URL
  set
* Repair local groups that haven't got their
  posts/todos/resources/events/conversations URL set
* Add a scheduled job to refresh remote groups every hour
* Add a user setting to pick when to receive notifications when there's
  new members to approve (will be used when this feature is available)
* Fix pagination for members

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-11-06 15:19:36 +01:00

58 lines
1.6 KiB
Elixir

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
alias Mobilizon.Federation.ActivityPub
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 """
Convert an member struct to an ActivityStream representation.
"""
@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,
"role" => to_string(member.role)
}
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"}
defp get_actor(actor), do: actor |> Utils.get_url() |> ActivityPub.get_or_fetch_actor_by_url()
end