Skip to content

Commit

Permalink
Adding new 'destroy' method in chats and messages views in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
F-Jackson committed Jan 1, 2023
1 parent 33efb26 commit 89c8ead
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
15 changes: 10 additions & 5 deletions backend/chat/logic/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 6 additions & 4 deletions backend/chat/logic/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
72 changes: 38 additions & 34 deletions backend/chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -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),
],
Expand All @@ -65,40 +65,42 @@ 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):
@swagger_auto_schema(
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={
Expand All @@ -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={
Expand All @@ -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"),
Expand All @@ -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()

0 comments on commit 89c8ead

Please sign in to comment.