chat_back/app/chat/router.py
2025-03-16 12:04:09 +03:00

513 lines
16 KiB
Python

from uuid import UUID
from fastapi import APIRouter, Depends, status
from app.chat.shemas import (
Responses,
SAllowedChats,
SChangeData,
SChatId,
SLastMessages,
SMessage,
SMessageList,
SPinnedChats,
SPinnedMessages,
)
from app.dependencies import get_verificated_user
from app.services import auth_service, chat_service, message_service
from app.users.schemas import SCreateInvitationLink, SUser
from app.utils.unit_of_work import UnitOfWork
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 chat_service.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 chat_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=user_data.chat_id, user_id=user.id)
await chat_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
message = await message_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
messages = await message_service.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: UUID,
user: SUser = Depends(get_verificated_user),
uow=Depends(UnitOfWork),
):
await auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
message = await message_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
invitation_link = chat_service.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 chat_service.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 chat_service.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 chat_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
await chat_service.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 unpin_chat(chat_id: int, user: SUser = Depends(get_verificated_user), uow=Depends(UnitOfWork)):
await auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
await chat_service.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 chat_service.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 auth_service.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id)
pinned_messages = await message_service.get_pinned_messages(uow=uow, chat_id=chat_id)
return pinned_messages