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

@@ -91,6 +91,18 @@ async def get_chat(
return await ChatService(session).get_chat(request)
@chat_router.get(
'/for-client/{client_id}',
operation_id='get_client_chats_list',
response_model=GetChatsListResponse,
)
async def get_client_chats_list(
session: Annotated[AsyncSession, Depends(get_session)],
client_id: int,
):
return await ChatService(session).get_client_chats_list(client_id)
@chat_router.post(
'/create',
operation_id='create_chat',

View File

@@ -56,6 +56,11 @@ class ChatSchema(BaseSchema):
card_id: Optional[int]
tg_group: Optional[TgGroupSchema]
class ChatsListItemSchema(ChatSchema):
name: str
# endregion
# region Requests
@@ -91,6 +96,10 @@ class GetMessagesRequest(BaseSchema):
# region Responses
class GetChatsListResponse(BaseSchema):
chats: list[ChatsListItemSchema]
class SendTextMessageResponse(OkMessageSchema):
pass

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)