forked from mtusi2026/mtusi2026lab5
Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -19,14 +19,13 @@ class CursorCodec(Protocol):
|
||||
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
|
||||
@runtime_checkable
|
||||
class MessageLog(Protocol):
|
||||
async def post(
|
||||
self, message: Message, key: str, fingerprint: bytes, since: datetime
|
||||
) -> Receipt | None: ...
|
||||
|
||||
async def append(self, receipt: Receipt) -> None: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class MessagingUnitOfWork(UnitOfWork, Protocol):
|
||||
|
||||
@@ -43,7 +43,7 @@ class ListChats:
|
||||
|
||||
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
|
||||
after: HistoryKey | None = None if cursor is None else self._cursors.decode(scope, cursor)
|
||||
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)
|
||||
@@ -67,7 +67,7 @@ class ReadHistory:
|
||||
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
|
||||
after: HistoryKey | None = None if cursor is None else self._cursors.decode(scope, cursor)
|
||||
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