From 6ed5044c4e1889a51a1dc6015b602759b83fc3b7 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 7 Dec 2018 11:04:39 +0300 Subject: [PATCH] [#394] Refactoring (using Ecto.Multi; "untag" route change). --- lib/pleroma/user.ex | 16 ++++++---------- lib/pleroma/web/router.ex | 2 +- test/web/admin_api/admin_api_controller_test.exs | 8 ++++---- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3984e610e..511e6956e 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2,6 +2,7 @@ defmodule Pleroma.User do use Ecto.Schema import Ecto.{Changeset, Query} + alias Ecto.Multi alias Pleroma.{Repo, User, Object, Web, Activity, Notification} alias Comeonin.Pbkdf2 alias Pleroma.Formatter @@ -844,18 +845,13 @@ defp tag_or_untag([hd | _] = users, tags, action) when is_map(hd) do |> List.flatten() |> Enum.map(&String.downcase(&1)) - Repo.transaction(fn -> - for user <- users do + multi = + Enum.reduce(users, Multi.new(), fn user, multi -> new_tags = mutate_tags(user, tags, action) + Multi.update(multi, {:user, user.id}, change(user, %{tags: new_tags})) + end) - {:ok, updated_user} = - user - |> change(%{tags: new_tags}) - |> Repo.update() - - updated_user - end - end) + Repo.transaction(multi) end defp mutate_tags(user, tags, :tag), do: Enum.uniq(user.tags ++ tags) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ae942701e..a07607366 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -99,7 +99,7 @@ defmodule Pleroma.Web.Router do delete("/user", AdminAPIController, :user_delete) post("/user", AdminAPIController, :user_create) put("/users/tag", AdminAPIController, :tag_users) - put("/users/untag", AdminAPIController, :untag_users) + delete("/users/tag", AdminAPIController, :untag_users) get("/permission_group/:nickname", AdminAPIController, :right_get) get("/permission_group/:nickname/:permission_group", AdminAPIController, :right_get) diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 55aa7418b..ba3b77fb6 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -37,7 +37,7 @@ test "Create" do end end - describe "/api/pleroma/admin//users/tag" do + describe "PUT /api/pleroma/admin/users/tag" do setup do admin = insert(:user, info: %{is_admin: true}) user1 = insert(:user, %{tags: ["x"]}) @@ -73,7 +73,7 @@ test "it does not modify tags of not specified users", %{conn: conn, user3: user end end - describe "/api/pleroma/admin//users/untag" do + describe "DELETE /api/pleroma/admin/users/tag" do setup do admin = insert(:user, info: %{is_admin: true}) user1 = insert(:user, %{tags: ["x"]}) @@ -84,8 +84,8 @@ test "it does not modify tags of not specified users", %{conn: conn, user3: user build_conn() |> assign(:user, admin) |> put_req_header("accept", "application/json") - |> put( - "/api/pleroma/admin/users/untag?nicknames[]=#{user1.nickname}&nicknames[]=#{ + |> delete( + "/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=#{ user2.nickname }&tags[]=x&tags[]=z" )