This commit is contained in:
2025-02-11 20:57:54 +00:00
16 changed files with 377 additions and 151 deletions

View File

@@ -1,6 +1,6 @@
from database.database import Database
from typing import Optional, List
from datetime import datetime
from datetime import datetime, timedelta
from dateutil import parser
def get_user_id(username: str) -> Optional[int]:
@@ -22,7 +22,7 @@ def get_username(user_id: str) -> Optional[str]:
with Database() as db:
data = db.fetchone("""
SELECT username
FROM user
FROM users
WHERE user_id = ?
""", (user_id,))
return data['username'] if data else None
@@ -31,10 +31,10 @@ def get_session_info_email(email: str) -> dict:
"""
Returns username and user_id given email
"""
with Database as db:
with Database() as db:
session_info = db.fetchone("""
SELECT user_id, username
FROM user
FROM users
WHERE email = ?
""", (email,))
return session_info
@@ -109,6 +109,39 @@ def unfollow(user_id: int, followed_id: int):
""", (user_id, followed_id))
return {"success": True}
def subscribe(user_id: int, streamer_id: int):
"""
Subscribes user_id to streamer_id
"""
# If user is already subscribed then extend the expiration date else create a new entry
with Database() as db:
existing = db.fetchone("""
SELECT expires
FROM subscribes
WHERE user_id = ? AND subscribed_id = ?
""", (user_id, streamer_id))
if existing:
db.execute("""
UPDATE subscribes SET expires = expires + ?
WHERE user_id = ? AND subscribed_id = ?
""", (timedelta(days=30), user_id, streamer_id))
else:
db.execute("""
INSERT INTO subscribes
(user_id, subscribed_id, since, expires)
VALUES (?,?,?,?)
""", (user_id, streamer_id, datetime.now(), datetime.now() + timedelta(days=30)))
def delete_subscription(user_id: int, subscribed_id: int):
"""
Deletes a subscription entry given user_id and streamer_id
"""
with Database() as db:
db.execute("""
DELETE FROM subscribes
WHERE user_id = ? AND subscribed_id = ?
""", (user_id, subscribed_id))
def subscription_expiration(user_id: int, subscribed_id: int) -> int:
"""

View File

@@ -37,7 +37,7 @@ def get_most_popular_category() -> Optional[List[dict]]:
return category
def sanitize(user_input: str, input_type="username") -> str:
def sanitize(user_input: str, input_type="default") -> str:
"""
Sanitizes user input based on the specified input type.
@@ -63,6 +63,11 @@ def sanitize(user_input: str, input_type="username") -> str:
"min_length": 8,
"max_length": 256,
},
"default": {
"pattern": r"^[\S]+$", # Non-whitespace characters only
"min_length": 1,
"max_length": 50,
},
}
# Get the validation rules for the specified type