Get multiple users infos

This commit is contained in:
Pierre 2017-05-27 15:11:30 +02:00
parent 96d86ce97a
commit a8e05437ca
3 changed files with 165 additions and 74 deletions

View File

@ -335,6 +335,11 @@ var ComunicWeb = {
*/ */
getUserInfos: function(userID, afterGetUserInfos){}, getUserInfos: function(userID, afterGetUserInfos){},
/**
* Get multiple users informations
*/
getMultipleUsersInfos: function(usersID, afterGetUserInfos){},
/** /**
* Empty user informations cache * Empty user informations cache
* Remove all entries from user informations cache * Remove all entries from user informations cache

View File

@ -80,31 +80,34 @@ ComunicWeb.components.searchForm = {
if(response.error) if(response.error)
return false; return false;
//Remove any remainging element in searchResultBox //Preload users informations
emptyElem(searchBoxContainer); ComunicWeb.user.userInfos.getMultipleUsersInfos(response, function(userInfos){
//Create menu list //Remove any remainging element in searchResultBox
var menuList = createElem("ul", searchBoxContainer); emptyElem(searchBoxContainer);
menuList.className = "menu";
//Process each result //Create menu list
for(i in response){ var menuList = createElem("ul", searchBoxContainer);
menuList.className = "menu";
//Retrieve userID //Process each result
var userID = response[i]; for(i in response){
//Display user informations //Retrieve userID
ComunicWeb.components.searchForm.displayUser(userID, menuList); var userID = response[i];
} //Display user informations
ComunicWeb.components.searchForm.displayUser(userID, menuList);
//Enable slimscroll }
$(menuList).slimScroll({
height: '200px', //Enable slimscroll
$(menuList).slimScroll({
height: '200px',
});
}); });
}); });
}, },
/** /**

View File

@ -6,73 +6,156 @@
ComunicWeb.user.userInfos = { ComunicWeb.user.userInfos = {
/** /**
* @var {String} User infos cache * @var {String} User infos cache
*/ */
usersInfos: {}, usersInfos: {},
/** /**
* Get user informations * Get user informations
* *
* @param {String} userID User on which to make request (current to get connected user) * @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 * @param {function} afterGetUserInfos What to do once user informations are available
* @return {Boolean} True for a success * @return {Boolean} True for a success
*/ */
getUserInfos: function(userID, afterGetUserInfos){ getUserInfos: function(userID, afterGetUserInfos){
//If requested user is the current user, replace "current" with userID //If requested user is the current user, replace "current" with userID
if(userID === "current") if(userID === "current")
userID = ComunicWeb.user.userLogin.__userID; userID = ComunicWeb.user.userLogin.__userID;
//First, check if informations are already available in the cache //First, check if informations are already available in the cache
if(this.usersInfos["user-"+userID]){ if(this.usersInfos["user-"+userID]){
afterGetUserInfos(this.usersInfos["user-"+userID]); //Then return these informations now afterGetUserInfos(this.usersInfos["user-"+userID]); //Then return these informations now
return true; return true;
} }
//Else we have to perform an API request //Else we have to perform an API request
var apiURI = "user/getInfos"; var apiURI = "user/getInfos";
var params = { var params = {
userID: userID userID: userID
}; };
//Specify what to do next //Specify what to do next
var onceGetUserInfos = function(result){ var onceGetUserInfos = function(result){
if(result.error){ if(result.error){
ComunicWeb.debug.logMessage("ERROR : couldn't get infos about user ID : "+userID+" !"); ComunicWeb.debug.logMessage("ERROR : couldn't get infos about user ID : "+userID+" !");
//Returns the error to the next function //Returns the error to the next function
afterGetUserInfos(result); afterGetUserInfos(result);
return false; return false;
} }
else { else {
//Save result //Save result
ComunicWeb.user.userInfos.usersInfos["user-"+userID] = result[0]; ComunicWeb.user.userInfos.usersInfos["user-"+userID] = result[0];
//Return result //Return result
afterGetUserInfos(result[0]); afterGetUserInfos(result[0]);
} }
} }
//Perform request //Perform request
ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, onceGetUserInfos); ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, onceGetUserInfos);
//Everything is OK //Everything is OK
return true; return true;
}, },
/** /**
* Empty user informations cache * Get multiple users informations
* Remove all entries from user informations cache *
* * @param {String} usersID User on which to make request (current to get connected user)
* @return {Boolean} True for a success * @param {function} afterGetUserInfos What to do once users informations are available
*/ * @return {Boolean} True for a success
emptyUserInfosCache: function(){ */
this.userInfos = undefined; //Mark user info cache as undefined getMultipleUsersInfos: function(usersID, afterGetUserInfos){
this.userInfos = {}; //Create a new variable
return true; //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
if(this.usersInfos["user-"+processUserID]){
//Add user information to cached informations
cachedInformations[processUserID] = this.usersInfos["user-"+processUserID];
}
else {
//Else we'll have to get data
needRequest = true;
usersToGetList += usersID+",";
}
}
//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;
},
} }