From a8e05437ca81f0aa3984e22874540b79065c6069 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 27 May 2017 15:11:30 +0200 Subject: [PATCH] Get multiple users infos --- assets/js/common/functionsSchema.js | 5 + assets/js/components/searchForm/searchForm.js | 37 ++-- assets/js/user/userInfos.js | 197 +++++++++++++----- 3 files changed, 165 insertions(+), 74 deletions(-) diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index b684d5d3..245c0816 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -335,6 +335,11 @@ var ComunicWeb = { */ getUserInfos: function(userID, afterGetUserInfos){}, + /** + * Get multiple users informations + */ + getMultipleUsersInfos: function(usersID, afterGetUserInfos){}, + /** * Empty user informations cache * Remove all entries from user informations cache diff --git a/assets/js/components/searchForm/searchForm.js b/assets/js/components/searchForm/searchForm.js index 91b057ca..41e13928 100644 --- a/assets/js/components/searchForm/searchForm.js +++ b/assets/js/components/searchForm/searchForm.js @@ -80,30 +80,33 @@ ComunicWeb.components.searchForm = { if(response.error) return false; - //Remove any remainging element in searchResultBox - emptyElem(searchBoxContainer); + //Preload users informations + ComunicWeb.user.userInfos.getMultipleUsersInfos(response, function(userInfos){ - //Create menu list - var menuList = createElem("ul", searchBoxContainer); - menuList.className = "menu"; - - //Process each result - for(i in response){ + //Remove any remainging element in searchResultBox + emptyElem(searchBoxContainer); - //Retrieve userID - var userID = response[i]; + //Create menu list + var menuList = createElem("ul", searchBoxContainer); + menuList.className = "menu"; - //Display user informations - ComunicWeb.components.searchForm.displayUser(userID, menuList); + //Process each result + for(i in response){ - } + //Retrieve userID + var userID = response[i]; - //Enable slimscroll - $(menuList).slimScroll({ - height: '200px', + //Display user informations + ComunicWeb.components.searchForm.displayUser(userID, menuList); + + } + + //Enable slimscroll + $(menuList).slimScroll({ + height: '200px', + }); }); }); - }, diff --git a/assets/js/user/userInfos.js b/assets/js/user/userInfos.js index eb65a75b..394fdb3d 100644 --- a/assets/js/user/userInfos.js +++ b/assets/js/user/userInfos.js @@ -6,73 +6,156 @@ ComunicWeb.user.userInfos = { - /** - * @var {String} User infos cache - */ - usersInfos: {}, + /** + * @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 - * @return {Boolean} True for a success - */ - getUserInfos: function(userID, afterGetUserInfos){ + /** + * 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 + * @return {Boolean} True for a success + */ + getUserInfos: function(userID, afterGetUserInfos){ - //If requested user is the current user, replace "current" with userID - if(userID === "current") - userID = ComunicWeb.user.userLogin.__userID; + //If requested user is the current user, replace "current" with userID + if(userID === "current") + userID = ComunicWeb.user.userLogin.__userID; - //First, check if informations are already available in the cache - if(this.usersInfos["user-"+userID]){ - afterGetUserInfos(this.usersInfos["user-"+userID]); //Then return these informations now - return true; - } - - //Else we have to perform an API request - var apiURI = "user/getInfos"; - var params = { - userID: userID - }; + //First, check if informations are already available in the cache + if(this.usersInfos["user-"+userID]){ + afterGetUserInfos(this.usersInfos["user-"+userID]); //Then return these informations now + return true; + } + + //Else we have to perform an API request + var apiURI = "user/getInfos"; + var params = { + userID: userID + }; - //Specify what to do next - var onceGetUserInfos = function(result){ - if(result.error){ - ComunicWeb.debug.logMessage("ERROR : couldn't get infos about user ID : "+userID+" !"); + //Specify what to do next + var onceGetUserInfos = function(result){ + if(result.error){ + ComunicWeb.debug.logMessage("ERROR : couldn't get infos about user ID : "+userID+" !"); - //Returns the error to the next function - afterGetUserInfos(result); + //Returns the error to the next function + afterGetUserInfos(result); - return false; - } - else { - //Save result - ComunicWeb.user.userInfos.usersInfos["user-"+userID] = result[0]; + return false; + } + else { + //Save result + ComunicWeb.user.userInfos.usersInfos["user-"+userID] = result[0]; - //Return result - afterGetUserInfos(result[0]); - } - } + //Return result + afterGetUserInfos(result[0]); + } + } - //Perform request - ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, onceGetUserInfos); + //Perform request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, onceGetUserInfos); - //Everything is OK - return true; + //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 + /** + * 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 + * @return {Boolean} True for a success + */ + getMultipleUsersInfos: function(usersID, afterGetUserInfos){ - 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; + }, } \ No newline at end of file