feat: chats list on the left of the client chat

This commit is contained in:
2025-04-16 13:57:29 +04:00
parent 68f94cc9a4
commit 4303f679b5
3 changed files with 59 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
import requests
from aiohttp import ClientConnectorError
from fastapi import HTTPException, UploadFile
from sqlalchemy import select
from sqlalchemy import select, or_
from sqlalchemy.orm import joinedload
from starlette.responses import StreamingResponse
@@ -30,6 +30,40 @@ class ChatService(BaseService):
chat = await self._get_chat(request.client_id, request.card_id)
return GetChatResponse(chat=chat)
async def get_client_chats_list(self, client_id: int) -> GetChatsListResponse:
stmt = (
select(Chat)
.join(TgGroup)
.where(
or_(
TgGroup.client_id == client_id,
Chat.client_id == client_id,
)
)
)
chats = (await self.session.scalars(stmt)).all()
chats_list_items = []
for chat in chats:
if chat.client_id:
name = f'[{chat.client_id}] {chat.client.name}'
else:
name = f'[{chat.card_id}] {chat.card.name}'
item = ChatsListItemSchema(
id=chat.id,
name=name,
tg_group=TgGroupSchema(
tg_group_id=chat.tg_group.tg_group_id,
tg_invite_link=chat.tg_group.tg_invite_link,
),
client_id=chat.client_id,
card_id=chat.card_id,
)
chats_list_items.append(item)
return GetChatsListResponse(chats=chats_list_items)
async def _get_group(self, client_id: int) -> Optional[TgGroup]:
stmt = (
select(TgGroup)
@@ -193,7 +227,8 @@ class ChatService(BaseService):
) -> RepeatSendingTextMessageResponse:
message: Optional[Message] = await self._get_message_by_id(request.message.id)
if not message:
return RepeatSendingTextMessageResponse(ok=False, message=f'Сообщение с ID: {request.message.id} не найдено')
return RepeatSendingTextMessageResponse(ok=False,
message=f'Сообщение с ID: {request.message.id} не найдено')
ok, msg = await ProducerService.send_message_to_connector(
request.message.text,
@@ -217,7 +252,7 @@ class ChatService(BaseService):
) -> LoadMessagesResponse:
chat: Optional[Chat] = await self.session.get(Chat, chat_id)
if not chat:
return SendTextMessageResponse(ok=False, message=f'Чат с ID: {chat_id} не найден')
return LoadMessagesResponse(ok=False, message=f'Чат с ID: {chat_id} не найден')
try:
chat_client = ChatClient(api_key=CHAT_CONNECTOR_API_KEY)