Refactoring of Email context

This commit is contained in:
miffy 2019-09-07 23:58:53 +02:00
parent f316f0a940
commit 96f51d3a27
4 changed files with 81 additions and 61 deletions

View File

@ -1,38 +1,36 @@
defmodule Mobilizon.Email.Admin do defmodule Mobilizon.Email.Admin do
@moduledoc """ @moduledoc """
Handles emails sent to admins Handles emails sent to admins.
""" """
use Bamboo.Phoenix, view: Mobilizon.EmailView
import Bamboo.{Email, Phoenix}
import MobilizonWeb.Gettext
alias Mobilizon.{Config, Email}
alias Mobilizon.Reports.Report
alias Mobilizon.Users.User alias Mobilizon.Users.User
import Bamboo.Email @spec report(User.t(), Report.t(), String.t()) :: Bamboo.Email.t()
import Bamboo.Phoenix def report(%User{email: email}, %Report{} = report, locale \\ "en") do
use Bamboo.Phoenix, view: Mobilizon.EmailView
import MobilizonWeb.Gettext
alias Mobilizon.Reports.Report
def report(%User{email: email} = _user, %Report{} = report, locale \\ "en") do
Gettext.put_locale(locale) Gettext.put_locale(locale)
instance_url = get_config(:hostname)
base_email() instance_url = Config.instance_url()
subject =
gettext(
"Mobilizon: New report on instance %{instance}",
instance: instance_url
)
Email.base_email()
|> to(email) |> to(email)
|> subject(gettext("Mobilizon: New report on instance %{instance}", instance: instance_url)) |> subject(subject)
|> put_header("Reply-To", get_config(:email_reply_to)) |> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:report, report) |> assign(:report, report)
|> assign(:instance, instance_url) |> assign(:instance, instance_url)
|> render(:report) |> render(:report)
end end
defp base_email do
# Here you can set a default from, default headers, etc.
new_email()
|> from(get_config(:email_from))
|> put_html_layout({Mobilizon.EmailView, "email.html"})
|> put_text_layout({Mobilizon.EmailView, "email.text"})
end
@spec get_config(atom()) :: any()
defp get_config(key) do
Mobilizon.CommonConfig.instance_config() |> Keyword.get(key)
end
end end

View File

@ -0,0 +1,17 @@
defmodule Mobilizon.Email do
@moduledoc """
The Email context.
"""
use Bamboo.Phoenix, view: Mobilizon.EmailView
alias Mobilizon.Config
@spec base_email :: Bamboo.Email.t()
def base_email do
new_email()
|> from(Config.instance_email_from())
|> put_html_layout({Mobilizon.EmailView, "email.html"})
|> put_text_layout({Mobilizon.EmailView, "email.text"})
end
end

View File

@ -1,57 +1,62 @@
defmodule Mobilizon.Email.User do defmodule Mobilizon.Email.User do
@moduledoc """ @moduledoc """
Handles emails sent to users Handles emails sent to users.
""" """
alias Mobilizon.Users.User
import Bamboo.Email
import Bamboo.Phoenix
use Bamboo.Phoenix, view: Mobilizon.EmailView use Bamboo.Phoenix, view: Mobilizon.EmailView
import Bamboo.{Email, Phoenix}
import MobilizonWeb.Gettext import MobilizonWeb.Gettext
def confirmation_email(%User{} = user, locale \\ "en") do alias Mobilizon.{Config, Email}
Gettext.put_locale(locale) alias Mobilizon.Users.User
instance_url = get_config(:instance)
base_email() @spec confirmation_email(User.t(), String.t()) :: Bamboo.Email.t()
|> to(user.email) def confirmation_email(
|> subject( %User{email: email, confirmation_token: confirmation_token},
gettext("Mobilizon: Confirmation instructions for %{instance}", instance: instance_url) locale \\ "en"
) ) do
|> put_header("Reply-To", get_config(:email_reply_to)) Gettext.put_locale(locale)
|> assign(:token, user.confirmation_token)
instance_url = Config.instance_url()
subject =
gettext(
"Mobilizon: Confirmation instructions for %{instance}",
instance: instance_url
)
Email.base_email()
|> to(email)
|> subject(subject)
|> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:token, confirmation_token)
|> assign(:instance, instance_url) |> assign(:instance, instance_url)
|> render(:registration_confirmation) |> render(:registration_confirmation)
end end
def reset_password_email(%User{} = user, locale \\ "en") do @spec reset_password_email(User.t(), String.t()) :: Bamboo.Email.t()
def reset_password_email(
%User{email: email, reset_password_token: reset_password_token},
locale \\ "en"
) do
Gettext.put_locale(locale) Gettext.put_locale(locale)
instance_url = get_config(:hostname)
base_email() instance_url = Config.instance_url()
|> to(user.email)
|> subject( subject =
gettext( gettext(
"Mobilizon: Reset your password on %{instance} instructions", "Mobilizon: Reset your password on %{instance} instructions",
instance: instance_url instance: instance_url
) )
)
|> put_header("Reply-To", get_config(:email_reply_to)) Email.base_email()
|> assign(:token, user.reset_password_token) |> to(email)
|> subject(subject)
|> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:token, reset_password_token)
|> assign(:instance, instance_url) |> assign(:instance, instance_url)
|> render(:password_reset) |> render(:password_reset)
end end
defp base_email do
# Here you can set a default from, default headers, etc.
new_email()
|> from(get_config(:email_from))
|> put_html_layout({Mobilizon.EmailView, "email.html"})
|> put_text_layout({Mobilizon.EmailView, "email.text"})
end
@spec get_config(atom()) :: any()
defp get_config(key) do
Mobilizon.CommonConfig.instance_config() |> Keyword.get(key)
end
end end

View File

@ -1,6 +1,6 @@
defmodule Mobilizon.Mailer do defmodule Mobilizon.Mailer do
@moduledoc """ @moduledoc """
Mailer Mobilizon Mailer.
""" """
use Bamboo.Mailer, otp_app: :mobilizon use Bamboo.Mailer, otp_app: :mobilizon
end end