Merge branch 'feature/new-user-routes' into 'develop'
[akkoma] / lib / pleroma / captcha / captcha.ex
1 defmodule Pleroma.Captcha do
2 use GenServer
3
4 @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
5
6 @doc false
7 def start_link() do
8 GenServer.start_link(__MODULE__, [], name: __MODULE__)
9 end
10
11 @doc false
12 def init(_) do
13 # Create a ETS table to store captchas
14 ets_name = Module.concat(method(), Ets)
15 ^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options)
16
17 # Clean up old captchas every few minutes
18 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
19 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
20
21 {:ok, nil}
22 end
23
24 @doc """
25 Ask the configured captcha service for a new captcha
26 """
27 def new() do
28 GenServer.call(__MODULE__, :new)
29 end
30
31 @doc """
32 Ask the configured captcha service to validate the captcha
33 """
34 def validate(token, captcha) do
35 GenServer.call(__MODULE__, {:validate, token, captcha})
36 end
37
38 @doc false
39 def handle_call(:new, _from, state) do
40 enabled = Pleroma.Config.get([__MODULE__, :enabled])
41
42 if !enabled do
43 {:reply, %{type: :none}, state}
44 else
45 {:reply, method().new(), state}
46 end
47 end
48
49 @doc false
50 def handle_call({:validate, token, captcha}, _from, state) do
51 {:reply, method().validate(token, captcha), state}
52 end
53
54 @doc false
55 def handle_info(:cleanup, state) do
56 :ok = method().cleanup()
57
58 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
59 # Schedule the next clenup
60 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
61
62 {:noreply, state}
63 end
64
65 defp method, do: Pleroma.Config.get!([__MODULE__, :method])
66 end