2017-06-03 11:41:09 -04:00
|
|
|
pragma Singleton
|
|
|
|
|
2017-06-13 08:43:25 -04:00
|
|
|
import QtQml 2.3
|
2017-06-03 11:41:09 -04:00
|
|
|
import QtQuick.Controls 2.0
|
|
|
|
import org.retroshare.qml_components.LibresapiLocalClient 1.0
|
|
|
|
import Qt.labs.settings 1.0
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
QtObject
|
|
|
|
{
|
2017-06-03 11:41:09 -04:00
|
|
|
|
|
|
|
id: chatCache
|
|
|
|
|
|
|
|
property QtObject lastMessageCache: QtObject
|
|
|
|
{
|
2017-06-05 10:06:59 -04:00
|
|
|
id: lastMessageCache
|
2017-06-03 11:41:09 -04:00
|
|
|
property var lastMessageList: ({})
|
|
|
|
|
2017-06-05 14:17:19 -04:00
|
|
|
signal lastMessageChanged(var chatI, var newLastMessage)
|
2017-06-05 10:06:59 -04:00
|
|
|
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function updateLastMessageCache (chatId, chatModel)
|
|
|
|
{
|
2017-06-03 11:41:09 -04:00
|
|
|
console.log("updateLastMessageCache (chatId, chatModel)", chatId)
|
2017-06-05 14:17:19 -04:00
|
|
|
// 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!")
|
|
|
|
}
|
2017-06-03 11:41:09 -04:00
|
|
|
if (!chatModel) {
|
|
|
|
rsApi.request( "/chat/messages/"+chatId, "", function (par){
|
|
|
|
updateLastMessage(chatId, par.response)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
updateLastMessage (chatId, chatModel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function updateLastMessage (chatId, chatModel)
|
|
|
|
{
|
2017-06-03 12:09:52 -04:00
|
|
|
console.log("updateLastMessage (chatId, chatModel)")
|
2017-06-05 10:06:59 -04:00
|
|
|
var lastMessage = findChatLastMessage (chatModel)
|
2017-06-05 14:17:19 -04:00
|
|
|
lastMessageList[chatId].lastMessage = lastMessage
|
|
|
|
lastMessageChanged(chatId, lastMessage)
|
2017-06-03 11:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function findChatLastMessage (chatModel)
|
|
|
|
{
|
2017-06-03 12:09:52 -04:00
|
|
|
var messagesData = JSON.parse(chatModel).data
|
2017-06-03 11:41:09 -04:00
|
|
|
return messagesData.slice(-1)[0]
|
|
|
|
}
|
2017-06-05 10:06:59 -04:00
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function findChatFirstMessage (chatModel)
|
|
|
|
{
|
2017-06-05 14:17:19 -04:00
|
|
|
var messagesData = JSON.parse(chatModel).data
|
|
|
|
return messagesData.slice[0]
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function setRemoteGXS (chatId, remoteGXS)
|
|
|
|
{
|
2017-06-12 09:44:07 -04:00
|
|
|
if (!lastMessageList[chatId]) {
|
2017-06-07 09:00:37 -04:00
|
|
|
lastMessageList[chatId] = {}
|
|
|
|
console.log("Last message cache created!")
|
|
|
|
}
|
2017-06-05 14:17:19 -04:00
|
|
|
if (lastMessageList[chatId] && !lastMessageList[chatId].remoteGXS){
|
|
|
|
lastMessageList[chatId].remoteGXS = remoteGXS
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 10:06:59 -04:00
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function getChatIdFromGxs (gxs)
|
|
|
|
{
|
2017-06-05 10:06:59 -04:00
|
|
|
for (var key in lastMessageList) {
|
2017-06-12 09:44:07 -04:00
|
|
|
if ( lastMessageList[key].remoteGXS &&
|
|
|
|
lastMessageList[key].remoteGXS.gxs === gxs ) {
|
2017-06-05 10:06:59 -04:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 14:17:19 -04:00
|
|
|
return ""
|
2017-06-05 10:06:59 -04:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function getGxsFromChatId (chatId)
|
|
|
|
{
|
2017-06-12 09:44:07 -04:00
|
|
|
if (lastMessageList[chatId]) return lastMessageList[chatId].remoteGXS
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2017-06-13 08:51:53 -04:00
|
|
|
function getChatLastMessage (chatId)
|
|
|
|
{
|
2017-06-05 10:06:59 -04:00
|
|
|
if (lastMessageList[chatId]) {
|
2017-06-05 14:17:19 -04:00
|
|
|
return lastMessageList[chatId].lastMessage
|
2017-06-05 10:06:59 -04:00
|
|
|
}
|
2017-06-05 14:17:19 -04:00
|
|
|
return ""
|
2017-06-05 10:06:59 -04:00
|
|
|
}
|
2017-06-03 11:41:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|