Merge branch 'develop' into 'new-user-emails'
authorfeld <feld@feld.me>
Mon, 8 Feb 2021 22:01:32 +0000 (22:01 +0000)
committerMark Felder <feld@feld.me>
Mon, 8 Feb 2021 22:01:50 +0000 (16:01 -0600)
CHANGELOG.md
lib/mix/tasks/pleroma/email.ex
lib/pleroma/emails/user_email.ex
lib/pleroma/user.ex
lib/pleroma/web/pleroma_api/controllers/account_controller.ex
test/pleroma/user_test.exs

index e89836b7a0ba75e490b4effdf78df0ab380661bd..8635ed4e7990f4db09728996247160554d8baa8d 100644 (file)
@@ -53,6 +53,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Ability to define custom HTTP headers per each frontend
 - MRF (`NoEmptyPolicy`): New MRF Policy which will deny empty statuses or statuses of only mentions from being created by local users
 
+- New users will receive a simple email confirming their registration if no other emails will be dispatched. (e.g., Welcome, Confirmation, or Approval Required)
+
 <details>
   <summary>API Changes</summary>
 - Admin API: (`GET /api/pleroma/admin/users`) filter users by `unconfirmed` status and `actor_type`.
index e05c207e50c8e15494f15c672fa5b5fc4981efb1..4ce8c9b05fc7fc0f9ed48b60b4fdece26403c536 100644 (file)
@@ -38,7 +38,7 @@ defmodule Mix.Tasks.Pleroma.Email do
       invisible: false
     })
     |> Pleroma.Repo.chunk_stream(500)
-    |> Stream.each(&Pleroma.User.try_send_confirmation_email(&1))
+    |> Stream.each(&Pleroma.User.maybe_send_confirmation_email(&1))
     |> Stream.run()
   end
 end
index dbd89f1c7d3feec5a0ebc73784a7a0b4df7b7a0f..e5a6feed91175ae58346f2c049000ff9fe3cadf5 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Emails.UserEmail do
   use Phoenix.Swoosh, view: Pleroma.Web.EmailView, layout: {Pleroma.Web.LayoutView, :email}
 
   alias Pleroma.Config
+  alias Pleroma.HTML
   alias Pleroma.User
   alias Pleroma.Web.Endpoint
   alias Pleroma.Web.Router
@@ -43,6 +44,7 @@ defmodule Pleroma.Emails.UserEmail do
     |> from(sender())
     |> subject("Password reset")
     |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
   end
 
   def user_invitation_email(
@@ -69,6 +71,7 @@ defmodule Pleroma.Emails.UserEmail do
     |> from(sender())
     |> subject("Invitation to #{instance_name()}")
     |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
   end
 
   def account_confirmation_email(user) do
@@ -81,9 +84,9 @@ defmodule Pleroma.Emails.UserEmail do
       )
 
     html_body = """
-    <h3>Welcome to #{instance_name()}!</h3>
+    <h3>Thank you for registering on #{instance_name()}</h3>
     <p>Email confirmation is required to activate the account.</p>
-    <p>Click the following link to proceed: <a href="#{confirmation_url}">activate your account</a>.</p>
+    <p>Please click the following link to <a href="#{confirmation_url}">activate your account</a>.</p>
     """
 
     new()
@@ -91,6 +94,7 @@ defmodule Pleroma.Emails.UserEmail do
     |> from(sender())
     |> subject("#{instance_name()} account confirmation")
     |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
   end
 
   def approval_pending_email(user) do
@@ -104,6 +108,22 @@ defmodule Pleroma.Emails.UserEmail do
     |> from(sender())
     |> subject("Your account is awaiting approval")
     |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
+  end
+
+  def successful_registration_email(user) do
+    html_body = """
+    <h3>Hello @#{user.nickname},</h3>
+    <p>Your account at #{instance_name()} has been registered successfully.</p>
+    <p>No further action is required to activate your account.</p>
+    """
+
+    new()
+    |> to(recipient(user))
+    |> from(sender())
+    |> subject("Account registered on #{instance_name()}")
+    |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
   end
 
   @doc """
@@ -227,5 +247,6 @@ defmodule Pleroma.Emails.UserEmail do
     |> from(sender())
     |> subject("Your account archive is ready")
     |> html_body(html_body)
+    |> text_body(HTML.strip_tags(html_body))
   end
 end
index b69709db40e53304942dd34a318df0c92a8addd2..51f5bc8ea1a5dd3c404cae92f9709e59d6ec6371 100644 (file)
@@ -798,7 +798,7 @@ defmodule Pleroma.User do
   end
 
   def post_register_action(%User{is_confirmed: false} = user) do
-    with {:ok, _} <- try_send_confirmation_email(user) do
+    with {:ok, _} <- maybe_send_confirmation_email(user) do
       {:ok, user}
     end
   end
@@ -814,9 +814,10 @@ defmodule Pleroma.User do
     with {:ok, user} <- autofollow_users(user),
          {:ok, _} <- autofollowing_users(user),
          {:ok, user} <- set_cache(user),
-         {:ok, _} <- send_welcome_email(user),
-         {:ok, _} <- send_welcome_message(user),
-         {:ok, _} <- send_welcome_chat_message(user) do
+         {:ok, _} <- maybe_send_registration_email(user),
+         {:ok, _} <- maybe_send_welcome_email(user),
+         {:ok, _} <- maybe_send_welcome_message(user),
+         {:ok, _} <- maybe_send_welcome_chat_message(user) do
       {:ok, user}
     end
   end
@@ -841,7 +842,7 @@ defmodule Pleroma.User do
     {:ok, :enqueued}
   end
 
-  def send_welcome_message(user) do
+  defp maybe_send_welcome_message(user) do
     if User.WelcomeMessage.enabled?() do
       User.WelcomeMessage.post_message(user)
       {:ok, :enqueued}
@@ -850,7 +851,7 @@ defmodule Pleroma.User do
     end
   end
 
-  def send_welcome_chat_message(user) do
+  defp maybe_send_welcome_chat_message(user) do
     if User.WelcomeChatMessage.enabled?() do
       User.WelcomeChatMessage.post_message(user)
       {:ok, :enqueued}
@@ -859,7 +860,7 @@ defmodule Pleroma.User do
     end
   end
 
-  def send_welcome_email(%User{email: email} = user) when is_binary(email) do
+  defp maybe_send_welcome_email(%User{email: email} = user) when is_binary(email) do
     if User.WelcomeEmail.enabled?() do
       User.WelcomeEmail.send_email(user)
       {:ok, :enqueued}
@@ -868,10 +869,10 @@ defmodule Pleroma.User do
     end
   end
 
-  def send_welcome_email(_), do: {:ok, :noop}
+  defp maybe_send_welcome_email(_), do: {:ok, :noop}
 
-  @spec try_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
-  def try_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
+  @spec maybe_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
+  def maybe_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
       when is_binary(email) do
     if Config.get([:instance, :account_activation_required]) do
       send_confirmation_email(user)
@@ -881,7 +882,7 @@ defmodule Pleroma.User do
     end
   end
 
-  def try_send_confirmation_email(_), do: {:ok, :noop}
+  def maybe_send_confirmation_email(_), do: {:ok, :noop}
 
   @spec send_confirmation_email(Uset.t()) :: User.t()
   def send_confirmation_email(%User{} = user) do
@@ -892,6 +893,24 @@ defmodule Pleroma.User do
     user
   end
 
+  @spec maybe_send_registration_email(User.t()) :: {:ok, :enqueued | :noop}
+  defp maybe_send_registration_email(%User{email: email} = user) when is_binary(email) do
+    with false <- User.WelcomeEmail.enabled?(),
+         false <- Config.get([:instance, :account_activation_required], false),
+         false <- Config.get([:instance, :account_approval_required], false) do
+      user
+      |> Pleroma.Emails.UserEmail.successful_registration_email()
+      |> Pleroma.Emails.Mailer.deliver_async()
+
+      {:ok, :enqueued}
+    else
+      _ ->
+        {:ok, :noop}
+    end
+  end
+
+  defp maybe_send_registration_email(_), do: {:ok, :noop}
+
   def needs_update?(%User{local: true}), do: false
 
   def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
index bca8e679c5dfdf494f27be11cccac7bb918c498c..165afd3b42391029c212bfceb95bf360e19f0993 100644 (file)
@@ -56,7 +56,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
     nickname_or_email = params[:email] || params[:nickname]
 
     with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email),
-         {:ok, _} <- User.try_send_confirmation_email(user) do
+         {:ok, _} <- User.maybe_send_confirmation_email(user) do
       json_response(conn, :no_content, "")
     end
   end
index be39339f34724d798c4c37b851875408635907b3..6f5bcab57c017b3a3542a5e8a96fee43f7565bbb 100644 (file)
@@ -551,6 +551,27 @@ defmodule Pleroma.UserTest do
       )
     end
 
+    test "it sends a registration confirmed email if no others will be sent" do
+      clear_config([:welcome, :email, :enabled], false)
+      clear_config([:instance, :account_activation_required], false)
+      clear_config([:instance, :account_approval_required], false)
+
+      {:ok, user} =
+        User.register_changeset(%User{}, @full_user_data)
+        |> User.register()
+
+      ObanHelpers.perform_all()
+
+      instance_name = Pleroma.Config.get([:instance, :name])
+      sender = Pleroma.Config.get([:instance, :notify_email])
+
+      assert_email_sent(
+        from: {instance_name, sender},
+        to: {user.name, user.email},
+        subject: "Account registered on #{instance_name}"
+      )
+    end
+
     test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
       clear_config([:instance, :account_activation_required], true)