diff --git a/frontend/src/components/Stream/StreamerRoute.tsx b/frontend/src/components/Stream/StreamerRoute.tsx index 8835479..edb4c99 100644 --- a/frontend/src/components/Stream/StreamerRoute.tsx +++ b/frontend/src/components/Stream/StreamerRoute.tsx @@ -25,7 +25,7 @@ const StreamerRoute: React.FC = () => { checkStreamStatus(); // Poll for live status changes - const interval = setInterval(checkStreamStatus, 20000); // Check every 20 seconds + const interval = setInterval(checkStreamStatus, 1000); // Check every 1 second return () => clearInterval(interval); }, [streamerName]); diff --git a/frontend/src/pages/HomePage.tsx b/frontend/src/pages/HomePage.tsx index f04cb79..0c5dd31 100644 --- a/frontend/src/pages/HomePage.tsx +++ b/frontend/src/pages/HomePage.tsx @@ -3,6 +3,7 @@ import Navbar from "../components/Layout/Navbar"; import ListRow from "../components/Layout/ListRow"; import { useNavigate } from "react-router-dom"; import { useStreams } from "../context/StreamsContext"; +import { useAuth } from "../context/AuthContext"; interface HomePageProps { variant?: "default" | "personalised"; @@ -11,6 +12,7 @@ interface HomePageProps { const HomePage: React.FC = ({ variant = "default" }) => { const { featuredStreams, featuredCategories } = useStreams(); const navigate = useNavigate(); + const { isLoggedIn } = useAuth(); const handleStreamClick = (streamId: number, streamerName: string) => { console.log(`Navigating to ${streamId}`); @@ -25,13 +27,25 @@ const HomePage: React.FC = ({ variant = "default" }) => { > + + {/* Not working - trying to display default streams */} + + {isLoggedIn && variant === "personalised" && ( + + )} Optional[int]: """ Queries user_preferences database to find users favourite streaming category and returns the category """ - with Database() as db: - data = db.fetchone(""" + db = Database() + data = db.fetchone(""" SELECT category_id FROM user_preferences WHERE user_id = ? @@ -20,14 +20,18 @@ def followed_categories_recommendations(user_id : int): """ Returns top 25 streams given a users category following """ - with Database() as db: - categories = db.fetchall(""" - SELECT user_id, title, num_viewers, categories.category_name - FROM streams - JOIN categories ON streams.category_id = categories.category_id - WHERE category_id IN (SELECT category_id FROM categories WHERE user_id = ?) - ORDER BY num_viewers DESC - LIMIT 25; """, (user_id,)) + db = Database() + # TODO: Change this to do what the function says + categories = db.fetchall(""" + SELECT s.stream_id, s.title, u.username, s.num_viewers, c.category_name + FROM streams AS s + JOIN users AS u ON u.user_id = s.user_id + JOIN categories AS c ON s.category_id = c.category_id + JOIN followed_categories AS f ON s.category_id = f.category_id + WHERE f.user_id = ? + ORDER BY s.num_viewers DESC + LIMIT 25; + """, (user_id,)) return categories #TODO Needs to be reworked to get categories instead of streams of categories @@ -36,8 +40,8 @@ def recommendations_based_on_category(category_id: int) -> Optional[List[Tuple[i Queries stream database to get top 25 most viewed streams based on given category and returns (user_id, title, username, num_viewers, category_name) """ - with Database() as db: - data = db.fetchall(""" + db = Database() + data = db.fetchall(""" SELECT streams.category_id, streams.user_id, streams.title, users.username, streams.num_viewers, categories.category_name FROM streams JOIN users ON users.user_id = streams.user_id @@ -53,8 +57,8 @@ def default_recommendations(): Return a list of 25 recommended live streams by number of viewers (user_id, title, username, num_viewers, category_name) """ - with Database() as db: - data = db.fetchall(""" + db = Database() + data = db.fetchall(""" SELECT stream_id, users.user_id, title, username, num_viewers, category_name FROM streams JOIN users ON users.user_id = streams.user_id @@ -68,8 +72,8 @@ def category_recommendations(): """ Returns a list of the top 5 most popular categories """ - with Database() as db: - categories = db.fetchall(""" + db = Database() + categories = db.fetchall(""" SELECT categories.category_id, categories.category_name FROM streams JOIN categories ON streams.category_id = categories.category_id diff --git a/web_server/utils/stream_utils.py b/web_server/utils/stream_utils.py index 270b9c8..af3af9a 100644 --- a/web_server/utils/stream_utils.py +++ b/web_server/utils/stream_utils.py @@ -7,8 +7,8 @@ def streamer_live_status(user_id: int) -> bool: """ Returns boolean on whether the given streamer is live """ - with Database() as db: - is_live = db.fetchone(""" + db = Database() + is_live = db.fetchone(""" SELECT isLive FROM streams WHERE user_id = ? @@ -19,8 +19,8 @@ def followed_live_streams(user_id: int) -> list[dict]: """ Searches for streamers who the user followed which are currently live """ - with Database() as db: - live_streams = db.fetchall(""" + db = Database() + live_streams = db.fetchall(""" SELECT user_id, stream_id, title, num_viewers FROM streams WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?) @@ -33,8 +33,8 @@ def followed_streamers(user_id: int) -> list[dict]: """ Returns a list of streamers who the user follows """ - with Database() as db: - followed_streamers = db.fetchall(""" + db = Database() + followed_streamers = db.fetchall(""" SELECT user_id, username FROM users WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?); @@ -45,8 +45,8 @@ def streamer_most_recent_stream(user_id: int) -> dict: """ Returns data of the most recent stream by a streamer """ - with Database() as db: - most_recent_stream = db.fetchone(""" + db = Database() + most_recent_stream = db.fetchone(""" SELECT * FROM streams WHERE user_id = ? AND stream_id = (SELECT MAX(stream_id) FROM streams WHERE user_id = ?) @@ -57,8 +57,8 @@ def user_stream(user_id: int, stream_id: int) -> dict: """ Returns data of a streamers selected stream """ - with Database() as db: - stream = db.fetchone(""" + db = Database() + stream = db.fetchone(""" SELECT * FROM streams WHERE user_id = ? AND stream_id = ? diff --git a/web_server/utils/user_utils.py b/web_server/utils/user_utils.py index 9b7009e..0abc8e6 100644 --- a/web_server/utils/user_utils.py +++ b/web_server/utils/user_utils.py @@ -13,11 +13,11 @@ def get_user_id(username: str) -> int: """ Returns user_id associated with given username """ - with Database() as db: - data = db.fetchone(""" + db = Database() + data = db.fetchone(""" SELECT user_id FROM users - WHERE username = ? + WHERE username = ?; """, (username,)) return data['user_id'] if data else None @@ -25,8 +25,8 @@ def get_username(user_id: str) -> Optional[str]: """ Returns username associated with given user_id """ - with Database() as db: - data = db.fetchone(""" + db = Database() + data = db.fetchone(""" SELECT username FROM user WHERE user_id = ? @@ -37,8 +37,8 @@ def is_user_partner(user_id: int) -> bool: """ Returns True if user is a partner, else False """ - with Database() as db: - data = db.fetchone(""" + db = Database() + data = db.fetchone(""" SELECT is_partnered FROM users WHERE user_id = ? @@ -49,8 +49,8 @@ def is_subscribed(user_id: int, streamer_id: int) -> bool: """ Returns True if user is subscribed to a streamer, else False """ - with Database() as db: - result = db.fetchone(""" + db = Database() + result = db.fetchone(""" SELECT 1 FROM subscribes WHERE user_id = ? @@ -63,8 +63,8 @@ def is_following(user_id: int, followed_id: int) -> bool: """ Returns where a user is following another """ - with Database() as db: - result = db.fetchone(""" + db = Database() + result = db.fetchone(""" SELECT 1 FROM follows WHERE user_id = ? @@ -76,8 +76,8 @@ def subscription_expiration(user_id: int, subscribed_id: int) -> int: """ Returns the amount of time left until user subscription to a streamer ends """ - with Database() as db: - data = db.fetchone(""" + db = Database() + data = db.fetchone(""" SELECT expires FROM subscriptions WHERE user_id = ? @@ -103,8 +103,8 @@ def reset_password(new_password: str, email: str): """ Given email and new password reset the password for a given user """ - with Database() as db: - db.execute(""" + db = Database() + db.execute(""" UPDATE users SET password = ? WHERE email = ? diff --git a/web_server/utils/utils.py b/web_server/utils/utils.py index f2012ce..26e56fb 100644 --- a/web_server/utils/utils.py +++ b/web_server/utils/utils.py @@ -4,8 +4,8 @@ def categories(): """ Returns all possible streaming categories """ - with Database() as db: - all_categories = db.fetchall("SELECT * FROM categories") + db = Database() + all_categories = db.fetchall("SELECT * FROM categories") return all_categories @@ -13,8 +13,8 @@ def tags(): """ Returns all possible streaming tags """ - with Database() as db: - all_tags = db.fetchall("SELECT * FROM tags") + db = Database() + all_tags = db.fetchall("SELECT * FROM tags") return all_tags @@ -22,8 +22,8 @@ def most_popular_category(): """ Returns the most popular category based on live stream viewers """ - with Database() as db: - category = db.fetchone(""" + db = Database() + category = db.fetchone(""" SELECT categories.category_id, categories.category_name FROM streams JOIN categories ON streams.category_id = categories.category_id