Remove toggle_confirmation; require explicit state change

Also cosmetic changes to make the code clearer
This commit is contained in:
Mark Felder 2020-10-13 16:07:36 -05:00 committed by Mark Felder
parent d36182c088
commit 2c0fe2ea9e
11 changed files with 37 additions and 41 deletions

View File

@ -213,7 +213,7 @@ def run(["set", nickname | rest]) do
user = user =
case Keyword.get(options, :confirmed) do case Keyword.get(options, :confirmed) do
nil -> user nil -> user
value -> set_confirmed(user, value) value -> set_confirmation(user, value)
end end
user = user =
@ -373,7 +373,7 @@ def run(["confirm_all"]) do
|> Pleroma.Repo.chunk_stream(500, :batches) |> Pleroma.Repo.chunk_stream(500, :batches)
|> Stream.each(fn users -> |> Stream.each(fn users ->
users users
|> Enum.each(fn user -> User.need_confirmation(user, false) end) |> Enum.each(fn user -> User.set_confirmation(user, true) end)
end) end)
|> Stream.run() |> Stream.run()
end end
@ -391,7 +391,7 @@ def run(["unconfirm_all"]) do
|> Pleroma.Repo.chunk_stream(500, :batches) |> Pleroma.Repo.chunk_stream(500, :batches)
|> Stream.each(fn users -> |> Stream.each(fn users ->
users users
|> Enum.each(fn user -> User.need_confirmation(user, true) end) |> Enum.each(fn user -> User.set_confirmation(user, false) end)
end) end)
|> Stream.run() |> Stream.run()
end end
@ -454,8 +454,8 @@ defp set_locked(user, value) do
user user
end end
defp set_confirmed(user, value) do defp set_confirmation(user, value) do
{:ok, user} = User.need_confirmation(user, !value) {:ok, user} = User.set_confirmation(user, value)
shell_info("Confirmation status of #{user.nickname}: #{user.is_confirmed}") shell_info("Confirmation status of #{user.nickname}: #{user.is_confirmed}")
user user

View File

@ -704,11 +704,11 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
reason_limit = Config.get([:instance, :registration_reason_length], 500) reason_limit = Config.get([:instance, :registration_reason_length], 500)
params = Map.put_new(params, :accepts_chat_messages, true) params = Map.put_new(params, :accepts_chat_messages, true)
need_confirmation? = confirmed? =
if is_nil(opts[:need_confirmation]) do if is_nil(opts[:confirmed]) do
Config.get([:instance, :account_activation_required]) !Config.get([:instance, :account_activation_required])
else else
opts[:need_confirmation] opts[:confirmed]
end end
need_approval? = need_approval? =
@ -719,7 +719,7 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
end end
struct struct
|> confirmation_changeset(need_confirmation: need_confirmation?) |> confirmation_changeset(set_confirmation: confirmed?)
|> approval_changeset(need_approval: need_approval?) |> approval_changeset(need_approval: need_approval?)
|> cast(params, [ |> cast(params, [
:bio, :bio,
@ -1643,7 +1643,7 @@ def confirm(users) when is_list(users) do
end end
def confirm(%User{is_confirmed: false} = user) do def confirm(%User{is_confirmed: false} = user) do
with chg <- confirmation_changeset(user, need_confirmation: false), with chg <- confirmation_changeset(user, set_confirmation: true),
{:ok, user} <- update_and_set_cache(chg) do {:ok, user} <- update_and_set_cache(chg) do
post_register_action(user) post_register_action(user)
{:ok, user} {:ok, user}
@ -2138,10 +2138,10 @@ def touch_last_digest_emailed_at(user) do
updated_user updated_user
end end
@spec need_confirmation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()} @spec set_confirmation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()}
def need_confirmation(%User{} = user, bool) do def set_confirmation(%User{} = user, bool) do
user user
|> confirmation_changeset(need_confirmation: bool) |> confirmation_changeset(set_confirmation: bool)
|> update_and_set_cache() |> update_and_set_cache()
end end
@ -2309,18 +2309,18 @@ def mastodon_settings_update(user, settings) do
end end
@spec confirmation_changeset(User.t(), keyword()) :: Changeset.t() @spec confirmation_changeset(User.t(), keyword()) :: Changeset.t()
def confirmation_changeset(user, need_confirmation: need_confirmation?) do def confirmation_changeset(user, set_confirmation: confirmed?) do
params = params =
if need_confirmation? do if confirmed? do
%{
is_confirmed: false,
confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
}
else
%{ %{
is_confirmed: true, is_confirmed: true,
confirmation_token: nil confirmation_token: nil
} }
else
%{
is_confirmed: false,
confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
}
end end
cast(user, params, [:is_confirmed, :confirmation_token]) cast(user, params, [:is_confirmed, :confirmation_token])

View File

@ -84,7 +84,7 @@ def create_from_registration(
password_confirmation: random_password password_confirmation: random_password
}, },
external: true, external: true,
need_confirmation: false confirmed: true
) )
|> Repo.insert(), |> Repo.insert(),
{:ok, _} <- {:ok, _} <-

View File

@ -11,9 +11,9 @@ defmodule Pleroma.Repo.Migrations.ConfirmLoggedInUsers do
def up do def up do
User User
|> where([u], u.confirmation_pending == true) |> where([u], u.is_confirmed == false)
|> join(:inner, [u], t in Token, on: t.user_id == u.id) |> join(:inner, [u], t in Token, on: t.user_id == u.id)
|> Repo.update_all(set: [confirmation_pending: false]) |> Repo.update_all(set: [is_confirmed: true])
end end
def down do def down do

View File

@ -436,13 +436,6 @@ test "invite is revoked" do
assert_received {:mix_shell, :info, [message]} assert_received {:mix_shell, :info, [message]}
assert message =~ "Invite for token #{invite.token} was revoked." assert message =~ "Invite for token #{invite.token} was revoked."
end end
test "it prints an error message when invite is not exist" do
Mix.Tasks.Pleroma.User.run(["revoke_invite", "foo"])
assert_received {:mix_shell, :error, [message]}
assert message =~ "No invite found"
end
end end
describe "running delete_activities" do describe "running delete_activities" do

View File

@ -14,12 +14,12 @@ defmodule Pleroma.Repo.Migrations.ConfirmLoggedInUsersTest do
test "up/0 confirms unconfirmed but previously-logged-in users", %{migration: migration} do test "up/0 confirms unconfirmed but previously-logged-in users", %{migration: migration} do
insert_list(25, :oauth_token) insert_list(25, :oauth_token)
Repo.update_all(User, set: [confirmation_pending: true]) Repo.update_all(User, set: [is_confirmed: false])
insert_list(5, :user, confirmation_pending: true) insert_list(5, :user, is_confirmed: false)
count = count =
User User
|> where(confirmation_pending: true) |> where(is_confirmed: false)
|> Repo.aggregate(:count) |> Repo.aggregate(:count)
assert count == 30 assert count == 30
@ -28,7 +28,7 @@ test "up/0 confirms unconfirmed but previously-logged-in users", %{migration: mi
count = count =
User User
|> where(confirmation_pending: true) |> where(is_confirmed: false)
|> Repo.aggregate(:count) |> Repo.aggregate(:count)
assert count == 5 assert count == 5

View File

@ -666,7 +666,7 @@ test "it creates unconfirmed user" do
end end
test "it creates confirmed user if :confirmed option is given" do test "it creates confirmed user if :confirmed option is given" do
changeset = User.register_changeset(%User{}, @full_user_data, need_confirmation: false) changeset = User.register_changeset(%User{}, @full_user_data, confirmed: true)
assert changeset.valid? assert changeset.valid?
{:ok, user} = Repo.insert(changeset) {:ok, user} = Repo.insert(changeset)

View File

@ -906,8 +906,11 @@ test "it confirms emails of two users", %{conn: conn, admin: admin} do
assert ret_conn.status == 200 assert ret_conn.status == 200
assert User.get_by_id(first_user.id).is_confirmed first_user = User.get_by_id(first_user.id)
assert User.get_by_id(second_user.id).is_confirmed second_user = User.get_by_id(second_user.id)
assert first_user.is_confirmed
assert second_user.is_confirmed
log_entry = Repo.one(ModerationLog) log_entry = Repo.one(ModerationLog)

View File

@ -928,7 +928,7 @@ test "rejects token exchange for valid credentials belonging to unconfirmed user
{:ok, user} = {:ok, user} =
insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password)) insert(:user, password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password))
|> User.confirmation_changeset(need_confirmation: true) |> User.confirmation_changeset(set_confirmation: false)
|> User.update_and_set_cache() |> User.update_and_set_cache()
refute Pleroma.User.account_status(user) == :active refute Pleroma.User.account_status(user) == :active

View File

@ -17,7 +17,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
setup do setup do
{:ok, user} = {:ok, user} =
insert(:user) insert(:user)
|> User.confirmation_changeset(need_confirmation: true) |> User.confirmation_changeset(set_confirmation: false)
|> User.update_and_set_cache() |> User.update_and_set_cache()
refute user.is_confirmed refute user.is_confirmed

View File

@ -64,7 +64,7 @@ test "with credentials, with params" do
setup do setup do
{:ok, user} = {:ok, user} =
insert(:user) insert(:user)
|> User.confirmation_changeset(need_confirmation: true) |> User.confirmation_changeset(set_confirmation: false)
|> Repo.update() |> Repo.update()
refute user.is_confirmed refute user.is_confirmed