from datetime import datetime from uuid import UUID from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator class CreateUserRequest(BaseModel): model_config = ConfigDict(extra="forbid", strict=True) username: str = Field(min_length=3, max_length=32, pattern=r"^[a-z0-9_]{3,32}$") display_name: str = Field(min_length=1, max_length=100) password: str = Field(min_length=12, max_length=128) @field_validator("display_name") @classmethod def display_name_must_not_be_whitespace(cls, value: str) -> str: """Не допускает обязательное имя, состоящее только из пробелов.""" if not value.strip(): raise ValueError("display_name не может состоять только из пробелов") return value class CreateChatRequest(BaseModel): model_config = ConfigDict(extra="forbid") title: str = Field(min_length=1, max_length=100) member_ids: list[UUID] = Field(min_length=1, max_length=100) @field_validator("title") @classmethod def title_must_not_be_whitespace(cls, value: str) -> str: """Не допускает обязательное название, состоящее только из пробелов.""" if not value.strip(): raise ValueError("title не может состоять только из пробелов") return value class ChatPageResponse(BaseModel): model_config = ConfigDict(extra="forbid") items: list["Chat"] next_cursor: str | None class CreateMessageRequest(BaseModel): model_config = ConfigDict(extra="forbid", strict=True) text: str = Field(max_length=4000) attachment_ids: list[str] = Field(default_factory=list, max_length=10) @field_validator("attachment_ids") @classmethod def valid_ids(cls, values: list[str]) -> list[str]: normalized = [str(UUID(value)) for value in values] if len(normalized) != len(set(normalized)): raise ValueError("Повторяющиеся attachment_ids") return normalized @model_validator(mode="after") def has_content(self): if not self.text.strip() and not self.attachment_ids: raise ValueError("Сообщение без содержимого") return self class MessagePageResponse(BaseModel): model_config = ConfigDict(extra="forbid") items: list["Message"] next_cursor: str | None class ErrorDetails(BaseModel): model_config = ConfigDict(extra="forbid") code: str message: str request_id: str class ErrorResponse(BaseModel): model_config = ConfigDict(extra="forbid") error: ErrorDetails class User(BaseModel): model_config = ConfigDict(extra="forbid") id: UUID username: str display_name: str created_at: datetime class Chat(BaseModel): model_config = ConfigDict(extra="forbid") id: UUID title: str member_ids: list[UUID] created_at: datetime class Message(BaseModel): model_config = ConfigDict(extra="forbid") id: UUID chat_id: UUID sender_id: UUID text: str attachment_ids: list[UUID] = Field(default_factory=list) created_at: datetime class Session(BaseModel): model_config = ConfigDict(extra="forbid") token: str = Field(min_length=32) token_type: str = "Bearer" expires_at: datetime user: User class Attachment(BaseModel): model_config = ConfigDict(extra="ignore") id: UUID chat_id: UUID owner_id: UUID filename: str content_type: str size_bytes: int sha256: str status: str variants: list["Variant"] = Field(default_factory=list) error_code: str | None = None created_at: datetime updated_at: datetime class Variant(BaseModel): model_config = ConfigDict(extra="forbid") name: str content_type: str size_bytes: int width: int height: int sha256: str