Forward the lab 5 foundation while preserving lab 6 telemetry

Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.

Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
This commit is contained in:
Artyom
2026-11-06 12:29:35 +03:00
parent dc9256baf9
commit 12db77133f
161 changed files with 11160 additions and 1467 deletions
+31 -29
View File
@@ -1,9 +1,14 @@
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.messaging.application.ports import ChatDirectory, MessageLog, MessagingUnitOfWork
from messenger.messaging.domain.errors import (
ChatNotFound,
InvalidAttachmentList,
InvalidMessageText,
)
from messenger.messaging.domain.models import Chat, Message, attachment_list
from messenger.messaging.domain.values import MessageText
from messenger.shared.application.ports import Clock, IdGenerator
@@ -30,44 +35,41 @@ class CreateChatHandler:
class PostMessageHandler:
__slots__ = ("_attachments", "_clock", "_ids", "_ttl", "_uow")
__slots__ = ("_chats", "_clock", "_ids", "_log", "_ttl", "_uow")
def __init__(
self,
uow: MessagingUnitOfWork,
attachments: AttachmentStates,
log: MessageLog,
chats: ChatDirectory,
clock: Clock,
ids: IdGenerator,
*,
ttl: timedelta,
) -> None:
self._uow = uow
self._attachments = attachments
self._log = log
self._chats = chats
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()
try:
text = MessageText(command.text)
attachments = attachment_list(text, command.attachment_ids)
except InvalidMessageText, InvalidAttachmentList:
if await self._chats.chat_for_member(command.chat_id, command.sender_id) is None:
raise ChatNotFound("чат не найден") from None
raise
now = self._clock.now()
draft = Message(
self._ids.new_id(), command.chat_id, command.sender_id, text, now, attachments
)
receipt = await self._log.post(draft, command.key, command.fingerprint, now - self._ttl)
if receipt is None:
raise ChatNotFound("чат не найден")
message = receipt.replay(command.fingerprint)
if message.id == draft.id:
self._uow.record(message.posted())
return message