Skip to content

Commit

Permalink
removed from config.toml + add with statement
Browse files Browse the repository at this point in the history
  • Loading branch information
robinkwilson committed Oct 19, 2020
1 parent bb7cf47 commit 52e4d7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
2 changes: 0 additions & 2 deletions habitat/config/config.toml
Expand Up @@ -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 }}"
Expand Down
33 changes: 20 additions & 13 deletions lib/ret_web/email.ex
Expand Up @@ -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()
Expand All @@ -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

Expand Down

0 comments on commit 52e4d7c

Please sign in to comment.