2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Admin do
|
2019-07-23 13:49:22 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the report-related GraphQL calls.
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
import Mobilizon.Users.Guards
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.{Actors, Admin, Config, Events}
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Admin.{ActionLog, Setting}
|
2020-10-07 15:37:23 +02:00
|
|
|
alias Mobilizon.Cldr.Language
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Config
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Events.Event
|
2020-07-07 15:51:42 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Service.Statistics
|
2019-12-03 11:29:51 +01:00
|
|
|
alias Mobilizon.Storage.Page
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-06-11 19:13:21 +02:00
|
|
|
require Logger
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2019-09-22 13:41:24 +02:00
|
|
|
def list_action_logs(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
when is_moderator(role) do
|
|
|
|
with action_logs <- Mobilizon.Admin.list_action_logs(page, limit) do
|
|
|
|
action_logs =
|
2019-09-22 13:41:24 +02:00
|
|
|
action_logs
|
|
|
|
|> Enum.map(fn %ActionLog{
|
|
|
|
target_type: target_type,
|
|
|
|
action: action,
|
|
|
|
actor: actor,
|
|
|
|
id: id,
|
|
|
|
inserted_at: inserted_at
|
|
|
|
} = action_log ->
|
2019-09-09 09:31:08 +02:00
|
|
|
with data when is_map(data) <-
|
|
|
|
transform_action_log(String.to_existing_atom(target_type), action, action_log) do
|
2019-09-22 13:41:24 +02:00
|
|
|
Map.merge(data, %{actor: actor, id: id, inserted_at: inserted_at})
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end)
|
2019-09-09 09:31:08 +02:00
|
|
|
|> Enum.filter(& &1)
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
{:ok, action_logs}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_action_logs(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to list action logs")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_action_log(
|
2019-09-09 09:31:08 +02:00
|
|
|
Report,
|
|
|
|
:update,
|
2019-07-23 13:49:22 +02:00
|
|
|
%ActionLog{} = action_log
|
|
|
|
) do
|
2019-09-09 09:31:08 +02:00
|
|
|
with %Report{} = report <- Mobilizon.Reports.get_report(action_log.target_id) do
|
|
|
|
action =
|
|
|
|
case action_log do
|
|
|
|
%ActionLog{changes: %{"status" => "closed"}} -> :report_update_closed
|
|
|
|
%ActionLog{changes: %{"status" => "open"}} -> :report_update_opened
|
|
|
|
%ActionLog{changes: %{"status" => "resolved"}} -> :report_update_resolved
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
%{
|
2019-09-09 09:31:08 +02:00
|
|
|
action: action,
|
2019-07-23 13:49:22 +02:00
|
|
|
object: report
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
defp transform_action_log(Note, :create, %ActionLog{changes: changes}) do
|
2019-07-23 13:49:22 +02:00
|
|
|
%{
|
2019-09-09 09:31:08 +02:00
|
|
|
action: :note_creation,
|
2019-07-23 13:49:22 +02:00
|
|
|
object: convert_changes_to_struct(Note, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
defp transform_action_log(Note, :delete, %ActionLog{changes: changes}) do
|
2019-07-23 13:49:22 +02:00
|
|
|
%{
|
2019-09-09 09:31:08 +02:00
|
|
|
action: :note_deletion,
|
2019-07-23 13:49:22 +02:00
|
|
|
object: convert_changes_to_struct(Note, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
defp transform_action_log(Event, :delete, %ActionLog{changes: changes}) do
|
2019-09-09 09:31:08 +02:00
|
|
|
%{
|
|
|
|
action: :event_deletion,
|
|
|
|
object: convert_changes_to_struct(Event, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
defp transform_action_log(Comment, :delete, %ActionLog{changes: changes}) do
|
2019-11-15 18:36:47 +01:00
|
|
|
%{
|
|
|
|
action: :comment_deletion,
|
|
|
|
object: convert_changes_to_struct(Comment, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
defp transform_action_log(Actor, :suspend, %ActionLog{changes: changes}) do
|
|
|
|
%{
|
|
|
|
action: :actor_suspension,
|
|
|
|
object: convert_changes_to_struct(Actor, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transform_action_log(Actor, :unsuspend, %ActionLog{changes: changes}) do
|
|
|
|
%{
|
|
|
|
action: :actor_unsuspension,
|
|
|
|
object: convert_changes_to_struct(Actor, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-15 19:41:11 +02:00
|
|
|
defp transform_action_log(User, :delete, %ActionLog{changes: changes}) do
|
|
|
|
%{
|
|
|
|
action: :user_deletion,
|
|
|
|
object: convert_changes_to_struct(User, changes)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
# Changes are stored as %{"key" => "value"} so we need to convert them back as struct
|
2019-09-09 09:31:08 +02:00
|
|
|
defp convert_changes_to_struct(struct, %{"report_id" => _report_id} = changes) do
|
|
|
|
with data <- for({key, val} <- changes, into: %{}, do: {String.to_atom(key), val}),
|
|
|
|
data <- Map.put(data, :report, Mobilizon.Reports.get_report(data.report_id)) do
|
|
|
|
struct(struct, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
defp convert_changes_to_struct(struct, changes) do
|
2020-08-18 17:22:06 +02:00
|
|
|
with changeset <- struct.__changeset__,
|
|
|
|
data <-
|
|
|
|
for(
|
|
|
|
{key, val} <- changes,
|
|
|
|
into: %{},
|
|
|
|
do: {String.to_atom(key), process_eventual_type(changeset, key, val)}
|
|
|
|
) do
|
2019-09-09 09:31:08 +02:00
|
|
|
struct(struct, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-18 17:22:06 +02:00
|
|
|
# datetimes are not unserialized as DateTime/NaiveDateTime so we do it manually with changeset data
|
|
|
|
defp process_eventual_type(changeset, key, val) do
|
|
|
|
cond do
|
|
|
|
changeset[String.to_atom(key)] == :utc_datetime and not is_nil(val) ->
|
|
|
|
{:ok, datetime, _} = DateTime.from_iso8601(val)
|
|
|
|
datetime
|
|
|
|
|
|
|
|
changeset[String.to_atom(key)] == :naive_datetime and not is_nil(val) ->
|
|
|
|
{:ok, datetime} = NaiveDateTime.from_iso8601(val)
|
|
|
|
datetime
|
|
|
|
|
|
|
|
true ->
|
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 15:37:23 +02:00
|
|
|
def get_list_of_languages(_parent, _args, _resolution) do
|
|
|
|
locale = Gettext.get_locale()
|
|
|
|
|
|
|
|
case Language.known_languages(locale) do
|
|
|
|
data when is_map(data) ->
|
|
|
|
data = Enum.map(data, fn {code, elem} -> %{code: code, name: elem.standard} end)
|
|
|
|
{:ok, data}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def get_dashboard(_parent, _args, %{context: %{current_user: %User{role: role}}})
|
2019-09-09 09:31:08 +02:00
|
|
|
when is_admin(role) do
|
|
|
|
last_public_event_published =
|
|
|
|
case Events.list_events(1, 1, :inserted_at, :desc) do
|
|
|
|
[event | _] -> event
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
2020-10-15 11:04:05 +02:00
|
|
|
last_group_created =
|
|
|
|
case Actors.list_actors(:Group) do
|
|
|
|
%Page{elements: [group | _]} -> group
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
2019-09-09 09:31:08 +02:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
number_of_users: Statistics.get_cached_value(:local_users),
|
|
|
|
number_of_events: Statistics.get_cached_value(:local_events),
|
2020-10-15 11:04:05 +02:00
|
|
|
number_of_groups: Statistics.get_cached_value(:local_groups),
|
2019-09-09 09:31:08 +02:00
|
|
|
number_of_comments: Statistics.get_cached_value(:local_comments),
|
2020-10-15 11:04:05 +02:00
|
|
|
number_of_confirmed_participations_to_local_events:
|
|
|
|
Statistics.get_cached_value(:confirmed_participations_to_local_events),
|
2019-09-09 09:31:08 +02:00
|
|
|
number_of_reports: Mobilizon.Reports.count_opened_reports(),
|
2020-10-15 11:04:05 +02:00
|
|
|
number_of_followers: Statistics.get_cached_value(:instance_followers),
|
|
|
|
number_of_followings: Statistics.get_cached_value(:instance_followings),
|
|
|
|
last_public_event_published: last_public_event_published,
|
|
|
|
last_group_created: last_group_created
|
2019-09-09 09:31:08 +02:00
|
|
|
}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_dashboard(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"You need to be logged-in and an administrator to access dashboard statistics"
|
|
|
|
)}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
2019-12-03 11:29:51 +01:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
def get_settings(_parent, _args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_admin(role) do
|
2020-07-07 15:51:42 +02:00
|
|
|
{:ok, Config.admin_settings()}
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_settings(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "You need to be logged-in and an administrator to access admin settings")}
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def save_settings(_parent, args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_admin(role) do
|
2020-07-07 15:51:42 +02:00
|
|
|
with {:ok, res} <- Admin.save_settings("instance", args),
|
|
|
|
res <-
|
|
|
|
res
|
|
|
|
|> Enum.map(fn {key, %Setting{value: value}} ->
|
2020-10-07 15:37:23 +02:00
|
|
|
{key, Admin.get_setting_value(value)}
|
2020-07-07 15:51:42 +02:00
|
|
|
end)
|
|
|
|
|> Enum.into(%{}),
|
|
|
|
:ok <- eventually_update_instance_actor(res) do
|
2019-12-20 13:04:34 +01:00
|
|
|
Config.clear_config_cache()
|
2020-07-07 15:51:42 +02:00
|
|
|
Cachex.put(:config, :admin_config, res)
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
{:ok, res}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_settings(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "You need to be logged-in and an administrator to save admin settings")}
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def list_relay_followers(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
when is_admin(role) do
|
|
|
|
with %Actor{} = relay_actor <- Relay.get_actor() do
|
|
|
|
%Page{} =
|
|
|
|
page = Actors.list_external_followers_for_actor_paginated(relay_actor, page, limit)
|
|
|
|
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def list_relay_followings(
|
|
|
|
_parent,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
when is_admin(role) do
|
|
|
|
with %Actor{} = relay_actor <- Relay.get_actor() do
|
|
|
|
%Page{} =
|
|
|
|
page = Actors.list_external_followings_for_actor_paginated(relay_actor, page, limit)
|
|
|
|
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_relay(_parent, %{address: address}, %{context: %{current_user: %User{role: role}}})
|
|
|
|
when is_admin(role) do
|
|
|
|
case Relay.follow(address) do
|
|
|
|
{:ok, _activity, follow} ->
|
|
|
|
{:ok, follow}
|
|
|
|
|
2020-09-02 08:59:59 +02:00
|
|
|
{:error, err} when is_binary(err) ->
|
2019-12-03 11:29:51 +01:00
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_relay(_parent, %{address: address}, %{context: %{current_user: %User{role: role}}})
|
|
|
|
when is_admin(role) do
|
|
|
|
case Relay.unfollow(address) do
|
|
|
|
{:ok, _activity, follow} ->
|
|
|
|
{:ok, follow}
|
|
|
|
|
|
|
|
{:error, {:error, err}} when is_bitstring(err) ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def accept_subscription(
|
|
|
|
_parent,
|
|
|
|
%{address: address},
|
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
when is_admin(role) do
|
|
|
|
case Relay.accept(address) do
|
|
|
|
{:ok, _activity, follow} ->
|
|
|
|
{:ok, follow}
|
|
|
|
|
|
|
|
{:error, {:error, err}} when is_bitstring(err) ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def reject_subscription(
|
|
|
|
_parent,
|
|
|
|
%{address: address},
|
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-12-03 11:29:51 +01:00
|
|
|
when is_admin(role) do
|
|
|
|
case Relay.reject(address) do
|
|
|
|
{:ok, _activity, follow} ->
|
|
|
|
{:ok, follow}
|
|
|
|
|
|
|
|
{:error, {:error, err}} when is_bitstring(err) ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
2020-07-07 15:51:42 +02:00
|
|
|
|
|
|
|
@spec eventually_update_instance_actor(map()) :: :ok
|
|
|
|
defp eventually_update_instance_actor(admin_setting_args) do
|
|
|
|
args = %{}
|
|
|
|
new_instance_description = Map.get(admin_setting_args, :instance_description)
|
|
|
|
new_instance_name = Map.get(admin_setting_args, :instance_name)
|
|
|
|
|
|
|
|
%{
|
|
|
|
instance_description: old_instance_description,
|
|
|
|
instance_name: old_instance_name
|
|
|
|
} = Config.admin_settings()
|
|
|
|
|
|
|
|
args =
|
|
|
|
if not is_nil(new_instance_description) &&
|
|
|
|
new_instance_description != old_instance_description,
|
|
|
|
do: Map.put(args, :summary, new_instance_description),
|
|
|
|
else: args
|
|
|
|
|
|
|
|
args =
|
|
|
|
if not is_nil(new_instance_name) && new_instance_name != old_instance_name,
|
|
|
|
do: Map.put(args, :name, new_instance_name),
|
|
|
|
else: args
|
|
|
|
|
|
|
|
with {:changes, true} <- {:changes, args != %{}},
|
|
|
|
%Actor{} = instance_actor <- Relay.get_actor(),
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, _activity, _actor} <- ActivityPub.update(instance_actor, args, true) do
|
2020-07-07 15:51:42 +02:00
|
|
|
:ok
|
|
|
|
else
|
|
|
|
{:changes, false} ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
err ->
|
|
|
|
err
|
|
|
|
end
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|