From dec3615da5b05586ccafbdd1d8c6ce6ab81b2653 Mon Sep 17 00:00:00 2001 From: SomewhatDamaged Date: Sun, 23 Aug 2020 02:11:50 +1000 Subject: [PATCH] Modify limit_plug for use by API (#15) --- lib/philomena_web/plugs/limit_plug.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/philomena_web/plugs/limit_plug.ex b/lib/philomena_web/plugs/limit_plug.ex index 5aa4f9f9..f045a783 100644 --- a/lib/philomena_web/plugs/limit_plug.ex +++ b/lib/philomena_web/plugs/limit_plug.ex @@ -51,6 +51,12 @@ defmodule PhilomenaWeb.LimitPlug do |> Conn.send_resp(:multiple_choices, "") |> Conn.halt() + api?(conn) -> + conn + |> Conn.put_status(:too_many_requests) + |> Controller.text("") + |> Conn.halt() + true -> conn |> Controller.put_flash(:error, error) @@ -67,6 +73,13 @@ defmodule PhilomenaWeb.LimitPlug do defp current_user_id(%{id: id}), do: id defp current_user_id(_), do: nil + defp api?(conn) do + case conn.path_info do + ["api" | _] -> true + _ -> false + end + end + defp ajax?(conn) do case Conn.get_req_header(conn, "x-requested-with") do [value] -> String.downcase(value) == "xmlhttprequest"