Commit 6ed5044c authored by Ivan Tashkinov's avatar Ivan Tashkinov
Browse files

[#394] Refactoring (using Ecto.Multi; "untag" route change).

parent 7a2162bb
......@@ -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)
......
......@@ -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)
......
......@@ -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"
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment