[#394] Refactoring (using Ecto.Multi; "untag" route change).
This commit is contained in:
parent
7a2162bbcb
commit
6ed5044c4e
|
@ -2,6 +2,7 @@ defmodule Pleroma.User do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
import Ecto.{Changeset, Query}
|
import Ecto.{Changeset, Query}
|
||||||
|
alias Ecto.Multi
|
||||||
alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
|
alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
|
||||||
alias Comeonin.Pbkdf2
|
alias Comeonin.Pbkdf2
|
||||||
alias Pleroma.Formatter
|
alias Pleroma.Formatter
|
||||||
|
@ -844,18 +845,13 @@ defp tag_or_untag([hd | _] = users, tags, action) when is_map(hd) do
|
||||||
|> List.flatten()
|
|> List.flatten()
|
||||||
|> Enum.map(&String.downcase(&1))
|
|> Enum.map(&String.downcase(&1))
|
||||||
|
|
||||||
Repo.transaction(fn ->
|
multi =
|
||||||
for user <- users do
|
Enum.reduce(users, Multi.new(), fn user, multi ->
|
||||||
new_tags = mutate_tags(user, tags, action)
|
new_tags = mutate_tags(user, tags, action)
|
||||||
|
Multi.update(multi, {:user, user.id}, change(user, %{tags: new_tags}))
|
||||||
|
end)
|
||||||
|
|
||||||
{:ok, updated_user} =
|
Repo.transaction(multi)
|
||||||
user
|
|
||||||
|> change(%{tags: new_tags})
|
|
||||||
|> Repo.update()
|
|
||||||
|
|
||||||
updated_user
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp mutate_tags(user, tags, :tag), do: Enum.uniq(user.tags ++ tags)
|
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)
|
delete("/user", AdminAPIController, :user_delete)
|
||||||
post("/user", AdminAPIController, :user_create)
|
post("/user", AdminAPIController, :user_create)
|
||||||
put("/users/tag", AdminAPIController, :tag_users)
|
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", AdminAPIController, :right_get)
|
||||||
get("/permission_group/:nickname/:permission_group", AdminAPIController, :right_get)
|
get("/permission_group/:nickname/:permission_group", AdminAPIController, :right_get)
|
||||||
|
|
|
@ -37,7 +37,7 @@ test "Create" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "/api/pleroma/admin//users/tag" do
|
describe "PUT /api/pleroma/admin/users/tag" do
|
||||||
setup do
|
setup do
|
||||||
admin = insert(:user, info: %{is_admin: true})
|
admin = insert(:user, info: %{is_admin: true})
|
||||||
user1 = insert(:user, %{tags: ["x"]})
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "/api/pleroma/admin//users/untag" do
|
describe "DELETE /api/pleroma/admin/users/tag" do
|
||||||
setup do
|
setup do
|
||||||
admin = insert(:user, info: %{is_admin: true})
|
admin = insert(:user, info: %{is_admin: true})
|
||||||
user1 = insert(:user, %{tags: ["x"]})
|
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()
|
build_conn()
|
||||||
|> assign(:user, admin)
|
|> assign(:user, admin)
|
||||||
|> put_req_header("accept", "application/json")
|
|> put_req_header("accept", "application/json")
|
||||||
|> put(
|
|> delete(
|
||||||
"/api/pleroma/admin/users/untag?nicknames[]=#{user1.nickname}&nicknames[]=#{
|
"/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=#{
|
||||||
user2.nickname
|
user2.nickname
|
||||||
}&tags[]=x&tags[]=z"
|
}&tags[]=x&tags[]=z"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue