Refactoring of Email context

master
miffy 3 years ago
parent f316f0a940
commit 96f51d3a27
  1. 42
      lib/mobilizon/email/admin.ex
  2. 17
      lib/mobilizon/email/email.ex
  3. 75
      lib/mobilizon/email/user.ex
  4. 2
      lib/mobilizon/mailer.ex

@ -1,38 +1,36 @@
defmodule Mobilizon.Email.Admin do
@moduledoc """
Handles emails sent to admins
Handles emails sent to admins.
"""
alias Mobilizon.Users.User
import Bamboo.Email
import Bamboo.Phoenix
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
def report(%User{email: email} = _user, %Report{} = report, locale \\ "en") do
@spec report(User.t(), Report.t(), String.t()) :: Bamboo.Email.t()
def report(%User{email: email}, %Report{} = report, locale \\ "en") do
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)
|> subject(gettext("Mobilizon: New report on instance %{instance}", instance: instance_url))
|> put_header("Reply-To", get_config(:email_reply_to))
|> subject(subject)
|> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:report, report)
|> assign(:instance, instance_url)
|> render(:report)
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

@ -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

@ -1,57 +1,62 @@
defmodule Mobilizon.Email.User do
@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
import Bamboo.{Email, Phoenix}
import MobilizonWeb.Gettext
def confirmation_email(%User{} = user, locale \\ "en") do
alias Mobilizon.{Config, Email}
alias Mobilizon.Users.User
@spec confirmation_email(User.t(), String.t()) :: Bamboo.Email.t()
def confirmation_email(
%User{email: email, confirmation_token: confirmation_token},
locale \\ "en"
) do
Gettext.put_locale(locale)
instance_url = get_config(:instance)
base_email()
|> to(user.email)
|> subject(
gettext("Mobilizon: Confirmation instructions for %{instance}", instance: instance_url)
)
|> put_header("Reply-To", get_config(:email_reply_to))
|> 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)
|> render(:registration_confirmation)
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)
instance_url = get_config(:hostname)
base_email()
|> to(user.email)
|> subject(
instance_url = Config.instance_url()
subject =
gettext(
"Mobilizon: Reset your password on %{instance} instructions",
instance: instance_url
)
)
|> put_header("Reply-To", get_config(:email_reply_to))
|> assign(:token, user.reset_password_token)
Email.base_email()
|> to(email)
|> subject(subject)
|> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:token, reset_password_token)
|> assign(:instance, instance_url)
|> render(:password_reset)
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

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

Loading…
Cancel
Save