""" Chat web interface routes Provides HTML templates and endpoints for web-based chat """ from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify from flask_login import login_required, current_user from sqlalchemy import desc, and_ from app.extensions import db from app.models import ChatRoom, ChatMessage, ChatParticipant, Post # Create blueprint chat = Blueprint('chat', __name__) @chat.route('/') @login_required def index(): """Chat main page with room list and rules""" # Get user's recent chat rooms user_rooms = db.session.query(ChatRoom).join(ChatParticipant).filter( and_( ChatParticipant.user_id == current_user.id, ChatRoom.is_active == True ) ).order_by(desc(ChatRoom.last_activity)).limit(10).all() # Get public rooms that are active public_rooms = ChatRoom.query.filter( ChatRoom.is_active == True, ChatRoom.is_private == False ).order_by(desc(ChatRoom.last_activity)).limit(10).all() return render_template('chat/index.html', user_rooms=user_rooms, public_rooms=public_rooms) @chat.route('/room/') @login_required def room(room_id): """Chat room interface""" room = ChatRoom.query.get_or_404(room_id) # Check access if room.is_private: participant = ChatParticipant.query.filter_by( room_id=room_id, user_id=current_user.id ).first() if not participant: flash('You do not have access to this chat room.', 'error') return redirect(url_for('chat.index')) # Get or create participant record participant = ChatParticipant.query.filter_by( room_id=room_id, user_id=current_user.id ).first() if not participant and not room.is_private: # Auto-join public rooms participant = ChatParticipant( room_id=room_id, user_id=current_user.id, role='member' ) db.session.add(participant) db.session.commit() # Get recent messages messages = ChatMessage.query.filter_by( room_id=room_id, is_deleted=False ).order_by(ChatMessage.created_at).limit(50).all() # Get participants participants = ChatParticipant.query.filter_by(room_id=room_id).all() return render_template('chat/room.html', room=room, messages=messages, participants=participants, current_participant=participant) @chat.route('/create') @login_required def create_room_form(): """Show create room form""" # Get available posts for post discussions recent_posts = Post.query.filter_by(published=True).order_by( desc(Post.created_at) ).limit(20).all() return render_template('chat/create_room.html', posts=recent_posts) @chat.route('/support') @login_required def support(): """Admin support page""" # Get user's recent support tickets (rooms they created for support) recent_tickets = ChatRoom.query.filter( ChatRoom.room_type == 'admin_support', ChatRoom.created_by_id == current_user.id ).order_by(desc(ChatRoom.created_at)).limit(5).all() return render_template('chat/support.html', recent_tickets=recent_tickets) @chat.route('/embed/') @login_required def embed_post_chat(post_id): """Embedded chat widget for post pages""" post = Post.query.get_or_404(post_id) # Find existing discussion room discussion_room = ChatRoom.query.filter( and_( ChatRoom.room_type == 'post_discussion', ChatRoom.related_post_id == post_id, ChatRoom.is_active == True ) ).first() return render_template('chat/embed.html', post=post, discussion_room=discussion_room)