diff --git a/lib/philomena_web/plugs/check_captcha_plug.ex b/lib/philomena_web/plugs/check_captcha_plug.ex index e73e7a73..d411d405 100644 --- a/lib/philomena_web/plugs/check_captcha_plug.ex +++ b/lib/philomena_web/plugs/check_captcha_plug.ex @@ -22,7 +22,7 @@ defmodule PhilomenaWeb.CheckCaptchaPlug do :error, "There was an error verifying you're not a robot. Please try again." ) - |> do_failure_response(ajax?(conn)) + |> do_failure_response(conn.assigns.ajax?) |> halt() end end @@ -54,13 +54,6 @@ defmodule PhilomenaWeb.CheckCaptchaPlug do redirect(conn, external: conn.assigns.referrer) end - def ajax?(conn) do - case get_req_header(conn, "x-requested-with") do - [value] -> String.downcase(value) == "xmlhttprequest" - _ -> false - end - end - def captcha_enabled? do Application.get_env(:philomena, :captcha) != false end diff --git a/lib/philomena_web/plugs/limit_plug.ex b/lib/philomena_web/plugs/limit_plug.ex index e572cb60..fbb92094 100644 --- a/lib/philomena_web/plugs/limit_plug.ex +++ b/lib/philomena_web/plugs/limit_plug.ex @@ -43,7 +43,7 @@ defmodule PhilomenaWeb.LimitPlug do is_staff(conn.assigns.current_user) and skip_staff -> conn - ajax?(conn) -> + conn.assigns.ajax? -> conn |> Controller.put_flash(:error, error) |> Conn.send_resp(:multiple_choices, "") @@ -78,13 +78,6 @@ defmodule PhilomenaWeb.LimitPlug do end end - defp ajax?(conn) do - case Conn.get_req_header(conn, "x-requested-with") do - [value] -> String.downcase(value) == "xmlhttprequest" - _ -> false - end - end - defp increment_after_post(conn, key, time) do Conn.register_before_send(conn, fn conn -> # Phoenix status returns 200 for form validation errors diff --git a/lib/philomena_web/plugs/referrer_plug.ex b/lib/philomena_web/plugs/referrer_plug.ex index bef81917..d7d545bd 100644 --- a/lib/philomena_web/plugs/referrer_plug.ex +++ b/lib/philomena_web/plugs/referrer_plug.ex @@ -17,14 +17,22 @@ defmodule PhilomenaWeb.ReferrerPlug do @doc false @spec call(Conn.t(), any()) :: Conn.t() def call(conn, _opts) do - case Conn.get_req_header(conn, "referer") do - [] -> - conn - |> Conn.assign(:referrer, "/") + conn + |> Conn.assign(:referrer, referer(conn)) + |> Conn.assign(:ajax?, ajax?(conn)) + end - [referrer] -> - conn - |> Conn.assign(:referrer, referrer) + defp referer(conn) do + case Conn.get_req_header(conn, "referer") do + [] -> "/" + [referrer] -> referrer + end + end + + defp ajax?(conn) do + case Conn.get_req_header(conn, "x-requested-with") do + [value] -> String.downcase(value) == "xmlhttprequest" + _ -> false end end end