From b22b12f73813b9c46701cac84cfe3a21a5ceacca Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 14:01:15 -0600 Subject: [PATCH 01/12] These welcome emails are not guaranteed and can be private functions --- lib/pleroma/user.ex | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b69709db4..6aab247d1 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -814,9 +814,9 @@ def post_register_action(%User{is_approved: true, is_confirmed: true} = 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_welcome_email(user), + {:ok, _} <- maybe_send_welcome_message(user), + {:ok, _} <- maybe_send_welcome_chat_message(user) do {:ok, user} end end @@ -841,7 +841,7 @@ defp send_admin_approval_emails(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 +850,7 @@ def send_welcome_message(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 +859,7 @@ def send_welcome_chat_message(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,7 +868,7 @@ def send_welcome_email(%User{email: email} = user) when is_binary(email) 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) From af37a5c51a3984d8e5ddbe5978b8c3edb7f9bbc2 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 14:33:49 -0600 Subject: [PATCH 02/12] Also make this maybe_ for consistency --- lib/mix/tasks/pleroma/email.ex | 2 +- lib/pleroma/user.ex | 8 ++++---- .../web/pleroma_api/controllers/account_controller.ex | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/mix/tasks/pleroma/email.ex b/lib/mix/tasks/pleroma/email.ex index e05c207e5..4ce8c9b05 100644 --- a/lib/mix/tasks/pleroma/email.ex +++ b/lib/mix/tasks/pleroma/email.ex @@ -38,7 +38,7 @@ def run(["resend_confirmation_emails"]) 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 diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6aab247d1..7a7956c8f 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -798,7 +798,7 @@ def register(%Ecto.Changeset{} = changeset) 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 @@ -870,8 +870,8 @@ defp maybe_send_welcome_email(%User{email: email} = user) when is_binary(email) 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 +881,7 @@ def try_send_confirmation_email(%User{is_confirmed: false, email: email} = user) 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 diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index bca8e679c..165afd3b4 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -56,7 +56,7 @@ def confirmation_resend(conn, params) 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 From 2956c21a55518f5f6f6648cc2d25f2b2114dc20f Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 15:10:43 -0600 Subject: [PATCH 03/12] Improve confirmation email language --- lib/pleroma/emails/user_email.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index dbd89f1c7..0c00069e2 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -81,9 +81,9 @@ def account_confirmation_email(user) do ) html_body = """ -

Welcome to #{instance_name()}!

+

Thank you for registering on #{instance_name()}

Email confirmation is required to activate the account.

-

Click the following link to proceed: activate your account.

+

Please click the following link to activate your account.

""" new() From e945ccc91bbc7c3479e842feb276c5efff30eed2 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 15:16:50 -0600 Subject: [PATCH 04/12] Add a registration email that only sends if no other emails (welcome, approval, confirmation) are enabled --- lib/pleroma/emails/user_email.ex | 14 ++++++++++++++ lib/pleroma/user.ex | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index 0c00069e2..a5233f373 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -106,6 +106,20 @@ def approval_pending_email(user) do |> html_body(html_body) end + def successful_registration_email(user) do + html_body = """ +

Hello @#{user.nickname}

+

Your account at #{instance_name()} has been registered successfully.

+

No further action is required to activate your account.

+ """ + + new() + |> to(recipient(user)) + |> from(sender()) + |> subject("Account registered on #{instance_name()}") + |> html_body(html_body) + end + @doc """ Email used in digest email notifications Includes Mentions and New Followers data diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 7a7956c8f..1d7cb22b2 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -814,6 +814,7 @@ def post_register_action(%User{is_approved: true, is_confirmed: true} = user) do with {:ok, user} <- autofollow_users(user), {:ok, _} <- autofollowing_users(user), {:ok, user} <- set_cache(user), + {: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 @@ -892,6 +893,23 @@ def send_confirmation_email(%User{} = 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() + + {: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 From 2a863987bc41c5fed26f430f47548a1cf49030ed Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 16:14:37 -0600 Subject: [PATCH 05/12] Added: New user registration mail --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dbdb3f4e..c1e490c30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Ability to set ActivityPub aliases for follower migration. - Configurable background job limits for RichMedia (link previews) and MediaProxyWarmingPolicy - Ability to define custom HTTP headers per each frontend +- New users will receive a simple email confirming their registration if no other emails will be dispatched. (e.g., Welcome, Confirmation, or Approval Required)
API Changes From 95930a7aa5b06ded61a2694989531846a527d0ed Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 17:42:09 -0600 Subject: [PATCH 06/12] Actually send the mail --- lib/pleroma/user.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 1d7cb22b2..51f5bc8ea 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -900,6 +900,7 @@ defp maybe_send_registration_email(%User{email: email} = user) when is_binary(em false <- Config.get([:instance, :account_approval_required], false) do user |> Pleroma.Emails.UserEmail.successful_registration_email() + |> Pleroma.Emails.Mailer.deliver_async() {:ok, :enqueued} else From c3614403966ddddeddecd45d97fdda8f2879cd32 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 4 Feb 2021 17:56:46 -0600 Subject: [PATCH 07/12] Add basic test to validate the registration email is dispatched when the others are disabled Also only check for subject as the body is a mess of html and we don't really need to prove its contents if the subject matches. --- test/pleroma/user_test.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index be39339f3..86f050fd1 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -551,6 +551,26 @@ test "sends a pending approval email" 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) From 6a3e75c8e65c11794bef8688464ce03ad978d7f1 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 5 Feb 2021 09:00:17 -0600 Subject: [PATCH 08/12] Lint --- test/pleroma/user_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 86f050fd1..6f5bcab57 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -559,6 +559,7 @@ test "it sends a registration confirmed email if no others will be sent" do {:ok, user} = User.register_changeset(%User{}, @full_user_data) |> User.register() + ObanHelpers.perform_all() instance_name = Pleroma.Config.get([:instance, :name]) From 5df9f68392f65a5688867b9bad4bda766e492923 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 5 Feb 2021 09:13:17 -0600 Subject: [PATCH 09/12] Add plaintext support for all emails except the digest --- lib/pleroma/emails/user_email.ex | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index a5233f373..74e3e6f41 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -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 @@ def password_reset_email(user, token) when is_binary(token) 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 @@ def user_invitation_email( |> 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 @@ -91,6 +94,7 @@ def account_confirmation_email(user) 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,7 @@ def approval_pending_email(user) 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 @@ -118,6 +123,7 @@ def successful_registration_email(user) do |> from(sender()) |> subject("Account registered on #{instance_name()}") |> html_body(html_body) + |> text_body(HTML.strip_tags(html_body)) end @doc """ @@ -241,5 +247,6 @@ def backup_is_ready_email(backup, admin_user_id \\ nil) do |> from(sender()) |> subject("Your account archive is ready") |> html_body(html_body) + |> text_body(HTML.strip_tags(html_body)) end end From 0368419fce04f636d2c5adcf44e7d35c43279dc1 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 5 Feb 2021 09:13:53 -0600 Subject: [PATCH 10/12] Slightly better formatting --- lib/pleroma/emails/user_email.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index 74e3e6f41..e5a6feed9 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -113,7 +113,7 @@ def approval_pending_email(user) do def successful_registration_email(user) do html_body = """ -

Hello @#{user.nickname}

+

Hello @#{user.nickname},

Your account at #{instance_name()} has been registered successfully.

No further action is required to activate your account.

""" From 4cacce4b42e25d608390a7fd06ab21dc64529e37 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 8 Feb 2021 16:39:55 -0600 Subject: [PATCH 11/12] Revert "Add plaintext support for all emails except the digest" This reverts commit 5df9f68392f65a5688867b9bad4bda766e492923. --- lib/pleroma/emails/user_email.ex | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index e5a6feed9..52f3d419d 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -8,7 +8,6 @@ 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 @@ -44,7 +43,6 @@ def password_reset_email(user, token) when is_binary(token) do |> from(sender()) |> subject("Password reset") |> html_body(html_body) - |> text_body(HTML.strip_tags(html_body)) end def user_invitation_email( @@ -71,7 +69,6 @@ def user_invitation_email( |> 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 @@ -94,7 +91,6 @@ def account_confirmation_email(user) 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 @@ -108,7 +104,6 @@ def approval_pending_email(user) 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 @@ -123,7 +118,6 @@ def successful_registration_email(user) do |> from(sender()) |> subject("Account registered on #{instance_name()}") |> html_body(html_body) - |> text_body(HTML.strip_tags(html_body)) end @doc """ @@ -247,6 +241,5 @@ def backup_is_ready_email(backup, admin_user_id \\ nil) do |> from(sender()) |> subject("Your account archive is ready") |> html_body(html_body) - |> text_body(HTML.strip_tags(html_body)) end end From 6e90b79d63729a8ee51a25fb010a1be29613a4d0 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 8 Feb 2021 16:40:27 -0600 Subject: [PATCH 12/12] Lint --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8635ed4e7..bbd898bdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Configurable background job limits for RichMedia (link previews) and MediaProxyWarmingPolicy - 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)