From c865196f29cd738d96cd77fc360f79be93587c3a Mon Sep 17 00:00:00 2001 From: urec56 Date: Sat, 8 Jun 2024 17:50:47 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chat/router.py | 23 ++++++++++--------- app/chat/shemas.py | 8 ------- app/chat/websocket.py | 9 ++++---- app/dao/chat.py | 40 ++++++++++++--------------------- app/models/pinned_chat.py | 7 +++--- app/services/message_service.py | 8 +++---- 6 files changed, 37 insertions(+), 58 deletions(-) diff --git a/app/chat/router.py b/app/chat/router.py index 76fc7bc..fada1d3 100644 --- a/app/chat/router.py +++ b/app/chat/router.py @@ -3,15 +3,12 @@ from fastapi import APIRouter, Depends, status from app.config import settings from app.chat.exceptions import ( UserDontHavePermissionException, - MessageNotFoundException, UserCanNotReadThisChatException, ) from app.chat.shemas import ( SMessage, SLastMessages, SPinnedChat, - SDeletedUser, - SDeletedChat, SAllowedChats, SMessageList, SPinnedChats, @@ -146,20 +143,21 @@ async def invite_to_chat( @router.delete( "/delete_chat/{chat_id}", status_code=status.HTTP_200_OK, - response_model=SDeletedChat, + response_model=None, ) async def delete_chat(chat_id: int, user: SUser = Depends(check_verificated_user_with_exc), uow=Depends(UnitOfWork)): async with uow: chat = await uow.chat.find_one_or_none(id=chat_id) - if user.id == chat.created_by: - return {"deleted_chat": await uow.chat.delete_chat(chat_id)} - raise UserDontHavePermissionException + if not user.id == chat.created_by: + raise UserDontHavePermissionException + await uow.chat.delete_chat(chat_id) + await uow.commit() @router.delete( "/delete_user_from_chat/{chat_id}", status_code=status.HTTP_200_OK, - response_model=SDeletedUser, + response_model=None, ) async def delete_user_from_chat( chat_id: int, @@ -169,9 +167,10 @@ async def delete_user_from_chat( ): async with uow: chat = await uow.chat.find_one_or_none(id=chat_id) - if user.id == chat.created_by: - return {"deleted_user": await uow.chat.delete_user(chat_id=chat_id, user_id=user_id)} - raise UserDontHavePermissionException + if not user.id == chat.created_by: + raise UserDontHavePermissionException + await uow.chat.delete_user_from_chat(chat_id=chat_id, user_id=user_id) + await uow.commit() @router.post( @@ -183,6 +182,7 @@ async def pinn_chat(chat_id: int, user: SUser = Depends(check_verificated_user_w await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) async with uow: await uow.chat.pin_chat(chat_id=chat_id, user_id=user.id) + await uow.commit() return {"chat_id": chat_id, "user_id": user.id} @@ -195,6 +195,7 @@ async def unpinn_chat(chat_id: int, user: SUser = Depends(check_verificated_user await AuthService.validate_user_access_to_chat(uow=uow, chat_id=chat_id, user_id=user.id) async with uow: await uow.chat.unpin_chat(chat_id=chat_id, user_id=user.id) + await uow.commit() return {"chat_id": chat_id, "user_id": user.id} diff --git a/app/chat/shemas.py b/app/chat/shemas.py index 8f914d8..82d65f5 100644 --- a/app/chat/shemas.py +++ b/app/chat/shemas.py @@ -30,14 +30,6 @@ class SPinnedChat(BaseModel): chat_id: int -class SDeletedUser(BaseModel): - deleted_user: bool - - -class SDeletedChat(BaseModel): - deleted_user: bool - - class SChat(BaseModel): chat_id: int chat_for: int diff --git a/app/chat/websocket.py b/app/chat/websocket.py index 822de5b..b576b17 100644 --- a/app/chat/websocket.py +++ b/app/chat/websocket.py @@ -89,8 +89,8 @@ class ConnectionManager: async def unpin(self, uow: UnitOfWork, _: int, chat_id: int, message: dict) -> dict: message = SUnpinMessage.model_validate(message) - unpinned_message = await self.unpin_message(uow=uow, chat_id=chat_id, message_id=message.id) - new_message = {"flag": "unpin", "id": unpinned_message} + await self.unpin_message(uow=uow, chat_id=chat_id, message_id=message.id) + new_message = {"flag": "unpin", "id": message.id} return new_message @staticmethod @@ -116,9 +116,8 @@ class ConnectionManager: return pinned_message @staticmethod - async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> int: - unpinned_message_id = await MessageService.unpin_message(uow=uow, chat_id=chat_id, message_id=message_id) - return unpinned_message_id + async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> None: + await MessageService.unpin_message(uow=uow, chat_id=chat_id, message_id=message_id) manager = ConnectionManager() diff --git a/app/dao/chat.py b/app/dao/chat.py index 9db4b68..ce02365 100644 --- a/app/dao/chat.py +++ b/app/dao/chat.py @@ -163,34 +163,24 @@ class ChatDAO(BaseDAO): ) await self.session.execute(stmt) - async def delete_chat(self, chat_id: int) -> bool: + async def delete_chat(self, chat_id: int) -> None: query = update(Chats).where(Chats.id == chat_id).values(visibility=False) await self.session.execute(query) - await self.session.commit() - return True - async def delete_user(self, chat_id: int, user_id: int) -> bool: + async def delete_user_from_chat(self, chat_id: int, user_id: int) -> None: query = delete(UserChat).where(UserChat.chat_id == chat_id, UserChat.user_id == user_id) await self.session.execute(query) - await self.session.commit() - return True - async def pin_chat(self, chat_id: int, user_id: int) -> bool: - query = select(PinnedChat.chat_id).where(PinnedChat.user_id == user_id) - result = await self.session.execute(query) - result = result.scalars().all() - if chat_id in result: + async def pin_chat(self, chat_id: int, user_id: int) -> None: + try: + stmt = insert(PinnedChat).values(chat_id=chat_id, user_id=user_id) + await self.session.execute(stmt) + except IntegrityError: raise UserAlreadyPinnedChatException - stmt = insert(PinnedChat).values(chat_id=chat_id, user_id=user_id) - await self.session.execute(stmt) - await self.session.commit() - return True - async def unpin_chat(self, chat_id: int, user_id: int) -> bool: + async def unpin_chat(self, chat_id: int, user_id: int) -> None: query = delete(PinnedChat).where(PinnedChat.chat_id == chat_id, PinnedChat.user_id == user_id) await self.session.execute(query) - await self.session.commit() - return True async def get_pinned_chats(self, user_id: int): chats_with_descriptions = ( @@ -231,15 +221,13 @@ class ChatDAO(BaseDAO): result = result.mappings().all() return result - async def pin_message(self, chat_id: int, message_id: int, user_id: int) -> bool: - query = insert(PinnedMessage).values(chat_id=chat_id, message_id=message_id, user_id=user_id) - await self.session.execute(query) - return True + async def pin_message(self, chat_id: int, message_id: int, user_id: int) -> None: + stmt = insert(PinnedMessage).values(chat_id=chat_id, message_id=message_id, user_id=user_id) + await self.session.execute(stmt) - async def unpin_message(self, chat_id: int, message_id: int) -> bool: - query = delete(PinnedMessage).where(PinnedMessage.chat_id == chat_id, PinnedMessage.message_id == message_id) - await self.session.execute(query) - return True + async def unpin_message(self, chat_id: int, message_id: int) -> None: + stmt = delete(PinnedMessage).where(PinnedMessage.chat_id == chat_id, PinnedMessage.message_id == message_id) + await self.session.execute(stmt) async def get_pinned_messages(self, chat_id: int) -> list[dict]: query = ( diff --git a/app/models/pinned_chat.py b/app/models/pinned_chat.py index f5bf8b2..dbc2062 100644 --- a/app/models/pinned_chat.py +++ b/app/models/pinned_chat.py @@ -1,5 +1,5 @@ from sqlalchemy import ForeignKey -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import mapped_column from app.database import Base @@ -7,6 +7,5 @@ from app.database import Base class PinnedChat(Base): __tablename__ = "pinned_chat" - id: Mapped[int] = mapped_column(primary_key=True) - user_id = mapped_column(ForeignKey("users.id")) - chat_id = mapped_column(ForeignKey("chat.id")) + user_id = mapped_column(ForeignKey("users.id"), primary_key=True) + chat_id = mapped_column(ForeignKey("chat.id"), primary_key=True) diff --git a/app/services/message_service.py b/app/services/message_service.py index fd6df4f..1b29b73 100644 --- a/app/services/message_service.py +++ b/app/services/message_service.py @@ -35,13 +35,13 @@ class MessageService: @staticmethod async def pin_message(uow: UnitOfWork, chat_id: int, user_id: int, message_id: int) -> SMessage: async with uow: - pinned_message = await uow.chat.pin_message(chat_id=chat_id, message_id=message_id, user_id=user_id) + await uow.chat.pin_message(chat_id=chat_id, message_id=message_id, user_id=user_id) + pinned_message = await uow.chat.get_message_by_id(message_id) await uow.commit() return pinned_message @staticmethod - async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> int: + async def unpin_message(uow: UnitOfWork, chat_id: int, message_id: int) -> None: async with uow: - unpinned_message = await uow.chat.unpin_message(chat_id=chat_id, message_id=message_id) + await uow.chat.unpin_message(chat_id=chat_id, message_id=message_id) await uow.commit() - return unpinned_message