Use :ets.match_delete to delete old captchas
This commit is contained in:
parent
6e2f64a0a6
commit
35522fef09
|
@ -19,7 +19,11 @@ def new() do
|
||||||
|
|
||||||
token = json_resp["token"]
|
token = json_resp["token"]
|
||||||
|
|
||||||
true = :ets.insert(@ets, {token, json_resp["md5"], DateTime.now_utc()})
|
true =
|
||||||
|
:ets.insert(
|
||||||
|
@ets,
|
||||||
|
{token, json_resp["md5"], DateTime.now_utc() |> DateTime.Format.unix()}
|
||||||
|
)
|
||||||
|
|
||||||
%{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}
|
%{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}
|
||||||
end
|
end
|
||||||
|
@ -42,14 +46,21 @@ def validate(token, captcha) do
|
||||||
@impl Service
|
@impl Service
|
||||||
def cleanup() do
|
def cleanup() do
|
||||||
seconds_retained = Pleroma.Config.get!([Pleroma.Captcha, :seconds_retained])
|
seconds_retained = Pleroma.Config.get!([Pleroma.Captcha, :seconds_retained])
|
||||||
|
# If the time in ETS is less than current_time - seconds_retained, then the time has
|
||||||
|
# already passed
|
||||||
|
delete_after =
|
||||||
|
DateTime.subtract!(DateTime.now_utc(), seconds_retained) |> DateTime.Format.unix()
|
||||||
|
|
||||||
# Go through captchas and remove expired ones
|
:ets.select_delete(
|
||||||
:ets.tab2list(@ets)
|
@ets,
|
||||||
|> Enum.each(fn {token, _, time_inserted} ->
|
[
|
||||||
# time created + expiration time = time when the captcha should be removed
|
{
|
||||||
remove_time = DateTime.add!(time_inserted, seconds_retained)
|
{:_, :_, :"$1"},
|
||||||
if DateTime.after?(DateTime.now_utc(), remove_time), do: :ets.delete(@ets, token)
|
[{:<, :"$1", {:const, delete_after}}],
|
||||||
end)
|
[true]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue