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,
"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

View file

@ -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

View file

@ -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