- def cleanup(token) do
- # Only delete the entry if it exists in the table, because ets:delete raises an exception if it does not
- case :ets.lookup(@ets, token) do
- [{^token, _}] -> :ets.delete(@ets, token)
- _ -> true
- end
+ 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()
+
+ :ets.select_delete(
+ @ets,
+ [
+ {
+ {:_, :_, :"$1"},
+ [{:<, :"$1", {:const, delete_after}}],
+ [true]
+ }
+ ]
+ )
+
+ :ok