diff --git a/backend/blueprints/chat/__init__.py b/backend/blueprints/chat/__init__.py index 10e8648..d9ea1b7 100644 --- a/backend/blueprints/chat/__init__.py +++ b/backend/blueprints/chat/__init__.py @@ -3,6 +3,7 @@ from uuid import UUID from db.model import db, User, Course, Enrollment,Chat from utils.auth import auth_required import requests +from sqlalchemy import desc chat = Blueprint('chat', __name__) @@ -122,5 +123,61 @@ def create_chat(): return jsonify({"message": "Chat sent successfully.", "chat_id": str(new_chat.id)}), 201 + except Exception as e: + return jsonify({"error": f"An error occurred: {str(e)}"}), 500 + +@chat.route("/get", methods=["GET"]) +@auth_required() +def get_chat_history(): + """ + Fetch chat history for a course. + """ + current_user = g.current_user # Logged-in user + course_id = request.args.get("course_id") + limit = int(request.args.get("limit", 20)) # Default to 20 messages + offset = int(request.args.get("offset", 0)) # Default to no offset (latest chats) + + if not course_id: + return jsonify({"error": "Course ID is required."}), 400 + + try: + # Verify the course exists + course = db.session.query(Course).filter_by(id=course_id).first() + if not course: + return jsonify({"error": "Invalid course ID."}), 404 + + # Check if the user is enrolled in the course + is_enrolled = db.session.query(Course).join(Enrollment).filter( + Enrollment.userID == current_user.id, + Enrollment.courseID == course_id + ).first() + + if not is_enrolled: + return jsonify({"error": "You are not enrolled in this course."}), 403 + + # Fetch the latest chat messages with limit and offset + chats = ( + db.session.query(Chat) + .filter_by(courseID=course_id) + .order_by(desc(Chat.chatDate)) + .offset(offset) + .limit(limit) + .all() + ) + + # Format the chat messages + chat_history = [ + { + "id": str(chat.id), + "textContent": chat.textContent, + "userID": str(chat.userID), + "chatDate": chat.chatDate.isoformat(), + "is_mine": chat.userID == current_user.id, + } + for chat in chats + ] + + return jsonify({"chats": chat_history}), 200 + except Exception as e: return jsonify({"error": f"An error occurred: {str(e)}"}), 500 \ No newline at end of file diff --git a/backend/blueprints/profile/__init__.py b/backend/blueprints/profile/__init__.py index 597aab3..e7082d9 100644 --- a/backend/blueprints/profile/__init__.py +++ b/backend/blueprints/profile/__init__.py @@ -113,17 +113,17 @@ def manage_profile(): if request.method == 'GET': try: - profile_url = url_for('send_file', filename=current_user.pfpFilename, _external=True) + profile_picture = url_for('send_file', filename=current_user.pfpFilename, _external=True) except: - profile_url = "" + profile_picture = "" try: # Construct the user profile data profile_data = { "id": str(current_user.id), "email": current_user.email, - "first_name": current_user.firstName, - "last_name": current_user.lastName, + "firstName": current_user.firstName, + "lastName": current_user.lastName, "username": current_user.username, "dob": current_user.dob.isoformat() if current_user.dob else None, "joined_date": current_user.joinedDate.isoformat(), @@ -131,7 +131,7 @@ def manage_profile(): "bio": current_user.bio, "role": current_user.role, "pfp_filename": current_user.pfpFilename, - "profile_url": profile_url, + "profile_picture": profile_picture, } return jsonify({"profile": profile_data}), 200