RetroShare/retroshare-qml-app/src/ChatCache.qml

140 lines
3.0 KiB
QML
Raw Normal View History

pragma Singleton
2017-06-13 08:43:25 -04:00
import QtQml 2.3
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
{
id: chatCache
property QtObject lastMessageCache: QtObject
{
id: lastMessageCache
property var lastMessageList: ({})
signal lastMessageChanged(var chatI, var newLastMessage)
2017-06-13 08:51:53 -04:00
function updateLastMessageCache (chatId, chatModel)
{
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) {
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)")
var lastMessage = findChatLastMessage (chatModel)
lastMessageList[chatId].lastMessage = lastMessage
lastMessageChanged(chatId, lastMessage)
}
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
return messagesData.slice(-1)[0]
}
2017-06-13 08:51:53 -04:00
function findChatFirstMessage (chatModel)
{
var messagesData = JSON.parse(chatModel).data
return messagesData.slice[0]
}
2017-06-13 08:51:53 -04:00
function setRemoteGXS (chatId, remoteGXS)
{
if (!lastMessageList[chatId]) {
lastMessageList[chatId] = {}
console.log("Last message cache created!")
}
if (lastMessageList[chatId] && !lastMessageList[chatId].remoteGXS){
lastMessageList[chatId].remoteGXS = remoteGXS
return true
}
else {
return false
}
}
2017-06-13 08:51:53 -04:00
function getChatIdFromGxs (gxs)
{
for (var key in lastMessageList) {
if ( lastMessageList[key].remoteGXS &&
lastMessageList[key].remoteGXS.gxs === gxs ) {
return key
}
}
return ""
}
2017-06-13 08:51:53 -04:00
function getGxsFromChatId (chatId)
{
if (lastMessageList[chatId]) return lastMessageList[chatId].remoteGXS
return undefined
}
2017-06-13 08:51:53 -04:00
function getChatLastMessage (chatId)
{
if (lastMessageList[chatId]) {
return lastMessageList[chatId].lastMessage
}
return ""
}
}
2017-06-21 10:30:48 -04:00
property QtObject contactsCache: QtObject
{
id: contactsCache
property var contactsList
2017-06-22 12:15:24 -04:00
property var own
property var identityDetails: ({})
2017-06-21 10:30:48 -04:00
function getContactFromGxsId (gxsId)
{
console.log("getContactFromGxsId (gxsId)", gxsId)
for(var i in contactsList)
{
if (contactsList[i].gxs_id == gxsId) return contactsList[i]
}
}
2017-06-21 10:30:48 -04:00
function getIdentityDetails (gxsId)
{
if (identityDetails[gxsId]) return identityDetails[gxsId]
return ""
}
2017-06-21 10:30:48 -04:00
function setIdentityDetails (jData)
{
identityDetails[jData.gxs_id] = jData
}
2017-06-21 10:30:48 -04:00
function getIdentityAvatar (gxsId)
{
if (identityDetails[gxsId] && identityDetails[gxsId].avatar !== undefined)
{
return identityDetails[gxsId].avatar
}
return ""
2017-06-21 10:30:48 -04:00
}
2017-06-21 10:30:48 -04:00
}
}