UPDATE: Updated chat message structure;
Bug: Session seems to be empty when chat.py routes are called.
This commit is contained in:
@@ -175,6 +175,7 @@ def login():
|
||||
# Set up session to avoid having unncessary state information
|
||||
session.clear()
|
||||
session["username"] = username
|
||||
print(f"Logged in as {username}. session: {session.get('username')}", flush=True)
|
||||
|
||||
# User has been logged in, let frontend know that
|
||||
return jsonify({
|
||||
|
||||
@@ -51,22 +51,20 @@ def get_past_chat(stream_id: int):
|
||||
# Connect to the database
|
||||
db = Database()
|
||||
|
||||
# fetched in format: [(chatter_id, message, time_sent)]
|
||||
# fetched in format: [(username, message, time_sent)]
|
||||
all_chats = db.fetchall("""
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT chatter_id, message, time_sent
|
||||
FROM chat
|
||||
WHERE stream_id = ?
|
||||
ORDER BY time_sent DESC
|
||||
LIMIT 50
|
||||
)
|
||||
ORDER BY time_sent ASC;""", (stream_id,))
|
||||
SELECT username, message, time_sent
|
||||
FROM chat
|
||||
JOIN users ON chat.chatter_id = users.user_id
|
||||
WHERE stream_id = ?
|
||||
ORDER BY time_sent DESC
|
||||
LIMIT 50;
|
||||
""", (stream_id,))
|
||||
|
||||
db.close_connection()
|
||||
|
||||
# Create JSON output of chat_history to pass through NGINX proxy
|
||||
chat_history = [{"chatter_id": chat["chatter_id"], "message": chat["message"], "time_sent": chat["time_sent"]} for chat in all_chats]
|
||||
chat_history = [{"chatter_username": chat["username"], "message": chat["message"], "time_sent": chat["time_sent"]} for chat in all_chats]
|
||||
|
||||
# Pass the chat history to the proxy
|
||||
return jsonify({"chat_history": chat_history}), 200
|
||||
@@ -85,7 +83,7 @@ def send_chat(data) -> None:
|
||||
|
||||
# Input validation - chatter is logged in, message is not empty, stream exists
|
||||
if not all([chatter_id, message, stream_id]):
|
||||
emit("error", {"error": "Unable to send a chat"}, broadcast=False)
|
||||
emit("error", {"error": f"Unable to send a chat. The following info was given: chatter_id={chatter_id}, message={message}, stream_id={stream_id}"}, broadcast=False)
|
||||
return
|
||||
|
||||
# Send the chat message to the client so it can be displayed
|
||||
|
||||
@@ -9,6 +9,7 @@ def logged_in_user():
|
||||
"""
|
||||
g.start_time = time()
|
||||
g.user = session.get("username", None)
|
||||
print(f"Path: {request.path}, session username: {g.user}", flush=True)
|
||||
g.admin = session.get("username", None)
|
||||
|
||||
def record_time(response):
|
||||
|
||||
@@ -77,6 +77,18 @@ INSERT INTO chat (stream_id, chatter_id, message) VALUES
|
||||
(1, 1, 'This stream is crazy man'),
|
||||
(1, 2, 'Woah, cannot believe that');
|
||||
|
||||
|
||||
SELECT * FROM users;
|
||||
SELECT * FROM follows;
|
||||
SELECT * FROM user_preferences;
|
||||
SELECT * FROM subscribes;
|
||||
SELECT * FROM categories;
|
||||
SELECT * FROM streams;
|
||||
SELECT * FROM chat;
|
||||
SELECT * FROM tags;
|
||||
SELECT * FROM stream_tags;
|
||||
|
||||
-- To see all tables in the database
|
||||
SELECT name FROM sqlite_master WHERE type='table';
|
||||
|
||||
|
||||
@@ -87,4 +99,11 @@ JOIN categories AS c ON s.category_id = c.category_id
|
||||
JOIN followed_categories AS f ON s.category_id = c.category_id
|
||||
WHERE f.user_id = 1
|
||||
ORDER BY s.num_viewers DESC
|
||||
LIMIT 25;
|
||||
LIMIT 25;
|
||||
|
||||
SELECT username, message, time_sent
|
||||
FROM chat
|
||||
JOIN users ON chat.chatter_id = users.user_id
|
||||
WHERE stream_id = 1
|
||||
ORDER BY time_sent DESC
|
||||
LIMIT 50;
|
||||
Reference in New Issue
Block a user