feat(matrix): create real rooms for new chats
This commit is contained in:
parent
82eb711844
commit
14c091b5f5
4 changed files with 89 additions and 8 deletions
|
|
@ -1,9 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Awaitable, Callable
|
||||
|
||||
from adapter.matrix.store import set_room_meta
|
||||
from core.protocol import IncomingCommand, OutgoingMessage
|
||||
|
||||
|
||||
async def handle_new_chat(
|
||||
async def _fallback_new_chat(
|
||||
event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr
|
||||
) -> list:
|
||||
if not await auth_mgr.is_authenticated(event.user_id):
|
||||
|
|
@ -26,6 +29,60 @@ async def handle_new_chat(
|
|||
]
|
||||
|
||||
|
||||
def make_handle_new_chat(
|
||||
client: Any | None,
|
||||
store: Any | None,
|
||||
) -> Callable[..., Awaitable[list]]:
|
||||
async def handle_new_chat(
|
||||
event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr
|
||||
) -> list:
|
||||
if client is None or store is None:
|
||||
return await _fallback_new_chat(event, auth_mgr, platform, chat_mgr, settings_mgr)
|
||||
|
||||
if not await auth_mgr.is_authenticated(event.user_id):
|
||||
return [OutgoingMessage(chat_id=event.chat_id, text="Введите !start чтобы начать.")]
|
||||
|
||||
name = " ".join(event.args).strip() if event.args else ""
|
||||
chats = await chat_mgr.list_active(event.user_id)
|
||||
chat_id = f"C{len(chats) + 1}"
|
||||
room_name = name or f"Чат {chat_id}"
|
||||
|
||||
response = await client.room_create(
|
||||
name=room_name,
|
||||
invite=[event.user_id],
|
||||
is_direct=False,
|
||||
)
|
||||
room_id = getattr(response, "room_id", None)
|
||||
if not room_id:
|
||||
return [OutgoingMessage(chat_id=event.chat_id, text="Не удалось создать комнату.")]
|
||||
|
||||
await set_room_meta(
|
||||
store,
|
||||
room_id,
|
||||
{
|
||||
"room_type": "chat",
|
||||
"chat_id": chat_id,
|
||||
"display_name": room_name,
|
||||
"matrix_user_id": event.user_id,
|
||||
},
|
||||
)
|
||||
ctx = await chat_mgr.get_or_create(
|
||||
user_id=event.user_id,
|
||||
chat_id=chat_id,
|
||||
platform=event.platform,
|
||||
surface_ref=room_id,
|
||||
name=room_name,
|
||||
)
|
||||
return [
|
||||
OutgoingMessage(
|
||||
chat_id=event.chat_id,
|
||||
text=f"Создан чат: {ctx.display_name} ({ctx.chat_id})\nКомната: {room_id}",
|
||||
)
|
||||
]
|
||||
|
||||
return handle_new_chat
|
||||
|
||||
|
||||
async def handle_list_chats(
|
||||
event: IncomingCommand, auth_mgr, platform, chat_mgr, settings_mgr
|
||||
) -> list:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue