2017-06-14 14:39:34 +00:00
|
|
|
/**
|
|
|
|
* Conversation service file
|
|
|
|
*
|
|
|
|
* Ensure that the content of the conversations is up to date
|
|
|
|
*
|
2018-04-26 04:43:38 +00:00
|
|
|
* @author Pierre HUBERT
|
2017-06-14 14:39:34 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-01 12:19:56 +00:00
|
|
|
const ConvService = {
|
2017-06-24 17:23:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var {Object} __serviceCache The service cache
|
|
|
|
*/
|
|
|
|
__serviceCache: false,
|
2017-06-24 13:07:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializate conversation service
|
|
|
|
*
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
init: function(){
|
2017-06-24 17:23:55 +00:00
|
|
|
//Make sure the cache is empty
|
|
|
|
this.emptyCache();
|
2017-06-25 14:06:16 +00:00
|
|
|
|
2017-06-24 13:07:23 +00:00
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-04-01 12:14:08 +00:00
|
|
|
* Register a new conversation
|
2017-06-24 13:07:23 +00:00
|
|
|
*
|
2020-04-01 12:14:08 +00:00
|
|
|
* @param {Integer} conversationID The ID of the conversation to register
|
2017-06-24 13:07:23 +00:00
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
2020-04-01 12:14:08 +00:00
|
|
|
registerConversation: async function(conversationID){
|
2017-06-24 17:23:55 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
try {
|
2017-06-24 17:23:55 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
//Create conversation object
|
|
|
|
if(!this.__serviceCache)
|
|
|
|
this.__serviceCache = {}; //Create service cache object
|
2017-06-25 14:06:16 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
// Get the last messages of the conversations
|
2021-03-06 11:04:01 +00:00
|
|
|
const list = await ConversationsInterface.asyncRefreshSingle(conversationID, 0);
|
2017-06-25 14:06:16 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
//Register conversation locally
|
|
|
|
this.__serviceCache['conversation-' + conversationID] = {
|
|
|
|
conversationID: conversationID,
|
|
|
|
first_message_id: list.length == 0 ? 0 : list[0].ID,
|
|
|
|
};
|
2017-06-25 14:06:16 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
// Register conversation remotly
|
2021-03-06 11:04:01 +00:00
|
|
|
await ConversationsInterface.register(conversationID)
|
2017-06-24 17:23:55 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
for(const msg of list)
|
2021-03-06 11:04:01 +00:00
|
|
|
ConvChatWindow.addMessage(conversationID, msg);
|
2017-06-24 17:23:55 +00:00
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
notify("Could not open conversation!", "danger");
|
2017-06-24 17:23:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister a conversation
|
|
|
|
*
|
|
|
|
* @param {Integer} conversationID The ID of the conversation to remove
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
2020-04-01 12:14:08 +00:00
|
|
|
unregisterConversation: async function(conversationID){
|
2017-06-24 17:23:55 +00:00
|
|
|
|
|
|
|
//Log action
|
|
|
|
ComunicWeb.debug.logMessage("Unregistering conversation " + conversationID + " from service.");
|
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
if(this.__serviceCache && this.__serviceCache['conversation-'+conversationID]){
|
|
|
|
delete this.__serviceCache['conversation-'+conversationID]; //Remove conversation
|
2017-06-24 17:23:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:14:08 +00:00
|
|
|
// Unregister remotly
|
|
|
|
await ComunicWeb.components.conversations.interface.unregister(conversationID)
|
2017-06-24 17:23:55 +00:00
|
|
|
},
|
|
|
|
|
2018-04-26 04:50:23 +00:00
|
|
|
/**
|
|
|
|
* Get the oldest messages of a conversation
|
|
|
|
*
|
|
|
|
* @param {number} conversationID The ID of the target conversation
|
|
|
|
* @return {numbert} The ID of the oldest message / -1 in case of failure
|
|
|
|
*/
|
|
|
|
getOldestMessageID: function(conversationID){
|
|
|
|
|
|
|
|
//Try to fetch information
|
|
|
|
if(this.__serviceCache){
|
|
|
|
if(this.__serviceCache['conversation-'+conversationID]){
|
|
|
|
return this.__serviceCache['conversation-'+conversationID].first_message_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//The conversation was not found
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the oldest messages of a conversation
|
|
|
|
*
|
|
|
|
* @param {number} conversationID The ID of the target conversation
|
|
|
|
* @param {numbert} firstMessageID New value for the first known message id
|
|
|
|
*/
|
|
|
|
setOldestMessageID: function(conversationID, firstMessageID){
|
|
|
|
|
|
|
|
//Try to fetch information
|
|
|
|
if(this.__serviceCache){
|
|
|
|
if(this.__serviceCache['conversation-'+conversationID]){
|
|
|
|
this.__serviceCache['conversation-'+conversationID].first_message_id = firstMessageID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-24 17:23:55 +00:00
|
|
|
/**
|
|
|
|
* Empty service cache (unregister all conversations)
|
|
|
|
*
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
emptyCache: function(){
|
|
|
|
if(this.__serviceCache){
|
|
|
|
clearObject(this.__serviceCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:19:56 +00:00
|
|
|
ComunicWeb.components.conversations.service = ConvService;
|
|
|
|
|
2017-06-24 17:23:55 +00:00
|
|
|
//Register service cache
|
2020-04-01 12:14:08 +00:00
|
|
|
ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.service.emptyCache");
|
|
|
|
|
2020-04-01 12:19:56 +00:00
|
|
|
// Register to new messages
|
|
|
|
document.addEventListener("newConvMessage", (e) => {
|
|
|
|
const msg = e.detail;
|
|
|
|
|
|
|
|
if(ConvService.__serviceCache.hasOwnProperty("conversation-" + msg.convID))
|
|
|
|
ComunicWeb.components.conversations.chatWindows.addMessage(msg.convID, msg);
|
|
|
|
})
|