Рефакторинг
This commit is contained in:
parent
e424142f19
commit
ac76cf9c9e
6 changed files with 39 additions and 39 deletions
|
@ -114,7 +114,7 @@ async def websocket_endpoint(
|
|||
subprotocol: str = Depends(get_subprotocol_ws),
|
||||
uow=Depends(UnitOfWork),
|
||||
):
|
||||
await AuthService.check_verificated_user_with_exc(uow=uow, user_id=user.id)
|
||||
await AuthService.check_verificated_user(uow=uow, user_id=user.id)
|
||||
await AuthService.validate_user_access_to_chat(uow=uow, user_id=user.id, chat_id=chat_id)
|
||||
await manager.connect(chat_id, websocket, subprotocol)
|
||||
try:
|
||||
|
|
|
@ -1,12 +1,19 @@
|
|||
from app.chat.exceptions import UserDontHavePermissionException, UserCanNotReadThisChatException
|
||||
from app.chat.shemas import SAllowedChats, SChangeData, SPinnedChats
|
||||
from app.chat.shemas import SAllowedChats, SChangeData, SPinnedChats, SChat
|
||||
from app.config import settings
|
||||
from app.services.user_service import UserService
|
||||
from app.unit_of_work import UnitOfWork
|
||||
from app.users.schemas import SInvitationData
|
||||
from app.utils.auth import encode_invitation_token, decode_invitation_token
|
||||
|
||||
|
||||
class ChatService:
|
||||
@staticmethod
|
||||
async def find_chat(uow: UnitOfWork, chat_id: int, user_id: int) -> SChat:
|
||||
async with uow:
|
||||
chat = await uow.chat.find_one(chat_id=chat_id, user_id=user_id)
|
||||
return chat
|
||||
|
||||
@staticmethod
|
||||
async def get_all_chats(uow: UnitOfWork, user_id: int) -> SAllowedChats:
|
||||
async with uow:
|
||||
|
@ -15,8 +22,8 @@ class ChatService:
|
|||
|
||||
@staticmethod
|
||||
async def create_chat(uow: UnitOfWork, user_id: int, user_to_exclude_id: int, chat_name: str) -> int:
|
||||
user_chat_for = await UserService.find_user(uow=uow, id=user_to_exclude_id)
|
||||
async with uow:
|
||||
user_chat_for = await uow.user.find_one(id=user_to_exclude_id)
|
||||
chat_id = await uow.chat.create_chat(
|
||||
user_id=user_to_exclude_id, chat_name=chat_name, created_by=user_id,
|
||||
avatar_image=user_chat_for.avatar_image
|
||||
|
@ -26,10 +33,10 @@ class ChatService:
|
|||
await uow.commit()
|
||||
return chat_id
|
||||
|
||||
@staticmethod
|
||||
async def change_chat_data(uow: UnitOfWork, user_id: int, user_data: SChangeData) -> None:
|
||||
@classmethod
|
||||
async def change_chat_data(cls, uow: UnitOfWork, user_id: int, user_data: SChangeData) -> None:
|
||||
chat = await cls.find_chat(uow=uow, chat_id=user_data.chat_id, user_id=user_id)
|
||||
async with uow:
|
||||
chat = await uow.chat.find_one(chat_id=user_data.chat_id, user_id=user_id)
|
||||
if chat.created_by != user_id:
|
||||
raise UserDontHavePermissionException
|
||||
await uow.chat.change_data(
|
||||
|
@ -43,29 +50,29 @@ class ChatService:
|
|||
invitation_link = settings.INVITATION_LINK_HOST + "/api/chat/invite_to_chat/" + invitation_token
|
||||
return invitation_link
|
||||
|
||||
@staticmethod
|
||||
async def invite_to_chat(uow: UnitOfWork, user_id: int, invitation_token: str) -> None:
|
||||
@classmethod
|
||||
async def invite_to_chat(cls, uow: UnitOfWork, user_id: int, invitation_token: str) -> None:
|
||||
invitation_data = decode_invitation_token(invitation_token)
|
||||
chat = await cls.find_chat(uow=uow, chat_id=invitation_data.chat_id, user_id=user_id)
|
||||
async with uow:
|
||||
chat = await uow.chat.find_one(chat_id=invitation_data.chat_id, user_id=user_id)
|
||||
if user_id == chat.chat_for:
|
||||
raise UserCanNotReadThisChatException
|
||||
await uow.chat.add_user_to_chat(chat_id=invitation_data.chat_id, user_id=user_id)
|
||||
await uow.commit()
|
||||
|
||||
@staticmethod
|
||||
async def delete_chat(uow: UnitOfWork, user_id: int, chat_id: int) -> None:
|
||||
@classmethod
|
||||
async def delete_chat(cls, uow: UnitOfWork, user_id: int, chat_id: int) -> None:
|
||||
chat = await cls.find_chat(uow=uow, chat_id=chat_id, user_id=user_id)
|
||||
async with uow:
|
||||
chat = await uow.chat.find_one(chat_id=chat_id, user_id=user_id)
|
||||
if not user_id == chat.created_by:
|
||||
raise UserDontHavePermissionException
|
||||
await uow.chat.delete_chat(chat_id)
|
||||
await uow.commit()
|
||||
|
||||
@staticmethod
|
||||
async def delete_user_from_chat(uow: UnitOfWork, user_id: int, chat_id: int, user_id_for_delete: int) -> None:
|
||||
@classmethod
|
||||
async def delete_user_from_chat(cls, uow: UnitOfWork, user_id: int, chat_id: int, user_id_for_delete: int) -> None:
|
||||
chat = await cls.find_chat(uow=uow, chat_id=chat_id, user_id=user_id)
|
||||
async with uow:
|
||||
chat = await uow.chat.find_one(chat_id=chat_id, user_id=user_id)
|
||||
if not user_id == chat.created_by:
|
||||
raise UserDontHavePermissionException
|
||||
await uow.chat.delete_user_from_chat(chat_id=chat_id, user_id=user_id_for_delete)
|
||||
|
|
|
@ -4,7 +4,7 @@ from app.users.schemas import SUser
|
|||
|
||||
class UserService:
|
||||
@staticmethod
|
||||
async def find_one_or_none(uow: UnitOfWork, user_id: int) -> SUser:
|
||||
async def find_user(uow: UnitOfWork, **find_by) -> SUser:
|
||||
async with uow:
|
||||
user = await uow.user.find_one(id=user_id)
|
||||
user = await uow.user.find_one(**find_by)
|
||||
return user
|
||||
|
|
|
@ -37,7 +37,7 @@ async def get_current_user(token: str = Depends(get_token), uow=Depends(UnitOfWo
|
|||
if not user_id:
|
||||
raise UserNotFoundException
|
||||
|
||||
user = await UserService.find_one_or_none(uow=uow, user_id=int(user_id))
|
||||
user = await UserService.find_user(uow=uow, id=int(user_id))
|
||||
|
||||
return user
|
||||
|
||||
|
@ -66,7 +66,7 @@ async def get_current_user_ws(token: str = Depends(get_token_ws), uow=Depends(Un
|
|||
if not user_id:
|
||||
raise UserNotFoundException
|
||||
|
||||
user = await UserService.find_one_or_none(uow=uow, user_id=int(user_id))
|
||||
user = await UserService.find_user(uow=uow, id=int(user_id))
|
||||
|
||||
return user
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ from fastapi import APIRouter, Depends, status
|
|||
|
||||
from app.config import settings
|
||||
from app.exceptions import UserNotFoundException, PasswordAlreadyInUseException
|
||||
from app.services.user_service import UserService
|
||||
from app.users.exceptions import (
|
||||
UserAlreadyExistsException,
|
||||
PasswordsMismatchException,
|
||||
|
@ -61,12 +62,11 @@ async def get_all_users(filter_by=Depends(SGetUsersFilter), uow=Depends(UnitOfWo
|
|||
response_model=None,
|
||||
)
|
||||
async def check_existing_user(user_filter: SUserFilter, uow=Depends(UnitOfWork)):
|
||||
async with uow:
|
||||
try:
|
||||
await uow.user.find_one(**user_filter.model_dump(exclude_none=True))
|
||||
raise UserAlreadyExistsException
|
||||
except UserNotFoundException:
|
||||
pass
|
||||
try:
|
||||
await UserService.find_user(uow=uow, **user_filter.model_dump(exclude_none=True))
|
||||
raise UserAlreadyExistsException
|
||||
except UserNotFoundException:
|
||||
pass
|
||||
|
||||
|
||||
@router.post(
|
||||
|
|
|
@ -5,10 +5,9 @@ from jose import jwt
|
|||
from passlib.context import CryptContext
|
||||
|
||||
from app.config import settings
|
||||
from app.exceptions import (
|
||||
UserNotFoundException,
|
||||
UserMustConfirmEmailException,
|
||||
)
|
||||
from app.exceptions import UserNotFoundException, UserMustConfirmEmailException
|
||||
from app.services.chat_service import ChatService
|
||||
from app.services.user_service import UserService
|
||||
from app.users.exceptions import IncorrectAuthDataException
|
||||
from app.chat.exceptions import UserDontHavePermissionException, ChatNotFoundException
|
||||
from app.unit_of_work import UnitOfWork
|
||||
|
@ -70,22 +69,16 @@ class AuthService:
|
|||
except UserNotFoundException:
|
||||
raise IncorrectAuthDataException
|
||||
|
||||
@staticmethod
|
||||
async def check_verificated_user(uow: UnitOfWork, user_id: int) -> bool:
|
||||
async with uow:
|
||||
user = await uow.user.find_one(id=user_id)
|
||||
return user.role >= settings.VERIFICATED_USER
|
||||
|
||||
@classmethod
|
||||
async def check_verificated_user_with_exc(cls, uow: UnitOfWork, user_id: int) -> None:
|
||||
if not await cls.check_verificated_user(uow=uow, user_id=user_id):
|
||||
async def check_verificated_user(cls, uow: UnitOfWork, user_id: int) -> None:
|
||||
user = await UserService.find_user(uow=uow, id=user_id)
|
||||
if user.role < settings.VERIFICATED_USER:
|
||||
raise UserMustConfirmEmailException
|
||||
|
||||
@classmethod
|
||||
async def validate_user_access_to_chat(cls, uow: UnitOfWork, user_id: int, chat_id: int) -> None:
|
||||
try:
|
||||
async with uow:
|
||||
await uow.chat.find_one(chat_id=chat_id, user_id=user_id)
|
||||
await ChatService.find_chat(uow=uow, chat_id=chat_id, user_id=user_id)
|
||||
except ChatNotFoundException:
|
||||
raise UserDontHavePermissionException
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue