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()}
| {:error, :is_only_organizer | :participant_not_found | Ecto.Changeset.t()}
@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 \\ %{})
@doc """
@ -73,13 +74,11 @@ defmodule Mobilizon.Federation.ActivityPub.Actions.Leave do
local,
additional
) do
with {:member, {:ok, %Member{id: member_id} = member}} <-
{:member, Actors.get_member(actor_id, group_id)},
{:is_not_only_admin, true} <-
{:is_not_only_admin,
Map.get(additional, :force_member_removal, false) ||
!Actors.is_only_administrator?(member_id, group_id)},
{:delete, {:ok, %Member{} = member}} <- {:delete, Actors.delete_member(member)} do
case Actors.get_member(actor_id, group_id) do
{:ok, %Member{id: member_id} = member} ->
if Map.get(additional, :force_member_removal, false) ||
!Actors.is_only_administrator?(member_id, group_id) do
with {:ok, %Member{} = member} <- Actors.delete_member(member) do
Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit")
leave_data = %{
@ -95,10 +94,13 @@ defmodule Mobilizon.Federation.ActivityPub.Actions.Leave do
maybe_federate(activity)
maybe_relay_if_group_activity(activity)
{:ok, activity, member}
end
else
{:member, nil} -> {:error, :member_not_found}
{:is_not_only_admin, false} -> {:error, :is_not_only_admin}
{:error, %Ecto.Changeset{} = err} -> {:error, err}
{:error, :is_not_only_admin}
end
{:error, :member_not_found} ->
nil
end
end
end