add license boilerplate to pleroma core
[akkoma] / lib / pleroma / captcha / captcha.ex
index 2dcbc47174a5f4b62ffceaf1f78d80eac9c57c4e..f80946c8bb5dd27abaf075652fdcb608d1005bb5 100644 (file)
@@ -1,3 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
 defmodule Pleroma.Captcha do
   use GenServer
 
@@ -14,6 +18,10 @@ defmodule Pleroma.Captcha do
     ets_name = Module.concat(method(), Ets)
     ^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options)
 
+    # Clean up old captchas every few minutes
+    seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
+    Process.send_after(self(), :cleanup, 1000 * seconds_retained)
+
     {:ok, nil}
   end
 
@@ -38,13 +46,7 @@ defmodule Pleroma.Captcha do
     if !enabled do
       {:reply, %{type: :none}, state}
     else
-      new_captcha = method().new()
-
-      minutes_retained = Pleroma.Config.get!([__MODULE__, :minutes_retained])
-      # Wait several minutes and if the captcha is still there, delete it
-      Process.send_after(self(), {:cleanup, new_captcha.token}, 1000 * 60 * minutes_retained)
-
-      {:reply, new_captcha, state}
+      {:reply, method().new(), state}
     end
   end
 
@@ -54,8 +56,12 @@ defmodule Pleroma.Captcha do
   end
 
   @doc false
-  def handle_info({:cleanup, token}, state) do
-    method().cleanup(token)
+  def handle_info(:cleanup, state) do
+    :ok = method().cleanup()
+
+    seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
+    # Schedule the next clenup
+    Process.send_after(self(), :cleanup, 1000 * seconds_retained)
 
     {:noreply, state}
   end