ComunicWeb/assets/js/components/conversations/service.js

144 lines
3.6 KiB
JavaScript
Raw Normal View History

/**
* 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
*/
2020-04-01 12:19:56 +00:00
const ConvService = {
/**
* @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(){
//Make sure the cache is empty
this.emptyCache();
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){
2020-04-01 12:14:08 +00:00
try {
2020-04-01 12:14:08 +00:00
//Create conversation object
if(!this.__serviceCache)
this.__serviceCache = {}; //Create service cache object
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);
2020-04-01 12:14:08 +00:00
//Register conversation locally
this.__serviceCache['conversation-' + conversationID] = {
conversationID: conversationID,
2021-03-06 13:29:15 +00:00
first_message_id: list.length == 0 ? 0 : list[0].id,
2020-04-01 12:14:08 +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)
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);
2020-04-01 12:14:08 +00:00
} catch(e) {
console.error(e);
notify("Could not open conversation!", "danger");
}
},
/**
* 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){
//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
}
2020-04-01 12:14:08 +00:00
// Unregister remotly
await ComunicWeb.components.conversations.interface.unregister(conversationID)
},
/**
* 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;
}
}
},
/**
* 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;
//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);
})