From 00a2044df1568bde6c10394c7d87ac65238da3ef Mon Sep 17 00:00:00 2001 From: blacdev Date: Fri, 29 Oct 2021 21:21:03 +0100 Subject: [PATCH 1/4] Refactored edit message endpoint and also single message endpoint --- backend/messaging.py | 166 +++++++++++++++++++++++++++++-------------- backend/urls.py | 10 +-- 2 files changed, 119 insertions(+), 57 deletions(-) diff --git a/backend/messaging.py b/backend/messaging.py index ee2590df0..7b47d6b45 100644 --- a/backend/messaging.py +++ b/backend/messaging.py @@ -55,11 +55,12 @@ def message_create_get(request, room_id): if room: messages = get_room_messages(room_id, DB.organization_id) if date != None: - messages_by_date = get_messages(room_id,DB.organization_id, date) - + messages_by_date = get_messages(room_id, DB.organization_id, date) + messages_page = paginator.paginate_queryset( - messages_by_date, request) - + messages_by_date, request + ) + return paginator.get_paginated_response(messages_page) else: if messages == None or "message" in messages: @@ -136,49 +137,59 @@ def message_create_get(request, room_id): return Response(status=status.HTTP_400_BAD_REQUEST) -@api_view(["GET", "PUT"]) +@api_view(["PUT"]) @db_init_with_credentials def edit_message(request, message_id, room_id): """ This is used to update message context using message id as identifier, - first --> we check if this message exist, if it does not exist we raise message doesnot exist, - if above message exists: - pass GET request to view the message one whats to edit. - or pass POST with data to update - + Updates a message from a room. + It access room with the 'room_id' and the message in the room with 'message_id' and then the new message. + The id of the organization (org_id) where the room is located is also needed. + Parameters: + org_id (str) : This is the id of the organization th user belongs to. + message (str) : This is the unique id of the message to be sent to a given room. + room_id(str) : This is the unique id of the room in the message + + Returns: + A dict object indicating the the message has been updated. Example: + { + "status" : "success", + "room_id" : "6169dbcef5998a09e3bbbcd3", + "message_id" : "616ad4f989454c2006018af2" + "message" : "The message" + } + Raises: + Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. + IOError: An error occurred while deleteing the message. """ - if request.method == "GET": - try: - message = DB.read("dm_messages", {"id": message_id}) - print(message) - return Response(message) - except: - return JsonResponse( - {"message": "The room does not exist"}, status=status.HTTP_404_NOT_FOUND - ) - - else: - message = DB.read("dm_messages", {"id": message_id}) - room_serializer = MessageSerializer(message, data=request.data, partial=True) - if room_serializer.is_valid(): - data = room_serializer.data - data = {"message": request.data["message"]} - # print(data) - response = DB.update("dm_messages", message_id, data) - if response.get("status") == 200: - data = { - "sender_id": request.data["sender_id"], - "message_id": message_id, - "room_id": room_id, - "message": request.data["message"], - "event": "edited_message", - } - centrifugo_data = send_centrifugo_data(room=room_id, data=data) - if centrifugo_data.get("error", None) == None: - return Response(data=data, status=status.HTTP_201_CREATED) - return Response(data) - return Response(room_serializer.errors, status=status.HTTP_400_BAD_REQUEST) + message = DB.read( + "dm_messages", {"id": message_id} + ) # Checks DB for message using the message_id + room_serializer = MessageSerializer(message, data=request.data, partial=True) + + # validates room_serializer with MessageSerializer. + if room_serializer.is_valid(): + data = room_serializer.data + data = {"message": request.data["message"]} + # print(data) + response = DB.update( + "dm_messages", message_id, data + ) # moves on to update the mesage + + if response.get("status") == 200: + data = { + "sender_id": request.data["sender_id"], + "message_id": message_id, + "room_id": room_id, + "message": request.data["message"], + "event": "edited_message", + } + centrifugo_data = send_centrifugo_data(room=room_id, data=data) + if centrifugo_data.get("error", None) == None: + return Response(data=data, status=status.HTTP_201_CREATED) + return Response(data) + return Response(room_serializer.errors, status=status.HTTP_400_BAD_REQUEST) @swagger_auto_schema( @@ -364,32 +375,81 @@ def pinned_message(request, message_id): @swagger_auto_schema( methods=["get"], operation_summary="Returns all messages in the dm collection", - responses={ - 200: "success", - 424: "Failed Dependency" - }, + responses={200: "success", 424: "Failed Dependency"}, ) @api_view(["GET"]) @db_init_with_credentials def all_messages(request): - """This endpoint is used to get all the messages in the dm_messages collection. + """This endpoint is used to get all the messages in the dm_messages collection. Also returns a messages with the read and unread status""" - + res = DB.read("dm_messages") + print(res) if res and "status_code" not in res: all_messages = res - read_messages = [message for message in all_messages if message["read"] == "true"] + read_messages = [ + message for message in all_messages if message["read"] == "true" + ] print(read_messages) - unread_messages = [message for message in all_messages if message["read"] == "false"] + unread_messages = [ + message for message in all_messages if message["read"] == "false" + ] message_data = { "all_messages": all_messages, "read_messages": read_messages, - "unread_messages":unread_messages + "unread_messages": unread_messages, } return Response(message_data, status=status.HTTP_200_OK) else: - return Response(f"something went wrong. message collection returned{res}", - status=status.HTTP_424_FAILED_DEPENDENCY) + return Response( + f"something went wrong. message collection returned{res}", + status=status.HTTP_424_FAILED_DEPENDENCY, + ) - \ No newline at end of file + +@swagger_auto_schema( + methods=["get"], + operation_summary="Returns a single message in the dm collection", + responses={200: "success", 424: "Failed Dependency"}, +) +@api_view(["GET"]) +@db_init_with_credentials +def single_message(request, message_id): + """ + Gets a single message from a room. + It access room with the 'message_id' and then displays the message if it exists. + The id of the organization (org_id) where the room is located is also needed. + Parameters: + org_id (str) : This is the id of the organization th user belongs to. + message_id (str) : This is the unique id of the message to be deleted from a given room. + + Returns: + A dict object indicating the the message has been deleted. Example: + { + "status" : "success", + "room_id" : "6169dbcef5998a09e3bbbcd3", + "message_id" : "616ad4f989454c2006018af2" + "message" : "The message" + } + Raises: + Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. + IOError: An error occurred while deleteing the message. + """ + data = request.data + request.data["message_id"] = message_id + try: + message = DB.read("dm_messages", {"_id": message_id}) + print(message) + room_id = message["room_id"] + data = { + "status": "success", + "room_id": room_id, + "message_id": message_id, + "message": message, + } + return Response(data, status=status.HTTP_200_OK) + except: + return JsonResponse( + {"message": "The room does not exist"}, status=status.HTTP_404_NOT_FOUND + ) diff --git a/backend/urls.py b/backend/urls.py index f16651447..3b34eabb9 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -40,13 +40,11 @@ rooms.search_DM, name="search DM", ), - path( "api/v1/search-suggestions//", rooms.search_suggestions, - name="search suggestions" + name="search suggestions", ), - path( "api/v1/org//room//members/", rooms.group_member_add, @@ -217,7 +215,11 @@ messaging.all_messages, name="all_messages", ), - + path( + "api/v1/org//message//", + messaging.single_message, + name="message", + ), path("api/v1/sync", sync.sync_notifier, name="sync_notifier"), path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), From 4d00e3cded60457ab38d89d071ddc5564670c8dd Mon Sep 17 00:00:00 2001 From: blacdev Date: Sun, 31 Oct 2021 21:26:38 +0100 Subject: [PATCH 2/4] Refactored editmessage, get_message, delete_message function to use Class Based View --- backend/messaging.py | 348 ++++++++++++++++++++++--------------------- backend/urls.py | 22 +-- 2 files changed, 182 insertions(+), 188 deletions(-) diff --git a/backend/messaging.py b/backend/messaging.py index 91951c460..105a9d501 100644 --- a/backend/messaging.py +++ b/backend/messaging.py @@ -79,7 +79,6 @@ def message_create_get(request, room_id): elif request.method == "POST": request.data["room_id"] = room_id - print(request) serializer = MessageSerializer(data=request.data) if serializer.is_valid(): @@ -137,131 +136,6 @@ def message_create_get(request, room_id): return Response(status=status.HTTP_400_BAD_REQUEST) -@api_view(["PUT"]) -@db_init_with_credentials -def edit_message(request, message_id, room_id): - """ - This is used to update message context using message id as identifier, - - Updates a message from a room. - It access room with the 'room_id' and the message in the room with 'message_id' and then the new message. - The id of the organization (org_id) where the room is located is also needed. - Parameters: - org_id (str) : This is the id of the organization th user belongs to. - message (str) : This is the unique id of the message to be sent to a given room. - room_id(str) : This is the unique id of the room in the message - - Returns: - A dict object indicating the the message has been updated. Example: - { - "status" : "success", - "room_id" : "6169dbcef5998a09e3bbbcd3", - "message_id" : "616ad4f989454c2006018af2" - "message" : "The message" - } - Raises: - Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. - IOError: An error occurred while deleteing the message. - """ - message = DB.read( - "dm_messages", {"id": message_id} - ) # Checks DB for message using the message_id - room_serializer = MessageSerializer(message, data=request.data, partial=True) - - # validates room_serializer with MessageSerializer. - if room_serializer.is_valid(): - data = room_serializer.data - data = {"message": request.data["message"]} - # print(data) - response = DB.update( - "dm_messages", message_id, data - ) # moves on to update the mesage - - if response.get("status") == 200: - data = { - "sender_id": request.data["sender_id"], - "message_id": message_id, - "room_id": room_id, - "message": request.data["message"], - "event": "edited_message", - } - centrifugo_data = send_centrifugo_data(room=room_id, data=data) - if centrifugo_data.get("error", None) == None: - return Response(data=data, status=status.HTTP_201_CREATED) - return Response(data) - return Response(room_serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - -@swagger_auto_schema( - methods=["delete"], - operation_summary="Deletes messages from rooms", - responses={400: "Error: Bad Request"}, -) -@api_view(["DELETE"]) -@db_init_with_credentials -def delete_message(request, message_id, room_id): - """ - Deletes a message from a room. - - It access room with the 'room_id' and the message in the room with 'message_id' and then deletes the message if it exists. - The id of the organization (org_id) where the room is located is also needed. - - Parameters: - org_id (str) : This is the id of the organization th user belongs to. - - room_id (str) : This is the unique id of the room the message to be deleted is in. - - message_id (str) : This is the unique id of the message to be deleted from a given room. - - Returns: - A dict object indicating the the message has been deleted. Example: - { - "status" : "success", - "event" : "message_delete", - "room_id" : "6169dbcef5998a09e3bbbcd3", - "message_id" : "616ad4f989454c2006018af2" - } - - Raises: - Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. - - IOError: An error occurred while deleteing the message. - """ - - if request.method == "DELETE": - try: - # Sends a get request to the database to fetch the message and the room of the message from. - message = DB.read("dm_messages", {"_id": message_id, "room_id": room_id}) - - # Checks if the room exists and if the message exists in the room. - # If this returns true, the message is deleted. Else an error message is returned. - if message: - response = DB.delete("dm_messages", message_id) - # if the delete operation was successful, it returns a success message. - if response.get("status") == 200: - response_output = { - "status": response["message"], - "event": "message_delete", - "room_id": room_id, - "message_id": message_id, - } - # This publishes the operation across all active devices in the room where the operation was performed. - centrifugo_data = centrifugo_client.publish( - room=room_id, data=response - ) - # Checks if the publish was successful and returns a success message if True, else an error message is returned. - if centrifugo_data and centrifugo_data.get("status_code") == 200: - return Response(response_output, status=status.HTTP_200_OK) - return Response( - data="message not sent", - status=status.HTTP_424_FAILED_DEPENDENCY, - ) - return Response("message not found", status=status.HTTP_404_NOT_FOUND) - except Exception as e: - # All exeptions are caught are returned here... - return Response(str(e), status=status.HTTP_400_BAD_REQUEST) - - @swagger_auto_schema( methods=["post"], operation_summary="Schedules messages in rooms", @@ -413,13 +287,11 @@ def all_messages(request): Also returns a messages with the read and unread status""" res = DB.read("dm_messages") - print(res) if res and "status_code" not in res: all_messages = res read_messages = [ message for message in all_messages if message["read"] == "true" ] - print(read_messages) unread_messages = [ message for message in all_messages if message["read"] == "false" ] @@ -437,48 +309,180 @@ def all_messages(request): ) -@swagger_auto_schema( - methods=["get"], - operation_summary="Returns a single message in the dm collection", - responses={200: "success", 424: "Failed Dependency"}, -) -@api_view(["GET"]) -@db_init_with_credentials -def single_message(request, message_id): - """ - Gets a single message from a room. - It access room with the 'message_id' and then displays the message if it exists. - The id of the organization (org_id) where the room is located is also needed. - Parameters: - org_id (str) : This is the id of the organization th user belongs to. - message_id (str) : This is the unique id of the message to be deleted from a given room. - - Returns: - A dict object indicating the the message has been deleted. Example: - { - "status" : "success", - "room_id" : "6169dbcef5998a09e3bbbcd3", - "message_id" : "616ad4f989454c2006018af2" - "message" : "The message" - } - Raises: - Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. - IOError: An error occurred while deleteing the message. - """ - data = request.data - request.data["message_id"] = message_id - try: - message = DB.read("dm_messages", {"_id": message_id}) - print(message) - room_id = message["room_id"] - data = { - "status": "success", - "room_id": room_id, - "message_id": message_id, - "message": message, - } - return Response(data, status=status.HTTP_200_OK) - except: - return JsonResponse( - {"message": "The room does not exist"}, status=status.HTTP_404_NOT_FOUND +class EditMessage(APIView): + def get(self, request, message_id, org_id): + + """ + Gets a single message from a room. + It access room with the 'message_id' and then displays the message if it exists. + The id of the organization (org_id) where the room is located is also needed. + Parameters: + org_id (str) : This is the id of the organization th user belongs to. + message_id (str) : This is the unique id of the message to be deleted from a given room. + + Returns: + A dict object indicating the the message has been deleted. Example: + { + "status" : "success", + "room_id" : "6169dbcef5998a09e3bbbcd3", + "message_id" : "616ad4f989454c2006018af2" + "message" : "The message" + } + Raises: + Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. + IOError: An error occurred while deleteing the message. + """ + data_storage = DataStorage() + data_storage.organization_id = org_id + data = request.data + request.data["message_id"] = message_id + + try: + message = data_storage.read("dm_messages", {"_id": message_id}) + + room_id = message["room_id"] + data = { + "status": "success", + "message": message, + } + return Response(data, status=status.HTTP_200_OK) + + except: + + return JsonResponse( + {"message": "The room does not exist"}, status=status.HTTP_404_NOT_FOUND + ) + + def put(self, request, message_id, org_id): + """ + This is used to update message context using message id as identifier, + + Updates a message from a room. + It access room with the 'room_id' and the message in the room with 'message_id' and then the new message. + The id of the organization (org_id) where the room is located is also needed. + Parameters: + org_id (str) : This is the id of the organization th user belongs to. + message (str) : This is the unique id of the message to be sent to a given room. + room_id(str) : This is the unique id of the room in the message + + Returns: + A dict object indicating the the message has been updated. Example: + { + "status" : "success", + "room_id" : "6169dbcef5998a09e3bbbcd3", + "message_id" : "616ad4f989454c2006018af2" + "message" : "The message" + } + Raises: + Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. + IOError: An error occurred while deleteing the message. + """ + + data_storage = DataStorage() + data_storage.organization_id = org_id + data = request.data + data["message_id"] = message_id + room_id = data["room_id"] + message_get = data_storage.read("dm_messages", {"_id": message_id}) + + # Checks DB for message using the message_id + room_serializer = MessageSerializer( + message_get, data=request.data, partial=True ) + + # validates room_serializer with MessageSerializer. + if room_serializer.is_valid(): + room_data = room_serializer.data + new_data = {"message": data["message"]} + response = DB.update( + "dm_messages", message_id, new_data + ) # moves on to update the mesage + + if response.get("status") == 200: + data = { + "sender_id": request.data["sender_id"], + "message_id": message_id, + "room_id": room_id, + "message": new_data["message"], + "event": "edited_message", + } + centrifugo_data = send_centrifugo_data(room=room_id, data=data) + + if centrifugo_data.get("error", None) == None: + return Response(data=data, status=status.HTTP_201_CREATED) + + return Response(data) + + return Response(room_serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def delete(self, request, message_id, org_id): + """ + Deletes a message from a room. + + It access room with the 'room_id' and the message in the room with 'message_id' and then deletes the message if it exists. + The id of the organization (org_id) where the room is located is also needed. + + Parameters: + org_id (str) : This is the id of the organization th user belongs to. + + room_id (str) : This is the unique id of the room the message to be deleted is in. + + message_id (str) : This is the unique id of the message to be deleted from a given room. + + Returns: + A dict object indicating the the message has been deleted. Example: + { + "status" : "success", + "event" : "message_delete", + "room_id" : "6169dbcef5998a09e3bbbcd3", + "message_id" : "616ad4f989454c2006018af2" + } + + Raises: + Not Found: If there is no message with specified id in the specified room, it returns 'message not found' and a '404' error message. + + IOError: An error occurred while deleteing the message. + """ + + try: + # Sends a get request to the database to fetch the message and the room of the message from. + data_storage = DataStorage() + data_storage.organization_id = org_id + data = request.data + data["message_id"] = message_id + message = data_storage.read("dm_messages", {"_id": message_id}) + room_id = message["room_id"] + + # Checks if the room exists and if the message exists in the room. + # If this returns true, the message is deleted. Else an error message is returned. + if message: + response = data_storage.delete("dm_messages", message_id) + + # if the delete operation was successful, it returns a success message. + if response.get("status") == 200: + response_output = { + "status": response["message"], + "event": "message_delete", + "room_id": room_id, + "message_id": message_id, + } + + # This publishes the operation across all active devices in the room where the operation was performed. + centrifugo_data = centrifugo_client.publish( + room=room_id, data=response + ) + + # Checks if the publish was successful and returns a success message if True, else an error message is returned. + if centrifugo_data.get("status_code") == 200: + return Response(response_output, status=status.HTTP_200_OK) + + return Response( + data="message not sent", + status=status.HTTP_424_FAILED_DEPENDENCY, + ) + + return Response("message not found", status=status.HTTP_404_NOT_FOUND) + + except Exception as e: + # All exeptions are caught are returned here... + return Response(str(e), status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/urls.py b/backend/urls.py index 3b34eabb9..e29b6639f 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -70,16 +70,6 @@ messaging.message_create_get, name="create_get_message", ), - path( - "api/v1/org//updatemessage//room/", - messaging.edit_message, - name="updateroom", - ), - path( - "api/v1/org//rooms//messages//message", - messaging.delete_message, - name="delete_message", - ), path( "api/v1/org//rooms//schedule-message", messaging.scheduled_messages, @@ -151,8 +141,13 @@ name="message_reactions", ), path( - "api/v1/org//rooms//messages//threads//reactions", + "api/v1/org//rooms//messages/", reactions.ThreadEmoji.as_view(), + name="edit_message", + ), + path( + "api/v1/org//message/", + messaging.EditMessage.as_view(), name="thread_message_reaction", ), path( @@ -215,11 +210,6 @@ messaging.all_messages, name="all_messages", ), - path( - "api/v1/org//message//", - messaging.single_message, - name="message", - ), path("api/v1/sync", sync.sync_notifier, name="sync_notifier"), path("api/token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), path("api/token/refresh/", TokenRefreshView.as_view(), name="token_refresh"), From b439e68c02cb148a046b8de4ba430e3dd242a8b5 Mon Sep 17 00:00:00 2001 From: blacdev Date: Sun, 31 Oct 2021 21:38:06 +0100 Subject: [PATCH 3/4] changed the name of editmessage class to MessageDetailsView --- backend/messaging.py | 2 +- backend/urls.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/messaging.py b/backend/messaging.py index 105a9d501..57c90439a 100644 --- a/backend/messaging.py +++ b/backend/messaging.py @@ -309,7 +309,7 @@ def all_messages(request): ) -class EditMessage(APIView): +class MessageDetailsView(APIView): def get(self, request, message_id, org_id): """ diff --git a/backend/urls.py b/backend/urls.py index e29b6639f..242b1657e 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -147,7 +147,7 @@ ), path( "api/v1/org//message/", - messaging.EditMessage.as_view(), + messaging.MessageDetailsView.as_view(), name="thread_message_reaction", ), path( From e0c2fe15831ab4e342b21e93561305a5b5b1085a Mon Sep 17 00:00:00 2001 From: blacdev Date: Sun, 31 Oct 2021 21:42:00 +0100 Subject: [PATCH 4/4] made a little correction to an error in urls.py --- backend/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/urls.py b/backend/urls.py index 242b1657e..64d31e0d6 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -141,7 +141,7 @@ name="message_reactions", ), path( - "api/v1/org//rooms//messages/", + "api/v1/org//rooms//messages//threads//reactions", reactions.ThreadEmoji.as_view(), name="edit_message", ),