From 13d7351588a310e8ee8111d7e096449a829bcd53 Mon Sep 17 00:00:00 2001 From: ThisBirchWood <98231127+ThisBirchWood@users.noreply.github.com> Date: Tue, 28 Jan 2025 11:01:06 +0000 Subject: [PATCH] UPDATE: Began on removing streamers table, and replacing the corresponding code, NOT TESTED (if it's too messy to work with then rollback) --- web_server/blueprints/user.py | 14 +++--- web_server/database/app.db | Bin 73728 -> 73728 bytes web_server/database/streaming.sql | 19 ++------ web_server/database/users.sql | 10 +++-- web_server/utils/recommendation_utils.py | 2 +- web_server/utils/stream_utils.py | 55 +++++------------------ web_server/utils/user_utils.py | 27 ++++++++--- 7 files changed, 51 insertions(+), 76 deletions(-) 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 d504165d067fe2ca0837306a790884e77a5d68f7..ce6601963300a3c465ebf47a9daaf5f8bdd26364 100644 GIT binary patch delta 472 zcmZoTz|wGlWrCCtBO3z)gAfq&05KB~|Cp#_qz4q$E9>TE?PlO-ie=zm$KlVnfmfY_ zkv*MTi`|^_8tW9cf6UidJ(*%T7x3qARus6%$lX-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QV9jT5tNHp5Cb7 z!v%mfRnyc&x*PB)8ej&~!gnE;o8fv&lrdSGA~M1z{n@`V%Hw9fP=w{!z>)?EHJMhl`E9HhTt#^H*m4~92d3y-Ej@WH>9s2SOQ)NP9xMsumMGRva$0INYdAKl!E&8&a zK09x~Cb2_Qtuc0D!bw$h$|E5&N6{#%{9j*=0EJ3$|Kk*2G<~3Z# z;OaNBi(*LPoxp-1NxT7= z(xAla#)QZslq=(qU*r|U{$am+d0qjEbF9ZNQk*Li82tFTA_);7TJ|NWB6f{^&fa8G ztO~)qSiMaYp}GIl1lkHWI;kdm+XQd3Q67I2L`b$#Z?jRU`ORdajd+`6P9rYbJgt^q zhGC@QaW3epIRWP*?WAu+E@>37=rm VGhiwO&wx>*o&obi;1I&c;y+0q_Q3!E 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]