Implement public actor ICS endpoint and event ICS export

Closes #83 and #84

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-03-06 17:07:42 +01:00
parent 4d47eb5c78
commit d3e2f28b49
9 changed files with 208 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ defmodule Mobilizon.Application do
"""
use Application
import Cachex.Spec
alias Mobilizon.Service.Export.{Feed, ICalendar}
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@@ -29,11 +30,27 @@ defmodule Mobilizon.Application do
default: :timer.minutes(60),
interval: :timer.seconds(60)
),
fallback: fallback(default: &Mobilizon.Service.Feed.create_cache/1)
fallback: fallback(default: &Feed.create_cache/1)
]
],
id: :cache_feed
),
worker(
Cachex,
[
:ics,
[
limit: 2500,
expiration:
expiration(
default: :timer.minutes(60),
interval: :timer.seconds(60)
),
fallback: fallback(default: &ICalendar.create_cache/1)
]
],
id: :cache_ics
),
worker(
Cachex,
[

View File

@@ -1,23 +0,0 @@
defmodule Mobilizon.Export.ICalendar do
@moduledoc """
Export an event to iCalendar format
"""
alias Mobilizon.Events.Event
@spec export_event(%Event{}) :: String
def export_event(%Event{} = event) do
events = [
%ICalendar.Event{
summary: event.title,
dtstart: event.begins_on,
dtend: event.ends_on,
description: event.description,
uid: event.uuid
}
]
%ICalendar{events: events}
|> ICalendar.to_ics()
end
end

View File

@@ -17,4 +17,32 @@ defmodule MobilizonWeb.FeedController do
|> send_file(404, "priv/static/index.html")
end
end
def actor(conn, %{"name" => name, "format" => "ics"}) do
with {status, data} when status in [:ok, :commit] <-
Cachex.fetch(:ics, "actor_" <> name) do
conn
|> put_resp_content_type("text/calendar")
|> send_resp(200, data)
else
_err ->
conn
|> put_resp_content_type("text/html")
|> send_file(404, "priv/static/index.html")
end
end
def event(conn, %{"uuid" => uuid, "format" => "ics"}) do
with {status, data} when status in [:ok, :commit] <-
Cachex.fetch(:ics, "event_" <> uuid) do
conn
|> put_resp_content_type("text/calendar")
|> send_resp(200, data)
else
_err ->
conn
|> put_resp_content_type("text/html")
|> send_file(404, "priv/static/index.html")
end
end
end

View File

@@ -26,8 +26,8 @@ defmodule MobilizonWeb.Router do
plug(:accepts, ["html", "activity-json"])
end
pipeline :rss do
plug(:accepts, ["atom", "html"])
pipeline :atom_and_ical do
plug(:accepts, ["atom", "ics", "html"])
end
pipeline :browser do
@@ -60,9 +60,10 @@ defmodule MobilizonWeb.Router do
end
scope "/", MobilizonWeb do
pipe_through(:rss)
pipe_through(:atom_and_ical)
get("/@:name/feed/:format", FeedController, :actor)
get("/events/:uuid/export/:format", FeedController, :event)
end
scope "/", MobilizonWeb do

View File

@@ -1,4 +1,4 @@
defmodule Mobilizon.Service.Feed do
defmodule Mobilizon.Service.Export.Feed do
@moduledoc """
Serve Atom Syndication Feeds
"""

View File

@@ -0,0 +1,75 @@
defmodule Mobilizon.Service.Export.ICalendar do
@moduledoc """
Export an event to iCalendar format
"""
alias Mobilizon.Events.Event
alias Mobilizon.Events
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors
@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, %ICalendar{events: [do_export_event(event)]} |> ICalendar.to_ics()}
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,
dtend: event.ends_on,
description: event.description,
uid: event.uuid,
categories: [event.category] ++ (event.tags |> Enum.map(& &1.slug))
}
end
@doc """
Export a public actor's events to iCalendar format.
The events must have a visibility of `:public` or `:unlisted`
"""
# TODO: The actor should also have visibility options
@spec export_public_actor(Actor.t()) :: String.t()
def export_public_actor(%Actor{} = actor) do
with {:ok, events, _} <- Events.get_public_events_for_actor(actor) do
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
end
end
@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
with %Event{} = event <- Events.get_event_full_by_uuid(uuid),
{:ok, res} <- export_public_event(event) do
{:commit, res}
else
err ->
{:ignore, err}
end
end
end