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
|
|
|
|
2022-01-13 15:04:43 +01:00
|
|
|
alias Mobilizon.{Actors, Admin, Config, Events, Instances, Users}
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Actors.{Actor, Follower}
|
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
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Relay}
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2022-01-14 18:10:50 +01:00
|
|
|
alias Mobilizon.Service.Auth.Authenticator
|
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
|
2022-01-14 18:10:50 +01:00
|
|
|
alias Mobilizon.Web.Email
|
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
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_action_logs(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(ActionLog.t())} | {:error, String.t()}
|
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
|
2021-04-27 17:53:11 +02:00
|
|
|
with %Page{elements: action_logs, total: total} <-
|
|
|
|
Mobilizon.Admin.list_action_logs(page, limit) do
|
2019-07-23 13:49:22 +02:00
|
|
|
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 ->
|
2021-09-28 19:40:37 +02:00
|
|
|
target_type
|
|
|
|
|> String.to_existing_atom()
|
|
|
|
|> transform_action_log(action, action_log)
|
2021-11-04 18:34:43 +01:00
|
|
|
|> add_extra_data(actor, id, inserted_at)
|
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
|
|
|
|
2021-04-27 17:53:11 +02:00
|
|
|
{:ok, %Page{elements: action_logs, total: total}}
|
2019-07-23 13:49:22 +02:00
|
|
|
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
|
|
|
|
|
2021-11-04 18:34:43 +01:00
|
|
|
defp add_extra_data(nil, _actor, _id, _inserted_at), do: nil
|
|
|
|
|
|
|
|
defp add_extra_data(map, actor, id, inserted_at) do
|
|
|
|
Map.merge(map, %{actor: actor, id: id, inserted_at: inserted_at})
|
|
|
|
end
|
|
|
|
|
2022-09-26 10:29:37 +02:00
|
|
|
@spec transform_action_log(module(), atom(), ActionLog.t()) :: map() | nil
|
2019-07-23 13:49:22 +02:00
|
|
|
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
|
|
|
|
|
2022-09-26 10:29:37 +02:00
|
|
|
defp transform_action_log(_, _, _), do: nil
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
# Changes are stored as %{"key" => "value"} so we need to convert them back as struct
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec convert_changes_to_struct(module(), map()) :: struct()
|
2019-09-09 09:31:08 +02:00
|
|
|
defp convert_changes_to_struct(struct, %{"report_id" => _report_id} = changes) do
|
2021-11-04 18:34:43 +01:00
|
|
|
data = for({key, val} <- changes, into: %{}, do: {String.to_existing_atom(key), val})
|
|
|
|
data = Map.put(data, :report, Mobilizon.Reports.get_report(data.report_id))
|
|
|
|
struct(struct, data)
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
defp convert_changes_to_struct(struct, changes) do
|
2021-11-04 18:34:43 +01:00
|
|
|
changeset = struct.__changeset__
|
|
|
|
|
|
|
|
data =
|
|
|
|
for(
|
|
|
|
{key, val} <- changes,
|
|
|
|
into: %{},
|
|
|
|
do: {String.to_existing_atom(key), process_eventual_type(changeset, key, val)}
|
|
|
|
)
|
|
|
|
|
|
|
|
struct(struct, data)
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
|
|
|
|
2020-08-18 17:22:06 +02:00
|
|
|
# datetimes are not unserialized as DateTime/NaiveDateTime so we do it manually with changeset data
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec process_eventual_type(Ecto.Changeset.t(), String.t(), String.t() | nil) ::
|
|
|
|
DateTime.t() | NaiveDateTime.t() | any()
|
2020-08-18 17:22:06 +02:00
|
|
|
defp process_eventual_type(changeset, key, val) do
|
|
|
|
cond do
|
2021-11-04 18:34:43 +01:00
|
|
|
changeset[String.to_existing_atom(key)] == Mobilizon.Actors.ActorType and not is_nil(val) ->
|
|
|
|
String.to_existing_atom(val)
|
|
|
|
|
2021-01-22 18:16:00 +01:00
|
|
|
changeset[String.to_existing_atom(key)] == :utc_datetime and not is_nil(val) ->
|
2020-08-18 17:22:06 +02:00
|
|
|
{:ok, datetime, _} = DateTime.from_iso8601(val)
|
|
|
|
datetime
|
|
|
|
|
2021-01-22 18:16:00 +01:00
|
|
|
changeset[String.to_existing_atom(key)] == :naive_datetime and not is_nil(val) ->
|
2020-08-18 17:22:06 +02:00
|
|
|
{:ok, datetime} = NaiveDateTime.from_iso8601(val)
|
|
|
|
datetime
|
|
|
|
|
|
|
|
true ->
|
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_list_of_languages(any(), any(), any()) :: {:ok, String.t()} | {:error, any()}
|
2020-11-02 11:22:54 +01:00
|
|
|
def get_list_of_languages(_parent, %{codes: codes}, _resolution) when is_list(codes) do
|
2022-03-21 17:50:38 +01:00
|
|
|
locale = Mobilizon.Cldr.locale_or_default(Gettext.get_locale())
|
2020-11-02 11:22:54 +01:00
|
|
|
|
2022-03-23 12:33:04 +01:00
|
|
|
case Language.known_languages(String.to_existing_atom(locale)) do
|
2020-11-02 11:22:54 +01:00
|
|
|
data when is_map(data) ->
|
|
|
|
data
|
|
|
|
|> Enum.map(fn {code, elem} -> %{code: code, name: elem.standard} end)
|
|
|
|
|> Enum.filter(fn %{code: code, name: _name} -> code in codes end)
|
|
|
|
|> (&{:ok, &1}).()
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 15:37:23 +02:00
|
|
|
def get_list_of_languages(_parent, _args, _resolution) do
|
|
|
|
locale = Gettext.get_locale()
|
|
|
|
|
2022-03-23 12:33:04 +01:00
|
|
|
case Language.known_languages(String.to_existing_atom(locale)) do
|
2020-10-07 15:37:23 +02:00
|
|
|
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
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_dashboard(any(), any(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, map()} | {:error, String.t()}
|
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
|
2020-12-09 17:55:38 +01:00
|
|
|
%Page{elements: [event | _]} -> event
|
2019-09-09 09:31:08 +02:00
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
|
|
|
{: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,
|
2022-04-22 12:02:14 +02:00
|
|
|
last_group_created: Actors.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
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_settings(any(), any(), Absinthe.Resolution.t()) :: {:ok, map()} | {:error, String.t()}
|
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
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec save_settings(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, map()} | {:error, String.t()}
|
2019-12-20 13:04:34 +01:00
|
|
|
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
|
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
@spec update_user(any, map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:error, :invalid_argument | :user_not_found | binary | Ecto.Changeset.t()}
|
|
|
|
| {:ok, Mobilizon.Users.User.t()}
|
2022-01-13 15:04:43 +01:00
|
|
|
def update_user(_parent, %{id: id, notify: notify} = args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_admin(role) do
|
|
|
|
case Users.get_user(id) do
|
|
|
|
nil ->
|
|
|
|
{:error, :user_not_found}
|
|
|
|
|
|
|
|
%User{} = user ->
|
|
|
|
case args |> Map.drop([:notify, :id]) |> Map.keys() do
|
|
|
|
[] ->
|
|
|
|
{:error, :invalid_argument}
|
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
[change | _] ->
|
2022-01-13 15:04:43 +01:00
|
|
|
case change do
|
|
|
|
:email -> change_email(user, Map.get(args, :email), notify)
|
|
|
|
:role -> change_role(user, Map.get(args, :role), notify)
|
|
|
|
:confirmed -> confirm_user(user, Map.get(args, :confirmed), notify)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_user(_parent, _args, _resolution) do
|
|
|
|
{:error,
|
|
|
|
dgettext("errors", "You need to be logged-in and an administrator to edit an user's details")}
|
|
|
|
end
|
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
@spec change_email(User.t(), String.t(), boolean()) :: {:ok, User.t()} | {:error, String.t()}
|
2022-01-13 15:04:43 +01:00
|
|
|
defp change_email(%User{email: old_email} = user, new_email, notify) do
|
|
|
|
if Authenticator.can_change_email?(user) do
|
|
|
|
if new_email != old_email do
|
|
|
|
if Email.Checker.valid?(new_email) do
|
2022-01-14 18:10:50 +01:00
|
|
|
case Users.update_user(user, %{email: new_email}) do
|
|
|
|
{:ok, %User{} = updated_user} ->
|
|
|
|
if notify do
|
|
|
|
updated_user
|
|
|
|
|> Email.Admin.user_email_change_old(old_email)
|
2022-04-05 12:16:22 +02:00
|
|
|
|> Email.Mailer.send_email()
|
2022-01-13 15:04:43 +01:00
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
updated_user
|
|
|
|
|> Email.Admin.user_email_change_new(old_email)
|
2022-04-05 12:16:22 +02:00
|
|
|
|> Email.Mailer.send_email()
|
2022-01-14 18:10:50 +01:00
|
|
|
end
|
2022-01-13 15:04:43 +01:00
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
{:ok, updated_user}
|
2022-01-13 15:04:43 +01:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
{:error, dgettext("errors", "Failed to update user email")}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "The new email doesn't seem to be valid")}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "The new email must be different")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-18 14:38:57 +02:00
|
|
|
@spec change_role(User.t(), atom(), boolean()) ::
|
2022-01-14 18:10:50 +01:00
|
|
|
{:ok, User.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
2022-01-13 15:04:43 +01:00
|
|
|
defp change_role(%User{role: old_role} = user, new_role, notify) do
|
|
|
|
if old_role != new_role do
|
2022-01-14 18:10:50 +01:00
|
|
|
with {:ok, %User{} = user} <- Users.update_user(user, %{role: new_role}) do
|
|
|
|
if notify do
|
|
|
|
user
|
|
|
|
|> Email.Admin.user_role_change(old_role)
|
2022-04-05 12:16:22 +02:00
|
|
|
|> Email.Mailer.send_email()
|
2022-01-14 18:10:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "The new role must be different")}
|
2022-01-13 15:04:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
@spec confirm_user(User.t(), boolean(), boolean()) ::
|
|
|
|
{:ok, User.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
|
|
|
defp confirm_user(%User{confirmed_at: nil} = user, true, notify) do
|
|
|
|
with {:ok, %User{} = user} <-
|
|
|
|
Users.update_user(user, %{
|
|
|
|
confirmed_at: DateTime.utc_now(),
|
|
|
|
confirmation_sent_at: nil,
|
|
|
|
confirmation_token: nil
|
|
|
|
}) do
|
|
|
|
if notify do
|
|
|
|
user
|
|
|
|
|> Email.Admin.user_confirmation()
|
2022-04-05 12:16:22 +02:00
|
|
|
|> Email.Mailer.send_email()
|
2022-01-14 18:10:50 +01:00
|
|
|
end
|
2022-01-13 15:04:43 +01:00
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
{:ok, user}
|
|
|
|
end
|
|
|
|
end
|
2022-01-13 15:04:43 +01:00
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
defp confirm_user(%User{confirmed_at: %DateTime{}} = _user, true, _notify) do
|
|
|
|
{:error, dgettext("errors", "Can't confirm an already confirmed user")}
|
|
|
|
end
|
2022-01-13 15:04:43 +01:00
|
|
|
|
2022-01-14 18:10:50 +01:00
|
|
|
defp confirm_user(_user, _confirm, _notify) do
|
|
|
|
{:error, dgettext("errors", "Deconfirming users is not supported")}
|
2022-01-13 15:04:43 +01:00
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_relay_followers(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Follower.t())} | {:error, :unauthorized | :unauthenticated}
|
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-11-02 18:11:27 +01:00
|
|
|
def list_relay_followers(_parent, _args, %{context: %{current_user: %User{}}}) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_relay_followers(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_relay_followings(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Follower.t())} | {:error, :unauthorized | :unauthenticated}
|
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
|
|
|
|
|
2020-11-02 18:11:27 +01:00
|
|
|
def list_relay_followings(_parent, _args, %{context: %{current_user: %User{}}}) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_relay_followings(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
2021-12-28 11:42:08 +01:00
|
|
|
def get_instances(
|
|
|
|
_parent,
|
|
|
|
args,
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
when is_admin(role) do
|
|
|
|
{:ok,
|
|
|
|
Instances.instances(
|
|
|
|
args
|
|
|
|
|> Keyword.new()
|
|
|
|
|> Keyword.take([
|
|
|
|
:page,
|
|
|
|
:limit,
|
|
|
|
:order_by,
|
|
|
|
:direction,
|
|
|
|
:filter_domain,
|
|
|
|
:filter_follow_status,
|
|
|
|
:filter_suspend_status
|
|
|
|
])
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_instances(_parent, _args, %{context: %{current_user: %User{}}}) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_instances(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
2022-09-26 10:20:51 +02:00
|
|
|
@spec get_instance(any, map(), Absinthe.Resolution.t()) ::
|
2022-09-27 10:50:14 +02:00
|
|
|
{:error, :unauthenticated | :unauthorized | :not_found}
|
|
|
|
| {:ok, Mobilizon.Instances.Instance.t()}
|
2021-12-28 11:42:08 +01:00
|
|
|
def get_instance(_parent, %{domain: domain}, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_admin(role) do
|
2022-05-06 16:58:33 +02:00
|
|
|
remote_relay = Actors.get_relay(domain)
|
2021-12-28 11:42:08 +01:00
|
|
|
local_relay = Relay.get_actor()
|
|
|
|
|
|
|
|
result = %{
|
2022-05-06 17:21:11 +02:00
|
|
|
has_relay: !is_nil(remote_relay),
|
|
|
|
relay_address:
|
|
|
|
if(is_nil(remote_relay),
|
|
|
|
do: nil,
|
|
|
|
else: "#{remote_relay.preferred_username}@#{remote_relay.domain}"
|
|
|
|
),
|
2021-12-28 11:42:08 +01:00
|
|
|
follower_status: follow_status(remote_relay, local_relay),
|
|
|
|
followed_status: follow_status(local_relay, remote_relay)
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:20:51 +02:00
|
|
|
case Instances.instance(domain) do
|
|
|
|
nil -> {:error, :not_found}
|
|
|
|
instance -> {:ok, Map.merge(instance, result)}
|
|
|
|
end
|
2021-12-28 11:42:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_instance(_parent, _args, %{context: %{current_user: %User{}}}) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_instance(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
2022-05-06 13:06:41 +02:00
|
|
|
@spec create_instance(any, map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:error, atom() | binary()}
|
|
|
|
| {:ok, Mobilizon.Instances.Instance.t()}
|
2021-12-28 11:42:08 +01:00
|
|
|
def create_instance(
|
|
|
|
parent,
|
|
|
|
%{domain: domain} = args,
|
|
|
|
%{context: %{current_user: %User{role: role}}} = resolution
|
|
|
|
)
|
|
|
|
when is_admin(role) do
|
2022-05-06 13:06:41 +02:00
|
|
|
case Relay.follow(domain) do
|
|
|
|
{:ok, _activity, _follow} ->
|
|
|
|
Instances.refresh()
|
|
|
|
get_instance(parent, args, resolution)
|
|
|
|
|
|
|
|
{:error, :http_error} ->
|
|
|
|
{:error, dgettext("errors", "Unable to find an instance to follow at this address")}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2021-12-28 11:42:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec remove_relay(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, any()}
|
2019-12-03 11:29:51 +01:00
|
|
|
def remove_relay(_parent, %{address: address}, %{context: %{current_user: %User{role: role}}})
|
|
|
|
when is_admin(role) do
|
2022-01-14 18:10:50 +01:00
|
|
|
with {:ok, _activity, follow} <- Relay.unfollow(address) do
|
|
|
|
{:ok, follow}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec accept_subscription(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, any()}
|
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
|
2022-01-14 18:10:50 +01:00
|
|
|
with {:ok, _activity, follow} <- Relay.accept(address) do
|
|
|
|
{:ok, follow}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec reject_subscription(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Follower.t()} | {:error, any()}
|
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
|
2022-01-14 18:10:50 +01:00
|
|
|
with {:ok, _activity, follow} <- Relay.reject(address) do
|
|
|
|
{:ok, follow}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
2020-07-07 15:51:42 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec eventually_update_instance_actor(map()) :: :ok | {:error, :instance_actor_update_failure}
|
2020-07-07 15:51:42 +02:00
|
|
|
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
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
if args != %{} do
|
|
|
|
%Actor{} = instance_actor = Relay.get_actor()
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
case Actions.Update.update(instance_actor, args, true) do
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, _activity, _actor} ->
|
|
|
|
:ok
|
2020-07-07 15:51:42 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, _err} ->
|
|
|
|
{:error, :instance_actor_update_failure}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
:ok
|
2020-07-07 15:51:42 +02:00
|
|
|
end
|
|
|
|
end
|
2021-12-28 11:42:08 +01:00
|
|
|
|
|
|
|
@spec follow_status(Actor.t() | nil, Actor.t() | nil) :: :approved | :pending | :none
|
|
|
|
defp follow_status(follower, followed) when follower != nil and followed != nil do
|
|
|
|
case Actors.check_follow(follower, followed) do
|
|
|
|
%Follower{approved: true} -> :approved
|
|
|
|
%Follower{approved: false} -> :pending
|
|
|
|
_ -> :none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp follow_status(_, _), do: :none
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|