194 lines
5.3 KiB
Python
194 lines
5.3 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from typing import Literal
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
|
|
|
|
class ApiModel(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
|
|
class CreateUser(ApiModel):
|
|
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("username")
|
|
@classmethod
|
|
def username_is_not_blank(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("username must not contain only spaces.")
|
|
return value
|
|
|
|
@field_validator("display_name")
|
|
@classmethod
|
|
def display_name_is_not_blank(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError(
|
|
"display_name must not contain only spaces."
|
|
)
|
|
return value
|
|
|
|
@field_validator("password")
|
|
@classmethod
|
|
def password_is_not_blank(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("password must not contain only spaces.")
|
|
return value
|
|
|
|
|
|
class User(ApiModel):
|
|
id: UUID
|
|
username: str
|
|
display_name: str
|
|
created_at: datetime
|
|
|
|
@field_validator("created_at")
|
|
@classmethod
|
|
def user_time_is_utc(cls, value: datetime) -> datetime:
|
|
if value.tzinfo is None:
|
|
raise ValueError("created_at must include UTC timezone.")
|
|
return value.astimezone(timezone.utc)
|
|
|
|
|
|
class CreateChat(ApiModel):
|
|
# Contract: 1-100 code points (lab2 allowed 255 by mistake).
|
|
title: str = Field(min_length=1, max_length=100)
|
|
# Contract: 1-100 members (minItems/maxItems in openapi.json).
|
|
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
|
|
|
@field_validator("title")
|
|
@classmethod
|
|
def title_is_not_blank(cls, value: str) -> str:
|
|
if not value.strip():
|
|
raise ValueError("title must not contain only spaces.")
|
|
return value
|
|
|
|
|
|
class Chat(ApiModel):
|
|
id: UUID
|
|
title: str
|
|
member_ids: list[UUID]
|
|
created_at: datetime
|
|
|
|
@field_validator("created_at")
|
|
@classmethod
|
|
def chat_time_is_utc(cls, value: datetime) -> datetime:
|
|
if value.tzinfo is None:
|
|
raise ValueError("created_at must include UTC timezone.")
|
|
return value.astimezone(timezone.utc)
|
|
|
|
|
|
class ChatPage(ApiModel):
|
|
items: list[Chat]
|
|
next_cursor: str | None = None
|
|
|
|
|
|
class CreateMessage(ApiModel):
|
|
# Lab4: text may be "" when the message carries attachments.
|
|
text: str = Field(max_length=4000)
|
|
# 0-10 unique IDs of attachments of the same chat; missing == [].
|
|
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
|
|
|
@field_validator("attachment_ids")
|
|
@classmethod
|
|
def attachment_ids_are_unique(cls, value: list[UUID]) -> list[UUID]:
|
|
if len(set(value)) != len(value):
|
|
raise ValueError("attachment_ids must be unique.")
|
|
return value
|
|
|
|
@model_validator(mode="after")
|
|
def text_or_attachments(self) -> "CreateMessage":
|
|
if not self.attachment_ids and not self.text.strip():
|
|
raise ValueError(
|
|
"text must not be blank when there are no attachments."
|
|
)
|
|
return self
|
|
|
|
|
|
class Message(ApiModel):
|
|
id: UUID
|
|
chat_id: UUID
|
|
sender_id: UUID
|
|
text: str
|
|
created_at: datetime
|
|
attachment_ids: list[UUID] = Field(default_factory=list)
|
|
|
|
@field_validator("created_at")
|
|
@classmethod
|
|
def message_time_is_utc(cls, value: datetime) -> datetime:
|
|
if value.tzinfo is None:
|
|
raise ValueError("created_at must include UTC timezone.")
|
|
return value.astimezone(timezone.utc)
|
|
|
|
|
|
class MessagePage(ApiModel):
|
|
items: list[Message]
|
|
next_cursor: str | None = None
|
|
|
|
|
|
class Session(ApiModel):
|
|
token: str = Field(min_length=1)
|
|
token_type: Literal["Bearer"]
|
|
expires_at: datetime
|
|
user: User
|
|
|
|
@field_validator("expires_at")
|
|
@classmethod
|
|
def expiry_time_is_utc(cls, value: datetime) -> datetime:
|
|
if value.tzinfo is None:
|
|
raise ValueError("expires_at must include UTC timezone.")
|
|
return value.astimezone(timezone.utc)
|
|
|
|
|
|
class Variant(ApiModel):
|
|
name: Literal["thumbnail", "crop", "optimized"]
|
|
content_type: Literal["image/jpeg", "image/png", "image/webp"]
|
|
size_bytes: int
|
|
width: int
|
|
height: int
|
|
sha256: str
|
|
|
|
|
|
class Attachment(ApiModel):
|
|
id: UUID
|
|
chat_id: UUID
|
|
owner_id: UUID
|
|
filename: str
|
|
content_type: Literal["image/png", "image/jpeg"]
|
|
size_bytes: int
|
|
sha256: str
|
|
status: Literal["queued", "processing", "ready", "failed"]
|
|
variants: list[Variant]
|
|
error_code: str | None
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
@field_validator("created_at", "updated_at")
|
|
@classmethod
|
|
def attachment_time_is_utc(cls, value: datetime) -> datetime:
|
|
if value.tzinfo is None:
|
|
raise ValueError("timestamps must include UTC timezone.")
|
|
return value.astimezone(timezone.utc)
|
|
|
|
|
|
class Health(ApiModel):
|
|
status: Literal["ok"]
|
|
|
|
|
|
class ErrorInner(ApiModel):
|
|
code: str = Field(min_length=1)
|
|
message: str = Field(min_length=1)
|
|
request_id: str = Field(min_length=1)
|
|
|
|
|
|
class Error(ApiModel):
|
|
error: ErrorInner
|