Attach event ics files to event-related emails

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-12-21 15:47:26 +01:00
parent 37c6742660
commit b69daa7b54
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
7 changed files with 33 additions and 4 deletions

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
Export an event to iCalendar format.
"""
alias Mobilizon.{Actors, Events, Users}
alias Mobilizon.{Actors, Config, Events, Users}
alias Mobilizon.Actors.Actor
alias Mobilizon.Addresses.Address
alias Mobilizon.Events.{Event, FeedToken}
@ -11,6 +11,8 @@ defmodule Mobilizon.Service.Export.ICalendar do
alias Mobilizon.Storage.Page
alias Mobilizon.Users.User
@vendor "Mobilizon #{Config.instance_version()}"
@doc """
Export a public event to iCalendar format.
@ -19,12 +21,19 @@ defmodule Mobilizon.Service.Export.ICalendar do
@spec export_public_event(Event.t()) :: {:ok, String.t()}
def export_public_event(%Event{visibility: visibility} = event)
when visibility in [:public, :unlisted] do
{:ok, %ICalendar{events: [do_export_event(event)]} |> ICalendar.to_ics(vendor: "Mobilizon")}
export_event(event)
end
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
def export_public_event(%Event{}), do: {:error, :event_not_public}
@doc """
Export an event to iCalendar format
"""
def export_event(%Event{} = event) do
{:ok, %ICalendar{events: [do_export_event(event)]} |> ICalendar.to_ics(vendor: @vendor)}
end
@spec do_export_event(Event.t()) :: ICalendar.Event.t()
defp do_export_event(%Event{} = event) do
%ICalendar.Event{

View file

@ -6,7 +6,9 @@ defmodule Mobilizon.Web.Email do
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
alias Ecto.UUID
alias Mobilizon.Config
alias Mobilizon.{Config, Events}
alias Mobilizon.Events.Event
alias Mobilizon.Service.Export.ICalendar
alias Mobilizon.Web.EmailView
@spec base_email(keyword()) :: Bamboo.Email.t()
@ -40,4 +42,18 @@ defmodule Mobilizon.Web.Email do
Timex.format!(DateTime.utc_now(), "{WDshort}, {D} {Mshort} {YYYY} {h24}:{m}:{s} {Z}")
end
end
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{
filename: Slugger.slugify_downcase(event.title),
content_type: "text/calendar",
data: event_ics_data
})
else
_ ->
email
end
end
end

View file

@ -46,6 +46,7 @@ defmodule Mobilizon.Web.Email.Event do
|> assign(:changes, changes)
|> assign(:subject, subject)
|> assign(:timezone, timezone)
|> Email.add_event_attachment(event)
|> render(:event_updated)
end

View file

@ -30,6 +30,7 @@ defmodule Mobilizon.Web.Email.Notification do
|> assign(:locale, locale)
|> assign(:participant, participant)
|> assign(:subject, subject)
|> Email.add_event_attachment(event)
|> render(:before_event_notification)
end

View file

@ -118,6 +118,7 @@ defmodule Mobilizon.Web.Email.Participation do
|> assign(:locale, locale)
|> assign(:event, event)
|> assign(:subject, subject)
|> Email.add_event_attachment(event)
|> render(:event_participation_approved)
end

View file

@ -133,6 +133,7 @@ defmodule Mobilizon.Mixfile do
{:xml_builder, "~> 2.1.1", override: true},
{:remote_ip, "~> 0.2.0"},
{:ex_cldr_languages, "~> 0.2.1"},
{:slugger, "~> 0.3"},
# Dev and test dependencies
{:phoenix_live_reload, "~> 1.2", only: [:dev, :e2e]},
{:ex_machina, "~> 2.3", only: [:dev, :test]},

View file

@ -17,7 +17,7 @@ defmodule Mobilizon.Service.ICalendarTest do
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
PRODID:-//ICalendar//Mobilizon//EN
PRODID:-//ICalendar//Mobilizon #{Mobilizon.Config.instance_version()}//EN
BEGIN:VEVENT
CATEGORIES:#{event.tags |> Enum.map(& &1.title) |> Enum.join(",")}
DESCRIPTION:Ceci est une description avec une première phrase assez longue\\,\\n puis sur une seconde ligne