2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Email do
|
2019-09-17 00:45:32 +00:00
|
|
|
@moduledoc """
|
|
|
|
The Email context.
|
|
|
|
"""
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
|
2019-09-17 00:45:32 +00:00
|
|
|
|
2020-12-21 14:47:26 +00:00
|
|
|
alias Mobilizon.{Config, Events}
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Service.Export.ICalendar
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.Web.EmailView
|
|
|
|
|
2019-09-23 17:33:58 +00:00
|
|
|
@spec base_email(keyword()) :: Bamboo.Email.t()
|
|
|
|
def base_email(args) do
|
2019-09-30 16:18:04 +00:00
|
|
|
args
|
|
|
|
|> new_email()
|
2019-11-19 10:12:59 +00:00
|
|
|
|> from({Config.instance_name(), Config.instance_email_from()})
|
2019-09-23 17:33:58 +00:00
|
|
|
|> put_header("Reply-To", Config.instance_email_reply_to())
|
2021-04-27 07:06:28 +00:00
|
|
|
|> maybe_put_date_header()
|
|
|
|
|> maybe_put_message_id()
|
2021-10-13 10:57:54 +00:00
|
|
|
|> assign(:jsonLDMetadata, nil)
|
2021-09-30 07:34:39 +00:00
|
|
|
|> assign(:instance_name, Config.instance_name())
|
2021-10-18 09:08:17 +00:00
|
|
|
|> assign(:offer_unsupscription, true)
|
2020-01-28 18:18:33 +00:00
|
|
|
|> put_html_layout({EmailView, "email.html"})
|
|
|
|
|> put_text_layout({EmailView, "email.text"})
|
2019-09-17 00:45:32 +00:00
|
|
|
end
|
2020-11-13 13:02:33 +00:00
|
|
|
|
|
|
|
# Generating an UUID randomly causes Bamboo.Test.assert_delivered_email/1 to fail
|
2021-04-27 07:06:28 +00:00
|
|
|
defp maybe_put_message_id(email) do
|
2020-11-13 13:02:33 +00:00
|
|
|
if Application.fetch_env!(:mobilizon, :env) == :test do
|
2021-04-27 07:06:28 +00:00
|
|
|
put_header(email, "Message-Id", "TEST_ENV_MESSAGE_ID@#{Config.instance_hostname()}")
|
2020-11-13 13:02:33 +00:00
|
|
|
else
|
2021-04-27 07:06:28 +00:00
|
|
|
email
|
2020-11-13 13:02:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-27 07:06:28 +00:00
|
|
|
defp maybe_put_date_header(email) do
|
2020-11-13 13:02:33 +00:00
|
|
|
if Application.fetch_env!(:mobilizon, :env) == :test do
|
2021-04-27 07:06:28 +00:00
|
|
|
put_header(email, "Date", "REMOVED FOR TESTING")
|
2020-11-13 13:02:33 +00:00
|
|
|
else
|
2021-04-27 07:06:28 +00:00
|
|
|
email
|
2020-11-13 13:02:33 +00:00
|
|
|
end
|
|
|
|
end
|
2020-12-21 14:47:26 +00:00
|
|
|
|
|
|
|
def add_event_attachment(%Bamboo.Email{} = email, %Event{id: event_id}) do
|
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
|
|
|
{:ok, event_ics_data} <- ICalendar.export_event(event) do
|
|
|
|
put_attachment(email, %Bamboo.Attachment{
|
2021-06-16 15:42:50 +00:00
|
|
|
filename: "#{Slugger.slugify_downcase(event.title)}.ics",
|
2020-12-21 14:47:26 +00:00
|
|
|
content_type: "text/calendar",
|
|
|
|
data: event_ics_data
|
|
|
|
})
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
email
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 00:45:32 +00:00
|
|
|
end
|