From ced77e718aa534fe9b0a2f80aa4f380607371cfd Mon Sep 17 00:00:00 2001 From: Oscar Cao Date: Thu, 23 Jan 2025 23:47:54 +0000 Subject: [PATCH] merged and resolved database conflicts --- web_server/blueprints/chat.py | 79 +++++++++++++++++++++++++-------- web_server/database/app.db | Bin 45056 -> 49152 bytes web_server/database/schema.sql | 31 ++++++++++--- 3 files changed, 84 insertions(+), 26 deletions(-) diff --git a/web_server/blueprints/chat.py b/web_server/blueprints/chat.py index 7029c16..9ea19ca 100644 --- a/web_server/blueprints/chat.py +++ b/web_server/blueprints/chat.py @@ -1,48 +1,89 @@ -from flask import Blueprint +from flask import Blueprint, request, jsonify from blueprints.utils import login_required from database.database import Database chat_bp = Blueprint("chat", __name__) +@chat_bp.route("/send_chat", methods=["POST"]) @login_required -def chat(): +def send_chat(): """ Works with react, takes the chat entered by a logged in user and stores in database """ + + # Take the message information from frontend + data = request.get_json() + chatter_id = data.get("chatter_id") + stream_id = data.get("stream_id") + message = data.get("message") + + # Input validation - chatter is logged in, message is not empty, stream exists + if not all(chatter_id, message, stream_id): + return {"chat_sent": False} + # Save chat information to database so other users can see + db = Database() + cursor = db.create_connection() + cursor.execute(""" + INSERT INTO chat (chatter_id, stream_id, message) + VALUES (?, ?, ?);""", (chatter_id, stream_id, message)) + db.commit_data() - return {} + return {"chat_sent": True} -def get_all_chat(): +# <---------------------- ROUTE NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ----------------------> +@chat_bp.route("/chat/", methods=["GET"]) +def get_past_chat(stream_id): """ - Returns a dictionary to be passed to the server. + Returns a JSON object to be passed to the server. - Output structure in the following format: `{(chatter, message), ...}` for all chats. + Output structure in the following format: `{chatter_id: message}` for all chats. - Rans once when a user first logs into a stream + Ran once when a user first logs into a stream to get the most recent 50 chat messages. """ # Connect to the database db = Database() cursor = db.create_connection() - # Returns list of tuples: (chatter_id, message) - all_chats = cursor.execute("""SELECT ?, ? FROM chat - ORDER BY ?;""", ("chatter_id", "message", "time_sent")).fetchall() + # fetched in format: [(chatter_id, message, time_sent)] + all_chats = cursor.execute(""" + 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,)).fetchall() + db.close_connection() # Create JSON output of chat_history to pass through NGINX proxy - chat_history = {} - for chat in all_chats: - chat_history[chat[0]] = chat[1] + chat_history = [{"chatter_id": chat[0], "message": chat[1], "time_sent": chat[2]} for chat in all_chats] # Pass the chat history to the proxy - return chat_history + return jsonify(chat_history) -def get_recent_chat(): +def get_recent_chat(stream_id): """ - Run periodically to return new chat messages on a stream a user has already loaded in to. - - + Fetch new chat messages on a stream a user has already loaded in to. """ - return {} \ No newline at end of file + # Get the last received chat to avoid repeating old chats + last_received = request.args.get("last_received") # last_received is a time stamp + + # Get the most recent chats from the database + db = Database() + cursor = db.create_connection() + + # fetched in format: [(chatter_id, message, time_sent)] + new_chats = cursor.execute(""" + SELECT chatter_id, message, time_sent + FROM chat + WHERE stream_id = ? + AND time_sent > ?;""", (stream_id, last_received)).fetchall() + db.close_connection() + + # Send the new chats to frontend + return jsonify(new_chats) \ No newline at end of file diff --git a/web_server/database/app.db b/web_server/database/app.db index 8228aa126625832c2fcab4ad69ac59d6727f58e4..eb231e8c9a89470f8caf6b7888a306fa7125b185 100644 GIT binary patch delta 398 zcmZp8z|_#dJV9E}pMima2Z&+7d!ml9vOj~KP5>{*cLr|O4hCKxc3F-%&KACQ9(!&M zPG8mzP8qJ<8yneJxtfIx*~LXg8Qa(=GjdwU=x`|jL2^c7Nl9u^d}fM5m}8K$hhvb2 zse$I?ja;IWb2ybIXYorlz&5=o>J352p<) z*p$fyJPMPgxB@oIvEO85;dEtCo&1|akwx8zO?>hS9#t;R8U_xK!*vZNFXGna=2`*a zHYw_CzR#z`m=wn#EG{k0*kWIjn3R*6mYKNjx z;OXb$8iDW#NLs<)4=kVo*Oa2!WTL-0hs%VKxzS5+@-6ODlX-YU*cdg%o60mcr|~K> tGB=g!0vTnDiwq1lIdCg%<~H~$KPiEGF}HyMD<{z19Ge9N-t#ju0032CXr%xE delta 290 zcmZo@U~YK8G(lR>l7WGN8;D`RbfS*2q9uc#P5>|Wc?K?aSq5GnzB|0h+}F7>xfgJJ z=XuO7%MrI(P+%_GW?A-Ij4Vz0O6-$=b0|(;%PKp0HxK`0M{bqLzgd+gU*;8_Y{4cy zIhW05@-}Xz$>-Ptm`oHlb90+8GU+N#_UF{)=2^nP0kXK!OMbEw&nXt(I0pO43Y@AE zAO=W6SBg#CQJRq