69 lines
2.8 KiB
Python
69 lines
2.8 KiB
Python
"""Chat and Message models — unified message for chats and task comments."""
|
|
|
|
import uuid
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import ForeignKey, Integer, String, Text
|
|
from sqlalchemy.dialects.postgresql import ARRAY, UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from tracker.enums import ChatKind
|
|
from tracker.models.base import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from tracker.models.project import Project
|
|
|
|
|
|
class Chat(Base):
|
|
"""Chat room — lobby, project, or custom."""
|
|
__tablename__ = "chats"
|
|
|
|
project_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("projects.id"))
|
|
kind: Mapped[str] = mapped_column(String(20), default=ChatKind.PROJECT) # lobby | project
|
|
|
|
project: Mapped["Project | None"] = relationship(back_populates="chats")
|
|
messages: Mapped[list["Message"]] = relationship(
|
|
back_populates="chat", cascade="all, delete-orphan",
|
|
foreign_keys="Message.chat_id"
|
|
)
|
|
|
|
|
|
class Message(Base):
|
|
"""Unified message — works for chat messages AND task comments."""
|
|
__tablename__ = "messages"
|
|
|
|
# Context: one of chat_id or task_id must be set
|
|
chat_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("chats.id"))
|
|
task_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("tasks.id"))
|
|
|
|
# Thread support
|
|
parent_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("messages.id"))
|
|
|
|
# Author
|
|
author_type: Mapped[str] = mapped_column(String(20), nullable=False) # human | agent | system
|
|
author_slug: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
|
|
# Content
|
|
content: Mapped[str] = mapped_column(Text, nullable=False)
|
|
mentions: Mapped[list[str]] = mapped_column(ARRAY(String), default=list)
|
|
voice_url: Mapped[str | None] = mapped_column(String(500))
|
|
|
|
# Relationships
|
|
chat: Mapped["Chat | None"] = relationship(back_populates="messages", foreign_keys=[chat_id])
|
|
attachments: Mapped[list["Attachment"]] = relationship(back_populates="message", cascade="all, delete-orphan")
|
|
replies: Mapped[list["Message"]] = relationship(back_populates="parent")
|
|
parent: Mapped["Message | None"] = relationship(back_populates="replies", remote_side="Message.id")
|
|
|
|
|
|
class Attachment(Base):
|
|
"""File attachment on a message."""
|
|
__tablename__ = "attachments"
|
|
|
|
message_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("messages.id"), nullable=False)
|
|
filename: Mapped[str] = mapped_column(String(500), nullable=False)
|
|
mime_type: Mapped[str | None] = mapped_column(String(100))
|
|
size: Mapped[int] = mapped_column(Integer, default=0) # bytes
|
|
storage_path: Mapped[str] = mapped_column(String(1000), nullable=False)
|
|
|
|
message: Mapped["Message"] = relationship(back_populates="attachments")
|