Make User.confirm/1 and User.approve/1 idempotent

This commit is contained in:
Alex Gleason 2020-10-12 16:42:59 -05:00
parent 6ebec50df6
commit cb29769a22
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 26 additions and 2 deletions

View file

@ -1590,7 +1590,7 @@ defmodule Pleroma.User do
end) end)
end end
def approve(%User{} = user) do def approve(%User{approval_pending: true} = user) do
with chg <- change(user, approval_pending: false), with chg <- change(user, approval_pending: false),
{:ok, user} <- update_and_set_cache(chg) do {:ok, user} <- update_and_set_cache(chg) do
post_register_action(user) post_register_action(user)
@ -1598,6 +1598,8 @@ defmodule Pleroma.User do
end end
end end
def approve(%User{} = user), do: {:ok, user}
def confirm(users) when is_list(users) do def confirm(users) when is_list(users) do
Repo.transaction(fn -> Repo.transaction(fn ->
Enum.map(users, fn user -> Enum.map(users, fn user ->
@ -1606,7 +1608,7 @@ defmodule Pleroma.User do
end) end)
end end
def confirm(%User{} = user) do def confirm(%User{confirmation_pending: true} = user) do
with chg <- confirmation_changeset(user, need_confirmation: false), with chg <- confirmation_changeset(user, need_confirmation: false),
{:ok, user} <- update_and_set_cache(chg) do {:ok, user} <- update_and_set_cache(chg) do
post_register_action(user) post_register_action(user)
@ -1614,6 +1616,8 @@ defmodule Pleroma.User do
end end
end end
def confirm(%User{} = user), do: {:ok, user}
def update_notification_settings(%User{} = user, settings) do def update_notification_settings(%User{} = user, settings) do
user user
|> cast(%{notification_settings: settings}, []) |> cast(%{notification_settings: settings}, [])

View file

@ -1371,6 +1371,17 @@ defmodule Pleroma.UserTest do
html_body: "Welcome to #{instance_name}" html_body: "Welcome to #{instance_name}"
) )
end end
test "approving an approved user does not trigger post-register actions" do
clear_config([:welcome, :email, :enabled], true)
user = insert(:user, approval_pending: false)
User.approve(user)
ObanHelpers.perform_all()
assert_no_email_sent()
end
end end
describe "confirm" do describe "confirm" do
@ -1424,6 +1435,15 @@ defmodule Pleroma.UserTest do
html_body: admin_email.html_body html_body: admin_email.html_body
) )
end end
test "confirming a confirmed user does not trigger post-register actions" do
user = insert(:user, confirmation_pending: false, approval_pending: true)
User.confirm(user)
ObanHelpers.perform_all()
assert_no_email_sent()
end
end end
describe "delete" do describe "delete" do