ComunicWeb/assets/js/user/userInfos.js

283 lines
6.8 KiB
JavaScript
Raw Permalink Normal View History

2017-01-29 14:49:21 +00:00
/**
* User informations functions
*
* @author Pierre HUBERT
*/
2017-05-27 09:57:05 +00:00
ComunicWeb.user.userInfos = {
2017-01-29 14:49:21 +00:00
2017-05-27 13:11:30 +00:00
/**
* @var {String} User infos cache
*/
usersInfos: {},
/**
* Get user informations
*
* @param {String} userID User on which to make request (current to get connected user)
* @param {function} afterGetUserInfos What to do once user informations are available
2017-05-27 13:35:10 +00:00
* @param {Boolean} forceRequest Force the request to be made
2017-05-27 13:11:30 +00:00
* @return {Boolean} True for a success
*/
2017-05-27 13:35:10 +00:00
getUserInfos: function(userID, afterGetUserInfos, forceRequest){
2017-05-27 13:11:30 +00:00
2017-05-27 13:25:26 +00:00
//Check if current user infos were required
if(userID == "current")
userID = ComunicWeb.user.userLogin.getUserID();
2017-05-27 13:11:30 +00:00
2017-05-27 13:25:26 +00:00
//getMultipleUsersInfos mirror
var usersID = [userID];
2017-05-27 13:11:30 +00:00
2017-05-27 13:25:26 +00:00
return this.getMultipleUsersInfos(usersID, function(result){
//We check if an error occured
if(result.error)
2017-05-27 13:11:30 +00:00
afterGetUserInfos(result);
2017-05-27 13:25:26 +00:00
//Return a simple array
else
afterGetUserInfos(result["user-"+userID]);
2017-05-27 13:35:10 +00:00
}, forceRequest);
2017-05-27 13:11:30 +00:00
},
/**
* Get multiple users informations
*
2017-06-13 09:58:08 +00:00
* @param {Array~Object} usersID User on which to make request (current to get connected user)
2017-05-27 13:11:30 +00:00
* @param {function} afterGetUserInfos What to do once users informations are available
2017-05-27 13:35:10 +00:00
* @param {Boolean} forceRequest Force the request to be made
2017-05-27 13:11:30 +00:00
* @return {Boolean} True for a success
*/
2017-05-27 13:35:10 +00:00
getMultipleUsersInfos: function(usersID, afterGetUserInfos, forceRequest){
2017-05-27 13:11:30 +00:00
//First, check if informations are already available in the cache for some users
var cachedInformations = {};
var needRequest = false; //By default the request isn't required
var usersToGetList = "";
for(i in usersID){
//Extract userID
var processUserID = usersID[i];
//Check the local cache
2017-05-27 13:35:10 +00:00
if(this.usersInfos["user-"+processUserID] && !forceRequest){
2017-05-27 13:11:30 +00:00
//Add user information to cached informations
cachedInformations["user-"+processUserID] = this.usersInfos["user-"+processUserID];
2017-05-27 13:11:30 +00:00
}
else {
//Else we'll have to get data
needRequest = true;
2017-05-27 13:35:10 +00:00
usersToGetList += processUserID + ",";
2017-05-27 13:11:30 +00:00
}
}
//Check if an API request is not required
if(!needRequest){
//Go immediatly to the next step
afterGetUserInfos(cachedInformations);
return true;
}
//Perform API request
var apiURI = "user/getInfosMultiple";
var params = {
usersID: usersToGetList,
}
//Specify what to do next
var onceGetUserInfos = function(result){
if(result.error){
//Log error
ComunicWeb.debug.logMessage("ERROR : couldn't get infos about users ID !");
//Returns the error to the next function
afterGetUserInfos(result);
//Something went wrong
return false;
}
else {
//Prepare return
var returnInformations = cachedInformations;
//Save results and prepare return
for(i in result){
//Get user ID
var userID = result[i]['userID'];
//Store
ComunicWeb.user.userInfos.usersInfos["user-"+userID] = result[i];
returnInformations["user-"+userID] = result[i];
2017-05-27 13:11:30 +00:00
}
//Return results
afterGetUserInfos(returnInformations);
}
}
//Perform request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, onceGetUserInfos);
//Everything is OK
return true;
},
/**
* Empty user informations cache
* Remove all entries from user informations cache
*
* @return {Boolean} True for a success
*/
emptyUserInfosCache: function(){
this.userInfos = undefined; //Mark user info cache as undefined
this.userInfos = {}; //Create a new variable
return true;
},
2017-06-05 12:35:28 +00:00
/**
* Given a query, search for users and return the result
*
* @param {String} query The query to search
* @param {Function} afterSearch What to do once we got results
* @return {Boolean} True for a success
*/
search: function(query, afterSearch){
//Perform a request on the server
apiURI = "user/search";
params = {
query: query,
};
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, function(response){
//Continue only in case of success
if(response.error){
afterSearch(false);
return false;
}
//Preload users informations
ComunicWeb.user.userInfos.getMultipleUsersInfos(response, function(usersInfos){
//Go to next action
afterSearch(usersInfos);
});
});
2017-06-13 14:42:09 +00:00
},
/**
* Given user IDs (in an array) the function return their names in a string
*
* @param {Array} usersID The users to return as a string
* @param {Function} afterNames What to do once we have got the names
* @return {Boolean} True for a success
*/
getNames: function(usersID, afterNames){
//Get users informations
this.getMultipleUsersInfos(usersID, function(usersInfo){
//Check for errors
if(usersInfo.error){
afterNames("Error");
}
//Prepare conversation name
var usersName = "";
//Process users informations
for(i in usersInfo){
if(usersInfo[i].firstName)
//Add a coma if required
if(usersName != "")
usersName += ", ";
usersName += usersInfo[i].firstName + " " + usersInfo[i].lastName;
}
//Perform next action with result
afterNames(usersName);
});
},
2017-06-14 09:46:10 +00:00
/**
* Get advanced informations about a user
*
* @param {Integer} userID The ID of the user to fetch
* @param {Function} callback What to do once we got the information
*/
getAdvancedInfos: function(userID, callback){
//Prepare an API request
var apiURI = "user/getAdvancedUserInfos";
var params = {
userID: userID
};
//Perform the request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback);
},
/**
* Get the user ID specified by its folder name
*
* @param {String} path The path of the user
* @param {Function} callback What to do once we got a response from the server
*/
getIDfromPath: function(path, callback){
//Prepare an API request
var apiURI = "user/findbyfolder";
var params = {
subfolder: path
};
//Define what to do next
var next = function(response){
if(response.userID){
callback(response.userID*1);
}
else
//An error occured
callback(-1);
}
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, next);
},
2018-01-02 17:58:40 +00:00
/**
* Get the ID or the path of a user, depending of what is available
*
* @param {Object} userInfos Informations about the user
* @return {String} The ID of the user or its path, if he has one
*/
getIDorPath: function(userInfos){
//Check if a virtual directory is available
2018-01-02 18:07:18 +00:00
if(userInfos.virtualDirectory != null && userInfos.virtualDirectory != ""){
2018-01-02 17:58:40 +00:00
return userInfos.virtualDirectory;
}
//Else return user ID
else {
return userInfos.userID;
}
},
2017-06-14 09:46:10 +00:00
/**
* Empty users cache
*
* @return {Boolean} True for a success
*/
emptyCache: function(){
this.usersInfos = {};
//Success
return true;
}
};
//Register cache cleaner
ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.user.userInfos.emptyCache");