mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-11 03:39:13 +00:00
[#114] Refactored User.register_changeset to init confirmation data.
Introduced User.register/1 to encapsulate User record creation and post-registration actions.
This commit is contained in:
parent
1de0aa2f10
commit
b86057cc7f
3 changed files with 39 additions and 33 deletions
|
@ -170,7 +170,14 @@ defmodule Pleroma.User do
|
|||
update_and_set_cache(password_update_changeset(user, data))
|
||||
end
|
||||
|
||||
def register_changeset(struct, params \\ %{}) do
|
||||
def register_changeset(struct, params \\ %{}, opts \\ []) do
|
||||
confirmation_status =
|
||||
if opts[:confirmed] || !Pleroma.Config.get([:instance, :account_activation_required]) do
|
||||
:confirmed
|
||||
else
|
||||
:unconfirmed
|
||||
end
|
||||
|
||||
changeset =
|
||||
struct
|
||||
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|
||||
|
@ -182,7 +189,7 @@ defmodule Pleroma.User do
|
|||
|> validate_format(:email, @email_regex)
|
||||
|> validate_length(:bio, max: 1000)
|
||||
|> validate_length(:name, min: 1, max: 100)
|
||||
|> put_change(:info, %Pleroma.User.Info{})
|
||||
|> put_change(:info, User.Info.confirmation_update(%User.Info{}, confirmation_status))
|
||||
|
||||
if changeset.valid? do
|
||||
hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
|
||||
|
@ -199,6 +206,20 @@ defmodule Pleroma.User do
|
|||
end
|
||||
end
|
||||
|
||||
@doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
|
||||
def register(%Ecto.Changeset{} = changeset) do
|
||||
with {:ok, user} <- Repo.insert(changeset) do
|
||||
if user.info.confirmation_pending do
|
||||
{:ok, _} =
|
||||
user
|
||||
|> Pleroma.UserEmail.account_confirmation_email()
|
||||
|> Pleroma.Mailer.deliver()
|
||||
end
|
||||
|
||||
{:ok, user}
|
||||
end
|
||||
end
|
||||
|
||||
def needs_update?(%User{local: true}), do: false
|
||||
|
||||
def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.{User, Repo}
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Relay
|
||||
|
||||
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
||||
|
@ -26,7 +26,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
|||
conn,
|
||||
%{"nickname" => nickname, "email" => email, "password" => password}
|
||||
) do
|
||||
new_user = %{
|
||||
user_data = %{
|
||||
nickname: nickname,
|
||||
name: nickname,
|
||||
email: email,
|
||||
|
@ -35,11 +35,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
|||
bio: "."
|
||||
}
|
||||
|
||||
User.register_changeset(%User{}, new_user)
|
||||
|> Repo.insert!()
|
||||
changeset = User.register_changeset(%User{}, user_data, confirmed: true)
|
||||
{:ok, user} = User.register(changeset)
|
||||
|
||||
conn
|
||||
|> json(new_user.nickname)
|
||||
|> json(user.nickname)
|
||||
end
|
||||
|
||||
def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
|
||||
|
|
|
@ -161,34 +161,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
Repo.get_by(UserInviteToken, %{token: tokenString})
|
||||
end
|
||||
|
||||
cond do
|
||||
registrations_open || (!is_nil(token) && !token.used) ->
|
||||
changeset = User.register_changeset(%User{info: %{}}, params)
|
||||
cond do
|
||||
registrations_open || (!is_nil(token) && !token.used) ->
|
||||
changeset = User.register_changeset(%User{}, params)
|
||||
|
||||
with {:ok, user} <- Repo.insert(changeset) do
|
||||
!registrations_open && UserInviteToken.mark_as_used(token.token)
|
||||
with {:ok, user} <- User.register(changeset) do
|
||||
!registrations_open && UserInviteToken.mark_as_used(token.token)
|
||||
|
||||
if Pleroma.Config.get([:instance, :account_activation_required]) do
|
||||
info_change = User.Info.confirmation_update(user.info, :unconfirmed)
|
||||
|
||||
{:ok, unconfirmed_user} =
|
||||
user
|
||||
|> Ecto.Changeset.change()
|
||||
|> Ecto.Changeset.put_embed(:info, info_change)
|
||||
|> Repo.update()
|
||||
|
||||
{:ok, _} =
|
||||
unconfirmed_user
|
||||
|> UserEmail.account_confirmation_email()
|
||||
|> Mailer.deliver()
|
||||
end
|
||||
|
||||
{:ok, user}
|
||||
else
|
||||
{:error, changeset} ->
|
||||
errors =
|
||||
Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
|
||||
|> Jason.encode!()
|
||||
{:ok, user}
|
||||
else
|
||||
{:error, changeset} ->
|
||||
errors =
|
||||
Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
|
||||
|> Jason.encode!()
|
||||
|
||||
{:error, %{error: errors}}
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue