from fastapi import APIRouter, Depends, status from app.chat.shemas import ( SMessage, SLastMessages, SAllowedChats, SMessageList, SPinnedChats, SPinnedMessages, SChangeData, SChatId, Responses, ) from app.services.chat_service import ChatService from app.services.message_service import MessageService from app.utils.unit_of_work import UnitOfWork from app.dependencies import get_verificated_user from app.services.auth_service import AuthService from app.users.schemas import SCreateInvitationLink, SUser router = APIRouter(prefix="/chat", tags=["Чат"]) @router.get( "", status_code=status.HTTP_200_OK, response_model=SAllowedChats, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_all_chats(user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): allowed_chats = await ChatService.get_all_chats(uow=uow, user_id=user.id) return allowed_chats @router.post( "/create_chat", status_code=status.HTTP_201_CREATED, response_model=SChatId, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def create_chat( user_to_exclude: int, chat_name: str, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): chat_id = await ChatService.create_chat( uow=uow, user_id=user.id, user_to_exclude_id=user_to_exclude, chat_name=chat_name ) return {"chat_id": chat_id} @router.post( "/change_data", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def change_chat_data( user_data: SChangeData, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=user_data.chat_id, user_id=user.id) await ChatService.change_chat_data(uow=uow, user_id=user.id, user_data=user_data) @router.get( "/get_last_message/{chat_id}", status_code=status.HTTP_200_OK, response_model=SMessageList, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_last_message(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) message = await MessageService.get_some_messages(uow=uow, chat_id=chat_id, message_number_from=0, messages_to_get=1) return message @router.get( "/get_some_messages/{chat_id}", status_code=status.HTTP_200_OK, response_model=SMessageList, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_some_messages( chat_id: int, last_messages: SLastMessages = Depends(), user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) messages = await MessageService.get_some_messages( uow=uow, chat_id=chat_id, message_number_from=last_messages.messages_loaded, messages_to_get=last_messages.messages_to_get, ) return messages @router.get( "/message/{chat_id}", status_code=status.HTTP_200_OK, response_model=SMessage, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_message_by_id( chat_id: int, message_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) message = await MessageService.get_message_by_id(uow=uow, message_id=message_id) return message @router.get( "/create_invitation_link", status_code=status.HTTP_201_CREATED, response_model=SCreateInvitationLink, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def create_invitation_link( chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) invitation_link = ChatService.create_invitation_link(chat_id=chat_id) return {"invitation_link": invitation_link} @router.get( "/invite_to_chat/{invitation_token}", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def invite_to_chat( invitation_token: str, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await ChatService.invite_to_chat(uow=uow, user_id=user.id, invitation_token=invitation_token) @router.delete( "/delete_chat/{chat_id}", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def delete_chat(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): await ChatService.delete_chat(uow=uow, user_id=user.id, chat_id=chat_id) @router.delete( "/delete_user_from_chat/{chat_id}", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def delete_user_from_chat( chat_id: int, user_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork) ): await ChatService.delete_user_from_chat(uow=uow, user_id=user.id, chat_id=chat_id, user_id_for_delete=user_id) @router.post( "/pin_chat", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def pin_chat(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) await ChatService.pin_chat(uow=uow, user_id=user.id, chat_id=chat_id) @router.delete( "/unpin_chat", status_code=status.HTTP_200_OK, response_model=None, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def unpinn_chat(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) await ChatService.unpin_chat(uow=uow, user_id=user.id, chat_id=chat_id) @router.get( "/get_pinned_chats", status_code=status.HTTP_200_OK, response_model=SPinnedChats, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_pinned_chats(user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): pinned_chats = await ChatService.get_pinned_chats(uow=uow, user_id=user.id) return pinned_chats @router.get( "/pinned_messages/{chat_id}", status_code=status.HTTP_200_OK, response_model=SPinnedMessages, responses={ status.HTTP_401_UNAUTHORIZED: { "model": Responses.STokenMissingException, "description": "Токен отсутствует" }, status.HTTP_403_FORBIDDEN: { "model": Responses.SNotAuthenticated, "description": "Not authenticated" }, status.HTTP_404_NOT_FOUND: { "model": Responses.SUserNotFoundException, "description": "Юзер не найден" }, status.HTTP_409_CONFLICT: { "model": Responses.SUserMustConfirmEmailException, "description": "Сначала подтвердите почту" }, status.HTTP_500_INTERNAL_SERVER_ERROR: { "model": Responses.SBlackPhoenixException, "description": "Внутренняя ошибка сервера" }, }, ) async def get_pinned_messages(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)): await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) pinned_messages = await MessageService.get_pinned_messages(uow=uow, chat_id=chat_id) return pinned_messages