2021-09-28 19:40:37 +02:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Actions.Remove do
|
|
|
|
@moduledoc """
|
|
|
|
Remove things
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Activity
|
2021-11-10 16:36:32 +01:00
|
|
|
alias Mobilizon.Web.Email.Member, as: EmailMember
|
2021-09-28 19:40:37 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
import Mobilizon.Federation.ActivityPub.Utils,
|
|
|
|
only: [
|
|
|
|
create_activity: 2,
|
|
|
|
maybe_federate: 1,
|
|
|
|
maybe_relay_if_group_activity: 1
|
|
|
|
]
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Remove an activity, using an activity of type `Remove`
|
|
|
|
"""
|
|
|
|
@spec remove(Member.t(), Actor.t(), Actor.t(), boolean, map) ::
|
|
|
|
{:ok, Activity.t(), Member.t()} | {:error, :member_not_found | Ecto.Changeset.t()}
|
|
|
|
def remove(
|
2021-11-12 15:42:52 +01:00
|
|
|
%Member{id: member_id},
|
2021-09-28 19:40:37 +02:00
|
|
|
%Actor{type: :Group, url: group_url, members_url: group_members_url},
|
|
|
|
%Actor{url: moderator_url} = moderator,
|
|
|
|
local,
|
|
|
|
_additional \\ %{}
|
|
|
|
) do
|
2021-11-12 15:42:52 +01:00
|
|
|
with %Member{actor: %Actor{url: actor_url}} = member <- Actors.get_member(member_id),
|
|
|
|
{:ok, %Member{}} <- Actors.delete_member(member) do
|
2021-09-28 19:40:37 +02:00
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
moderator: moderator,
|
|
|
|
subject: "member_removed"
|
|
|
|
)
|
|
|
|
|
2021-11-12 15:42:52 +01:00
|
|
|
Cachex.del(:activity_pub, "member_#{member_id}")
|
|
|
|
|
2021-11-10 16:36:32 +01:00
|
|
|
EmailMember.send_notification_to_removed_member(member)
|
2021-09-28 19:40:37 +02:00
|
|
|
|
|
|
|
remove_data = %{
|
2021-11-12 15:42:52 +01:00
|
|
|
"to" => [actor_url, group_members_url],
|
2021-09-28 19:40:37 +02:00
|
|
|
"type" => "Remove",
|
|
|
|
"actor" => moderator_url,
|
|
|
|
"object" => member.url,
|
|
|
|
"origin" => group_url
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, activity} = create_activity(remove_data, local)
|
|
|
|
maybe_federate(activity)
|
|
|
|
maybe_relay_if_group_activity(activity)
|
|
|
|
{:ok, activity, member}
|
|
|
|
else
|
|
|
|
nil -> {:error, :member_not_found}
|
|
|
|
{:error, %Ecto.Changeset{} = err} -> {:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|