from typing import Optional from uuid import UUID from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator from ..domain.models import Attachment, Chat, Message, Page, User from . import cursors from .timefmt import to_rfc3339 NAME_MAX = 100 TEXT_MAX = 4000 MEMBERS_MAX = 100 LIMIT_MIN = 1 LIMIT_MAX = 100 LIMIT_DEFAULT = 50 def not_blank(value: str) -> str: if not value.strip(): raise ValueError('строка не может состоять только из пробельных символов') return value class Strict(BaseModel): model_config = ConfigDict(extra='forbid') class HealthOut(Strict): status: str = 'ok' class CreateUser(Strict): 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=NAME_MAX) password: str = Field(min_length=12, max_length=128) @field_validator('display_name') @classmethod def _display_name(cls, value: str) -> str: return not_blank(value) class UserOut(Strict): id: UUID username: str display_name: str created_at: str @classmethod def from_domain(cls, user: User) -> 'UserOut': return cls( id=user.id, username=user.username, display_name=user.display_name, created_at=to_rfc3339(user.created_at), ) class SessionOut(Strict): token: str = Field(min_length=32) token_type: str = 'Bearer' expires_at: str user: UserOut class CreateChat(Strict): title: str = Field(min_length=1, max_length=NAME_MAX) member_ids: list[UUID] = Field(min_length=1, max_length=MEMBERS_MAX) @field_validator('title') @classmethod def _title(cls, value: str) -> str: return not_blank(value) @field_validator('member_ids') @classmethod def _members_unique(cls, value: list[UUID]) -> list[UUID]: if len(set(value)) != len(value): raise ValueError('member_ids не должен содержать дубликаты') return value class ChatOut(Strict): id: UUID title: str member_ids: list[UUID] created_at: str @classmethod def from_domain(cls, chat: Chat) -> 'ChatOut': return cls( id=chat.id, title=chat.title, member_ids=list(chat.member_ids), created_at=to_rfc3339(chat.created_at), ) class CreateMessage(Strict): text: str = Field(max_length=TEXT_MAX) attachment_ids: list[UUID] = Field(default_factory=list, max_length=10) @field_validator('attachment_ids') @classmethod def _attachments_unique(cls, value: list[UUID]) -> list[UUID]: if len(set(value)) != len(value): raise ValueError('attachment_ids не должен содержать дубликаты') return value @model_validator(mode='after') def _content_present(self) -> 'CreateMessage': if not self.text.strip() and not self.attachment_ids: raise ValueError('сообщение должно содержать текст или вложение') return self class MessageOut(Strict): id: UUID chat_id: UUID sender_id: UUID text: str created_at: str attachment_ids: list[UUID] @classmethod def from_domain(cls, message: Message) -> 'MessageOut': return cls( id=message.id, chat_id=message.chat_id, sender_id=message.sender_id, text=message.text, created_at=to_rfc3339(message.created_at), attachment_ids=list(message.attachment_ids), ) class AttachmentOut(Strict): id: UUID chat_id: UUID owner_id: UUID filename: str content_type: str size_bytes: int sha256: str status: str variants: list = Field(default_factory=list) error_code: str | None created_at: str updated_at: str @classmethod def from_domain(cls, attachment: Attachment) -> 'AttachmentOut': return cls( id=attachment.id, chat_id=attachment.chat_id, owner_id=attachment.owner_id, filename=attachment.filename, content_type=attachment.content_type, size_bytes=attachment.size_bytes, sha256=attachment.sha256, status=attachment.status, variants=[], error_code=attachment.error_code, created_at=to_rfc3339(attachment.created_at), updated_at=to_rfc3339(attachment.updated_at), ) class ChatPageOut(Strict): items: list[ChatOut] next_cursor: Optional[str] @classmethod def from_domain(cls, page: Page, scope: str) -> 'ChatPageOut': return cls( items=[ChatOut.from_domain(chat) for chat in page.items], next_cursor=cursors.encode(scope, page.next) if page.next else None, ) class MessagePageOut(Strict): items: list[MessageOut] next_cursor: Optional[str] @classmethod def from_domain(cls, page: Page, scope: str) -> 'MessagePageOut': return cls( items=[MessageOut.from_domain(message) for message in page.items], next_cursor=cursors.encode(scope, page.next) if page.next else None, )