forked from mtusi2026/mtusi2026lab5
Carry over the lab 4 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and scenario scripts and the CI job from the lab 4 solution commit d2744bc, merge the lab 4 Makefile targets into the lab 5 skeleton Makefile without touching its targets, and append the lab 4 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.5.0, and the image and the throwaway test containers are tagged lab5 so they do not collide with a lab 4 stand on the same host. This is the starting point of the lab 5 cycle: lint, own tests and the offline skeleton check pass before any lab 5 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1210dd899c
commit
540fcb35ba
@@ -0,0 +1,5 @@
|
||||
from messenger.shared.application.messagebus import CommandBus
|
||||
|
||||
|
||||
class MessagingCommandBus(CommandBus):
|
||||
__slots__ = ()
|
||||
@@ -0,0 +1,22 @@
|
||||
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
|
||||
attachment_ids: tuple[UUID, ...] = ()
|
||||
@@ -0,0 +1,63 @@
|
||||
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,
|
||||
attachment_ids=command.attachment_ids,
|
||||
)
|
||||
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)
|
||||
Reference in New Issue
Block a user