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

This commit is contained in:
Ivan Tashkinov 2018-12-07 11:04:39 +03:00
parent 7a2162bbcb
commit 6ed5044c4e
3 changed files with 11 additions and 15 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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"
)