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
|
2022-10-27 17:05:59 +02:00
|
|
|
require Logger
|
2019-02-27 16:28:09 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec instance(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
2021-03-29 18:22:14 +02:00
|
|
|
def instance(conn, %{"format" => format}) do
|
2021-03-26 15:40:10 +01: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 16:28:09 +01:00
|
|
|
end
|
|
|
|
end
|
2019-03-06 17:07:42 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec actor(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
2021-03-29 18:22:14 +02:00
|
|
|
def actor(conn, %{"format" => format, "name" => name}) do
|
2021-03-26 15:40:10 +01:00
|
|
|
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
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec event(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
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-09-28 19:40:37 +02:00
|
|
|
@spec going(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
2021-03-29 18:22:14 +02:00
|
|
|
def going(conn, %{"token" => token, "format" => format}) 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
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec return_data(Plug.Conn.t(), String.t(), String.t(), String.t()) ::
|
|
|
|
Plug.Conn.t() | {:error, :not_found}
|
2022-10-27 17:05:59 +02:00
|
|
|
defp return_data(conn, "atom", key, filename) do
|
|
|
|
case Cachex.fetch(:feed, key) 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)
|
|
|
|
|
2022-10-27 17:05:59 +02:00
|
|
|
# No need to log these two
|
|
|
|
{:ignore, :actor_not_found} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
|
|
|
{:ignore, :actor_not_public} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
|
|
|
err ->
|
2023-08-02 09:59:09 +02:00
|
|
|
Logger.warning("Unable to find feed data cached for key #{key}, returned #{inspect(err)}")
|
2019-04-30 14:21:33 +02:00
|
|
|
{:error, :not_found}
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-27 17:05:59 +02:00
|
|
|
defp return_data(conn, "ics", key, filename) do
|
|
|
|
case Cachex.fetch(:ics, key) 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)
|
|
|
|
|
2022-10-27 17:05:59 +02:00
|
|
|
# No need to log these two
|
|
|
|
{:ignore, :actor_not_found} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
|
|
|
{:ignore, :actor_not_public} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
|
|
|
err ->
|
2023-08-02 09:59:09 +02:00
|
|
|
Logger.warning("Unable to find feed data cached for key #{key}, returned #{inspect(err)}")
|
2019-04-30 14:21:33 +02:00
|
|
|
{:error, :not_found}
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
2021-03-29 18:22:14 +02:00
|
|
|
|
|
|
|
defp return_data(_conn, _, _, _) do
|
|
|
|
{:error, :not_found}
|
|
|
|
end
|
2019-02-27 16:28:09 +01:00
|
|
|
end
|