Carry over the lab 3 solution

Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy and k6 configuration, migrations, hooks, evidence and level 4
scripts and the CI job from the lab 3 solution commit 61c0c28, merge
the lab 3 Makefile targets into the lab 4 skeleton Makefile without
touching its targets, and append the lab 3 solution variables to the
skeleton .env.example. The package and the application follow the
contract version 1.4.0, and the image and the throwaway test containers
are tagged lab4 so they do not collide with a lab 3 stand on the same
host.

This is the starting point of the lab 4 cycle: lint, own tests and the
offline skeleton check pass before any lab 4 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 20:17:51 +03:00
co-authored by Claude Opus 5.5
parent b0f463ac5c
commit bf8e4e3cd1
129 changed files with 8698 additions and 1 deletions
@@ -0,0 +1,5 @@
from messenger.shared.application.messagebus import CommandBus
class MessagingCommandBus(CommandBus):
__slots__ = ()
@@ -0,0 +1,21 @@
from collections.abc import Sequence
from dataclasses import dataclass
from uuid import UUID
from messenger.shared.application.commands import Command
@dataclass(frozen=True, slots=True)
class CreateChat(Command):
creator_id: UUID
title: str
member_ids: Sequence[UUID]
@dataclass(frozen=True, slots=True)
class PostMessage(Command):
sender_id: UUID
chat_id: UUID
text: str
key: str
fingerprint: bytes
@@ -0,0 +1,62 @@
from datetime import timedelta
from messenger.messaging.application.commands import CreateChat, PostMessage
from messenger.messaging.application.ports import MessagingUnitOfWork
from messenger.messaging.domain.errors import ChatNotFound
from messenger.messaging.domain.models import Chat, Message, Receipt
from messenger.shared.application.ports import Clock, IdGenerator
class CreateChatHandler:
__slots__ = ("_clock", "_ids", "_uow")
def __init__(self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator) -> None:
self._uow = uow
self._clock = clock
self._ids = ids
async def __call__(self, command: CreateChat) -> Chat:
chat = Chat.create(
chat_id=self._ids.new_id(),
title=command.title,
member_ids=command.member_ids,
creator_id=command.creator_id,
created_at=self._clock.now(),
)
async with self._uow.transaction():
await self._uow.chats.add(chat)
await self._uow.commit()
return chat
class PostMessageHandler:
__slots__ = ("_clock", "_ids", "_ttl", "_uow")
def __init__(
self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator, ttl: timedelta
) -> None:
self._uow = uow
self._clock = clock
self._ids = ids
self._ttl = ttl
async def __call__(self, command: PostMessage) -> Message:
async with self._uow.transaction():
chat = await self._uow.chats.get(command.chat_id)
if chat is None:
raise ChatNotFound("чат не найден")
now = self._clock.now()
message = chat.post(
message_id=self._ids.new_id(),
sender_id=command.sender_id,
text=command.text,
now=now,
)
known = await self._uow.chats.receipt(
chat.id, command.sender_id, command.key, now - self._ttl
)
if known is not None:
return known.replay(command.fingerprint)
await self._uow.chats.append(Receipt(command.key, command.fingerprint, message))
await self._uow.commit()
return message
@@ -0,0 +1,46 @@
from datetime import datetime
from typing import Protocol, runtime_checkable
from uuid import UUID
from messenger.messaging.domain.keys import HistoryKey
from messenger.messaging.domain.models import Chat, Message, Receipt
from messenger.shared.application.ports import UnitOfWork
@runtime_checkable
class CursorCodec(Protocol):
def encode(self, scope: str, key: HistoryKey) -> str: ...
def decode(self, scope: str, raw: str) -> HistoryKey: ...
@runtime_checkable
class ChatRepository(Protocol):
async def add(self, chat: Chat) -> None: ...
async def get(self, chat_id: UUID) -> Chat | None: ...
async def receipt(
self, chat_id: UUID, sender_id: UUID, key: str, since: datetime
) -> Receipt | None: ...
async def append(self, receipt: Receipt) -> None: ...
@runtime_checkable
class MessagingUnitOfWork(UnitOfWork, Protocol):
@property
def chats(self) -> ChatRepository: ...
@runtime_checkable
class ChatDirectory(Protocol):
async def chat_for_member(self, chat_id: UUID, user_id: UUID) -> Chat | None: ...
async def chats_of(
self, user_id: UUID, *, after: HistoryKey | None, limit: int
) -> tuple[list[Chat], bool]: ...
async def history(
self, chat_id: UUID, *, after: HistoryKey | None, limit: int
) -> tuple[list[Message], bool]: ...
@@ -0,0 +1,73 @@
from dataclasses import dataclass
from uuid import UUID
from messenger.messaging.application.ports import ChatDirectory, CursorCodec
from messenger.messaging.domain.errors import ChatNotFound
from messenger.messaging.domain.keys import HistoryKey
from messenger.messaging.domain.models import Chat, Message
@dataclass(frozen=True, slots=True)
class Page[ItemT]:
items: list[ItemT]
next_cursor: str | None
def chats_scope(user_id: UUID) -> str:
return f"chats:{user_id}"
def messages_scope(chat_id: UUID) -> str:
return f"messages:{chat_id}"
class GetChat:
__slots__ = ("_chats",)
def __init__(self, chats: ChatDirectory) -> None:
self._chats = chats
async def __call__(self, chat_id: UUID, user_id: UUID) -> Chat:
chat = await self._chats.chat_for_member(chat_id, user_id)
if chat is None:
raise ChatNotFound("чат не найден")
return chat
class ListChats:
__slots__ = ("_chats", "_cursors")
def __init__(self, chats: ChatDirectory, cursors: CursorCodec) -> None:
self._chats = chats
self._cursors = cursors
async def __call__(self, user_id: UUID, *, limit: int, cursor: str | None) -> Page[Chat]:
scope = chats_scope(user_id)
after: HistoryKey | None = self._cursors.decode(scope, cursor) if cursor else None
items, has_more = await self._chats.chats_of(user_id, after=after, limit=limit)
next_cursor = self._cursors.encode(scope, items[-1].key) if has_more and items else None
return Page(items=items, next_cursor=next_cursor)
class ReadHistory:
__slots__ = ("_chats", "_cursors")
def __init__(self, chats: ChatDirectory, cursors: CursorCodec) -> None:
self._chats = chats
self._cursors = cursors
async def __call__(
self,
chat_id: UUID,
user_id: UUID,
*,
limit: int,
cursor: str | None,
) -> Page[Message]:
if await self._chats.chat_for_member(chat_id, user_id) is None:
raise ChatNotFound("чат не найден")
scope = messages_scope(chat_id)
after: HistoryKey | None = self._cursors.decode(scope, cursor) if cursor else None
items, has_more = await self._chats.history(chat_id, after=after, limit=limit)
next_cursor = self._cursors.encode(scope, items[-1].key) if has_more and items else None
return Page(items=items, next_cursor=next_cursor)