add api endpoint to preview babycode
This commit is contained in:
21
util.lua
21
util.lua
@ -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
|
||||
|
Reference in New Issue
Block a user