From 5879d3685425bebaece3ecfe1e090654c91f44b1 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Wed, 22 Jul 2020 15:34:47 +0300 Subject: [PATCH] fix sender for welcome email --- config/config.exs | 2 +- config/description.exs | 9 +++++---- docs/configuration/cheatsheet.md | 22 +++++++++++++++++++--- lib/pleroma/user/welcome_email.ex | 24 +++++++++--------------- test/user/welcome_email_test.exs | 18 +++++++++++++++--- test/user_test.exs | 2 +- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/config/config.exs b/config/config.exs index 16b7f6dc7..baee67d93 100644 --- a/config/config.exs +++ b/config/config.exs @@ -260,7 +260,7 @@ ], email: [ enabled: false, - sender_nickname: nil, + sender: nil, subject: "Welcome to <%= instance_name %>", html: "Welcome to <%= instance_name %>", text: "Welcome to <%= instance_name %>" diff --git a/config/description.exs b/config/description.exs index 3786a608d..e012040f5 100644 --- a/config/description.exs +++ b/config/description.exs @@ -990,11 +990,12 @@ description: "Enables sends direct message for new user after registration" }, %{ - key: :sender_nickname, - type: :string, - description: "The nickname of the local user that sends the welcome email", + key: :sender, + type: [:string, :tuple], + description: + "The email address or tuple with `{nickname, email}` that will use as sender to the welcome email.", suggestions: [ - "lain" + {"Pleroma App", "welcome@pleroma.app"} ] }, %{ diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 7e8f86aba..e1eccea1f 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -46,8 +46,6 @@ To add configuration to your config file, you can copy it from the base config. * `max_pinned_statuses`: The maximum number of pinned statuses. `0` will disable the feature. * `autofollowed_nicknames`: Set to nicknames of (local) users that every new user should automatically follow. * `attachment_links`: Set to true to enable automatically adding attachment link text to statuses. -* `welcome_message`: A message that will be send to a newly registered users as a direct message. -* `welcome_user_nickname`: The nickname of the local user that sends the welcome message. * `max_report_comment_size`: The maximum size of the report comment (Default: `1000`). * `safe_dm_mentions`: If set to true, only mentions at the beginning of a post will be used to address people in direct messages. This is to prevent accidental mentioning of people when talking about them (e.g. "@friend hey i really don't like @enemy"). Default: `false`. * `healthcheck`: If set to true, system data will be shown on ``/api/pleroma/healthcheck``. @@ -70,11 +68,29 @@ To add configuration to your config file, you can copy it from the base config. * `message`: A message that will be send to a newly registered users as a direct message. * `email`: - welcome message sent as a email. * `enabled`: Enables the send a welcome email to a newly registered user. Defaults to `false`. - * `sender_nickname`: The nickname of the local user that sends the welcome email. + * `sender`: The email address or tuple with `{nickname, email}` that will use as sender to the welcome email. * `subject`: A subject of welcome email. * `html`: A html that will be send to a newly registered users as a email. * `text`: A text that will be send to a newly registered users as a email. + Example: + + ```elixir + config :pleroma, :welcome, + direct_message: [ + enabled: true, + sender_nickname: "lain", + message: "Hi, @username! Welcome on board!" + ], + email: [ + enabled: true, + sender: {"Pleroma App", "welcome@pleroma.app"}, + subject: "Welcome to <%= instance_name %>", + html: "Welcome to <%= instance_name %>", + text: "Welcome to <%= instance_name %>" + ] + ``` + ## Message rewrite facility ### :mrf diff --git a/lib/pleroma/user/welcome_email.ex b/lib/pleroma/user/welcome_email.ex index 53062b961..91a9591dd 100644 --- a/lib/pleroma/user/welcome_email.ex +++ b/lib/pleroma/user/welcome_email.ex @@ -27,7 +27,7 @@ defp email_options(user) do bindings = [user: user, instance_name: instance_name()] %{} - |> add_sender(Config.get([:welcome, :email, :sender_nickname], nil)) + |> add_sender(Config.get([:welcome, :email, :sender], nil)) |> add_option(:subject, bindings) |> add_option(:html, bindings) |> add_option(:text, bindings) @@ -40,28 +40,22 @@ defp add_option(opts, option, bindings) do |> merge_options(opts, option) end - def add_sender(opts, nickname) do - nickname - |> fetch_sender() - |> merge_options(opts, :sender) + defp add_sender(opts, {_name, _email} = sender) do + merge_options(sender, opts, :sender) end + defp add_sender(opts, sender) when is_binary(sender) do + add_sender(opts, {instance_name(), sender}) + end + + defp add_sender(opts, _), do: opts + defp merge_options(nil, options, _option), do: options defp merge_options(value, options, option) do Map.merge(options, %{option => value}) end - defp fetch_sender(nickname) when is_binary(nickname) do - with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do - {instance_name(), user.email} - else - _ -> nil - end - end - - defp fetch_sender(_), do: nil - defp eval_string(nil, _), do: nil defp eval_string("", _), do: nil defp eval_string(str, bindings), do: EEx.eval_string(str, bindings) diff --git a/test/user/welcome_email_test.exs b/test/user/welcome_email_test.exs index 1a80109d4..d005d11b2 100644 --- a/test/user/welcome_email_test.exs +++ b/test/user/welcome_email_test.exs @@ -16,11 +16,10 @@ defmodule Pleroma.User.WelcomeEmailTest do describe "send_email/1" do test "send a welcome email" do - welcome_user = insert(:user) user = insert(:user, name: "Jimm") Config.put([:welcome, :email, :enabled], true) - Config.put([:welcome, :email, :sender_nickname], welcome_user.nickname) + Config.put([:welcome, :email, :sender], "welcome@pleroma.app") Config.put( [:welcome, :email, :subject], @@ -39,7 +38,20 @@ test "send a welcome email" do ObanHelpers.perform_all() assert_email_sent( - from: {instance_name, welcome_user.email}, + from: {instance_name, "welcome@pleroma.app"}, + to: {user.name, user.email}, + subject: "Hello, welcome to pleroma: #{instance_name}", + html_body: "

Hello #{user.name}.

Welcome to #{instance_name}

" + ) + + Config.put([:welcome, :email, :sender], {"Pleroma App", "welcome@pleroma.app"}) + + {:ok, _job} = WelcomeEmail.send_email(user) + + ObanHelpers.perform_all() + + assert_email_sent( + from: {"Pleroma App", "welcome@pleroma.app"}, to: {user.name, user.email}, subject: "Hello, welcome to pleroma: #{instance_name}", html_body: "

Hello #{user.name}.

Welcome to #{instance_name}

" diff --git a/test/user_test.exs b/test/user_test.exs index e887a3fb2..132697139 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -414,7 +414,7 @@ test "it sends a welcome message if it is set" do Pleroma.Config.put([:welcome, :direct_message, :message], "Hello, this is a cool site") Pleroma.Config.put([:welcome, :email, :enabled], true) - Pleroma.Config.put([:welcome, :email, :sender_nickname], welcome_user.nickname) + Pleroma.Config.put([:welcome, :email, :sender], welcome_user.email) Pleroma.Config.put( [:welcome, :email, :subject],