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,13 +74,11 @@ 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)},
{:delete, {:ok, %Member{} = member}} <- {:delete, Actors.delete_member(member)} do
Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit") Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit")
leave_data = %{ leave_data = %{
@ -95,10 +94,13 @@ defmodule Mobilizon.Federation.ActivityPub.Actions.Leave do
maybe_federate(activity) maybe_federate(activity)
maybe_relay_if_group_activity(activity) maybe_relay_if_group_activity(activity)
{:ok, activity, member} {:ok, activity, member}
end
else else
{:member, nil} -> {:error, :member_not_found} {:error, :is_not_only_admin}
{:is_not_only_admin, false} -> {:error, :is_not_only_admin} end
{:error, %Ecto.Changeset{} = err} -> {:error, err}
{:error, :member_not_found} ->
nil
end end
end end
end end