2019-03-06 17:07:42 +01:00
|
|
|
defmodule Mobilizon.Service.Export.ICalendar do
|
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Export an event to iCalendar format.
|
2019-03-06 17:07:42 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2019-11-05 17:49:40 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2021-03-26 15:40:10 +01:00
|
|
|
alias Mobilizon.{Config, Events}
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Service.Export.Common
|
2020-06-24 16:33:59 +02:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2019-03-06 17:07:42 +01:00
|
|
|
|
|
|
|
@doc """
|
2020-11-06 15:43:38 +01:00
|
|
|
Create cache for an actor, an event or an user token
|
2019-03-06 17:07:42 +01:00
|
|
|
"""
|
|
|
|
def create_cache("actor_" <> name) do
|
2021-03-26 15:40:10 +01:00
|
|
|
case export_public_actor(name) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-06 17:07:42 +01:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_cache("event_" <> uuid) do
|
2019-09-16 02:07:44 +02:00
|
|
|
with %Event{} = event <- Events.get_public_event_by_uuid_with_preload(uuid),
|
2019-03-06 17:07:42 +01:00
|
|
|
{:ok, res} <- export_public_event(event) do
|
|
|
|
{:commit, res}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
|
|
|
def create_cache("token_" <> token) do
|
2019-07-23 18:06:22 +02:00
|
|
|
case fetch_events_from_token(token) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
def create_cache("instance") do
|
|
|
|
case fetch_instance_feed() do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_instance_feed :: {:ok, String.t()}
|
|
|
|
defp fetch_instance_feed do
|
|
|
|
case Common.fetch_instance_public_content() do
|
|
|
|
{:ok, events, _posts} ->
|
|
|
|
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Export an event to iCalendar format.
|
|
|
|
"""
|
|
|
|
@spec export_event(Event.t()) :: {:ok, String.t()}
|
|
|
|
def export_event(%Event{} = event), do: {:ok, events_to_ics([event])}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Export a public event to iCalendar format.
|
|
|
|
|
|
|
|
The event must have a visibility of `:public` or `:unlisted`
|
|
|
|
"""
|
|
|
|
@spec export_public_event(Event.t()) :: {:ok, String.t()}
|
|
|
|
def export_public_event(%Event{visibility: visibility} = event)
|
|
|
|
when visibility in [:public, :unlisted] do
|
|
|
|
{:ok, events_to_ics([event])}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
|
|
|
|
def export_public_event(%Event{}), do: {:error, :event_not_public}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Export a public actor's events to iCalendar format.
|
|
|
|
|
|
|
|
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
|
|
|
|
"""
|
|
|
|
@spec export_public_actor(String.t()) :: String.t()
|
|
|
|
def export_public_actor(name) do
|
|
|
|
case Common.fetch_actor_event_feed(name) do
|
|
|
|
{:ok, _actor, events, _posts} ->
|
|
|
|
{:ok, events_to_ics(events)}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec export_private_actor(Actor.t()) :: String.t()
|
|
|
|
def export_private_actor(%Actor{} = actor) do
|
|
|
|
with events <- Common.fetch_actor_private_events(actor) do
|
|
|
|
{:ok, events_to_ics(events)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
@spec fetch_events_from_token(String.t()) :: String.t()
|
|
|
|
defp fetch_events_from_token(token) do
|
2021-03-26 15:40:10 +01:00
|
|
|
with %{events: events} <- Common.fetch_events_from_token(token) do
|
|
|
|
{:ok, events_to_ics(events)}
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
2019-09-26 16:38:58 +02:00
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
@spec events_to_ics(list(Events.t())) :: String.t()
|
|
|
|
defp events_to_ics(events) do
|
|
|
|
%ICalendar{events: events |> Enum.map(&do_export_event/1)}
|
2021-06-07 10:24:57 +02:00
|
|
|
|> ICalendar.to_ics(vendor: vendor())
|
2021-03-26 15:40:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_export_event(Event.t()) :: ICalendar.Event.t()
|
|
|
|
defp do_export_event(%Event{} = event) do
|
|
|
|
%ICalendar.Event{
|
|
|
|
summary: event.title,
|
|
|
|
dtstart: event.begins_on,
|
|
|
|
dtstamp: event.publish_at || DateTime.utc_now(),
|
|
|
|
dtend: event.ends_on,
|
|
|
|
description: HTML.strip_tags(event.description),
|
|
|
|
uid: event.uuid,
|
|
|
|
url: event.url,
|
|
|
|
geo: Address.coords(event.physical_address),
|
|
|
|
location: Address.representation(event.physical_address),
|
|
|
|
categories: event.tags |> Enum.map(& &1.title)
|
|
|
|
}
|
2019-09-26 16:38:58 +02:00
|
|
|
end
|
2021-06-07 10:24:57 +02:00
|
|
|
|
|
|
|
defp vendor do
|
|
|
|
"Mobilizon #{Config.instance_version()}"
|
|
|
|
end
|
2019-03-06 17:07:42 +01:00
|
|
|
end
|