diff --git a/Period-5/aaak/messages.txt b/Period-5/aaak/messages.txt deleted file mode 100644 index 4f04e07..0000000 --- a/Period-5/aaak/messages.txt +++ /dev/null @@ -1,4 +0,0 @@ -Hey everyone! Just finished the new level, it's insane! -Did you see the latest update? So many cool features! - -hello bub \ No newline at end of file diff --git a/api/id.py b/api/id.py new file mode 100644 index 0000000..f184cc5 --- /dev/null +++ b/api/id.py @@ -0,0 +1,8 @@ +from flask import Blueprint, jsonify + +id_api = Blueprint('id_api', __name__, url_prefix='/api') + +@id_api.route('/id', methods=['GET']) +def get_id(): + # Example response + return jsonify({"id": 1}) \ No newline at end of file diff --git a/api/user.py b/api/user.py index 0d0641d..1548cb3 100644 --- a/api/user.py +++ b/api/user.py @@ -246,4 +246,15 @@ def get(self): api.add_resource(UserAPI._ID, '/id') api.add_resource(UserAPI._BULK_CRUD, '/users') api.add_resource(UserAPI._CRUD, '/user') -api.add_resource(UserAPI._Security, '/authenticate') \ No newline at end of file +api.add_resource(UserAPI._Security, '/authenticate') + +@user_api.route('/user', methods=['POST']) +def create_user_simple(): + data = request.json + # Add user creation logic here + return jsonify({"message": "User created"}), 201 + +@user_api.route('/user', methods=['GET']) +def get_user_simple(): + # Add user fetching logic here + return jsonify({"user": "example"}), 200 \ No newline at end of file diff --git a/main.py b/main.py index 30e05ef..bf0a55a 100644 --- a/main.py +++ b/main.py @@ -9,8 +9,10 @@ from flask import current_app from werkzeug.security import generate_password_hash import shutil - - +from flask_cors import CORS +from flask_socketio import SocketIO, join_room, leave_room, send, emit +from collections import defaultdict +from datetime import datetime # import "objects" from "this" project from __init__ import app, db, login_manager # Key Flask objects @@ -28,6 +30,7 @@ from api.carChat import car_chat_api from api.vote import vote_api +from api.id import id_api # database Initialization functions from model.carChat import CarChat from model.user import User, initUsers @@ -53,6 +56,7 @@ app.register_blueprint(nestImg_api) app.register_blueprint(vote_api) app.register_blueprint(car_api) +app.register_blueprint(id_api) # Tell Flask-Login the view function name of your login route login_manager.login_view = "login" @@ -225,8 +229,65 @@ def restore_data_command(): # Register the custom command group with the Flask application app.cli.add_command(custom_cli) - + +CORS(app) # Enable CORS for all routes + +socketio = SocketIO(app, cors_allowed_origins="*") +rooms_users = defaultdict(set) # Track usernames per room +chat_history = defaultdict(list) # Track message history per room + +@socketio.on('join') +def handle_join(data): + username = data.get('username') + room = data.get('room') + sid = request.sid + + if not username or not room: + return + + join_room(room) + rooms_users[room].add(username) + + # Send chat history to new user + for message in chat_history[room]: + emit('message', message, room=sid) + + # Broadcast join event + join_message = { + "username": "System", + "text": f"{username} has joined the room.", + "timestamp": datetime.utcnow().isoformat() + } + chat_history[room].append(join_message) + emit('message', join_message, room=room) + emit('user_list', list(rooms_users[room]), room=room) + +@socketio.on('message') +def handle_message(data): + username = data.get('username') + room = data.get('room') + text = data.get('text') + + if not all([username, room, text]): + return + + message = { + "username": username, + "text": text, + "timestamp": datetime.utcnow().isoformat() + } + + chat_history[room].append(message) + emit('message', message, room=room) + +@socketio.on('disconnect') +def handle_disconnect(): + sid = request.sid + print(f"User disconnected: {sid}") + # Optional: Clean up user tracking here + # You can emit a 'user_left' message if you store sid->username mapping + # this runs the flask application on the development server if __name__ == "__main__": # change name for testing - app.run(debug=True, host="0.0.0.0", port="8887") + socketio.run(app, debug=True, host="0.0.0.0", port="8000") diff --git a/server.py b/server.py new file mode 100644 index 0000000..1534b38 --- /dev/null +++ b/server.py @@ -0,0 +1,76 @@ +from flask import Flask, render_template, request +from flask_socketio import SocketIO, join_room, leave_room, send, emit +from collections import defaultdict +from datetime import datetime +from flask_cors import CORS +from flask_socketio import SocketIO, join_room, leave_room, send, emit + +app = Flask(__name__) +CORS(app) # Enable CORS for all routes +socketio = SocketIO(app, cors_allowed_origins="*") + +rooms_users = defaultdict(set) # Track usernames per room +chat_history = defaultdict(list) # Track message history per room + + +@app.route('/') +def index(): + return render_template('index.html') + + +@socketio.on('join') +def handle_join(data): + username = data.get('username') + room = data.get('room') + sid = request.sid + + if not username or not room: + return + + join_room(room) + rooms_users[room].add(username) + + # Send chat history to new user + for message in chat_history[room]: + emit('message', message, room=sid) + + # Broadcast join event + join_message = { + "username": "System", + "text": f"{username} has joined the room.", + "timestamp": datetime.utcnow().isoformat() + } + chat_history[room].append(join_message) + emit('message', join_message, room=room) + emit('user_list', list(rooms_users[room]), room=room) + + +@socketio.on('message') +def handle_message(data): + username = data.get('username') + room = data.get('room') + text = data.get('text') + + if not all([username, room, text]): + return + + message = { + "username": username, + "text": text, + "timestamp": datetime.utcnow().isoformat() + } + + chat_history[room].append(message) + emit('message', message, room=room) + + +@socketio.on('disconnect') +def handle_disconnect(): + sid = request.sid + print(f"User disconnected: {sid}") + # Optional: Clean up user tracking here + # You can emit a 'user_left' message if you store sid->username mapping + + +if __name__ == '__main__': + socketio.run(app, debug=True, port=8887)