diff --git a/web_server/blueprints/user.py b/web_server/blueprints/user.py index aa7943f..cc6743e 100644 --- a/web_server/blueprints/user.py +++ b/web_server/blueprints/user.py @@ -4,21 +4,21 @@ from utils.user_utils import is_subscribed, is_following, subscription_expiratio user_bp = Blueprint("user", __name__) -@user_bp.route('/is_subscribed//') -def user_subscribed(user_id: int, streamer_id: int): +@user_bp.route('/is_subscribed//') +def user_subscribed(user_id: int, subscribed_id: int): """ - Checks to see if user is subscribed to a streamer + Checks to see if user is subscribed to another user """ - if is_subscribed(user_id, streamer_id): + if is_subscribed(user_id, subscribed_id): return jsonify({"subscribed": True}) return jsonify({"subscribed": False}) -@user_bp.route('/is_following//') -def user_following(user_id: int, streamer_id: int): +@user_bp.route('/is_following//') +def user_following(user_id: int, subscribed_id: int): """ Checks to see if user is following a streamer """ - if is_following(user_id, streamer_id): + if is_following(user_id, subscribed_id): return jsonify({"following": True}) return jsonify({"following": False}) diff --git a/web_server/database/app.db b/web_server/database/app.db index d504165..ce66019 100644 Binary files a/web_server/database/app.db and b/web_server/database/app.db differ diff --git a/web_server/database/streaming.sql b/web_server/database/streaming.sql index c71bca7..3fb9a52 100644 --- a/web_server/database/streaming.sql +++ b/web_server/database/streaming.sql @@ -38,7 +38,7 @@ CREATE TABLE categories DROP TABLE IF EXISTS streams; CREATE TABLE streams ( - streamer_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, stream_id INTEGER NOT NULL, title TEXT NOT NULL, start_time DATETIME NOT NULL, @@ -46,18 +46,7 @@ CREATE TABLE streams isLive BOOLEAN NOT NULL DEFAULT 0, vod_id INTEGER, category_id NOT NULL, - PRIMARY KEY (streamer_id, stream_id), + PRIMARY KEY (user_id, stream_id), FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE, - FOREIGN KEY (streamer_id) REFERENCES streamers(user_id) ON DELETE CASCADE -); - -DROP TABLE IF EXISTS streamers; -CREATE TABLE streamers -( - user_id INTEGER PRIMARY KEY NOT NULL, - streamer_id INTEGER NOT NULL, - since DATETIME, - isPartnered BOOLEAN NOT NULL DEFAULT 0, - stream_key VARCHAR(60) NOT NULL, - FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE -); + FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE +); \ No newline at end of file diff --git a/web_server/database/users.sql b/web_server/database/users.sql index 2c89fb7..613aa10 100644 --- a/web_server/database/users.sql +++ b/web_server/database/users.sql @@ -9,6 +9,8 @@ CREATE TABLE users password VARCHAR(256) NOT NULL, email VARCHAR(128) NOT NULL, num_followers INTEGER NOT NULL, + stream_key VARCHAR(60) NOT NULL, + is_partnered BOOLEAN NOT NULL DEFAULT 0, bio VARCHAR(1024) ); @@ -18,11 +20,11 @@ DROP TABLE IF EXISTS follows; CREATE TABLE follows ( user_id INTEGER NOT NULL, - streamer_id INTEGER NOT NULL, + followed_id INTEGER NOT NULL, since DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (user_id, streamer_id), FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE, - FOREIGN KEY (streamer_id) REFERENCES streamers(streamer_id) ON DELETE CASCADE + FOREIGN KEY (followed_id) REFERENCES users(user_id) ON DELETE CASCADE ); DROP TABLE IF EXISTS user_preferences; @@ -40,11 +42,11 @@ DROP TABLE IF EXISTS subscribes; CREATE TABLE subscribes ( user_id INTEGER NOT NULL, - streamer_id INTEGER NOT NULL, + subscribed_id INTEGER NOT NULL, since DATETIME NOT NULL, expires DATETIME NOT NULL, PRIMARY KEY (user_id,streamer_id), FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE, - FOREIGN KEY(streamer_id) REFERENCES streamers(streamer_id) ON DELETE CASCADE + FOREIGN KEY(subscribed_id) REFERENCES users(user_id) ON DELETE CASCADE ); diff --git a/web_server/utils/recommendation_utils.py b/web_server/utils/recommendation_utils.py index 58ada9f..61351fe 100644 --- a/web_server/utils/recommendation_utils.py +++ b/web_server/utils/recommendation_utils.py @@ -22,5 +22,5 @@ def recommendations_based_on_category(category_id: int) -> Optional[List[Tuple[i cursor = db.create_connection() data = cursor.execute( - "SELECT streamer_id, stream_id, title, num_viewers FROM streams WHERE category_id = ? ORDER BY num_viwers DESC LIMIT 10", (category_id,)).fetchall() + "SELECT user_id, stream_id, title, num_viewers FROM streams WHERE category_id = ? ORDER BY num_viwers DESC LIMIT 10", (category_id,)).fetchall() return data \ No newline at end of file diff --git a/web_server/utils/stream_utils.py b/web_server/utils/stream_utils.py index f97d785..a432b58 100644 --- a/web_server/utils/stream_utils.py +++ b/web_server/utils/stream_utils.py @@ -1,25 +1,14 @@ from database.database import Database from typing import Optional -def streamer_data(streamer_id: int): - """ - Retrieves data given streamer (username, since, isPartnered) - """ - db = Database() - cursor = db.create_connection() - streamer_data = cursor.execute("""SELECT username, since, isPartnered FROM - streamers JOIN users ON - streamers.user_id = users.user_id - WHERE streamer_id = ?""", (streamer_id,)).fetchone() - return streamer_data -def streamer_live_status(streamer_id: int) -> bool: +def user_live_status(user_id: int) -> bool: """ Returns whether the given streamer is live """ db = Database() cursor = db.create_connection() - return bool(cursor.execute("SELECT 1 FROM streams WHERE streamer_id = ? AND isLive = 1 ORDER BY stream_id DESC", (streamer_id,)).fetchone()) + return bool(cursor.execute("SELECT 1 FROM streams WHERE user_id = ? AND isLive = 1 ORDER BY stream_id DESC", (user_id,)).fetchone()) def followed_live_streams(user_id: int): """ @@ -29,38 +18,16 @@ def followed_live_streams(user_id: int): cursor = db.create_connection() live_streams = cursor.execute(""" - SELECT streamer_id, stream_id, title, num_viewers + SELECT user_id, stream_id, title, num_viewers FROM streams - WHERE streamer_id IN (SELECT streamer_id FROM follows WHERE user_id = ?) - AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE streamer_id = streams.streamer_id) - AND isLive = 1 + WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?) + AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = streams.user_id) + AND isLive = 1; """, (user_id,)).fetchall() return live_streams -def streamer_name(streamer_id: int) -> Optional[str]: - """ - Returns streamers username given streamer_id - """ - db = Database() - cursor = db.create_connection() - - streamer_username = cursor.execute( - "SELECT username FROM users WHERE user_id = (SELECT user_id FROM streamers WHERE streamer_id = ?)", (streamer_id,)).fetchone() - return streamer_username[0] if streamer_username else None - -def streamer_id(streamer_name: str) -> Optional[int]: - """ - Returns streamers id given streamers name - """ - db = Database() - cursor = db.create_connection() - - streamer_id = cursor.execute( - "SELECT streamer_id FROM streamers WHERE user_id = (SELECT user_id FROM users WHERE username = ?)",(streamer_name,)).fetchone() - return streamer_id[0] if streamer_id else None - -def streamer_most_recent_stream(streamer_id: int): +def user_most_recent_stream(user_id: int): """ Returns data of the most recent stream by a streamer """ @@ -68,18 +35,18 @@ def streamer_most_recent_stream(streamer_id: int): cursor = db.create_connection() most_recent_stream = cursor.execute("""SELECT * FROM streams WHERE - streamer_id = ? AND + user_id = ? AND stream_id = (SELECT MAX(stream_id) FROM - streams WHERE streamer_id = ?)""", (streamer_id,streamer_id)).fetchone() + streams WHERE user_id = ?)""", (user_id, user_id)).fetchone() return most_recent_stream -def streamer_stream(streamer_id: int, stream_id: int): +def user_stream(user_id: int, stream_id: int): """ Returns data of a streamers selected stream """ db = Database() cursor = db.create_connection() - stream = cursor.execute("SELECT * FROM streams WHERE streamer_id = ? AND stream_id = ?", (streamer_id,stream_id)).fetchone() + stream = cursor.execute("SELECT * FROM streams WHERE user_id = ? AND stream_id = ?", (user_id,stream_id)).fetchone() return stream diff --git a/web_server/utils/user_utils.py b/web_server/utils/user_utils.py index 42848cd..00afede 100644 --- a/web_server/utils/user_utils.py +++ b/web_server/utils/user_utils.py @@ -42,6 +42,23 @@ def get_username(user_id: str) -> Optional[str]: except Exception as e: print(f"Error: {e}") return None + +def is_user_partner(user_id: int) -> bool: + """ + Returns True if user is a partner, else False + """ + db = Database() + cursor = db.create_connection() + + try: + data = cursor.execute( + "SELECT is_partnered FROM users WHERE user_id = ?", + (user_id,) + ).fetchone() + return bool(data) + except Exception as e: + print(f"Error: {e}") + return False def is_subscribed(user_id: int, streamer_id: int) -> bool: """ @@ -60,21 +77,21 @@ def is_subscribed(user_id: int, streamer_id: int) -> bool: print(f"Error: {e}") return False -def is_following(user_id: int, streamer_id: int) -> bool: +def is_following(user_id: int, followed_id: int) -> bool: db = Database() cursor = db.create_connection() try: result = cursor.execute( - "SELECT 1 FROM follows WHERE user_id = ? AND streamer_id = ?", - (user_id, streamer_id) + "SELECT 1 FROM follows WHERE user_id = ? AND followed_id = ?", + (user_id, followed_id) ).fetchone() return bool(result) except Exception as e: print(f"Error: {e}") return False -def subscription_expiration(user_id: int, streamer_id: int) -> int: +def subscription_expiration(user_id: int, subscribed_id: int) -> int: """ Returns the amount of time left until user subscription to a streamer ends """ @@ -83,7 +100,7 @@ def subscription_expiration(user_id: int, streamer_id: int) -> int: remaining_time = 0 try: data = cursor.execute( - "SELECT expires from subscriptions WHERE user_id = ? AND streamer_id = ? AND expires > since", (user_id,streamer_id)).fetchone() + "SELECT expires from subscriptions WHERE user_id = ? AND subscribed_id = ? AND expires > since", (user_id,subscribed_id)).fetchone() if data: expiration_date = data[0]