
Complete rewrite from task management to full-featured chat system: Core Features: - Real-time WebSocket messaging with connection management - Direct messages and group chats with admin controls - Message types: text, images, videos, audio, documents - Message status tracking: sent, delivered, read receipts - Typing indicators and user presence (online/offline) - Message replies, editing, and deletion Security & Encryption: - End-to-end encryption with RSA + AES hybrid approach - JWT authentication for API and WebSocket connections - Secure file storage with access control - Automatic RSA key pair generation per user Media & File Sharing: - Multi-format file upload (images, videos, audio, documents) - Automatic thumbnail generation for images/videos - File size validation and MIME type checking - Secure download endpoints with permission checks Notifications & Alerts: - Real-time WebSocket notifications - Push notifications via Firebase integration - @username mention alerts with notification history - Unread message and mention counting - Custom notification types (message, mention, group invite) Advanced Features: - Group chat management with roles (member, admin, owner) - User search and chat member management - Message pagination and chat history - Last seen timestamps and activity tracking - Comprehensive API documentation with WebSocket events Architecture: - Clean layered architecture with services, models, schemas - WebSocket connection manager for real-time features - Modular notification system with multiple channels - Comprehensive error handling and validation - Production-ready with Docker support Technologies: FastAPI, WebSocket, SQLAlchemy, SQLite, Cryptography, Firebase, Pillow
41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
from sqlalchemy import Column, Integer, DateTime, Boolean, Text, ForeignKey, Enum
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
from enum import Enum as PyEnum
|
|
from app.db.base import Base
|
|
|
|
class MessageType(PyEnum):
|
|
TEXT = "text"
|
|
IMAGE = "image"
|
|
VIDEO = "video"
|
|
AUDIO = "audio"
|
|
FILE = "file"
|
|
SYSTEM = "system" # For system messages like "User joined"
|
|
|
|
class MessageStatus(PyEnum):
|
|
SENT = "sent"
|
|
DELIVERED = "delivered"
|
|
READ = "read"
|
|
FAILED = "failed"
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
chat_id = Column(Integer, ForeignKey("chats.id"), nullable=False)
|
|
sender_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
reply_to_id = Column(Integer, ForeignKey("messages.id"), nullable=True) # For replies
|
|
content = Column(Text, nullable=True) # Encrypted content
|
|
content_type = Column(Enum(MessageType), default=MessageType.TEXT)
|
|
status = Column(Enum(MessageStatus), default=MessageStatus.SENT)
|
|
is_edited = Column(Boolean, default=False)
|
|
is_deleted = Column(Boolean, default=False)
|
|
edited_at = Column(DateTime(timezone=True), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Relationships
|
|
chat = relationship("Chat", back_populates="messages")
|
|
sender = relationship("User", foreign_keys=[sender_id], back_populates="sent_messages")
|
|
reply_to = relationship("Message", remote_side=[id])
|
|
media_files = relationship("Media", back_populates="message", cascade="all, delete-orphan")
|
|
mentions = relationship("Mention", back_populates="message", cascade="all, delete-orphan") |