forked from mtusi2026/mtusi2026lab3
148 lines
3.7 KiB
Python
148 lines
3.7 KiB
Python
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
from ..domain.models import 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(min_length=1, max_length=TEXT_MAX)
|
|
|
|
@field_validator('text')
|
|
@classmethod
|
|
def _text(cls, value: str) -> str:
|
|
return not_blank(value)
|
|
|
|
|
|
class MessageOut(Strict):
|
|
id: UUID
|
|
chat_id: UUID
|
|
sender_id: UUID
|
|
text: str
|
|
created_at: str
|
|
|
|
@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),
|
|
)
|
|
|
|
|
|
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,
|
|
)
|