From 52e4d7c2c8c793dfbfba474b74c59567d29a9bb9 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 19 Oct 2020 13:45:10 -0700 Subject: [PATCH] removed from config.toml + add with statement --- habitat/config/config.toml | 2 -- lib/ret_web/email.ex | 33 ++++++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/habitat/config/config.toml b/habitat/config/config.toml index 88636776d..e937f483c 100644 --- a/habitat/config/config.toml +++ b/habitat/config/config.toml @@ -242,8 +242,6 @@ server = {{ toToml cfg.email.server }} port = {{ toToml cfg.email.port }} username = {{ toToml cfg.email.username }} password = {{ toToml cfg.email.password }} -custom_email_subject = {{ toToml cfg.email.custom_email_subject }} -custom_email_message = {{ toToml cfg.email.custom_email_message }} [ret."Elixir.Ret.Support"] slack_webhook_url = "{{ cfg.support.slack_webhook_url }}" diff --git a/lib/ret_web/email.ex b/lib/ret_web/email.ex index 245ca7d42..2f136b88e 100644 --- a/lib/ret_web/email.ex +++ b/lib/ret_web/email.ex @@ -6,21 +6,28 @@ defmodule RetWeb.Email do app_name = AppConfig.get_cached_config_value("translations|en|app-name") app_full_name = AppConfig.get_cached_config_value("translations|en|app-full-name") || app_name admin_email = Application.get_env(:ret, Ret.Account)[:admin_email] - custom_login_subject = AppConfig.get_cached_config_value("auth|login_subject") - custom_login_body = AppConfig.get_cached_config_value("auth|login_body") email_subject = - if string_is_nil_or_empty(custom_login_subject), - do: "Your #{app_name} Sign-In Link", - else: custom_login_subject + with custom_login_subject <- + AppConfig.get_config_value("auth|login_subject"), + false <- string_is_nil_or_empty(custom_login_subject) do + custom_login_subject + else + _ -> + "Your #{app_name} Sign-In Link" + end email_body = - if string_is_nil_or_empty(custom_login_body), - do: + with custom_login_body <- + AppConfig.get_cached_config_value("auth|login_body"), + false <- string_is_nil_or_empty(custom_login_body) do + add_magic_link_to_custom_login_body(custom_login_body, signin_args) + else + _ -> "To sign-in to #{app_name}, please visit the link below. If you did not make this request, please ignore this e-mail.\n\n #{ RetWeb.Endpoint.url() - }/?#{URI.encode_query(signin_args)}", - else: add_magic_link_to_custom_login_body(custom_login_body, signin_args) + }/?#{URI.encode_query(signin_args)}" + end email = new_email() @@ -41,12 +48,12 @@ defmodule RetWeb.Email do end defp add_magic_link_to_custom_login_body(custom_message, signin_args) do + magic_link = "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" + if Regex.match?(~r/{{ link }}/, custom_message) do - Regex.replace(~r/{{ link }}/, custom_message, "#{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}", - global: false - ) + Regex.replace(~r/{{ link }}/, custom_message, magic_link, global: false) else - custom_message <> "\n\n #{RetWeb.Endpoint.url()}/?#{URI.encode_query(signin_args)}" + custom_message <> "\n\n" <> magic_link end end