diff --git a/adapter/matrix/bot.py b/adapter/matrix/bot.py index 974882d..cc1146d 100644 --- a/adapter/matrix/bot.py +++ b/adapter/matrix/bot.py @@ -153,13 +153,16 @@ class MatrixBot: return sender = getattr(event, "sender", None) body = (getattr(event, "body", None) or "").strip() + room_meta = await get_room_meta(self.runtime.store, room.room_id) + if room_meta is not None and not room_meta.get("redirect_room_id"): + await self._ensure_platform_chat_id(room.room_id, room_meta) + load_pending = await get_load_pending(self.runtime.store, sender, room.room_id) if load_pending is not None and (body.isdigit() or body == "!cancel"): outgoing = await self._handle_load_selection(sender, room.room_id, body, load_pending) await self._send_all(room.room_id, outgoing) return - room_meta = await get_room_meta(self.runtime.store, room.room_id) if room_meta is None: outgoing = await self._bootstrap_unregistered_room(room, sender) if outgoing: @@ -187,9 +190,6 @@ class MatrixBot: user=sender, ) return - else: - await self._ensure_platform_chat_id(room.room_id, room_meta) - chat_id = await resolve_chat_id(self.runtime.store, room.room_id, sender) incoming = from_room_event(event, room_id=room.room_id, chat_id=chat_id) if incoming is None: diff --git a/tests/adapter/matrix/test_dispatcher.py b/tests/adapter/matrix/test_dispatcher.py index 68f27a8..b3efa85 100644 --- a/tests/adapter/matrix/test_dispatcher.py +++ b/tests/adapter/matrix/test_dispatcher.py @@ -9,7 +9,14 @@ from nio.responses import SyncResponse from adapter.matrix.bot import MatrixBot, build_runtime, prepare_live_sync from adapter.matrix.handlers.auth import handle_invite -from adapter.matrix.store import get_platform_chat_id, get_room_meta, get_user_meta, set_room_meta, set_user_meta +from adapter.matrix.store import ( + get_platform_chat_id, + get_room_meta, + get_user_meta, + set_load_pending, + set_room_meta, + set_user_meta, +) from core.protocol import IncomingCallback, IncomingCommand, OutgoingMessage from sdk.interface import PlatformError from sdk.mock import MockPlatformClient @@ -270,6 +277,37 @@ async def test_bot_leaves_existing_platform_chat_id_unchanged(): runtime.dispatcher.dispatch.assert_awaited_once() +async def test_bot_assigns_platform_chat_id_before_load_selection(): + runtime = build_runtime(platform=MockPlatformClient()) + await set_room_meta( + runtime.store, + "!chat1:example.org", + {"chat_id": "C1", "matrix_user_id": "@alice:example.org"}, + ) + client = SimpleNamespace( + user_id="@bot:example.org", + room_send=AsyncMock(), + ) + bot = MatrixBot(client, runtime) + await set_load_pending( + runtime.store, + "@alice:example.org", + "!chat1:example.org", + {"saves": [{"name": "session-a", "created_at": "2026-04-17T00:00:00+00:00"}]}, + ) + room = SimpleNamespace(room_id="!chat1:example.org") + event = SimpleNamespace(sender="@alice:example.org", body="0") + + await bot.on_room_message(room, event) + + assert await get_platform_chat_id(runtime.store, "!chat1:example.org") == "matrix:!chat1:example.org" + client.room_send.assert_awaited_once_with( + "!chat1:example.org", + "m.room.message", + {"msgtype": "m.text", "body": "Отменено."}, + ) + + async def test_unregistered_room_bootstraps_space_and_chat_on_first_message(): runtime = build_runtime(platform=MockPlatformClient()) await set_user_meta(runtime.store, "@alice:example.org", {"next_chat_index": 1})