make bulk user creation from admin works as a transaction

This commit is contained in:
Sachin Joshi 2019-06-01 11:17:53 +05:45
parent 5c028b8f92
commit 5534d4c676
4 changed files with 104 additions and 17 deletions

View file

@ -276,7 +276,13 @@ defp autofollow_users(user) do
@doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)" @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
def register(%Ecto.Changeset{} = changeset) do def register(%Ecto.Changeset{} = changeset) do
with {:ok, user} <- Repo.insert(changeset), with {:ok, user} <- Repo.insert(changeset),
{:ok, user} <- autofollow_users(user), {:ok, user} <- post_register_action(user) do
{:ok, user}
end
end
def post_register_action(%User{} = user) do
with {:ok, user} <- autofollow_users(user),
{:ok, user} <- set_cache(user), {:ok, user} <- set_cache(user),
{:ok, _} <- Pleroma.User.WelcomeMessage.post_welcome_message_to_user(user), {:ok, _} <- Pleroma.User.WelcomeMessage.post_welcome_message_to_user(user),
{:ok, _} <- try_send_confirmation_email(user) do {:ok, _} <- try_send_confirmation_email(user) do

View file

@ -47,7 +47,7 @@ def user_unfollow(conn, %{"follower" => follower_nick, "followed" => followed_ni
end end
def users_create(conn, %{"users" => users}) do def users_create(conn, %{"users" => users}) do
result = changesets =
Enum.map(users, fn %{"nickname" => nickname, "email" => email, "password" => password} -> Enum.map(users, fn %{"nickname" => nickname, "email" => email, "password" => password} ->
user_data = %{ user_data = %{
nickname: nickname, nickname: nickname,
@ -58,19 +58,40 @@ def users_create(conn, %{"users" => users}) do
bio: "." bio: "."
} }
changeset = User.register_changeset(%User{}, user_data, need_confirmation: false) User.register_changeset(%User{}, user_data, need_confirmation: false)
end)
case User.register(changeset) do |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
{:ok, user} -> Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
AccountView.render("created.json", %{user: user})
{:error, changeset} ->
AccountView.render("create-error.json", %{changeset: changeset})
end
end) end)
conn case Pleroma.Repo.transaction(changesets) do
|> json(result) {:ok, users} ->
res =
users
|> Map.values()
|> Enum.map(fn user ->
{:ok, user} = User.post_register_action(user)
user
end)
|> Enum.map(&AccountView.render("created.json", %{user: &1}))
conn
|> json(res)
{:error, id, changeset, _} ->
res =
Enum.map(changesets.operations, fn
{current_id, {:changeset, _current_changeset, _}} when current_id == id ->
AccountView.render("create-error.json", %{changeset: changeset})
{_, {:changeset, current_changeset, _}} ->
AccountView.render("create-error.json", %{changeset: current_changeset})
end)
conn
|> put_status(:conflict)
|> json(res)
end
end end
def user_show(conn, %{"nickname" => nickname}) do def user_show(conn, %{"nickname" => nickname}) do

View file

@ -48,7 +48,7 @@ def render("invites.json", %{invites: invites}) do
def render("created.json", %{user: user}) do def render("created.json", %{user: user}) do
%{ %{
type: "success", type: "success",
code: 201, code: 200,
data: %{ data: %{
nickname: user.nickname, nickname: user.nickname,
email: user.email email: user.email

View file

@ -36,18 +36,31 @@ test "Create" do
"nickname" => "lain", "nickname" => "lain",
"email" => "lain@example.org", "email" => "lain@example.org",
"password" => "test" "password" => "test"
},
%{
"nickname" => "lain2",
"email" => "lain2@example.org",
"password" => "test"
} }
] ]
}) })
assert json_response(conn, 200) == [ assert json_response(conn, 200) == [
%{ %{
"code" => 201, "code" => 200,
"data" => %{ "data" => %{
"email" => "lain@example.org", "email" => "lain@example.org",
"nickname" => "lain" "nickname" => "lain"
}, },
"type" => "success" "type" => "success"
},
%{
"code" => 200,
"data" => %{
"email" => "lain2@example.org",
"nickname" => "lain2"
},
"type" => "success"
} }
] ]
end end
@ -70,7 +83,7 @@ test "Cannot create user with exisiting email" do
] ]
}) })
assert json_response(conn, 200) == [ assert json_response(conn, 409) == [
%{ %{
"code" => 409, "code" => 409,
"data" => %{ "data" => %{
@ -101,7 +114,7 @@ test "Cannot create user with exisiting nickname" do
] ]
}) })
assert json_response(conn, 200) == [ assert json_response(conn, 409) == [
%{ %{
"code" => 409, "code" => 409,
"data" => %{ "data" => %{
@ -113,6 +126,53 @@ test "Cannot create user with exisiting nickname" do
} }
] ]
end end
test "Multiple user creation works in transaction" do
admin = insert(:user, info: %{is_admin: true})
user = insert(:user)
conn =
build_conn()
|> assign(:user, admin)
|> put_req_header("accept", "application/json")
|> post("/api/pleroma/admin/users", %{
"users" => [
%{
"nickname" => "newuser",
"email" => "newuser@pleroma.social",
"password" => "test"
},
%{
"nickname" => "lain",
"email" => user.email,
"password" => "test"
}
]
})
assert json_response(conn, 409) == [
%{
"code" => 409,
"data" => %{
"email" => user.email,
"nickname" => "lain"
},
"error" => "email has already been taken",
"type" => "error"
},
%{
"code" => 409,
"data" => %{
"email" => "newuser@pleroma.social",
"nickname" => "newuser"
},
"error" => "",
"type" => "error"
}
]
assert User.get_by_nickname("newuser") === nil
end
end end
describe "/api/pleroma/admin/users/:nickname" do describe "/api/pleroma/admin/users/:nickname" do