fix: ensure lazy platform chat ids before load selection

This commit is contained in:
Mikhail Putilovskij 2026-04-19 17:29:36 +03:00
parent 9cb1657d21
commit 0cdee532c4
2 changed files with 43 additions and 5 deletions

View file

@ -153,13 +153,16 @@ class MatrixBot:
return return
sender = getattr(event, "sender", None) sender = getattr(event, "sender", None)
body = (getattr(event, "body", None) or "").strip() 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) 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"): 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) outgoing = await self._handle_load_selection(sender, room.room_id, body, load_pending)
await self._send_all(room.room_id, outgoing) await self._send_all(room.room_id, outgoing)
return return
room_meta = await get_room_meta(self.runtime.store, room.room_id)
if room_meta is None: if room_meta is None:
outgoing = await self._bootstrap_unregistered_room(room, sender) outgoing = await self._bootstrap_unregistered_room(room, sender)
if outgoing: if outgoing:
@ -187,9 +190,6 @@ class MatrixBot:
user=sender, user=sender,
) )
return 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) 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) incoming = from_room_event(event, room_id=room.room_id, chat_id=chat_id)
if incoming is None: if incoming is None:

View file

@ -9,7 +9,14 @@ from nio.responses import SyncResponse
from adapter.matrix.bot import MatrixBot, build_runtime, prepare_live_sync from adapter.matrix.bot import MatrixBot, build_runtime, prepare_live_sync
from adapter.matrix.handlers.auth import handle_invite 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 core.protocol import IncomingCallback, IncomingCommand, OutgoingMessage
from sdk.interface import PlatformError from sdk.interface import PlatformError
from sdk.mock import MockPlatformClient 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() 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(): async def test_unregistered_room_bootstraps_space_and_chat_on_first_message():
runtime = build_runtime(platform=MockPlatformClient()) runtime = build_runtime(platform=MockPlatformClient())
await set_user_meta(runtime.store, "@alice:example.org", {"next_chat_index": 1}) await set_user_meta(runtime.store, "@alice:example.org", {"next_chat_index": 1})