Created new chat table in scheme.sql, Added chat.py with 3 methods (incomplete)

This commit is contained in:
white
2025-01-23 13:10:22 +00:00
parent 233e2df781
commit 33111ea077
3 changed files with 75 additions and 12 deletions

View File

@@ -36,13 +36,13 @@ def signup():
cursor.execute("""INSERT INTO users (username, password, email, num_followers, isPartenered, bio)
VALUES (?, ?, ?, ?, ?, ?);""", (username, generate_password_hash(password), email, 0, 0, "This user does not have a Bio."))
db.commit_data()
return redirect(url_for("auth.login"))
return {"account_created": True}
# Close connection to prevent data leaks
db.close_connection()
return render_template("signup.html", form=form)
return {"account_created": False}
@auth_bp.route("/login", methods=["GET", "POST"])
def login():
@@ -81,12 +81,12 @@ def login():
if not next_page:
next_page = url_for("app.index")
db.close_connection()
return redirect(next_page)
return {"logged_in": True}
return render_template("login.html", form=form)
return {"logged_in": False}
@auth_bp.route("/logout")
@login_required
def logout():
session.clear()
return redirect(url_for("index"))
return {"logged_in": False}

View File

@@ -0,0 +1,48 @@
from flask import Blueprint
from blueprints.utils import login_required
from database.database import Database
chat_bp = Blueprint("chat", __name__)
@login_required
def chat():
"""
Works with react, takes the chat entered by a logged in user and stores in database
"""
return {}
def get_all_chat():
"""
Returns a dictionary to be passed to the server.
Output structure in the following format: `{(chatter, message), ...}` for all chats.
Rans once when a user first logs into a stream
"""
# 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()
# Create JSON output of chat_history to pass through NGINX proxy
chat_history = {}
for chat in all_chats:
chat_history[chat[0]] = chat[1]
# Pass the chat history to the proxy
return chat_history
def get_recent_chat():
"""
Run periodically to return new chat messages on a stream a user has already loaded in to.
"""
return {}

View File

@@ -1,12 +1,13 @@
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
username VARCHAR(50) PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
password VARCHAR(256) NOT NULL,
email VARCHAR(64) NOT NULL,
num_followers INTEGER NOT NULL,
isPartenered BOOLEAN NOT NULL DEFAULT 0,
bio TEXT
bio TEXT,
PRIMARY KEY (username)
);
SELECT * FROM users;
@@ -15,22 +16,36 @@ SELECT * FROM users;
DROP TABLE IF EXISTS streams;
CREATE TABLE streams
(
stream_id INTEGER PRIMARY KEY AUTOINCREMENT,
stream_id INTEGER AUTOINCREMENT,
title TEXT NOT NULL,
start_time DATETIME NOT NULL,
num_viewers INT NOT NULL DEFAULT 0,
isLive BOOLEAN NOT NULL DEFAULT 0,
vod_id INT,
streamer_id VARCHAR NOT NULL,
PRIMARY KEY (stream_id),
FOREIGN KEY (streamer_id) REFERENCES users(username) ON DELETE CASCADE
);
DROP TABLE IF EXISTS follows;
CREATE TABLE follows
(
user_id INT NOT NULL,
following_id INT NOT NULL,
PRIMARY KEY (user_id, following_id),
FOREIGN KEY (user_id) REFERENCES users(username) ON DELETE CASCADE,
username VARCHAR(50) NOT NULL,
following_id VARCHAR(50) NOT NULL,
PRIMARY KEY (username, following_id),
FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE,
FOREIGN KEY (following_id) REFERENCES users(username) ON DELETE CASCADE
);
DROP TABLE IF EXISTS chat;
CREATE TABLE chat
(
message_id INT AUTOINCREMENT,
chatter_id VARCHAR(50) NOT NULL,
stream_id INT NOT NULL,
message TEXT NOT NULL,
time_sent DATETIME NOT NULL,
PRIMARY KEY (message_id),
FOREIGN KEY (chatter_id) REFERENCES users(username),
FOREIGN KEY (stream_id) REFERENCES streams(stream_id) ON DELETE CASCADE
);