2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Email do
|
2019-09-17 02:45:32 +02:00
|
|
|
@moduledoc """
|
|
|
|
The Email context.
|
|
|
|
"""
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView, layout: {Mobilizon.Web.EmailView, :email}
|
2019-09-17 02:45:32 +02:00
|
|
|
|
2020-12-21 15:47:26 +01:00
|
|
|
alias Mobilizon.{Config, Events}
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Service.Export.ICalendar
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.EmailView
|
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
@spec base_email(keyword()) :: Swoosh.Email.t()
|
2019-09-23 19:33:58 +02:00
|
|
|
def base_email(args) do
|
2022-04-05 12:16:22 +02:00
|
|
|
[reply_to: Config.instance_email_reply_to() || Config.instance_email_from()]
|
|
|
|
|> Keyword.merge(args)
|
|
|
|
|> new()
|
2019-11-19 11:12:59 +01:00
|
|
|
|> from({Config.instance_name(), Config.instance_email_from()})
|
2021-10-13 12:57:54 +02:00
|
|
|
|> assign(:jsonLDMetadata, nil)
|
2021-09-30 09:34:39 +02:00
|
|
|
|> assign(:instance_name, Config.instance_name())
|
2021-10-18 11:08:17 +02:00
|
|
|
|> assign(:offer_unsupscription, true)
|
2022-04-05 12:16:22 +02:00
|
|
|
|> put_layout({EmailView, :email})
|
2019-09-17 02:45:32 +02:00
|
|
|
end
|
2020-11-13 14:02:33 +01:00
|
|
|
|
2022-04-05 12:16:22 +02:00
|
|
|
def add_event_attachment(%Swoosh.Email{} = email, %Event{id: event_id}) do
|
2020-12-21 15:47:26 +01:00
|
|
|
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
|
|
|
{:ok, event_ics_data} <- ICalendar.export_event(event) do
|
2022-04-05 12:16:22 +02:00
|
|
|
attachment(email, %Swoosh.Attachment{
|
2021-06-16 17:42:50 +02:00
|
|
|
filename: "#{Slugger.slugify_downcase(event.title)}.ics",
|
2020-12-21 15:47:26 +01:00
|
|
|
content_type: "text/calendar",
|
|
|
|
data: event_ics_data
|
|
|
|
})
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
email
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 02:45:32 +02:00
|
|
|
end
|