Spaces:
Sleeping
Sleeping
| from typing import Optional | |
| from fastapi import Query, Depends | |
| from trauma.api.account.dto import AccountType | |
| from trauma.api.account.model import AccountModel | |
| from trauma.api.chat import chat_router | |
| from trauma.api.chat.db_requests import (get_chat_obj, | |
| create_chat_obj, | |
| delete_chat_obj, | |
| update_chat_obj_title, | |
| get_all_chats_obj, save_intro_message) | |
| from trauma.api.chat.schemas import ChatWrapper, AllChatWrapper, CreateChatRequest, AllChatResponse, ChatTitleRequest | |
| from trauma.api.common.dto import Paging | |
| from trauma.core.security import PermissionDependency | |
| from trauma.core.wrappers import TraumaResponseWrapper | |
| async def get_all_chats( | |
| pageSize: Optional[int] = Query(10, description="Number of objects to return per page"), | |
| pageIndex: Optional[int] = Query(0, description="Page index to retrieve"), | |
| _: AccountModel = Depends(PermissionDependency([AccountType.Admin])) | |
| ) -> AllChatWrapper: | |
| chats, total_count = await get_all_chats_obj(pageSize, pageIndex) | |
| response = AllChatResponse( | |
| paging=Paging(pageSize=pageSize, pageIndex=pageIndex, totalCount=total_count), | |
| data=chats | |
| ) | |
| return AllChatWrapper(data=response) | |
| async def get_chat( | |
| chatId: str, | |
| account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User])) | |
| ) -> ChatWrapper: | |
| chat = await get_chat_obj(chatId, account) | |
| return ChatWrapper(data=chat) | |
| async def delete_chat( | |
| chatId: str, | |
| account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User])) | |
| ) -> TraumaResponseWrapper: | |
| await delete_chat_obj(chatId, account) | |
| return TraumaResponseWrapper() | |
| async def update_chat_title( | |
| chatId: str, chat: ChatTitleRequest, | |
| account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User])) | |
| ) -> ChatWrapper: | |
| chat = await update_chat_obj_title(chatId, chat, account) | |
| return ChatWrapper(data=chat) | |
| async def create_chat( | |
| chat_data: CreateChatRequest, | |
| account: AccountModel = Depends(PermissionDependency([AccountType.Admin, AccountType.User])) | |
| ) -> ChatWrapper: | |
| chat = await create_chat_obj(chat_data, account) | |
| await save_intro_message(chat.id) | |
| return ChatWrapper(data=chat) |