Merge branch 'new-user-emails' into 'develop'

Basic new user registration email, various improvements

See merge request pleroma/pleroma!3304
This commit is contained in:
feld 2021-02-08 22:47:03 +00:00
commit fc42e714e2
6 changed files with 70 additions and 15 deletions

View file

@ -52,6 +52,7 @@ 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 - Configurable background job limits for RichMedia (link previews) and MediaProxyWarmingPolicy
- Ability to define custom HTTP headers per each frontend - 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 - 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> <details>
<summary>API Changes</summary> <summary>API Changes</summary>

View file

@ -38,7 +38,7 @@ defmodule Mix.Tasks.Pleroma.Email do
invisible: false invisible: false
}) })
|> Pleroma.Repo.chunk_stream(500) |> 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() |> Stream.run()
end end
end end

View file

@ -81,9 +81,9 @@ defmodule Pleroma.Emails.UserEmail do
) )
html_body = """ 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>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() new()
@ -106,6 +106,20 @@ defmodule Pleroma.Emails.UserEmail do
|> html_body(html_body) |> html_body(html_body)
end 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)
end
@doc """ @doc """
Email used in digest email notifications Email used in digest email notifications
Includes Mentions and New Followers data Includes Mentions and New Followers data

View file

@ -798,7 +798,7 @@ defmodule Pleroma.User do
end end
def post_register_action(%User{is_confirmed: false} = user) do 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} {:ok, user}
end end
end end
@ -814,9 +814,10 @@ defmodule Pleroma.User do
with {:ok, user} <- autofollow_users(user), with {:ok, user} <- autofollow_users(user),
{:ok, _} <- autofollowing_users(user), {:ok, _} <- autofollowing_users(user),
{:ok, user} <- set_cache(user), {:ok, user} <- set_cache(user),
{:ok, _} <- send_welcome_email(user), {:ok, _} <- maybe_send_registration_email(user),
{:ok, _} <- send_welcome_message(user), {:ok, _} <- maybe_send_welcome_email(user),
{:ok, _} <- send_welcome_chat_message(user) do {:ok, _} <- maybe_send_welcome_message(user),
{:ok, _} <- maybe_send_welcome_chat_message(user) do
{:ok, user} {:ok, user}
end end
end end
@ -841,7 +842,7 @@ defmodule Pleroma.User do
{:ok, :enqueued} {:ok, :enqueued}
end end
def send_welcome_message(user) do defp maybe_send_welcome_message(user) do
if User.WelcomeMessage.enabled?() do if User.WelcomeMessage.enabled?() do
User.WelcomeMessage.post_message(user) User.WelcomeMessage.post_message(user)
{:ok, :enqueued} {:ok, :enqueued}
@ -850,7 +851,7 @@ defmodule Pleroma.User do
end end
end end
def send_welcome_chat_message(user) do defp maybe_send_welcome_chat_message(user) do
if User.WelcomeChatMessage.enabled?() do if User.WelcomeChatMessage.enabled?() do
User.WelcomeChatMessage.post_message(user) User.WelcomeChatMessage.post_message(user)
{:ok, :enqueued} {:ok, :enqueued}
@ -859,7 +860,7 @@ defmodule Pleroma.User do
end end
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 if User.WelcomeEmail.enabled?() do
User.WelcomeEmail.send_email(user) User.WelcomeEmail.send_email(user)
{:ok, :enqueued} {:ok, :enqueued}
@ -868,10 +869,10 @@ defmodule Pleroma.User do
end end
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} @spec maybe_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
def try_send_confirmation_email(%User{is_confirmed: false, email: email} = user) def maybe_send_confirmation_email(%User{is_confirmed: false, email: email} = user)
when is_binary(email) do when is_binary(email) do
if Config.get([:instance, :account_activation_required]) do if Config.get([:instance, :account_activation_required]) do
send_confirmation_email(user) send_confirmation_email(user)
@ -881,7 +882,7 @@ defmodule Pleroma.User do
end end
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() @spec send_confirmation_email(Uset.t()) :: User.t()
def send_confirmation_email(%User{} = user) do def send_confirmation_email(%User{} = user) do
@ -892,6 +893,24 @@ defmodule Pleroma.User do
user user
end 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: true}), do: false
def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true

View file

@ -56,7 +56,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
nickname_or_email = params[:email] || params[:nickname] nickname_or_email = params[:email] || params[:nickname]
with %User{} = user <- User.get_by_nickname_or_email(nickname_or_email), 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, "") json_response(conn, :no_content, "")
end end
end end

View file

@ -551,6 +551,27 @@ defmodule Pleroma.UserTest do
) )
end 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 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) clear_config([:instance, :account_activation_required], true)