From 90d0d055fccc630066e66627281432e382aefdb1 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Wed, 27 Feb 2019 00:13:38 +0300 Subject: [PATCH] Add more admin actions --- lib/pleroma/user.ex | 6 ++++++ .../web/admin_api/admin_api_controller.ex | 17 +++++++++++++++++ lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/views/user_view.ex | 14 ++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index c98b942ff..12e0e818e 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -772,6 +772,12 @@ def search(query, resolve \\ false, for_user \\ nil) do Enum.uniq_by(fts_results ++ trigram_results, & &1.id) end + def all_except_one(user) do + query = from(u in User, where: u.id != ^user.id) + + Repo.all(query) + end + defp do_search(subquery, for_user, options \\ []) do q = from( diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index 9ec50bb90..ef72509fe 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do use Pleroma.Web, :controller alias Pleroma.User alias Pleroma.Web.ActivityPub.Relay + alias Pleroma.Web.TwitterAPI.UserView import Pleroma.Web.ControllerHelper, only: [json_response: 3] @@ -41,6 +42,15 @@ def user_create( |> json(user.nickname) end + def user_toggle_activation(conn, %{"nickname" => nickname}) do + user = User.get_by_nickname(nickname) + + {:ok, updated_user} = User.deactivate(user, !user.info.deactivated) + + conn + |> json(UserView.render("show_for_admin.json", %{user: updated_user})) + end + def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do with {:ok, _} <- User.tag(nicknames, tags), do: json_response(conn, :no_content, "") @@ -51,6 +61,13 @@ def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do do: json_response(conn, :no_content, "") end + def list_users(%{assigns: %{user: admin}} = conn, _data) do + users = User.all_except_one(admin) + + conn + |> json(UserView.render("index_for_admin.json", %{users: users})) + end + def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname}) when permission_group in ["moderator", "admin"] do user = User.get_by_nickname(nickname) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 5aebcb353..3b1fd46a5 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -139,7 +139,9 @@ defmodule Pleroma.Web.Router do scope "/api/pleroma/admin", Pleroma.Web.AdminAPI do pipe_through([:admin_api, :oauth_write]) + get("/users", AdminAPIController, :list_users) delete("/user", AdminAPIController, :user_delete) + patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation) post("/user", AdminAPIController, :user_create) put("/users/tag", AdminAPIController, :tag_users) delete("/users/tag", AdminAPIController, :untag_users) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index df7384476..c5034cf36 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do alias Pleroma.User alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.MediaProxy + alias Pleroma.Web.TwitterAPI.UserView def render("show.json", %{user: user = %User{}} = assigns) do render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns) @@ -26,6 +27,19 @@ def render("user.json", %{user: user = %User{}} = assigns) do else: %{} end + def render("index_for_admin.json", %{users: users} = opts) do + users + |> render_many(UserView, "show_for_admin.json", opts) + end + + def render("show_for_admin.json", %{user: user}) do + %{ + "id" => user.id, + "nickname" => user.nickname, + "deactivated" => user.info.deactivated + } + end + def render("short.json", %{ user: %User{ nickname: nickname,