add api endpoint to preview babycode

This commit is contained in:
2025-06-01 00:45:11 +03:00
parent 72709226c0
commit 8a00500387
3 changed files with 53 additions and 0 deletions

View File

@ -316,4 +316,25 @@ function util.inject_warn_infobox(req, message)
req.session.infobox = ib
end
function util.rate_limit_allowed(user_id, method, seconds)
local last_call = db.query([[
SELECT logged_at FROM api_rate_limits
WHERE user_id = ? AND method = ?
ORDER BY logged_at DESC LIMIT 1
]], user_id, method)
if #last_call == 0 or (os.time() - last_call[1].logged_at) >= seconds then
db.query(
"DELETE FROM api_rate_limits WHERE user_id = ? AND method = ?",
user_id, method
)
db.query(
"INSERT INTO api_rate_limits (user_id, method) VALUES (?, ?)",
user_id, method
)
return true
else
return false
end
end
return util