2017-06-16 15 views
0

我有以下的情況下,我做一個聊天和使用火力地堡作爲後端,我要找到下一個問題的最佳解決方案。如果羣組聊天已打開,則每封收到的消息都應該有一個發件人個人資料圖片。聊天分爲三種結構,這是對話,userConversation和消息模型。消息模型僅包含senderID,因爲我認爲不建議存儲profileImageURL,因爲用戶可以更改頭像。我想到的第二個選擇是救profileImageURL在對話模式,當用戶更改頭像使用雲功能,這將工作去改變它,但是這是一個非常糟糕的決定,因爲資源的成本(例如,如果用戶有300個對話,他會每天更換頭像)。請告訴我,做這種情況的最好方法是什麼?最好的辦法讓每個消息的輪廓圖像中火力地堡

消息模型

"-KmfKFxY2BsLjpGixowG" : { 
     "conversationID" : "-KmfK4m1t2nDKFX_MZr8", 
     "creationTimeStamp" : 1.497523097283577E9, 
     "id" : "-KmfKFxY2BsLjpGixowG", 
     "senderID" : "xpyM19QVjJTgrtdntlbcJPkb0jB2", 
     "sendingStatusIndex" : 0, 
     "textMessage" : "3reds", 
     "typeIndex" : 0 
     }, 

會話模型

"-KmfK4m1t2nDKFX_MZr8" : { 
     "id" : "-KmfK4m1t2nDKFX_MZr8", 
     "lastMessage" : { 
      "conversationID" : "-KmfK4m1t2nDKFX_MZr8", 
      "creationTimeStamp" : 1.497591480636771E9, 
      "id" : "-KmjP72nyEJUX7yQmwYp", 
      "senderID" : "AoG6HmxXE8ahAESx98C2UZ0ieAh1", 
      "sendingStatusIndex" : 0, 
      "textMessage" : "C", 
      "typeIndex" : 0 
     }, 
     "typeIndex" : 0, 
     "userAcitivities" : [ { 
      "removedChatTimeStamp" : 0, 
      "userID" : "xpyM19QVjJTgrtdntlbcJPkb0jB2" 
     }, { 
      "removedChatTimeStamp" : 0, 
      "userID" : "AoG6HmxXE8ahAESx98C2UZ0ieAh1" 
     } ] 
     } 

用戶會話模型

"AoG6HmxXE8ahAESx98C2UZ0ieAh1" : { 
     "-KmeqR8RYXo-5Pt0gue1" : { 
      "friendID" : "QscqImQoCGdAciaVMoRJN35KjEH2", 
      "id" : "-KmeqR8RYXo-5Pt0gue1", 
      "removedChatTimeStamp" : 0, 
      "typeIndex" : 0 
     }, 

更新描述:

你好!我正在聊天! Firebase被用作後端。問題在於如何在羣聊中最好地上傳用戶圖片。消息模型有一個senderID,當然在應用程序中。在每個單元格中出現的最差選項(我不會使用它)是查詢最新的url並使用Kingfisher加載和緩存圖像。啓動應用程序/聊天時的第二個選項是更新或上傳聊天室中可用用戶的所有化身,但這裏有兩個問題。第一個問題,如果聊天是50,並且每次聊天50個用戶,那麼一次執行2500個查詢也不是一種選擇。第二個問題是,如果以某種方式避免了大量請求,那麼從這些數據中可以創建一個字典並將其傳遞給一個單元格,並通過senderID獲取Kingfisher的實際URL,但我認爲這很糟糕,和Plus可以說性能。這個或那個基於firebase的聊天的最簡單的例子。

也有多種選擇,但他們都是壞的。你能建議如何做到最好嗎?或者在哪裏找到並閱讀關於這個「模塊」的正確架構。

回答

0

我解決了這個問題,如下所示,打開ChatViewController的時候,我要求使用雲功能鏈接到所有用戶的圖像和我得到的準備字典[用戶名:userAvatarPath]在應用程序中。

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 

module.exports = functions.https.onRequest((req, res) => { 

    const conversationID = req.query.conversationID; 
    const currentUserID = req.query.currentUserID; 

    console.log("conversationID", conversationID, "currentUserID", currentUserID); 

    const conversationUsersRef = admin.database().ref("chat").child("conversationUsers").child(conversationID); 
    conversationUsersRef.once("value").then(conversationUsersRefSnap => { 
     var index = 0; 
     var totalIndex = 0; 
     var conversationUsersImagesArray = []; 
     conversationUsersRefSnap.forEach(function(conversationUserSnap) { 
      console.log("conversationUserSnap", conversationUserSnap.val()); 
      console.log("$index", index); 
      const dict = conversationUserSnap.val(); 
      const conversationUserID = dict["userID"]; 
      if (conversationUserID != currentUserID) { 
       index += 1; 
       const userSenderImageQuery = admin.database().ref('userImages').child(conversationUserID).child('userProfileImages').orderByChild('timeStamp').limitToLast(1); 
       userSenderImageQuery.once('value', function (snapshot, error) { 
        var imagePath = ''; 
        if (error) { 
         index -= 1; 
         if (conversationUsersImagesArray.length == index) { 
          console.log("total", conversationUsersImagesArray); 
          res.status(200).send(conversationUsersImagesArray); 
         }; 
        } else { 
         if (snapshot.val()) { 
          const value = snapshot.val(); 
          const key = Object.keys(value)[0]; 
          const requestJSON = value[key]; 
          console.log('senderImageQuery requestJSON', requestJSON); 
          const userImagePath = requestJSON['path']; 
          imagePath = userImagePath; 
          const compressPath = requestJSON["compressPath"]; 
          if (compressPath) { 
           imagePath = compressPath; 
          }; 

          conversationUsersImagesArray.push({"userID" : conversationUserID, "imagePath" : imagePath, "conversationID" : conversationID}); 
          console.log("conversationUsersImages", conversationUsersImagesArray.length, "index", index); 

          if (conversationUsersImagesArray.length == index) { 
           console.log("total", conversationUsersImagesArray); 
           res.status(200).send(conversationUsersImagesArray); 
          }; 
         } else { 
          index -= 1; 
          if (conversationUsersImagesArray.length == index) { 
           console.log("total", conversationUsersImagesArray); 
           res.status(200).send(conversationUsersImagesArray); 
          }; 
         }; 
        }; 
       }); 
      }; 
     }); 
    }); 
});