Make User.confirm/1 and User.approve/1 idempotent
authorAlex Gleason <alex@alexgleason.me>
Mon, 12 Oct 2020 21:42:59 +0000 (16:42 -0500)
committerAlex Gleason <alex@alexgleason.me>
Mon, 12 Oct 2020 21:42:59 +0000 (16:42 -0500)
lib/pleroma/user.ex
test/user_test.exs

index 4329fde12e884839104f982fa2449fc2a37a4075..c6767cfca19ed4c667f8fb961d8a515da0568adc 100644 (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),
          {:ok, user} <- update_and_set_cache(chg) do
       post_register_action(user)
     with chg <- change(user, approval_pending: false),
          {:ok, user} <- update_and_set_cache(chg) do
       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
     Repo.transaction(fn ->
       Enum.map(users, fn user ->
   def confirm(users) when is_list(users) do
     Repo.transaction(fn ->
       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),
          {:ok, user} <- update_and_set_cache(chg) do
       post_register_action(user)
     with chg <- confirmation_changeset(user, need_confirmation: false),
          {:ok, user} <- update_and_set_cache(chg) do
       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
     user
     |> cast(%{notification_settings: settings}, [])
   def update_notification_settings(%User{} = user, settings) do
     user
     |> cast(%{notification_settings: settings}, [])
index e83275fa5274047c8ad49cd45f3868a4787af70e..18a143919a85c3682395cfe81f9eb58b7f028951 100644 (file)
@@ -1371,6 +1371,17 @@ defmodule Pleroma.UserTest do
         html_body: "Welcome to #{instance_name}"
       )
     end
         html_body: "Welcome to #{instance_name}"
       )
     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
 
   describe "confirm" do
   end
 
   describe "confirm" do
@@ -1424,6 +1435,15 @@ defmodule Pleroma.UserTest do
         html_body: admin_email.html_body
       )
     end
         html_body: admin_email.html_body
       )
     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
 
   describe "delete" do
   end
 
   describe "delete" do