From d57481de29ea3b423ef9d2a15b0a0a46ba6fe8a9 Mon Sep 17 00:00:00 2001
From: Antoine Gaudron-desjardins <antoine.gaudrondesjardins@student-cs.fr>
Date: Fri, 15 Jul 2022 11:30:16 +0200
Subject: [PATCH] change websocket message structure

---
 backend/routers/comments.py         | 2 +-
 frontend/src/components/Comments.js | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/backend/routers/comments.py b/backend/routers/comments.py
index cdd8599..d1b57e9 100644
--- a/backend/routers/comments.py
+++ b/backend/routers/comments.py
@@ -21,7 +21,7 @@ async def create_comment(place: str, connect_id: str = Cookie(...), comment: sch
     user = crud.get_user(connect_id, db)
     if user:
         saved_comment = crud.create_comment(user, place, comment, db)
-        await manager.broadcast(json.dumps(saved_comment.dict(), default=str))
+        await manager.broadcast(json.dumps({"type": "comment", "comment": saved_comment.dict()}, default=str))
         return saved_comment
     else:
         raise Exception
diff --git a/frontend/src/components/Comments.js b/frontend/src/components/Comments.js
index e0a52e0..714bec6 100644
--- a/frontend/src/components/Comments.js
+++ b/frontend/src/components/Comments.js
@@ -117,9 +117,9 @@ export default function Messages({ place, infos, lastMessage }) {
   useEffect(() => {
     if (lastMessage?.data) {
       let new_message = JSON.parse(lastMessage.data);
-      if (new_message.username != user) {
+      if (new_message.type == "comment" && new_message.comment.username != user) {
         setMessages((messages) => {
-          messages.push(new_message);
+          messages.push(new_message.comment);
           return messages;
         });
       }
-- 
GitLab