2020-08-27 09:53:24 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Actor do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the group-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Mobilizon.Users.Guards
|
2021-09-10 09:35:32 +00:00
|
|
|
alias Mobilizon.{Actors, Admin}
|
2020-08-27 09:53:24 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actions
|
2021-05-12 16:18:20 +00:00
|
|
|
alias Mobilizon.Service.Workers.Background
|
2020-08-27 09:53:24 +00:00
|
|
|
alias Mobilizon.Users.User
|
2021-07-27 17:47:54 +00:00
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 2]
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec refresh_profile(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t()}
|
2020-08-27 09:53:24 +00:00
|
|
|
def refresh_profile(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}})
|
|
|
|
when is_admin(role) do
|
|
|
|
case Actors.get_actor(id) do
|
2021-05-12 16:18:20 +00:00
|
|
|
%Actor{domain: domain, id: actor_id} = actor when not is_nil(domain) ->
|
|
|
|
Background.enqueue("refresh_profile", %{
|
|
|
|
"actor_id" => actor_id
|
|
|
|
})
|
|
|
|
|
2020-08-27 09:53:24 +00:00
|
|
|
{:ok, actor}
|
|
|
|
|
|
|
|
%Actor{} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Only remote profiles may be refreshed")}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
_ ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No profile found with this ID")}
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec suspend_profile(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t()}
|
2020-08-27 09:53:24 +00:00
|
|
|
def suspend_profile(_parent, %{id: id}, %{
|
2021-09-10 09:35:32 +00:00
|
|
|
context: %{
|
|
|
|
current_user: %User{role: role},
|
|
|
|
current_actor: %Actor{} = moderator_actor
|
|
|
|
}
|
2020-08-27 09:53:24 +00:00
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case Actors.get_actor_with_preload(id) do
|
2021-09-28 17:40:37 +00:00
|
|
|
# Suspend a group on this instance
|
|
|
|
%Actor{suspended: false, type: :Group, domain: nil} = actor ->
|
|
|
|
Logger.debug("We're suspending a group on this very instance")
|
|
|
|
Actions.Delete.delete(actor, moderator_actor, true, %{suspension: true})
|
|
|
|
Admin.log_action(moderator_actor, "suspend", actor)
|
|
|
|
{:ok, actor}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
# Delete a remote actor
|
|
|
|
%Actor{suspended: false, domain: domain} = actor when not is_nil(domain) ->
|
|
|
|
Logger.debug("We're just deleting a remote instance")
|
|
|
|
Actors.delete_actor(actor, suspension: true)
|
|
|
|
Admin.log_action(moderator_actor, "suspend", actor)
|
|
|
|
{:ok, actor}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
%Actor{suspended: false, domain: nil} ->
|
|
|
|
{:error, dgettext("errors", "No remote profile found with this ID")}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
%Actor{suspended: true} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile already suspended")}
|
2021-09-28 17:40:37 +00:00
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, dgettext("errors", "Profile not found")}
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def suspend_profile(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Only moderators and administrators can suspend a profile")}
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec unsuspend_profile(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t()}
|
2020-08-27 09:53:24 +00:00
|
|
|
def unsuspend_profile(_parent, %{id: id}, %{
|
2021-09-10 09:35:32 +00:00
|
|
|
context: %{
|
|
|
|
current_user: %User{role: role},
|
|
|
|
current_actor: %Actor{} = moderator_actor
|
|
|
|
}
|
2020-08-27 09:53:24 +00:00
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
2021-09-10 09:35:32 +00:00
|
|
|
with %Actor{suspended: true} = actor <-
|
2020-08-27 09:53:24 +00:00
|
|
|
Actors.get_actor_with_preload(id, true),
|
|
|
|
{:delete_tombstones, {_, nil}} <-
|
|
|
|
{:delete_tombstones, Mobilizon.Tombstone.delete_actor_tombstones(id)},
|
|
|
|
{:ok, %Actor{} = actor} <- Actors.update_actor(actor, %{suspended: false}),
|
2021-05-12 16:18:20 +00:00
|
|
|
:ok <- refresh_if_remote(actor),
|
2020-08-27 09:53:24 +00:00
|
|
|
{:ok, _} <- Admin.log_action(moderator_actor, "unsuspend", actor) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
{:moderator_actor, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No profile found for the moderator user")}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
nil ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No remote profile found with this ID")}
|
2020-08-27 09:53:24 +00:00
|
|
|
|
|
|
|
{:error, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Error while performing background task")}
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsuspend_profile(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Only moderators and administrators can unsuspend a profile")}
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|
|
|
|
|
2021-05-12 16:18:20 +00:00
|
|
|
@spec refresh_if_remote(Actor.t()) :: :ok
|
|
|
|
defp refresh_if_remote(%Actor{domain: nil}), do: :ok
|
|
|
|
|
|
|
|
defp refresh_if_remote(%Actor{id: actor_id}) do
|
|
|
|
Background.enqueue("refresh_profile", %{
|
|
|
|
"actor_id" => actor_id
|
|
|
|
})
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2020-08-27 09:53:24 +00:00
|
|
|
end
|