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
|
|
|
"""
|
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2019-03-06 17:07:42 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-11-05 17:49:40 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Events.{Event, FeedToken}
|
2020-06-24 16:33:59 +02:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2020-06-11 19:13:21 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2019-03-08 12:25:06 +01:00
|
|
|
alias Mobilizon.Users.User
|
2019-03-06 17:07:42 +01:00
|
|
|
|
|
|
|
@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
|
2019-04-03 17:29:03 +02:00
|
|
|
{:ok, %ICalendar{events: [do_export_event(event)]} |> ICalendar.to_ics(vendor: "Mobilizon")}
|
2019-03-06 17:07:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
|
|
|
|
def export_public_event(%Event{}), do: {:error, :event_not_public}
|
|
|
|
|
|
|
|
@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,
|
2019-04-03 17:29:03 +02:00
|
|
|
dtstamp: event.publish_at || DateTime.utc_now(),
|
2019-03-06 17:07:42 +01:00
|
|
|
dtend: event.ends_on,
|
2020-06-24 16:33:59 +02:00
|
|
|
description: HTML.strip_tags(event.description),
|
2019-03-06 17:07:42 +01:00
|
|
|
uid: event.uuid,
|
2019-11-05 17:49:40 +01:00
|
|
|
url: event.url,
|
|
|
|
geo: Address.coords(event.physical_address),
|
|
|
|
location: Address.representation(event.physical_address),
|
|
|
|
categories: event.tags |> Enum.map(& &1.title)
|
2019-03-06 17:07:42 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Export a public actor's events to iCalendar format.
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
|
2019-03-06 17:07:42 +01:00
|
|
|
"""
|
|
|
|
@spec export_public_actor(Actor.t()) :: String.t()
|
|
|
|
def export_public_actor(%Actor{} = actor) do
|
2019-09-09 00:52:49 +02:00
|
|
|
with true <- Actor.is_public_visibility(actor),
|
2019-09-16 02:07:44 +02:00
|
|
|
{:ok, events, _} <- Events.list_public_events_for_actor(actor) do
|
2019-03-06 17:07:42 +01:00
|
|
|
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
@spec export_private_actor(Actor.t()) :: String.t()
|
|
|
|
def export_private_actor(%Actor{} = actor) do
|
2019-09-26 16:38:58 +02:00
|
|
|
with events <-
|
|
|
|
actor |> Events.list_event_participations_for_actor() |> participations_to_events() do
|
2019-03-08 12:25:06 +01:00
|
|
|
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 17:07:42 +01:00
|
|
|
@doc """
|
|
|
|
Create cache for an actor
|
|
|
|
"""
|
|
|
|
def create_cache("actor_" <> name) do
|
|
|
|
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
|
|
|
{:ok, res} <- export_public_actor(actor) do
|
|
|
|
{:commit, res}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create cache for an actor
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create cache for an actor
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
@spec fetch_events_from_token(String.t()) :: String.t()
|
|
|
|
defp fetch_events_from_token(token) do
|
|
|
|
with %FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
|
|
|
|
case actor do
|
|
|
|
%Actor{} = actor ->
|
|
|
|
export_private_actor(actor)
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
events <-
|
|
|
|
actors
|
2019-09-26 16:38:58 +02:00
|
|
|
|> Enum.map(fn actor ->
|
|
|
|
actor
|
|
|
|
|> Events.list_event_participations_for_actor()
|
|
|
|
|> participations_to_events()
|
|
|
|
end)
|
2019-03-08 12:25:06 +01:00
|
|
|
|> Enum.concat() do
|
|
|
|
{:ok,
|
|
|
|
%ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-26 16:38:58 +02:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
defp participations_to_events(%Page{elements: participations}) do
|
2019-09-26 16:38:58 +02:00
|
|
|
participations
|
|
|
|
|> Enum.map(& &1.event_id)
|
|
|
|
|> Enum.map(&Events.get_event_with_preload!/1)
|
|
|
|
end
|
2019-03-06 17:07:42 +01:00
|
|
|
end
|