forked from mtusi2026/mtusi2026lab4
80 lines
1.7 KiB
Python
80 lines
1.7 KiB
Python
"""Immutable domain values; no dependency on HTTP or a storage technology."""
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import UTC, datetime
|
|
from uuid import uuid4
|
|
|
|
|
|
def utc_now() -> str:
|
|
return datetime.now(UTC).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
|
|
|
|
|
def new_id() -> str:
|
|
return str(uuid4())
|
|
|
|
|
|
class DomainError(Exception):
|
|
def __init__(self, status: int, code: str, message: str, headers: dict | None = None):
|
|
super().__init__(message)
|
|
self.status = status
|
|
self.code = code
|
|
self.message = message
|
|
self.headers = headers or {}
|
|
|
|
|
|
def invalid(message: str = "Некорректный запрос") -> DomainError:
|
|
return DomainError(400, "invalid_request", message)
|
|
|
|
|
|
def not_found() -> DomainError:
|
|
return DomainError(404, "not_found", "Объект не найден")
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class User:
|
|
id: str
|
|
username: str
|
|
display_name: str
|
|
created_at: str
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Chat:
|
|
id: str
|
|
title: str
|
|
member_ids: tuple[str, ...]
|
|
created_at: str
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Message:
|
|
id: str
|
|
chat_id: str
|
|
sender_id: str
|
|
text: str
|
|
created_at: str
|
|
attachment_ids: tuple[str, ...] = ()
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Attachment:
|
|
id: str
|
|
chat_id: str
|
|
owner_id: str
|
|
filename: str
|
|
content_type: str
|
|
size_bytes: int
|
|
sha256: str
|
|
created_at: str
|
|
updated_at: str
|
|
status: str = "ready"
|
|
variants: tuple = ()
|
|
error_code: str | None = None
|
|
|
|
|
|
type HistoryKey = tuple[str, str]
|
|
|
|
|
|
def history_key(item: Chat | Message) -> HistoryKey:
|
|
return item.created_at, item.id
|