2017-06-07 17:24:48 +00:00
|
|
|
/**
|
|
|
|
* Interface between the graphical conversation system and the API
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
ComunicWeb.components.conversations.interface = {
|
2017-06-11 13:14:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var {Object} __conversationsList Cached list of conversations
|
|
|
|
*/
|
2017-06-16 09:42:28 +00:00
|
|
|
__conversationsList: {},
|
2017-06-11 13:14:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get and return the list of available conversations
|
|
|
|
*
|
|
|
|
* @param {Function} onceGotList What to do next
|
|
|
|
* @param {Boolean} force Force the list to be loaded even if present in the cache
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
getList: function(onceGotList, force){
|
|
|
|
|
|
|
|
//First, check if the list is already present in the cache or not
|
|
|
|
if(this.__conversationsList && !force){
|
|
|
|
//Perform next action now
|
|
|
|
onceGotList(this.__conversationsList);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Else, prepare an API request
|
|
|
|
var apiURI = "conversations/getList";
|
|
|
|
var params = {}; //No params required now
|
|
|
|
|
|
|
|
//Perform the API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(results){
|
|
|
|
|
|
|
|
//Check for error
|
|
|
|
if(results.error){
|
|
|
|
//Log error
|
|
|
|
ComunicWeb.debug.logMessage("ERROR : couldn't get conversations list !");
|
|
|
|
|
|
|
|
//Perform next action
|
|
|
|
onceGotList(results);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Process the list
|
|
|
|
var conversationsList = {};
|
|
|
|
for(i in results){
|
|
|
|
conversationsList["conversation-"+results[i].ID] = results[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save the list in the cache
|
|
|
|
ComunicWeb.components.conversations.interface.__conversationsList = conversationsList;
|
|
|
|
|
|
|
|
//Perform next action
|
|
|
|
onceGotList(conversationsList);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-06-07 17:24:48 +00:00
|
|
|
/**
|
|
|
|
* Create a conversation
|
|
|
|
*
|
|
|
|
* @param {Object} infos Informations about the conversation to create
|
2017-06-10 07:42:09 +00:00
|
|
|
* * @info {Array} users A list of the members of the conversation
|
2017-06-17 15:32:21 +00:00
|
|
|
* * @info {Boolean} follow Defines if the current user wants to follow the conversation or not
|
2017-06-10 07:42:09 +00:00
|
|
|
* * @info {Mixed} conversationName The name of the conversation
|
2017-06-07 17:24:48 +00:00
|
|
|
* @param {Function} afterCreate What to do once the conversation is created
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
2017-06-10 07:42:09 +00:00
|
|
|
createConversation: function(infos, afterCreate){
|
|
|
|
|
|
|
|
//Prepare an API request
|
|
|
|
var apiURI = "conversations/create";
|
|
|
|
var params = {
|
|
|
|
name: infos.conversationName,
|
|
|
|
follow : infos.follow,
|
|
|
|
users: infos.users,
|
|
|
|
};
|
|
|
|
|
|
|
|
//Perform the API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(result){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
//Log error
|
|
|
|
ComunicWeb.debug.logMessage("ERROR ! Couldn't create a conversation!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Perform next action
|
|
|
|
afterCreate(result);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
2017-06-11 13:14:23 +00:00
|
|
|
},
|
2017-06-16 09:42:28 +00:00
|
|
|
|
2017-06-17 15:32:21 +00:00
|
|
|
/**
|
|
|
|
* Update conversation settings
|
|
|
|
*
|
|
|
|
* @param {infos} infos Informations about the conversation to update
|
|
|
|
* @info {Integer} conversationID The ID of the conversation to update
|
|
|
|
* @info {Boolean} following Specify if the user is following the conversation or not
|
|
|
|
* @info {String} name Specify a new name for the conversation
|
|
|
|
* @info {array} members Specify the new list of members for the conversation
|
|
|
|
* @param {function} callback The function callback
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
updateSettings: function(infos, callback){
|
2017-06-18 07:23:08 +00:00
|
|
|
//Prepare the API request
|
|
|
|
var apiURI = "conversations/updateSettings";
|
|
|
|
var params = {
|
|
|
|
conversationID: infos.conversationID
|
|
|
|
};
|
|
|
|
|
|
|
|
//Add conversation name (if specified)
|
2017-06-18 09:14:26 +00:00
|
|
|
if(infos.name !== undefined)
|
2017-06-18 07:23:08 +00:00
|
|
|
params.name = infos.name;
|
|
|
|
|
|
|
|
//Add conversation members (if specified)
|
|
|
|
if(infos.members)
|
|
|
|
params.members = infos.members;
|
|
|
|
|
|
|
|
//Add conversation following status (if specified)
|
|
|
|
if(infos.following !== undefined)
|
|
|
|
params.following = infos.following;
|
|
|
|
|
|
|
|
//Perform API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(result){
|
2017-06-18 09:14:26 +00:00
|
|
|
//Empty the cache (considered as deprecated)
|
|
|
|
ComunicWeb.components.conversations.interface.emptyCache(true);
|
|
|
|
|
|
|
|
//Check for error
|
|
|
|
if(result.error)
|
|
|
|
ComunicWeb.debug.logMessage("Error! An error occured while trying to update conversation settings !");
|
|
|
|
|
|
|
|
//Perform next action
|
|
|
|
callback(result);
|
|
|
|
|
2017-06-18 07:23:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
2017-06-17 15:32:21 +00:00
|
|
|
},
|
|
|
|
|
2017-06-16 09:42:28 +00:00
|
|
|
/**
|
|
|
|
* Get informations about a unique conversation
|
|
|
|
*
|
|
|
|
* @param {Integer} conversationID The ID of the conversation
|
|
|
|
* @param {function} nextStep What to do once the operation is completed
|
|
|
|
* @param {Boolean} forceRefresh Force informations about the conversation to be fetched (ignore cached informations)
|
|
|
|
* @return {Boolan} True for a success
|
|
|
|
*/
|
|
|
|
getInfosOne: function(conversationID, nextStep, forceRefresh){
|
|
|
|
|
|
|
|
//First, if the conversation is available in the cache
|
|
|
|
if(!forceRefresh && this.__conversationsList['conversation-'+conversationID]){
|
|
|
|
|
|
|
|
//Perform next action now without getting fresh informations on the server
|
|
|
|
nextStep(this.__conversationsList['conversation-'+conversationID]);
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Else, perform an API request
|
2017-06-16 17:11:41 +00:00
|
|
|
var apiURI = "conversations/getInfosOne";
|
|
|
|
var params = {
|
|
|
|
conversationID: conversationID,
|
|
|
|
};
|
|
|
|
|
|
|
|
//Perform the API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(result){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error){
|
|
|
|
|
|
|
|
//Log error
|
2018-05-13 13:40:19 +00:00
|
|
|
ComunicWeb.debug.logMessage("Couldn't get informations about the conversation number "+conversationID+" !");
|
2017-06-16 17:11:41 +00:00
|
|
|
|
|
|
|
//Perform next action now
|
|
|
|
nextStep(result);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Else it is a success
|
|
|
|
//Cache the result
|
|
|
|
ComunicWeb.components.conversations.interface.__conversationsList["conversation-"+conversationID] = result;
|
|
|
|
|
|
|
|
//Perform next action
|
|
|
|
nextStep(result);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
});
|
2017-06-16 09:42:28 +00:00
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-06-19 12:55:29 +00:00
|
|
|
/**
|
|
|
|
* Search for private conversation
|
|
|
|
*
|
|
|
|
* @param {Integer} otherUser The ID of the other user
|
|
|
|
* @param {Boolean} allowCreation Allow the server to create the conversation if not found
|
|
|
|
* @param {function} callback What to do once the request is completed
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
searchPrivate: function(otherUser, allowCreation, callback){
|
|
|
|
|
|
|
|
//Perform an API request
|
|
|
|
var apiURI = "conversations/getPrivate";
|
|
|
|
var params = {
|
|
|
|
otherUser: otherUser,
|
|
|
|
allowCreate: allowCreation
|
|
|
|
}
|
|
|
|
|
|
|
|
//Perform API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(result){
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(result.error)
|
|
|
|
ComunicWeb.debug.logMessage("An error occured while trying to get a private conversation ID !");
|
|
|
|
|
|
|
|
//Perfrorm next action
|
|
|
|
callback(result);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-06-22 09:16:53 +00:00
|
|
|
/**
|
|
|
|
* Send a new message
|
|
|
|
*
|
|
|
|
* @param {Object} infos Informations about the message to send
|
|
|
|
* @info {Integer} conversationID The ID of the conversation
|
|
|
|
* @info {String} message The message to send
|
2018-04-21 09:00:13 +00:00
|
|
|
* @info {HTMLElement} image Optionnal, input field with an image
|
2017-06-22 09:16:53 +00:00
|
|
|
* @info {function} callback What to do once the image was successfully sent
|
|
|
|
* @return {Boolean} true for a success
|
|
|
|
*/
|
|
|
|
sendMessage: function(infos){
|
|
|
|
|
|
|
|
//Perform an API request
|
2017-06-23 17:01:15 +00:00
|
|
|
var apiURI = "conversations/sendMessage";
|
2018-04-21 09:00:13 +00:00
|
|
|
|
|
|
|
var hasImage = false;
|
|
|
|
if(infos.image){
|
|
|
|
if(infos.image.files[0])
|
|
|
|
hasImage = true;
|
2017-06-22 09:16:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 09:00:13 +00:00
|
|
|
//Check wether an image has to be included or not
|
|
|
|
if(!hasImage){
|
|
|
|
|
|
|
|
//Prepare request
|
|
|
|
var params = {
|
|
|
|
message: infos.message,
|
|
|
|
conversationID: infos.conversationID,
|
|
|
|
};
|
2017-06-22 09:16:53 +00:00
|
|
|
|
2018-04-21 09:00:13 +00:00
|
|
|
//Perform an API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, infos.callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
//If we have an image, we must do a formdata request
|
|
|
|
else {
|
|
|
|
|
|
|
|
var fd = new FormData();
|
|
|
|
fd.append("message", infos.message);
|
|
|
|
fd.append("conversationID", infos.conversationID);
|
|
|
|
fd.append("image", infos.image.files[0], infos.image.files[0].name);
|
|
|
|
|
|
|
|
//Perform an API request
|
|
|
|
ComunicWeb.common.api.makeFormDatarequest(apiURI, fd, true, infos.callback);
|
|
|
|
}
|
2017-06-22 09:16:53 +00:00
|
|
|
},
|
|
|
|
|
2017-06-25 14:15:23 +00:00
|
|
|
/**
|
|
|
|
* Refresh a conversation
|
|
|
|
*
|
|
|
|
* @param {Array} newConversations New conversations (which requires the 10 last messages)
|
|
|
|
* @param {Object} toRefresh Conversations to refresh
|
|
|
|
* @param {Function} callback The callback function
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
|
|
|
refreshConversations: function(newConversations, toRefresh, callback){
|
|
|
|
|
|
|
|
//Perform a request on the API
|
|
|
|
var apiURI = "conversations/refresh";
|
|
|
|
var params = {
|
|
|
|
newConversations: newConversations,
|
|
|
|
toRefresh: JSON.stringify(toRefresh),
|
|
|
|
}
|
|
|
|
|
|
|
|
//Perform an API request
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-02-24 13:24:33 +00:00
|
|
|
/**
|
|
|
|
* Get the list of unread conversations
|
|
|
|
*
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
getUnreadConversations: function(callback){
|
|
|
|
|
|
|
|
//Perform a request on the API
|
|
|
|
var apiURI = "conversations/get_list_unread";
|
|
|
|
var params = {};
|
|
|
|
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-04-26 15:55:38 +00:00
|
|
|
/**
|
|
|
|
* Get older message of a conversation
|
|
|
|
*
|
|
|
|
* @param {number} conversationID The ID of the conversation
|
|
|
|
* @param {number} oldestMessageID The ID of the oldest message known
|
|
|
|
* @param {number} limit The limit
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
getOlderMessages: function(conversationID, oldestMessageID, limit, callback){
|
|
|
|
|
|
|
|
//Perform a request on the API
|
|
|
|
var apiURI = "conversations/get_older_messages";
|
|
|
|
var params = {
|
|
|
|
conversationID: conversationID,
|
|
|
|
oldest_message_id: oldestMessageID,
|
|
|
|
limit: limit
|
|
|
|
};
|
|
|
|
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
|
|
|
},
|
|
|
|
|
2018-05-14 15:23:33 +00:00
|
|
|
/**
|
|
|
|
* Get the the lastest messages of a single conversation
|
|
|
|
*
|
|
|
|
* @param {Number} convID Target conversation ID
|
|
|
|
* @param {Number} lastMessageID The ID of the last known message
|
|
|
|
* @param {function} callback
|
|
|
|
*/
|
|
|
|
refreshSingleConversation: function(convID, lastMessageID, callback){
|
|
|
|
|
|
|
|
//Perform a request on the API
|
|
|
|
var apiURI = "conversations/refresh_single";
|
|
|
|
var params = {
|
|
|
|
conversationID: convID,
|
|
|
|
last_message_id: lastMessageID
|
|
|
|
};
|
|
|
|
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2019-01-11 16:57:55 +00:00
|
|
|
/**
|
2019-01-11 17:17:37 +00:00
|
|
|
* Intend to update the content of a single message
|
|
|
|
*
|
|
|
|
* @param {Number} messageID The ID of the message to update
|
|
|
|
* @param {String} content New content for the message
|
|
|
|
* @param {(success : Boolean) => any} callback Function called when
|
|
|
|
* the request is terminated
|
|
|
|
*/
|
|
|
|
UpdateSingleMessage: function(messageID, content, callback){
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(
|
|
|
|
"conversations/updateMessage",
|
|
|
|
{
|
|
|
|
"messageID": messageID,
|
|
|
|
"content": content
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
|
|
|
|
function(result){
|
|
|
|
callback(result.error ? false : true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-01-11 16:57:55 +00:00
|
|
|
* Intend to delete a single conversation message
|
|
|
|
*
|
|
|
|
* @param {Number} messageID The ID of the message to delete
|
|
|
|
* @param {(success: Boolean) => any} callback Function to call once the
|
|
|
|
* conversation message has been deleted
|
|
|
|
*/
|
|
|
|
DeleteSingleMessage: function(messageID, callback){
|
|
|
|
|
|
|
|
ComunicWeb.common.api.makeAPIrequest(
|
|
|
|
"conversations/deleteMessage",
|
|
|
|
{"messageID": messageID},
|
|
|
|
true,
|
|
|
|
|
|
|
|
function(result){
|
|
|
|
callback(result.error ? false : true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-06-16 09:42:28 +00:00
|
|
|
/**
|
|
|
|
* Empty conversations cache
|
|
|
|
*
|
2017-06-18 09:14:26 +00:00
|
|
|
* @param {Boolean} notHard Specify that the object hasn't to be recursively cleaned
|
2017-06-16 09:42:28 +00:00
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
2017-06-18 09:14:26 +00:00
|
|
|
emptyCache: function(notHard){
|
2017-06-16 09:42:28 +00:00
|
|
|
//Empty cache
|
2017-06-18 09:14:26 +00:00
|
|
|
if(!notHard)
|
|
|
|
clearObject(this.__conversationsList);
|
|
|
|
else
|
|
|
|
this.__conversationsList = {}; //"Light" clean
|
2017-06-16 09:42:28 +00:00
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
2017-06-19 12:55:29 +00:00
|
|
|
},
|
2017-06-16 09:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Register conversations cache cleaning function
|
|
|
|
ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.interface.emptyCache");
|