re-add api route

This commit is contained in:
2025-07-02 13:35:14 +03:00
parent 3c3837b3f2
commit 64e18f16dd
4 changed files with 85 additions and 3 deletions

View File

@ -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