UPDATE: Began on removing streamers table, and replacing the corresponding code, NOT TESTED (if it's too messy to work with then rollback)

This commit is contained in:
ThisBirchWood
2025-01-28 11:01:06 +00:00
parent 46a8895398
commit 13d7351588
7 changed files with 51 additions and 76 deletions

View File

@@ -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/<int:user_id>/<int:streamer_id>')
def user_subscribed(user_id: int, streamer_id: int):
@user_bp.route('/is_subscribed/<int:user_id>/<int:subscribed_id>')
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/<int:user_id>/<int:streamer_id>')
def user_following(user_id: int, streamer_id: int):
@user_bp.route('/is_following/<int:user_id>/<int:subscribed_id>')
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})

Binary file not shown.

View File

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

View File

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

View File

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

View File

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

View File

@@ -43,6 +43,23 @@ def get_username(user_id: str) -> Optional[str]:
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:
"""
Returns True if user is subscribed to a streamer, else False
@@ -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]