from datetime import datetime from typing import TYPE_CHECKING from sqlalchemy import Boolean, DateTime, ForeignKey, String, UniqueConstraint, func from sqlalchemy.orm import Mapped, mapped_column, relationship from app.database.base import Base if TYPE_CHECKING: from app.auth.models import EmailVerificationToken, PasswordResetToken from app.chats.models import ChatMember from app.messages.models import Message class User(Base): __tablename__ = "users" id: Mapped[int] = mapped_column(primary_key=True, index=True) name: Mapped[str] = mapped_column(String(100), nullable=False) username: Mapped[str] = mapped_column(String(50), unique=True, index=True) email: Mapped[str] = mapped_column(String(255), unique=True, index=True) password_hash: Mapped[str] = mapped_column(String(255)) avatar_url: Mapped[str | None] = mapped_column(String(512), nullable=True) bio: Mapped[str | None] = mapped_column(String(500), nullable=True) email_verified: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True) allow_private_messages: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, server_default="true") privacy_private_messages: Mapped[str] = mapped_column(String(16), nullable=False, default="everyone", server_default="everyone") privacy_last_seen: Mapped[str] = mapped_column(String(16), nullable=False, default="everyone", server_default="everyone") privacy_avatar: Mapped[str] = mapped_column(String(16), nullable=False, default="everyone", server_default="everyone") privacy_group_invites: Mapped[str] = mapped_column(String(16), nullable=False, default="everyone", server_default="everyone") twofa_enabled: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, server_default="false") twofa_secret: Mapped[str | None] = mapped_column(String(64), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, ) last_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True) access_revoked_before: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) memberships: Mapped[list["ChatMember"]] = relationship(back_populates="user", cascade="all, delete-orphan") sent_messages: Mapped[list["Message"]] = relationship(back_populates="sender") email_verification_tokens: Mapped[list["EmailVerificationToken"]] = relationship( back_populates="user", cascade="all, delete-orphan", ) password_reset_tokens: Mapped[list["PasswordResetToken"]] = relationship( back_populates="user", cascade="all, delete-orphan", ) class BlockedUser(Base): __tablename__ = "blocked_users" __table_args__ = (UniqueConstraint("user_id", "blocked_user_id", name="uq_blocked_users_user_target"),) id: Mapped[int] = mapped_column(primary_key=True, index=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) blocked_user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) class UserContact(Base): __tablename__ = "user_contacts" __table_args__ = (UniqueConstraint("user_id", "contact_user_id", name="uq_user_contacts_pair"),) id: Mapped[int] = mapped_column(primary_key=True, index=True) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) contact_user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)