from __future__ import annotations import re import uuid from typing import List, Optional from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$") class StrictModel(BaseModel): model_config = ConfigDict(extra="forbid", str_strip_whitespace=False) # ---------- Users ---------- class CreateUser(StrictModel): username: str = Field(min_length=3, max_length=32) display_name: str = Field(min_length=1, max_length=100) password: str = Field(min_length=12, max_length=128) @field_validator("username") @classmethod def _username(cls, v: str) -> str: if not USERNAME_RE.match(v): raise ValueError("username must match [a-z0-9_]{3,32}") return v @field_validator("display_name") @classmethod def _display_name(cls, v: str) -> str: if not v.strip(): raise ValueError("display_name must not be blank") if len(v) > 100: raise ValueError("display_name too long") return v class User(StrictModel): id: str username: str display_name: str created_at: str # ---------- Chats ---------- class CreateChat(StrictModel): title: str = Field(min_length=1, max_length=100) member_ids: List[str] = Field(min_length=1, max_length=100) @field_validator("title") @classmethod def _title(cls, v: str) -> str: if not v.strip(): raise ValueError("title must not be blank") return v @field_validator("member_ids") @classmethod def _members(cls, v: List[str]) -> List[str]: if len(set(v)) != len(v): raise ValueError("member_ids must be unique") for item in v: try: uuid.UUID(item) except ValueError as exc: raise ValueError("member_ids must be UUIDs") from exc return v class Chat(StrictModel): id: str title: str member_ids: List[str] created_at: str class ChatPage(StrictModel): items: List[Chat] next_cursor: Optional[str] = None # ---------- Attachments (lab4) ---------- class Variant(StrictModel): name: str content_type: str size_bytes: int = Field(ge=1) width: int = Field(ge=1) height: int = Field(ge=1) sha256: str = Field(pattern=r"^[a-f0-9]{64}$") class Attachment(StrictModel): id: str chat_id: str owner_id: str filename: str = Field(min_length=1, max_length=255) content_type: str # image/png или image/jpeg size_bytes: int = Field(ge=1, le=10485760) sha256: str = Field(pattern=r"^[a-f0-9]{64}$") status: str # 'ready' в lab4 variants: List[Variant] = Field(default_factory=list) error_code: Optional[str] = None created_at: str updated_at: str # ---------- Messages ---------- class CreateMessage(StrictModel): text: str = Field(default="", max_length=4000) attachment_ids: List[str] = Field(default_factory=list, max_length=10) @field_validator("text") @classmethod def _text(cls, v: str) -> str: if len(v) > 4000: raise ValueError("text too long") return v @field_validator("attachment_ids") @classmethod def _attachments(cls, v: List[str]) -> List[str]: if len(set(v)) != len(v): raise ValueError("attachment_ids must be unique") for item in v: try: uuid.UUID(item) except ValueError as exc: raise ValueError("attachment_ids must be UUIDs") from exc return v @model_validator(mode="after") def _text_or_attachment(self) -> "CreateMessage": has_text = bool(self.text and self.text.strip()) has_attachment = bool(self.attachment_ids) if not has_text and not has_attachment: raise ValueError("text must be non-blank or attachment_ids must be non-empty") return self class Message(StrictModel): id: str chat_id: str sender_id: str text: str created_at: str attachment_ids: List[str] = Field(default_factory=list) class MessagePage(StrictModel): items: List[Message] next_cursor: Optional[str] = None # ---------- Sessions ---------- class Session(StrictModel): token: str = Field(min_length=32) token_type: str = Field(default="Bearer") expires_at: str user: User # ---------- Misc ---------- class Health(StrictModel): status: str = "ok" class ErrorBody(StrictModel): code: str message: str request_id: str class ErrorEnvelope(StrictModel): error: ErrorBody