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:
Artyom
2026-10-29 01:09:31 +03:00
co-authored by Claude Opus 5.5
parent b152b6e07e
commit 34741dc2d2
8 changed files with 95 additions and 12 deletions
@@ -1,4 +1,5 @@
from messenger.messaging.domain.errors import (
AttachmentUnavailable,
ChatNotFound,
IdempotencyConflict,
InvalidAttachmentList,
@@ -17,4 +18,5 @@ ERROR_CODES: dict[type[DomainError], tuple[str, int]] = {
ChatNotFound: ("not_found", 404),
UnknownAttachment: ("not_found", 404),
IdempotencyConflict: ("conflict", 409),
AttachmentUnavailable: ("conflict", 409),
}
+8 -2
View File
@@ -5,7 +5,12 @@ from dishka import Provider, Scope, provide
from messenger.messaging.application.bus import MessagingCommandBus
from messenger.messaging.application.commands import CreateChat, PostMessage
from messenger.messaging.application.handlers import CreateChatHandler, PostMessageHandler
from messenger.messaging.application.ports import ChatDirectory, CursorCodec, MessagingUnitOfWork
from messenger.messaging.application.ports import (
AttachmentStates,
ChatDirectory,
CursorCodec,
MessagingUnitOfWork,
)
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
from messenger.messaging.config import MessagingSettings
from messenger.messaging.infrastructure.cursor import SignedCursorCodec
@@ -56,6 +61,7 @@ class MessagingProvider(Provider):
def command_bus(
self,
uow: MessagingUnitOfWork,
attachments: AttachmentStates,
publisher: EventPublisher,
clock: Clock,
ids: IdGenerator,
@@ -64,6 +70,6 @@ class MessagingProvider(Provider):
ttl = timedelta(seconds=settings.idempotency_ttl_seconds)
handlers: dict[type[Command], CommandHandler] = {
CreateChat: CreateChatHandler(uow, clock, ids),
PostMessage: PostMessageHandler(uow, clock, ids, ttl),
PostMessage: PostMessageHandler(uow, attachments, clock, ids, ttl),
}
return MessagingCommandBus(handlers, uow, publisher)