2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.FeedController do
|
2019-02-27 16:28:09 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller to serve RSS, ATOM and iCal Feeds
|
|
|
|
"""
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web, :controller
|
2019-07-08 16:53:54 +02:00
|
|
|
plug(:put_layout, false)
|
2020-01-26 21:36:50 +01:00
|
|
|
action_fallback(Mobilizon.Web.FallbackController)
|
2021-03-26 15:40:10 +01:00
|
|
|
alias Mobilizon.Config
|
2019-02-27 16:28:09 +01:00
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
@formats ["ics", "atom"]
|
2019-07-23 18:06:22 +02:00
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
def instance(conn, %{"format" => format}) when format in @formats do
|
|
|
|
if Config.get([:instance, :enable_instance_feeds], false) do
|
|
|
|
return_data(conn, format, "instance", Config.instance_name())
|
|
|
|
else
|
|
|
|
send_resp(conn, 401, "Instance feeds are not enabled.")
|
2019-02-27 16:28:09 +01:00
|
|
|
end
|
|
|
|
end
|
2019-03-06 17:07:42 +01:00
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
def actor(conn, %{"format" => format, "name" => name}) when format in @formats do
|
|
|
|
return_data(conn, format, "actor_" <> name, name)
|
2019-03-06 17:07:42 +01:00
|
|
|
end
|
|
|
|
|
2021-01-19 11:24:21 +01:00
|
|
|
def actor(_conn, _) do
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
|
2019-03-06 17:07:42 +01:00
|
|
|
def event(conn, %{"uuid" => uuid, "format" => "ics"}) do
|
2021-03-26 19:01:55 +01:00
|
|
|
return_data(conn, "ics", "event_" <> uuid, "event")
|
2019-03-06 17:07:42 +01:00
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
2021-01-19 11:24:21 +01:00
|
|
|
def event(_conn, _) do
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
def going(conn, %{"token" => token, "format" => format}) when format in @formats do
|
2021-03-26 19:01:55 +01:00
|
|
|
return_data(conn, format, "token_" <> token, "events")
|
2021-03-26 15:40:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def going(_conn, _) do
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp return_data(conn, "atom", type, filename) do
|
|
|
|
case Cachex.fetch(:feed, type) do
|
2019-10-14 15:44:16 +02:00
|
|
|
{status, data} when status in [:commit, :ok] ->
|
2019-07-23 18:06:22 +02:00
|
|
|
conn
|
2021-03-26 15:40:10 +01:00
|
|
|
|> put_resp_content_type("application/atom+xml")
|
|
|
|
|> put_resp_header(
|
|
|
|
"content-disposition",
|
|
|
|
"attachment; filename=\"#{filename}.atom\""
|
|
|
|
)
|
2019-07-23 18:06:22 +02:00
|
|
|
|> send_resp(200, data)
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
_err ->
|
2019-04-30 14:21:33 +02:00
|
|
|
{:error, :not_found}
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
defp return_data(conn, "ics", type, filename) do
|
|
|
|
case Cachex.fetch(:ics, type) do
|
2019-10-14 15:44:16 +02:00
|
|
|
{status, data} when status in [:commit, :ok] ->
|
2019-07-23 18:06:22 +02:00
|
|
|
conn
|
2021-03-26 15:40:10 +01:00
|
|
|
|> put_resp_content_type("text/calendar")
|
|
|
|
|> put_resp_header(
|
|
|
|
"content-disposition",
|
|
|
|
"attachment; filename=\"#{filename}.ics\""
|
|
|
|
)
|
2019-07-23 18:06:22 +02:00
|
|
|
|> send_resp(200, data)
|
|
|
|
|
2021-03-26 15:40:10 +01:00
|
|
|
_ ->
|
2019-04-30 14:21:33 +02:00
|
|
|
{:error, :not_found}
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
2019-02-27 16:28:09 +01:00
|
|
|
end
|