Refactor chat cache to add remoteGXS property to make chat last message more identificable

This commit is contained in:
Angela Mazzurco 2017-06-05 20:17:19 +02:00
parent d3e3f14afd
commit 78fc0bc995
3 changed files with 68 additions and 38 deletions

View File

@ -14,12 +14,16 @@ QtObject {
id: lastMessageCache id: lastMessageCache
property var lastMessageList: ({}) property var lastMessageList: ({})
signal lastMessageChanged() signal lastMessageChanged(var chatI, var newLastMessage)
function updateLastMessageCache (chatId, chatModel){ function updateLastMessageCache (chatId, chatModel){
console.log("updateLastMessageCache (chatId, chatModel)", chatId) console.log("updateLastMessageCache (chatId, chatModel)", chatId)
// First creates the chat id object for don't wait to work with the object if is needed to call RS api
if (!lastMessageList[chatId]) {
lastMessageList[chatId] = {}
console.log("Last message cache created!")
}
if (!chatModel) { if (!chatModel) {
rsApi.request( "/chat/messages/"+chatId, "", function (par){ rsApi.request( "/chat/messages/"+chatId, "", function (par){
updateLastMessage(chatId, par.response) updateLastMessage(chatId, par.response)
@ -32,8 +36,12 @@ QtObject {
function updateLastMessage (chatId, chatModel){ function updateLastMessage (chatId, chatModel){
console.log("updateLastMessage (chatId, chatModel)") console.log("updateLastMessage (chatId, chatModel)")
var lastMessage = findChatLastMessage (chatModel) var lastMessage = findChatLastMessage (chatModel)
lastMessageList[chatId] = lastMessage lastMessageList[chatId].lastMessage = lastMessage
if (!lastMessageList[chatId].remoteGXS) {
var firstMessage = findChatFirstMessage (chatModel)
setRemoteGXS(chatId, firstMessage.author_id)
}
lastMessageChanged(chatId, lastMessage)
} }
function findChatLastMessage (chatModel){ function findChatLastMessage (chatModel){
@ -41,22 +49,35 @@ QtObject {
return messagesData.slice(-1)[0] return messagesData.slice(-1)[0]
} }
function findChatFirstMessage (chatModel){
var messagesData = JSON.parse(chatModel).data
return messagesData.slice[0]
}
function setRemoteGXS (chatId, remoteGXS){
if (lastMessageList[chatId] && !lastMessageList[chatId].remoteGXS){
lastMessageList[chatId].remoteGXS = remoteGXS
return true
}
else {
return false
}
}
function getChatIdFromGxs (gxs){ function getChatIdFromGxs (gxs){
for (var key in lastMessageList) { for (var key in lastMessageList) {
if ( lastMessageList[key].author_id === gxs ) { if ( lastMessageList[key].remoteGXS === gxs ) {
return key return key
} }
} }
return undefined return ""
} }
function getChatLastMessage (chatId){ function getChatLastMessage (chatId){
if (lastMessageList[chatId]) { if (lastMessageList[chatId]) {
return lastMessageList[chatId] return lastMessageList[chatId].lastMessage
} }
return ""
return undefined
} }
} }

View File

@ -103,6 +103,7 @@ Item
{'action': 'refreshUnread', 'response': par.response}) {'action': 'refreshUnread', 'response': par.response})
json.data.forEach (function (chat){ json.data.forEach (function (chat){
ChatCache.lastMessageCache.updateLastMessageCache(chat.chat_id) ChatCache.lastMessageCache.updateLastMessageCache(chat.chat_id)
ChatCache.lastMessageCache.setRemoteGXS (chat.chat_id, chat.remote_author_id)
}) })
} }
function refreshUnread() function refreshUnread()

View File

@ -26,17 +26,10 @@ Item
height: 50 height: 50
width: parent.width width: parent.width
// property var lastMessageData: getLastMessage()
property var chatId: undefined property var chatId: undefined
property var lastMessageData: "" property var lastMessageData: ""
property var authorName: lastMessageData.author_name
// JSONListModel
// {
// id: lastMessageData
// query: "$.*"
// }
Rectangle { Rectangle {
@ -70,7 +63,8 @@ Item
hoverEnabled: true hoverEnabled: true
} }
Rectangle { Rectangle
{
anchors.fill: parent anchors.fill: parent
color: "transparent" color: "transparent"
anchors.margins: 5 anchors.margins: 5
@ -93,9 +87,11 @@ Item
} }
} }
Column { Column
{
height: parent.height height: parent.height
width: parent.width
anchors.left: colorHash.right anchors.left: colorHash.right
anchors.leftMargin: 5 anchors.leftMargin: 5
@ -107,12 +103,24 @@ Item
font.bold: true font.bold: true
} }
Text Row
{ {
id: lastMessage id: lastMessageText
color: model.own ? "blue" : "black" width: parent.width
text: (lastMessageData.msg) ? lastMessageData.msg : "" Text
font.italic: true {
id: lastMessageSender
font.italic: true
color: "dodgerblue"
text: (!lastMessageData.incoming && lastMessageData.msg)? "You: " : ""
}
Text
{
id: lastMessageMsg
text: (lastMessageData.msg) ? lastMessageData.msg : ""
font.italic: true
}
} }
} }
@ -161,22 +169,7 @@ Item
} }
} }
} }
// signal lastMessageChanged(var chatI, var newLastMessage)
// lastMessageChanged(chatId, lastMessage)
// Connections {
// target: ChatCache.lastMessageCache
// onLastMessageChanged: {
// if (!chatId) {
// getChatIdFromGXS()
// }
// if (chatId && chatId === chatI){
// delegateRoot.lastMessageData = newLastMessage
// console.log("@@@@@@@@@@@2 Last message changed ",chatI ,newLastMessage.msg, chatId )
// }
// }
// }
Component.onCompleted: { Component.onCompleted: {
if (!chatId){ if (!chatId){
@ -187,11 +180,26 @@ Item
} }
} }
Connections {
target: ChatCache.lastMessageCache
onLastMessageChanged: {
if (!chatId) {
chatId = getChatIdFromGXS()
}
if (chatId && chatId === chatI){
console.log("New last message received!")
lastMessageData = newLastMessage
}
}
}
function getChatLastMessage (chatId){ function getChatLastMessage (chatId){
return ChatCache.lastMessageCache.getChatLastMessage(chatId) return ChatCache.lastMessageCache.getChatLastMessage(chatId)
} }
function getChatIdFromGXS (){ function getChatIdFromGXS (){
var id= ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id)
return ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id) return ChatCache.lastMessageCache.getChatIdFromGxs(model.gxs_id)
} }