merged and resolved database conflicts
This commit is contained in:
@@ -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")
|
||||
|
||||
return {}
|
||||
# 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 {"chat_sent": True}
|
||||
|
||||
|
||||
def get_all_chat():
|
||||
# <---------------------- ROUTE NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
|
||||
@chat_bp.route("/chat/<int:stream_id>", 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 {}
|
||||
# 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)
|
||||
Binary file not shown.
@@ -48,16 +48,18 @@ CREATE TABLE follows
|
||||
DROP TABLE IF EXISTS chat;
|
||||
CREATE TABLE chat
|
||||
(
|
||||
message_id INTEGER NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
message_id INTEGER,
|
||||
chatter_id VARCHAR(50) NOT NULL,
|
||||
stream_id INTEGER NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
time_sent DATETIME NOT NULL,
|
||||
message VARCHAR(256) NOT NULL,
|
||||
time_sent DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (message_id, stream_id),
|
||||
FOREIGN KEY (user_id) REFERENCES users(user_id),
|
||||
FOREIGN KEY (chatter_id) REFERENCES users(user_id),
|
||||
FOREIGN KEY (stream_id) REFERENCES streams(stream_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX chatter_index ON chat(chatter_id);
|
||||
|
||||
DROP TABLE IF EXISTS categories;
|
||||
CREATE TABLE categories
|
||||
(
|
||||
@@ -81,5 +83,20 @@ CREATE TABLE user_preferences
|
||||
user_id INT NOT NULL,
|
||||
category_id INT NOT NULL,
|
||||
favourability INT NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY(user_id, category_id)
|
||||
PRIMARY KEY(user_id, category_id),
|
||||
FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(category_id) REFERENCES categories(category_id) ON DELETE CASCADE
|
||||
)
|
||||
|
||||
DROP TABLE IF EXISTS subscribed;
|
||||
CREATE TABLE subscribed
|
||||
(
|
||||
user_id INTEGER NOT NULL,
|
||||
streamer_id INTEGER NOT NULL,
|
||||
since DATETIME NOT NULL,
|
||||
ends 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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user