X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fcaptcha%2Fkocaptcha.ex;h=54f4c8bcd8027186c7a7afecf158f9a11a31c7e5;hb=66d1c31461826b34d5c907dc3a91e86cce808c3e;hp=7f9637ad0f5c67c1a3d0520f05cdcc8fe3bd4ae4;hpb=ef6829382aa32c03cf8536422537a9c219bd0035;p=akkoma diff --git a/lib/pleroma/captcha/kocaptcha.ex b/lib/pleroma/captcha/kocaptcha.ex index 7f9637ad0..54f4c8bcd 100644 --- a/lib/pleroma/captcha/kocaptcha.ex +++ b/lib/pleroma/captcha/kocaptcha.ex @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Captcha.Kocaptcha do alias Calendar.DateTime @@ -19,7 +23,11 @@ defmodule Pleroma.Captcha.Kocaptcha do 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"]} end @@ -42,14 +50,21 @@ defmodule Pleroma.Captcha.Kocaptcha do @impl Service def cleanup() do 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.tab2list(@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) - if DateTime.after?(DateTime.now_utc(), remove_time), do: :ets.delete(@ets, token) - end) + :ets.select_delete( + @ets, + [ + { + {:_, :_, :"$1"}, + [{:<, :"$1", {:const, delete_after}}], + [true] + } + ] + ) :ok end