Files
mtusi2026lab2/src/messenger/messaging/application/handlers.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

74 lines
2.7 KiB
Python

from datetime import timedelta
from messenger.messaging.application.commands import CreateChat, PostMessage
from messenger.messaging.application.ports import AttachmentStates, MessagingUnitOfWork
from messenger.messaging.domain.errors import AttachmentUnavailable, 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__ = ("_attachments", "_clock", "_ids", "_ttl", "_uow")
def __init__(
self,
uow: MessagingUnitOfWork,
attachments: AttachmentStates,
clock: Clock,
ids: IdGenerator,
ttl: timedelta,
) -> None:
self._uow = uow
self._attachments = attachments
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)
if message.attachment_ids and await self._attachments.unusable(
chat.id, message.attachment_ids
):
raise AttachmentUnavailable("вложение не обработано и не может быть отправлено")
await self._uow.chats.append(Receipt(command.key, command.fingerprint, message))
await self._uow.commit()
return message