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
|
|
|
"""
|
|
|
|
|
2021-11-09 10:46:54 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
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}
|
2022-03-24 11:51:23 +00:00
|
|
|
alias Mobilizon.Service.Export.{Cachable, Common}
|
2020-06-24 14:33:59 +00:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2019-03-06 16:07:42 +00:00
|
|
|
|
2022-03-24 11:51:23 +00:00
|
|
|
@behaviour Cachable
|
|
|
|
|
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
|
|
|
"""
|
2022-03-24 11:51:23 +00:00
|
|
|
@impl Cachable
|
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
|
|
|
|
|
2022-03-24 11:51:23 +00:00
|
|
|
@impl Cachable
|
2019-03-06 16:07:42 +00:00
|
|
|
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
|
|
|
|
2022-03-24 11:51:23 +00:00
|
|
|
@impl Cachable
|
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
|
|
|
|
|
2022-03-24 11:51:23 +00:00
|
|
|
@impl Cachable
|
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
|
2021-11-04 11:41:32 +00:00
|
|
|
icalendar_event = %ICalendar.Event{
|
2021-03-26 14:40:10 +00:00
|
|
|
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,
|
2021-11-09 10:46:54 +00:00
|
|
|
url: event.url,
|
|
|
|
organizer: organizer(event)
|
2021-03-26 14:40:10 +00:00
|
|
|
}
|
2021-11-04 11:41:32 +00:00
|
|
|
|
|
|
|
icalendar_event =
|
|
|
|
if event.physical_address do
|
|
|
|
%ICalendar.Event{
|
|
|
|
icalendar_event
|
|
|
|
| geo: Address.coords(event.physical_address),
|
|
|
|
location: Address.representation(event.physical_address)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
icalendar_event
|
|
|
|
end
|
|
|
|
|
|
|
|
icalendar_event =
|
|
|
|
if length(event.tags) > 0 do
|
|
|
|
%ICalendar.Event{icalendar_event | categories: event.tags |> Enum.map(& &1.title)}
|
|
|
|
else
|
|
|
|
icalendar_event
|
|
|
|
end
|
|
|
|
|
|
|
|
icalendar_event
|
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
|
2022-01-10 09:18:43 +00:00
|
|
|
defp shift_tz(nil, _), do: nil
|
2021-11-09 10:46:54 +00:00
|
|
|
|
|
|
|
defp organizer(%Event{attributed_to: %Actor{} = group}) do
|
|
|
|
Actor.display_name(group)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp organizer(%Event{organizer_actor: %Actor{} = profile}) do
|
|
|
|
Actor.display_name(profile)
|
|
|
|
end
|
2022-03-24 11:51:23 +00:00
|
|
|
|
|
|
|
@impl Cachable
|
|
|
|
@spec clear_caches(%{
|
|
|
|
:__struct__ => Mobilizon.Actors.Actor | Mobilizon.Events.Event | Mobilizon.Posts.Post,
|
|
|
|
optional(any) => any
|
|
|
|
}) :: {:error, boolean} | {:ok, boolean}
|
|
|
|
def clear_caches(%Event{attributed_to: %Actor{} = actor} = event) do
|
|
|
|
clear_actor_feed(actor)
|
|
|
|
clear_caches(%{event | attributed_to: nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Cachable
|
|
|
|
def clear_caches(%Event{uuid: uuid}) do
|
|
|
|
# TODO: It would be nice to clear feed token cache based on participations as well,
|
|
|
|
# but that's harder, as it would require loading all participations
|
|
|
|
Cachex.del(:ics, "event_#{uuid}")
|
|
|
|
clear_instance()
|
|
|
|
end
|
|
|
|
|
|
|
|
# Not applicable for posts
|
|
|
|
@impl Cachable
|
|
|
|
def clear_caches(%Mobilizon.Posts.Post{}) do
|
|
|
|
{:ok, true}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Cachable
|
|
|
|
def clear_caches(%Actor{} = actor) do
|
|
|
|
clear_actor_feed(actor)
|
|
|
|
|
|
|
|
clear_instance()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp clear_instance do
|
|
|
|
Cachex.del(:ics, "instance")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp clear_actor_feed(%Actor{preferred_username: preferred_username} = actor) do
|
|
|
|
if Actor.is_public_visibility?(actor) do
|
|
|
|
Cachex.del(:ics, "actor_#{preferred_username}")
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 16:07:42 +00:00
|
|
|
end
|