support for idna domains
[akkoma] / lib / pleroma / plugs / rate_limiter.ex
index e02ba4213acbcc8d0b4c5b506b4d55cad6163b52..c5e0957e8823b2369ed414c26f2a57348b26821d 100644 (file)
@@ -14,13 +14,20 @@ defmodule Pleroma.Plugs.RateLimiter do
 
   It is also possible to have different limits for unauthenticated and authenticated users: the keyword value must be a list of two tuples where the first one is a config for unauthenticated users and the second one is for authenticated.
 
+  To disable a limiter set its value to `nil`.
+
   ### Example
 
       config :pleroma, :rate_limit,
         one: {1000, 10},
-        two: [{10_000, 10}, {10_000, 50}]
+        two: [{10_000, 10}, {10_000, 50}],
+        foobar: nil
+
+  Here we have three limiters:
 
-  Here we have two limiters: `one` which is not over 10req/1s and `two` which has two limits 10req/10s for unauthenticated users and 50req/10s for authenticated users.
+  * `one` which is not over 10req/1s
+  * `two` which has two limits: 10req/10s for unauthenticated users and 50req/10s for authenticated users
+  * `foobar` which is disabled
 
   ## Usage
 
@@ -37,8 +44,7 @@ defmodule Pleroma.Plugs.RateLimiter do
         ...
       end
   """
-
-  import Phoenix.Controller, only: [json: 2]
+  import Pleroma.Web.TranslationHelpers
   import Plug.Conn
 
   alias Pleroma.User
@@ -56,7 +62,7 @@ defmodule Pleroma.Plugs.RateLimiter do
   def call(conn, opts) do
     case check_rate(conn, opts) do
       {:ok, _count} -> conn
-      {:error, _count} -> render_error(conn)
+      {:error, _count} -> render_throttled_error(conn)
     end
   end
 
@@ -78,10 +84,9 @@ defmodule Pleroma.Plugs.RateLimiter do
     |> Enum.join(".")
   end
 
-  defp render_error(conn) do
+  defp render_throttled_error(conn) do
     conn
-    |> put_status(:too_many_requests)
-    |> json(%{error: "Throttled"})
+    |> render_error(:too_many_requests, "Throttled")
     |> halt()
   end
 end