forked from mtusi2026/mtusi2026lab2
111 lines
2.5 KiB
Python
111 lines
2.5 KiB
Python
|
|
from datetime import datetime, timezone
|
|
from typing import List, Optional
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
class BaseConfig(BaseModel):
|
|
model_config = ConfigDict(
|
|
from_attributes=True,
|
|
json_encoders={
|
|
datetime: lambda value: (
|
|
value.astimezone(timezone.utc)
|
|
.isoformat(timespec="microseconds")
|
|
.replace("+00:00", "Z")
|
|
)
|
|
},
|
|
)
|
|
|
|
|
|
class CreateUser(BaseConfig):
|
|
username: str = Field(..., 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 validate_display_name(cls, value: str) -> str:
|
|
value = value.strip()
|
|
if not value:
|
|
raise ValueError("display_name must not be blank")
|
|
return value
|
|
|
|
|
|
class User(BaseConfig):
|
|
id: str
|
|
username: str
|
|
display_name: str
|
|
created_at: datetime
|
|
|
|
|
|
class CreateChat(BaseConfig):
|
|
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 validate_title(cls, value: str) -> str:
|
|
value = value.strip()
|
|
if not value:
|
|
raise ValueError("title must not be blank")
|
|
return value
|
|
|
|
@field_validator("member_ids")
|
|
@classmethod
|
|
def validate_unique_members(cls, value: List[str]) -> List[str]:
|
|
if len(value) != len(set(value)):
|
|
raise ValueError("member_ids must be unique")
|
|
return value
|
|
|
|
|
|
class Chat(BaseConfig):
|
|
id: str
|
|
title: str
|
|
member_ids: List[str]
|
|
created_at: datetime
|
|
|
|
|
|
class CreateMessage(BaseConfig):
|
|
text: str = Field(..., min_length=1, max_length=4000)
|
|
|
|
@field_validator("text")
|
|
@classmethod
|
|
def validate_text(cls, value: str) -> str:
|
|
value = value.strip()
|
|
if not value:
|
|
raise ValueError("text must not be blank")
|
|
return value
|
|
|
|
|
|
class Message(BaseConfig):
|
|
id: str
|
|
chat_id: str
|
|
sender_id: str
|
|
text: str
|
|
created_at: datetime
|
|
|
|
|
|
class Health(BaseConfig):
|
|
status: str = "ok"
|
|
|
|
|
|
class ErrorDetail(BaseConfig):
|
|
code: str
|
|
message: str
|
|
request_id: str
|
|
|
|
|
|
class ErrorResponse(BaseConfig):
|
|
error: ErrorDetail
|
|
|
|
|
|
class ChatPage(BaseConfig):
|
|
items: List[Chat]
|
|
next_cursor: Optional[str] = None
|
|
|
|
|
|
class MessagePage(BaseConfig):
|
|
items: List[Message]
|
|
next_cursor: Optional[str] = None
|