ComunicWeb/assets/js/user/userInfos.js

139 lines
3.4 KiB
JavaScript
Raw 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[userID]);
2017-05-27 13:35:10 +00:00
}, forceRequest);
2017-05-27 13:11:30 +00:00
},
/**
* Get multiple users informations
*
* @param {String} usersID User on which to make request (current to get connected user)
* @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[processUserID] = this.usersInfos["user-"+processUserID];
}
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[userID] = result[i];
}
//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-01-29 14:49:21 +00:00
}