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
|
2022-10-27 15:05:59 +00:00
|
|
|
require Logger
|
2019-02-27 15:28:09 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec instance(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
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-09-28 17:40:37 +00:00
|
|
|
@spec actor(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
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
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec event(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
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-09-28 17:40:37 +00:00
|
|
|
@spec going(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
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
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec return_data(Plug.Conn.t(), String.t(), String.t(), String.t()) ::
|
|
|
|
Plug.Conn.t() | {:error, :not_found}
|
2022-10-27 15:05:59 +00:00
|
|
|
defp return_data(conn, "atom", key, filename) do
|
|
|
|
case Cachex.fetch(:feed, key) 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)
|
|
|
|
|
2022-10-27 15:05:59 +00: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 07:59:09 +00:00
|
|
|
Logger.warning("Unable to find feed data cached for key #{key}, returned #{inspect(err)}")
|
2019-04-30 12:21:33 +00:00
|
|
|
{:error, :not_found}
|
2019-03-08 11:25:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-27 15:05:59 +00:00
|
|
|
defp return_data(conn, "ics", key, filename) do
|
|
|
|
case Cachex.fetch(:ics, key) 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)
|
|
|
|
|
2022-10-27 15:05:59 +00: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 07:59:09 +00:00
|
|
|
Logger.warning("Unable to find feed data cached for key #{key}, returned #{inspect(err)}")
|
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
|