Merge branch 'feature/ics-endpoints' into 'master'

Implement public actor ICS endpoint and event ICS export

Closes #84 et #83

See merge request framasoft/mobilizon!89
This commit is contained in:
Thomas Citharel 2019-03-06 17:24:32 +01:00
commit f4c09c36a7
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

View File

@ -65,7 +65,8 @@ defmodule Mobilizon.Mixfile do
{:geo, "~> 3.0"},
{:geo_postgis, "~> 3.1"},
{:timex, "~> 3.0"},
{:icalendar, "~> 0.6"},
# Waiting for https://github.com/lpil/icalendar/pull/29
{:icalendar, git: "https://framagit.org/tcit/icalendar"},
{:exgravatar, "~> 2.0.1"},
{:httpoison, "~> 1.0"},
{:json_ld, "~> 0.3"},

View File

@ -55,7 +55,7 @@
"hackney": {:hex, :hackney, "1.15.1", "9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"http_sign": {:hex, :http_sign, "0.1.1", "b16edb83aa282892f3271f9a048c155e772bf36e15700ab93901484c55f8dd10", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.5.0", "71ae9f304bdf7f00e9cd1823f275c955bdfc68282bc5eb5c85c3a9ade865d68e", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"icalendar": {:hex, :icalendar, "0.7.0", "6acf28c7e38ad1c4515c59e336878fb78bb646c8aa70d2ee3786ea194711a7b7", [:mix], [{:timex, "~> 3.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm"},
"icalendar": {:git, "git@framagit.org:tcit/icalendar.git", "7090ac1f72093c6178a67e167ebaed248f60dd64", []},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"jose": {:hex, :jose, "1.9.0", "4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm"},

View File

@ -4,7 +4,7 @@ defmodule MobilizonWeb.FeedControllerTest do
alias MobilizonWeb.Router.Helpers, as: Routes
alias MobilizonWeb.Endpoint
describe "/@:preferred_username.atom" do
describe "/@:preferred_username/feed/atom" do
test "it returns an RSS representation of the actor's public events", %{conn: conn} do
actor = insert(:actor)
tag1 = insert(:tag, title: "RSS", slug: "rss")
@ -61,4 +61,82 @@ defmodule MobilizonWeb.FeedControllerTest do
assert response(conn, 404)
end
end
describe "/@:preferred_username/feed/ics" do
test "it returns an iCalendar representation of the actor's public events", %{conn: conn} do
actor = insert(:actor)
tag1 = insert(:tag, title: "iCalendar", slug: "icalendar")
tag2 = insert(:tag, title: "Apple", slug: "apple")
event1 = insert(:event, organizer_actor: actor, tags: [tag1])
event2 = insert(:event, organizer_actor: actor, tags: [tag1, tag2])
conn =
conn
|> get(
Routes.feed_url(Endpoint, :actor, actor.preferred_username, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
[entry1, entry2] = entries = ExIcal.parse(conn.resp_body)
Enum.each(entries, fn entry ->
assert entry.summary in [event1.title, event2.title]
end)
assert entry1.categories == [event1.category, tag1.slug]
assert entry2.categories == [event2.category, tag1.slug, tag2.slug]
end
test "it returns an iCalendar representation of the actor's public events with the proper accept header",
%{conn: conn} do
actor = insert(:actor)
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get(
Routes.feed_url(Endpoint, :actor, actor.preferred_username, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
end
test "it doesn't return anything for an not existing actor", %{conn: conn} do
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get("/@notexistent/feed/ics")
assert response(conn, 404)
end
end
describe "/events/:uuid/export/ics" do
test "it returns an iCalendar representation of the event", %{conn: conn} do
tag1 = insert(:tag, title: "iCalendar", slug: "icalendar")
tag2 = insert(:tag, title: "Apple", slug: "apple")
event1 = insert(:event, tags: [tag1, tag2])
conn =
conn
|> get(
Routes.feed_url(Endpoint, :event, event1.uuid, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
[entry1] = ExIcal.parse(conn.resp_body)
assert entry1.summary == event1.title
assert entry1.categories == [event1.category, tag1.slug, tag2.slug]
end
end
end