110 lines
4.4 KiB
Python
110 lines
4.4 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import String, DateTime, ForeignKey, Integer, UniqueConstraint, Index
|
|
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from .db import Base
|
|
|
|
|
|
def _uuid() -> uuid.UUID:
|
|
return uuid.uuid4()
|
|
|
|
|
|
def _now() -> datetime:
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=_uuid)
|
|
username: Mapped[str] = mapped_column(String(32), unique=True, index=True)
|
|
display_name: Mapped[str] = mapped_column(String(100))
|
|
password_hash: Mapped[str] = mapped_column(String(255))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
|
|
|
|
|
|
class Session(Base):
|
|
__tablename__ = "sessions"
|
|
token_hash: Mapped[str] = mapped_column(String(64), primary_key=True) # sha256(token) hex
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), index=True
|
|
)
|
|
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
|
|
|
|
|
|
class Chat(Base):
|
|
__tablename__ = "chats"
|
|
id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=_uuid)
|
|
title: Mapped[str] = mapped_column(String(100))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, index=True)
|
|
|
|
|
|
class ChatMember(Base):
|
|
__tablename__ = "chat_members"
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=_uuid)
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), index=True
|
|
)
|
|
sender_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id")
|
|
)
|
|
text: Mapped[str] = mapped_column(String(4000), default="")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, index=True)
|
|
|
|
|
|
class MessageAttachment(Base):
|
|
__tablename__ = "message_attachments"
|
|
message_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
attachment_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("attachments.id"), primary_key=True
|
|
)
|
|
position: Mapped[int] = mapped_column(Integer, default=0)
|
|
|
|
|
|
class IdempotencyKey(Base):
|
|
__tablename__ = "idempotency_keys"
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
method: Mapped[str] = mapped_column(String(8), primary_key=True)
|
|
path: Mapped[str] = mapped_column(String(255), primary_key=True)
|
|
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
|
request_hash: Mapped[str] = mapped_column(String(64))
|
|
response_json: Mapped[str] = mapped_column(String)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
|
|
|
|
__table_args__ = (Index("ix_idem_created", "created_at"),)
|
|
|
|
|
|
class Attachment(Base):
|
|
__tablename__ = "attachments"
|
|
id: Mapped[uuid.UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, default=_uuid)
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), index=True
|
|
)
|
|
owner_id: Mapped[uuid.UUID] = mapped_column(
|
|
PGUUID(as_uuid=True), ForeignKey("users.id")
|
|
)
|
|
filename: Mapped[str] = mapped_column(String(255))
|
|
content_type: Mapped[str] = mapped_column(String(32))
|
|
size_bytes: Mapped[int] = mapped_column(Integer)
|
|
sha256: Mapped[str] = mapped_column(String(64))
|
|
s3_key: Mapped[str] = mapped_column(String(255))
|
|
status: Mapped[str] = mapped_column(String(16), default="ready")
|
|
error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, onupdate=_now) |