2019-03-06 16:07:42 +00:00
|
|
|
defmodule Mobilizon.Service.Export.ICalendar do
|
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Export an event to iCalendar format.
|
2019-03-06 16:07:42 +00:00
|
|
|
"""
|
|
|
|
|
2019-11-05 16:49:40 +00:00
|
|
|
alias Mobilizon.Addresses.Address
|
2021-03-26 14:40:10 +00:00
|
|
|
alias Mobilizon.{Config, Events}
|
2021-10-12 09:53:54 +00:00
|
|
|
alias Mobilizon.Events.{Event, EventOptions}
|
2021-03-26 14:40:10 +00:00
|
|
|
alias Mobilizon.Service.Export.Common
|
2020-06-24 14:33:59 +00:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2019-03-06 16:07:42 +00:00
|
|
|
|
2021-08-16 14:15:52 +00:00
|
|
|
@item_limit 500
|
|
|
|
|
2019-03-06 16:07:42 +00:00
|
|
|
@doc """
|
2020-11-06 14:43:38 +00:00
|
|
|
Create cache for an actor, an event or an user token
|
2019-03-06 16:07:42 +00:00
|
|
|
"""
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, atom()}
|
2019-03-06 16:07:42 +00:00
|
|
|
def create_cache("actor_" <> name) do
|
2021-03-26 14:40:10 +00:00
|
|
|
case export_public_actor(name) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err} ->
|
2019-03-06 16:07:42 +00:00
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_cache("event_" <> uuid) do
|
2019-09-16 00:07:44 +00:00
|
|
|
with %Event{} = event <- Events.get_public_event_by_uuid_with_preload(uuid),
|
2019-03-06 16:07:42 +00:00
|
|
|
{:ok, res} <- export_public_event(event) do
|
|
|
|
{:commit, res}
|
|
|
|
else
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err} ->
|
2019-03-06 16:07:42 +00:00
|
|
|
{:ignore, err}
|
2021-09-24 14:46:42 +00:00
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:ignore, :event_not_found}
|
2019-03-06 16:07:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-08 11:25:06 +00:00
|
|
|
|
|
|
|
def create_cache("token_" <> token) do
|
2019-07-23 16:06:22 +00:00
|
|
|
case fetch_events_from_token(token) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err} ->
|
2019-03-08 11:25:06 +00:00
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-26 14:40:10 +00:00
|
|
|
def create_cache("instance") do
|
2021-09-24 14:46:42 +00:00
|
|
|
{:ok, res} = fetch_instance_feed()
|
|
|
|
{:commit, res}
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_instance_feed :: {:ok, String.t()}
|
|
|
|
defp fetch_instance_feed do
|
2021-09-24 14:46:42 +00:00
|
|
|
{:ok, events, _posts} = Common.fetch_instance_public_content(@item_limit)
|
|
|
|
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
2021-03-26 14:40:10 +00:00
|
|
|
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`
|
|
|
|
"""
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec export_public_event(Event.t()) :: {:ok, String.t()} | {:error, :event_not_public}
|
2021-03-26 14:40:10 +00:00
|
|
|
def export_public_event(%Event{visibility: visibility} = event)
|
|
|
|
when visibility in [:public, :unlisted] do
|
|
|
|
{:ok, events_to_ics([event])}
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_public_event(%Event{}), do: {:error, :event_not_public}
|
|
|
|
|
2021-10-05 13:29:06 +00:00
|
|
|
# Export a public actor's events to iCalendar format.
|
|
|
|
# The actor must have a visibility of `:public` or `:unlisted`, as well as the events
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec export_public_actor(String.t()) ::
|
|
|
|
{:ok, String.t()} | {:error, :actor_not_public | :actor_not_found}
|
2021-10-05 13:29:06 +00:00
|
|
|
defp export_public_actor(name, limit \\ @item_limit) do
|
2021-08-16 14:15:52 +00:00
|
|
|
case Common.fetch_actor_event_feed(name, limit) do
|
2021-03-26 14:40:10 +00:00
|
|
|
{:ok, _actor, events, _posts} ->
|
|
|
|
{:ok, events_to_ics(events)}
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err} ->
|
2021-03-26 14:40:10 +00:00
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec fetch_events_from_token(String.t(), integer()) ::
|
|
|
|
{:ok, String.t()} | {:error, :actor_not_public | :actor_not_found}
|
2021-08-16 14:15:52 +00:00
|
|
|
defp fetch_events_from_token(token, limit \\ @item_limit) do
|
2021-09-24 14:46:42 +00:00
|
|
|
case Common.fetch_events_from_token(token, limit) do
|
|
|
|
%{events: events} ->
|
|
|
|
{:ok, events_to_ics(events)}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2019-03-08 11:25:06 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-26 14:38:58 +00:00
|
|
|
|
2021-03-26 14:40:10 +00: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 08:24:57 +00:00
|
|
|
|> ICalendar.to_ics(vendor: vendor())
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec do_export_event(Event.t()) :: ICalendar.Event.t()
|
|
|
|
defp do_export_event(%Event{} = event) do
|
|
|
|
%ICalendar.Event{
|
|
|
|
summary: event.title,
|
2021-10-12 09:53:54 +00:00
|
|
|
dtstart: begins_on(event),
|
2021-03-26 14:40:10 +00:00
|
|
|
dtstamp: event.publish_at || DateTime.utc_now(),
|
2021-10-12 09:53:54 +00:00
|
|
|
dtend: ends_on(event),
|
2021-03-26 14:40:10 +00:00
|
|
|
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 14:38:58 +00:00
|
|
|
end
|
2021-06-07 08:24:57 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec vendor :: String.t()
|
2021-06-07 08:24:57 +00:00
|
|
|
defp vendor do
|
|
|
|
"Mobilizon #{Config.instance_version()}"
|
|
|
|
end
|
2021-10-12 09:53:54 +00:00
|
|
|
|
|
|
|
defp begins_on(%Event{begins_on: begins_on, options: %EventOptions{timezone: timezone}}) do
|
|
|
|
shift_tz(begins_on, timezone)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp ends_on(%Event{ends_on: ends_on, options: %EventOptions{timezone: timezone}}) do
|
|
|
|
shift_tz(ends_on, timezone)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp shift_tz(%DateTime{} = date, timezone) when is_binary(timezone) do
|
|
|
|
DateTime.shift_zone!(date, timezone)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp shift_tz(%DateTime{} = date, _), do: date
|
2019-03-06 16:07:42 +00:00
|
|
|
end
|