forked from mtusi2026/mtusi2026lab5
Refuse failed attachments in messages through a zone port
The messaging zone asks a new AttachmentStates port whether any listed attachment of the chat has failed and answers 409 conflict before the message is stored; queued and processing attachments can still be sent right after the upload. The check runs only on the first execution of a key, so a replay returns the original 201 even if the attachment failed later. main.py wires the port to the attachments directory, so neither zone reads the other's tables. 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
b152b6e07e
commit
34741dc2d2
@@ -1,8 +1,8 @@
|
||||
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.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
|
||||
|
||||
@@ -30,12 +30,18 @@ class CreateChatHandler:
|
||||
|
||||
|
||||
class PostMessageHandler:
|
||||
__slots__ = ("_clock", "_ids", "_ttl", "_uow")
|
||||
__slots__ = ("_attachments", "_clock", "_ids", "_ttl", "_uow")
|
||||
|
||||
def __init__(
|
||||
self, uow: MessagingUnitOfWork, clock: Clock, ids: IdGenerator, ttl: timedelta
|
||||
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
|
||||
@@ -58,6 +64,10 @@ class PostMessageHandler:
|
||||
)
|
||||
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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from collections.abc import Sequence
|
||||
from datetime import datetime
|
||||
from typing import Protocol, runtime_checkable
|
||||
from uuid import UUID
|
||||
@@ -33,6 +34,11 @@ class MessagingUnitOfWork(UnitOfWork, Protocol):
|
||||
def chats(self) -> ChatRepository: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class AttachmentStates(Protocol):
|
||||
async def unusable(self, chat_id: UUID, attachment_ids: Sequence[UUID]) -> bool: ...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ChatDirectory(Protocol):
|
||||
async def chat_for_member(self, chat_id: UUID, user_id: UUID) -> Chat | None: ...
|
||||
|
||||
Reference in New Issue
Block a user