From 397a290b7d46c945223007dd2b74f268910746ec Mon Sep 17 00:00:00 2001
From: aj3g19 <aj3g19@soton.ac.uk>
Date: Tue, 4 Jan 2022 23:12:53 +0000
Subject: [PATCH] now shows chatName as well as chatID

---
 .gitignore                                 |  2 ++
 app.js                                     | 15 ++++++++-------
 public/game.js                             |  7 +++++--
 triggers/HttpGetChatsForUserID/__init__.py |  7 +++++--
 views/chatDisplay.ejs                      |  1 +
 views/showChats.ejs                        |  3 ++-
 6 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index cafb8da..3a5b25f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,8 @@
 node_modules
 npm-debug.log
 
+__pycache__
+
 # Local development
 *.env
 *.dev
diff --git a/app.js b/app.js
index b1a0bd0..376eb35 100644
--- a/app.js
+++ b/app.js
@@ -65,9 +65,8 @@ function handleLogin(username,socket){
         memberID: un
       }
     }, function (error, response, body){
-      chatIDs = body;
-      //upon receiving chatIDs, get most recent message of each one
-      getMostRecentMessages(chatIDs,socket);
+      //upon receiving chatIDs and names, get most recent message of each one
+      getMostRecentMessages(body,socket);
     });
     
     /*
@@ -76,19 +75,21 @@ function handleLogin(username,socket){
     */  
 }
 
-function getMostRecentMessages(chatIds,socket){
+function getMostRecentMessages(chatIdsNames,socket){
   //go through each chat, get most recent message and pass this information to the client
-  for (const chatID of chatIDs){
+  for (const idChatName of chatIdsNames){
     request('http://localhost:7071/api/HttpGetLastChatMessage', {
       json: true,
       body: {
-        "ChatID": chatID
+        "ChatID": idChatName.ChatID
       }
     }, function (error, response, body){
       console.log(body);
       //upon receiving it, tell the client to store the chatID 
       if(body != "error"){
-        body['ChatID'] = chatID
+        body['ChatID'] = idChatName.ChatID
+        body['chatName'] = idChatName.chatName
+        
         socket.emit('chatAndLastMsg',body);
       }
     });
diff --git a/public/game.js b/public/game.js
index 14511fe..d550310 100644
--- a/public/game.js
+++ b/public/game.js
@@ -13,6 +13,7 @@ var app = new Vue({
         lastMessage:'',
         chatIDsAndLstMsgs:[],
         currentChatID:-1,
+        currentChatName:'',
         state:{state:0}
     },
     mounted: function() {
@@ -28,7 +29,9 @@ var app = new Vue({
             socket.emit('sendChat',userMsgChatID);
             this.chatMessage = '';
         },
-        retrieveChatMessages(ChatID){
+        retrieveChatMessages(ChatID,chatName){
+            this.currentChatName = chatName;
+            this.currentChatID = ChatID;
             socket.emit('requestChatMessages',ChatID);
         },
         back(){
@@ -52,11 +55,11 @@ function connect() {
     });
 
     socket.on('allChatMessages',function(ChatIDMessages){
-        app.currentChatID = ChatIDMessages.ChatID;
         app.messages = ChatIDMessages.messages;
         app.state.state = 2;
     });
     
+    //list of dictionaries {username,message,date,time,ChatID,chatName}
     socket.on('chatAndLastMsg',function(ChatIDMsg){
         app.chatIDsAndLstMsgs.push(ChatIDMsg);
     });
diff --git a/triggers/HttpGetChatsForUserID/__init__.py b/triggers/HttpGetChatsForUserID/__init__.py
index fd6e1b4..8d176d8 100644
--- a/triggers/HttpGetChatsForUserID/__init__.py
+++ b/triggers/HttpGetChatsForUserID/__init__.py
@@ -5,7 +5,7 @@ from azure.cosmos.exceptions import CosmosHttpResponseError
 
 
 #takes in memberID
-#returns chatID of chats the member is in
+#returns chatID and chatName of chats the member is in
 
 
 def main(req: func.HttpRequest,documents: func.DocumentList) -> func.HttpResponse:
@@ -21,7 +21,10 @@ def main(req: func.HttpRequest,documents: func.DocumentList) -> func.HttpRespons
     #go through each chat and find memberID
     for document in documents:
         if(memID in document["members"]):
-            chatIDs.append(document["ChatID"])
+            new_dict = {}
+            new_dict["ChatID"] = document["ChatID"]
+            new_dict["chatName"] = document["name"]
+            chatIDs.append(new_dict)
 
     
     return func.HttpResponse(json.dumps(chatIDs))
diff --git a/views/chatDisplay.ejs b/views/chatDisplay.ejs
index 432ee04..402d26d 100644
--- a/views/chatDisplay.ejs
+++ b/views/chatDisplay.ejs
@@ -1,3 +1,4 @@
+<h3>{{currentChatName}}</h3>
 <button class="btn btn-primary" @click='back'>Back</button>
 <div v-for="message in messages">
     <h6>{{message.username}}: {{message.time}} {{message.message}}</h6>
diff --git a/views/showChats.ejs b/views/showChats.ejs
index c1ed7ab..4ecd4df 100644
--- a/views/showChats.ejs
+++ b/views/showChats.ejs
@@ -1,3 +1,4 @@
 <div v-for="chatIDMsg in chatIDsAndLstMsgs">
-    <button class="btn btn-primary" @click='retrieveChatMessages(chatIDMsg.ChatID)'>{{chatIDMsg.ChatID}}</button>
+    <button class="btn btn-primary" @click='retrieveChatMessages(chatIDMsg.ChatID,chatIDMsg.chatName)'>
+        {{chatIDMsg.ChatID}}  {{chatIDMsg.chatName}} </button>
 </div>
\ No newline at end of file
-- 
GitLab