diff --git a/backend/chat/logic/chat.py b/backend/chat/logic/chat.py index 40648ed..c4d1078 100644 --- a/backend/chat/logic/chat.py +++ b/backend/chat/logic/chat.py @@ -81,11 +81,16 @@ def _create_chat(encrypt_keys: dict, user: User, user_2: User) -> Response: return Response(status=status.HTTP_201_CREATED) -def destroy_chat(data: dict, pk: int) -> Response: +def destroy_chat(data: dict, request_data: dict, user: User) -> Response: try: - chat = ChatModel.objects.get(pk=pk) - except ChatModel.DoesNotExist: - return send_error(data, 'Cant find chat id', status.HTTP_404_NOT_FOUND) + request_data_field(request_data, 'chats_id_to_delete', list) + + chats = ChatModel.objects.filter(pk__in=request_data['chats_id_to_delete']) + except ValueError as e: + return send_error(data, 'Request needs a "chats_id_to_delete" list', status.HTTP_400_BAD_REQUEST) else: - chat.delete() + for chat in chats: + if chat.user_1_id == user or chat.user_2_id == user: + chat.delete() + return Response(data, status=status.HTTP_200_OK) diff --git a/backend/chat/logic/message.py b/backend/chat/logic/message.py index 6380b99..55272d9 100644 --- a/backend/chat/logic/message.py +++ b/backend/chat/logic/message.py @@ -115,11 +115,13 @@ def _create_message_in_db(data: dict, chat: ChatModel, message: bytes, user: Use return Response(data, status=status.HTTP_201_CREATED) -def detroy_message(data: dict, user: User, pk: int): +def detroy_message(data: dict, user: User, request_data: dict): try: - message = MessagesModel.objects.get(pk=pk, user=user) - except MessagesModel.DoesNotExist: - return send_error(data, 'Message does not exists', status.HTTP_404_NOT_FOUND) + request_data_field(request_data, 'messages_id_to_delete', list) + + message = MessagesModel.objects.filter(pk__in=request_data['messages_id_to_delete']) + except ValueError as e: + return send_error(data, 'Request needs a "messages_id_to_delete" list', status.HTTP_400_BAD_REQUEST) else: message.delete() return Response(data, status=status.HTTP_200_OK) diff --git a/backend/chat/views.py b/backend/chat/views.py index c8a429b..49a10ef 100644 --- a/backend/chat/views.py +++ b/backend/chat/views.py @@ -16,7 +16,7 @@ class ChatsViewset(viewsets.ViewSet): operation_summary="Get Chat info", operation_description="Get Chat info if authenticated", manual_parameters=[ - openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", type=openapi.TYPE_STRING) ], responses={ @@ -40,9 +40,9 @@ def list(self, request) -> Response: operation_summary="Create new chat", operation_description="Create new chat between the authenticated User and the given User", manual_parameters=[ - openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", type=openapi.TYPE_STRING), - openapi.Parameter("send_to", openapi.IN_QUERY, description="User id that you want to chat with", + openapi.Parameter("talk_to", openapi.IN_BODY, description="User username that you want to chat with", type=openapi.TYPE_NUMBER), ], @@ -65,30 +65,32 @@ def create(self, request) -> Response: return create_new_chat(data, request.data, user) return invalid_token() - # @swagger_auto_schema( - # operation_summary="Destroy chat", - # operation_description="Destroy given chat if User is authenticated and is the owner of the chat", - # manual_parameters=[ - # openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", - # type=openapi.TYPE_STRING), - # ], - # responses={ - # 200: openapi.Response("Retrives new client token"), - # 404: openapi.Response("Retrives new client token") - # } - # ) - # def destroy(self, request, pk=None) -> Response: - # jwt_is_valid = verify_user_auth(request, True) - # - # if jwt_is_valid: - # token, user = jwt_is_valid - # - # data = { - # 'token': token - # } - # - # return destroy_chat(data, pk) - # return invalid_token() + @swagger_auto_schema( + operation_summary="Destroy chat", + operation_description="Destroy given chat if User is authenticated and is the owner of the chat", + manual_parameters=[ + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", + type=openapi.TYPE_STRING), + openapi.Parameter("chats_id_to_delete", openapi.IN_BODY, description="List of chats ids to delete", + type=openapi.TYPE_ARRAY) + ], + responses={ + 200: openapi.Response("Retrives new client token"), + 404: openapi.Response("Retrives new client token") + } + ) + def destroy(self, request, pk=None) -> Response: + jwt_is_valid = verify_user_auth(request, True) + + if jwt_is_valid: + token, user = jwt_is_valid + + data = { + 'token': token + } + + return destroy_chat(data, request.data, user) + return invalid_token() class MessagesViewset(viewsets.ViewSet): @@ -96,9 +98,9 @@ class MessagesViewset(viewsets.ViewSet): operation_summary="Get Messages List", operation_description="Get Messages from the given chat if User is authenticated and is the owner of the chat", manual_parameters=[ - openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", type=openapi.TYPE_STRING), - openapi.Parameter("chat", openapi.IN_QUERY, description="Chat id that you to see the messages", + openapi.Parameter("chat", openapi.IN_BODY, description="Chat id that you to see the messages", type=openapi.TYPE_NUMBER), ], responses={ @@ -125,11 +127,11 @@ def list(self, request) -> Response: operation_summary="Create new message", operation_description="Create new message inside the given chat", manual_parameters=[ - openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", type=openapi.TYPE_STRING), - openapi.Parameter("chat", openapi.IN_QUERY, description="Chat id that you to the message inside", + openapi.Parameter("chat", openapi.IN_BODY, description="Chat id that you to the message inside", type=openapi.TYPE_NUMBER), - openapi.Parameter("message", openapi.IN_QUERY, description="Message that you want to send", + openapi.Parameter("message", openapi.IN_BODY, description="Message that you want to send", type=openapi.TYPE_STRING), ], responses={ @@ -156,8 +158,10 @@ def create(self, request): operation_summary="Destroy message", operation_description="Destroy given message if User is authenticated and is the owner of the message", manual_parameters=[ - openapi.Parameter("token", openapi.IN_QUERY, description="Client Jwt Token must be given in request Header", + openapi.Parameter("token", openapi.IN_HEADER, description="Client Jwt Token", type=openapi.TYPE_STRING), + openapi.Parameter("messages_id_to_delete", openapi.IN_BODY, description="List of messages ids to delete", + type=openapi.TYPE_ARRAY) ], responses={ 200: openapi.Response("Retrives new client token"), @@ -174,5 +178,5 @@ def destroy(self, request, pk=None) -> Response: 'token': token } - return detroy_message(data, user, pk) + return detroy_message(data, user, request.data) return invalid_token()