centralize ajax determination

This commit is contained in:
byte[] 2020-09-27 23:47:32 -04:00
parent c7cd7f3af3
commit fc159c3782
3 changed files with 17 additions and 23 deletions

View file

@ -22,7 +22,7 @@ defmodule PhilomenaWeb.CheckCaptchaPlug do
:error, :error,
"There was an error verifying you're not a robot. Please try again." "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() |> halt()
end end
end end
@ -54,13 +54,6 @@ defmodule PhilomenaWeb.CheckCaptchaPlug do
redirect(conn, external: conn.assigns.referrer) redirect(conn, external: conn.assigns.referrer)
end 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 def captcha_enabled? do
Application.get_env(:philomena, :captcha) != false Application.get_env(:philomena, :captcha) != false
end end

View file

@ -43,7 +43,7 @@ defmodule PhilomenaWeb.LimitPlug do
is_staff(conn.assigns.current_user) and skip_staff -> is_staff(conn.assigns.current_user) and skip_staff ->
conn conn
ajax?(conn) -> conn.assigns.ajax? ->
conn conn
|> Controller.put_flash(:error, error) |> Controller.put_flash(:error, error)
|> Conn.send_resp(:multiple_choices, "") |> Conn.send_resp(:multiple_choices, "")
@ -78,13 +78,6 @@ defmodule PhilomenaWeb.LimitPlug do
end end
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 defp increment_after_post(conn, key, time) do
Conn.register_before_send(conn, fn conn -> Conn.register_before_send(conn, fn conn ->
# Phoenix status returns 200 for form validation errors # Phoenix status returns 200 for form validation errors

View file

@ -17,14 +17,22 @@ defmodule PhilomenaWeb.ReferrerPlug do
@doc false @doc false
@spec call(Conn.t(), any()) :: Conn.t() @spec call(Conn.t(), any()) :: Conn.t()
def call(conn, _opts) do def call(conn, _opts) do
case Conn.get_req_header(conn, "referer") do conn
[] -> |> Conn.assign(:referrer, referer(conn))
conn |> Conn.assign(:ajax?, ajax?(conn))
|> Conn.assign(:referrer, "/") end
[referrer] -> defp referer(conn) do
conn case Conn.get_req_header(conn, "referer") do
|> Conn.assign(:referrer, referrer) [] -> "/"
[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 end
end end