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