feat: commit staged matrix attachments on next message

This commit is contained in:
Mikhail Putilovskij 2026-04-20 21:39:37 +03:00
parent f111ed3348
commit 323a6d3144
3 changed files with 155 additions and 23 deletions

View file

@ -46,6 +46,7 @@ from core.chat import ChatManager
from core.handler import EventDispatcher
from core.handlers import register_all
from core.protocol import (
Attachment,
IncomingCommand,
IncomingMessage,
OutgoingEvent,
@ -246,6 +247,13 @@ class MatrixBot:
sender,
incoming,
)
clear_staged_after_dispatch = False
if isinstance(incoming, IncomingMessage) and incoming.text:
incoming, clear_staged_after_dispatch = await self._merge_staged_attachments(
room.room_id,
sender,
incoming,
)
try:
outgoing = await self.runtime.dispatcher.dispatch(incoming)
except PlatformError as exc:
@ -262,6 +270,9 @@ class MatrixBot:
text="Сервис временно недоступен. Попробуйте ещё раз позже.",
)
]
else:
if clear_staged_after_dispatch:
await clear_staged_attachments(self.runtime.store, room.room_id, sender)
await self._send_all(room.room_id, outgoing)
def _is_file_only_event(
@ -351,6 +362,37 @@ class MatrixBot:
)
]
async def _merge_staged_attachments(
self,
room_id: str,
user_id: str,
incoming: IncomingMessage,
) -> tuple[IncomingMessage, bool]:
staged = await get_staged_attachments(self.runtime.store, room_id, user_id)
if not staged:
return incoming, False
attachments = [
Attachment(
type=item.get("type", "document"),
url=item.get("url"),
filename=item.get("filename"),
mime_type=item.get("mime_type"),
workspace_path=item.get("workspace_path"),
)
for item in staged
]
return (
IncomingMessage(
user_id=incoming.user_id,
platform=incoming.platform,
chat_id=incoming.chat_id,
text=incoming.text,
attachments=attachments,
reply_to=incoming.reply_to,
),
True,
)
async def _materialize_incoming_attachments(
self,
room_id: str,