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.
|
|
|
|
"""
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView, layout: {Mobilizon.Web.EmailView, :email}
|
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
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
@spec base_email(keyword()) :: Swoosh.Email.t()
|
2019-09-23 17:33:58 +00:00
|
|
|
def base_email(args) do
|
2022-04-05 10:16:22 +00:00
|
|
|
[reply_to: Config.instance_email_reply_to() || Config.instance_email_from()]
|
|
|
|
|> Keyword.merge(args)
|
|
|
|
|> new()
|
2019-11-19 10:12:59 +00:00
|
|
|
|> from({Config.instance_name(), Config.instance_email_from()})
|
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)
|
2022-04-05 10:16:22 +00:00
|
|
|
|> put_layout({EmailView, :email})
|
2019-09-17 00:45:32 +00:00
|
|
|
end
|
2020-11-13 13:02:33 +00:00
|
|
|
|
2022-04-07 16:37:44 +00:00
|
|
|
@spec add_event_attachment(Swoosh.Email.t(), Event.t()) :: Swoosh.Email.t()
|
2022-04-05 10:16:22 +00:00
|
|
|
def add_event_attachment(%Swoosh.Email{} = email, %Event{id: event_id}) do
|
2020-12-21 14:47:26 +00:00
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
|
|
|
{:ok, event_ics_data} <- ICalendar.export_event(event) do
|
2022-04-05 10:16:22 +00:00
|
|
|
attachment(email, %Swoosh.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
|