Make captcha (kocaptcha) stateless
[akkoma] / lib / pleroma / captcha / captcha.ex
1 defmodule Pleroma.Captcha do
2 use GenServer
3
4 @doc false
5 def start_link() do
6 GenServer.start_link(__MODULE__, [], name: __MODULE__)
7 end
8
9 @doc false
10 def init(_) do
11 {:ok, nil}
12 end
13
14 @doc """
15 Ask the configured captcha service for a new captcha
16 """
17 def new() do
18 GenServer.call(__MODULE__, :new)
19 end
20
21 @doc """
22 Ask the configured captcha service to validate the captcha
23 """
24 def validate(token, captcha, answer_data) do
25 GenServer.call(__MODULE__, {:validate, token, captcha, answer_data})
26 end
27
28 @doc false
29 def handle_call(:new, _from, state) do
30 enabled = Pleroma.Config.get([__MODULE__, :enabled])
31
32 if !enabled do
33 {:reply, %{type: :none}, state}
34 else
35 {:reply, method().new(), state}
36 end
37 end
38
39 @doc false
40 def handle_call({:validate, token, captcha, answer_data}, _from, state) do
41 {:reply, method().validate(token, captcha, answer_data), state}
42 end
43
44 defp method, do: Pleroma.Config.get!([__MODULE__, :method])
45 end