re-add api route
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
from .db import Model, db
|
||||
from .constants import PermissionLevel
|
||||
import time
|
||||
|
||||
class Users(Model):
|
||||
table = "users"
|
||||
@ -219,3 +220,27 @@ class Avatars(Model):
|
||||
|
||||
class Subscriptions(Model):
|
||||
table = "subscriptions"
|
||||
|
||||
class APIRateLimits(Model):
|
||||
table = 'api_rate_limits'
|
||||
|
||||
@classmethod
|
||||
def is_allowed(cls, user_id, method, seconds):
|
||||
q = """
|
||||
SELECT logged_at FROM api_rate_limits
|
||||
WHERE user_id = ? AND method = ?
|
||||
ORDER BY logged_at DESC LIMIT 1"""
|
||||
last_call = db.fetch_one(q, user_id, method)
|
||||
if last_call is None or (int(time.time()) - int(last_call['logged_at']) >= seconds):
|
||||
with db.transaction():
|
||||
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
|
||||
|
Reference in New Issue
Block a user