main
Kushal Dotel 6 months ago
parent 7852c8c559
commit a6d03971eb
  1. 57
      backend/blueprints/chat/__init__.py
  2. 10
      backend/blueprints/profile/__init__.py

@ -3,6 +3,7 @@ from uuid import UUID
from db.model import db, User, Course, Enrollment,Chat from db.model import db, User, Course, Enrollment,Chat
from utils.auth import auth_required from utils.auth import auth_required
import requests import requests
from sqlalchemy import desc
chat = Blueprint('chat', __name__) chat = Blueprint('chat', __name__)
@ -122,5 +123,61 @@ def create_chat():
return jsonify({"message": "Chat sent successfully.", "chat_id": str(new_chat.id)}), 201 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: except Exception as e:
return jsonify({"error": f"An error occurred: {str(e)}"}), 500 return jsonify({"error": f"An error occurred: {str(e)}"}), 500

@ -113,17 +113,17 @@ def manage_profile():
if request.method == 'GET': if request.method == 'GET':
try: 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: except:
profile_url = "" profile_picture = ""
try: try:
# Construct the user profile data # Construct the user profile data
profile_data = { profile_data = {
"id": str(current_user.id), "id": str(current_user.id),
"email": current_user.email, "email": current_user.email,
"first_name": current_user.firstName, "firstName": current_user.firstName,
"last_name": current_user.lastName, "lastName": current_user.lastName,
"username": current_user.username, "username": current_user.username,
"dob": current_user.dob.isoformat() if current_user.dob else None, "dob": current_user.dob.isoformat() if current_user.dob else None,
"joined_date": current_user.joinedDate.isoformat(), "joined_date": current_user.joinedDate.isoformat(),
@ -131,7 +131,7 @@ def manage_profile():
"bio": current_user.bio, "bio": current_user.bio,
"role": current_user.role, "role": current_user.role,
"pfp_filename": current_user.pfpFilename, "pfp_filename": current_user.pfpFilename,
"profile_url": profile_url, "profile_picture": profile_picture,
} }
return jsonify({"profile": profile_data}), 200 return jsonify({"profile": profile_data}), 200

Loading…
Cancel
Save