Fix an issue when leaving a group

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-09-29 18:22:32 +02:00
parent 120d79d580
commit c5624ae33d
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773

View file

@ -21,7 +21,8 @@ defmodule Mobilizon.Federation.ActivityPub.Actions.Leave do
{:ok, Activity.t(), Participant.t()} {:ok, Activity.t(), Participant.t()}
| {:error, :is_only_organizer | :participant_not_found | Ecto.Changeset.t()} | {:error, :is_only_organizer | :participant_not_found | Ecto.Changeset.t()}
@spec leave(Actor.t(), Actor.t(), boolean, map) :: @spec leave(Actor.t(), Actor.t(), boolean, map) ::
{:ok, Activity.t(), Member.t()} | {:error, atom() | Ecto.Changeset.t()} {:ok, Activity.t(), Member.t()}
| {:error, :is_not_only_admin | :member_not_found | Ecto.Changeset.t()}
def leave(object, actor, local \\ true, additional \\ %{}) def leave(object, actor, local \\ true, additional \\ %{})
@doc """ @doc """
@ -73,32 +74,33 @@ defmodule Mobilizon.Federation.ActivityPub.Actions.Leave do
local, local,
additional additional
) do ) do
with {:member, {:ok, %Member{id: member_id} = member}} <- case Actors.get_member(actor_id, group_id) do
{:member, Actors.get_member(actor_id, group_id)}, {:ok, %Member{id: member_id} = member} ->
{:is_not_only_admin, true} <- if Map.get(additional, :force_member_removal, false) ||
{:is_not_only_admin, !Actors.is_only_administrator?(member_id, group_id) do
Map.get(additional, :force_member_removal, false) || with {:ok, %Member{} = member} <- Actors.delete_member(member) do
!Actors.is_only_administrator?(member_id, group_id)}, Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit")
{:delete, {:ok, %Member{} = member}} <- {:delete, Actors.delete_member(member)} do
Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit")
leave_data = %{ leave_data = %{
"to" => [group_members_url], "to" => [group_members_url],
"cc" => [group_url], "cc" => [group_url],
"attributedTo" => group_url, "attributedTo" => group_url,
"type" => "Leave", "type" => "Leave",
"actor" => actor_url, "actor" => actor_url,
"object" => group_url "object" => group_url
} }
{:ok, activity} = create_activity(leave_data, local) {:ok, activity} = create_activity(leave_data, local)
maybe_federate(activity) maybe_federate(activity)
maybe_relay_if_group_activity(activity) maybe_relay_if_group_activity(activity)
{:ok, activity, member} {:ok, activity, member}
else end
{:member, nil} -> {:error, :member_not_found} else
{:is_not_only_admin, false} -> {:error, :is_not_only_admin} {:error, :is_not_only_admin}
{:error, %Ecto.Changeset{} = err} -> {:error, err} end
{:error, :member_not_found} ->
nil
end end
end end
end end