Merge branch 'develop' into activation-meta
[akkoma] / lib / pleroma / web / admin_api / controllers / admin_api_controller.ex
index edd3abc63da0eebef1a765d0b578eb66b15fdbc3..f9545d8950caf135aca2ec223ada5113a4ded57b 100644 (file)
@@ -16,7 +16,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Builder
   alias Pleroma.Web.ActivityPub.Pipeline
-  alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.AdminAPI
   alias Pleroma.Web.AdminAPI.AccountView
   alias Pleroma.Web.AdminAPI.ModerationLogView
@@ -59,7 +58,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   plug(
     OAuthScopesPlug,
     %{scopes: ["write:follows"], admin: true}
-    when action in [:user_follow, :user_unfollow, :relay_follow, :relay_unfollow]
+    when action in [:user_follow, :user_unfollow]
   )
 
   plug(
@@ -74,7 +73,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     when action in [
            :list_log,
            :stats,
-           :relay_list,
            :need_reboot
          ]
   )
@@ -113,8 +111,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       action: "delete"
     })
 
-    conn
-    |> json(nicknames)
+    json(conn, nicknames)
   end
 
   def user_follow(%{assigns: %{user: admin}} = conn, %{
@@ -133,8 +130,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       })
     end
 
-    conn
-    |> json("ok")
+    json(conn, "ok")
   end
 
   def user_unfollow(%{assigns: %{user: admin}} = conn, %{
@@ -153,8 +149,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       })
     end
 
-    conn
-    |> json("ok")
+    json(conn, "ok")
   end
 
   def users_create(%{assigns: %{user: admin}} = conn, %{"users" => users}) do
@@ -193,8 +188,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
           action: "create"
         })
 
-        conn
-        |> json(res)
+        json(conn, res)
 
       {:error, id, changeset, _} ->
         res =
@@ -365,8 +359,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     filters
     |> String.split(",")
     |> Enum.filter(&Enum.member?(@filters, &1))
-    |> Enum.map(&String.to_atom(&1))
-    |> Enum.into(%{}, &{&1, true})
+    |> Enum.map(&String.to_atom/1)
+    |> Map.new(&{&1, true})
   end
 
   def right_add_multiple(%{assigns: %{user: admin}} = conn, %{
@@ -491,50 +485,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     render_error(conn, :forbidden, "You can't revoke your own admin status.")
   end
 
-  def relay_list(conn, _params) do
-    with {:ok, list} <- Relay.list() do
-      json(conn, %{relays: list})
-    else
-      _ ->
-        conn
-        |> put_status(500)
-    end
-  end
-
-  def relay_follow(%{assigns: %{user: admin}} = conn, %{"relay_url" => target}) do
-    with {:ok, _message} <- Relay.follow(target) do
-      ModerationLog.insert_log(%{
-        action: "relay_follow",
-        actor: admin,
-        target: target
-      })
-
-      json(conn, target)
-    else
-      _ ->
-        conn
-        |> put_status(500)
-        |> json(target)
-    end
-  end
-
-  def relay_unfollow(%{assigns: %{user: admin}} = conn, %{"relay_url" => target}) do
-    with {:ok, _message} <- Relay.unfollow(target) do
-      ModerationLog.insert_log(%{
-        action: "relay_unfollow",
-        actor: admin,
-        target: target
-      })
-
-      json(conn, target)
-    else
-      _ ->
-        conn
-        |> put_status(500)
-        |> json(target)
-    end
-  end
-
   @doc "Get a password reset token (base64 string) for given nickname"
   def get_password_reset(conn, %{"nickname" => nickname}) do
     (%User{local: true} = user) = User.get_cached_by_nickname(nickname)
@@ -614,10 +564,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       {:error, changeset} ->
         errors = Map.new(changeset.errors, fn {key, {error, _}} -> {key, error} end)
 
-        json(conn, %{errors: errors})
+        {:errors, errors}
 
       _ ->
-        json(conn, %{error: "Unable to update user."})
+        {:error, :not_found}
     end
   end
 
@@ -662,7 +612,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
   def reload_emoji(conn, _params) do
     Pleroma.Emoji.reload()
 
-    conn |> json("ok")
+    json(conn, "ok")
   end
 
   def confirm_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
@@ -676,7 +626,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       action: "confirm_email"
     })
 
-    conn |> json("")
+    json(conn, "")
   end
 
   def resend_confirmation_email(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
@@ -690,14 +640,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
       action: "resend_confirmation_email"
     })
 
-    conn |> json("")
+    json(conn, "")
   end
 
-  def stats(conn, _) do
-    count = Stats.get_status_visibility_count()
+  def stats(conn, params) do
+    counters = Stats.get_status_visibility_count(params["instance"])
 
-    conn
-    |> json(%{"status_visibility" => count})
+    json(conn, %{"status_visibility" => counters})
   end
 
   defp page_params(params) do