diff --git a/assets/js/components/conversations/utils.js b/assets/js/components/conversations/utils.js index a931eebb..190d596c 100644 --- a/assets/js/components/conversations/utils.js +++ b/assets/js/components/conversations/utils.js @@ -4,7 +4,7 @@ * @author Pierre HUBERT */ -ComunicWeb.components.conversations.utils = { +const ConversationsUtils = { /** * Given conversation informations, returns its name @@ -128,4 +128,19 @@ ComunicWeb.components.conversations.utils = { //Return result return form; }, +} + +ComunicWeb.components.conversations.utils = ConversationsUtils; + + +/** + * Get information about a conversation + * + * @param {Conversation} conv Information about the + * target conversation + */ +async function getConvName(conv) { + return new Promise((res, rej) => { + ConversationsUtils.getName(conv, (name) => res(name)); + }) } \ No newline at end of file diff --git a/assets/js/components/sidebar/main.js b/assets/js/components/sidebar/main.js index b1f76109..831768a8 100644 --- a/assets/js/components/sidebar/main.js +++ b/assets/js/components/sidebar/main.js @@ -311,7 +311,7 @@ ComunicWeb.components.sideBar.main = { refreshMemberships: function(target){ ComunicWeb.components.webApp.interface.getMemberships( () => notify("Could not refresh your memberships!", "error"), - (m, u, g) => this.applyMemberships(target, m, u, g) + (m, u, g, c) => this.applyMemberships(target, m, u, g, c) ); }, @@ -322,8 +322,9 @@ ComunicWeb.components.sideBar.main = { * @param {*} memberships * @param {*} users * @param {*} groups + * @param {*} convs */ - applyMemberships: function(target, memberships, users, groups) { + applyMemberships: function(target, memberships, users, groups, convs) { // Empty liste target.innerHTML = ""; @@ -337,10 +338,10 @@ ComunicWeb.components.sideBar.main = { memberships.forEach(e => { if(e.type == "friend") - this.applyFriend(friendsTarget, e.friend, users["user-"+e.friend.ID_friend]); + this.applyFriend(friendsTarget, e.friend, users.get(e.friend.ID_friend)); if(e.type == "group") - this.applyGroup(friendsTarget, groups[e.id], e.last_activity); + this.applyGroup(friendsTarget, groups.get(e.id), e.last_activity); }); createElem2({ @@ -381,7 +382,7 @@ ComunicWeb.components.sideBar.main = { appendTo: a, type: "img", class: "img-circle", - src: user.accountImage + src: user.image }); // User name diff --git a/assets/js/components/webApp/interface.js b/assets/js/components/webApp/interface.js index 1ac40114..04a74a60 100644 --- a/assets/js/components/webApp/interface.js +++ b/assets/js/components/webApp/interface.js @@ -10,46 +10,41 @@ ComunicWeb.components.webApp.interface = { * Get all the membership of the user * * @param {Function} err Function called in case of errors - * @param {Function(memberships, usersInfo, groupsInfo) : void} success Function called in case of success + * @param {Function(memberships, usersInfo, groupsInfo, convNames) : void} success Function called in case of success */ - getMemberships: function(err, success) { + getMemberships: async function(err, success) { - // Peform the request on the server - ComunicWeb.common.api.makeAPIrequest( - "webApp/getMemberships", - {}, - true, - memberships => { - - // Check for error - if(memberships.error) - return err(memberships.error); - - // Get users & groups ID in case of success - let usersID = []; - let groupsID = []; + try { - memberships.forEach(el => { - if(el.type == "friend") - usersID.push(el.friend.ID_friend); - else - groupsID.push(el.id); - }); + // Get the list of memberships + const memberships = await api("webApp/getMemberships", {}, true); - getMultipleUsersInfo(usersID, users => { - if(users.error) - return err(memberships.error); - - getInfoMultipleGroups(groupsID, groupsInfo => { - if(groupsInfo.error) - return err(groupsInfo.error); - - success(memberships, users, groupsInfo); - }); - }); + // Get users & groups ID in case of success + const usersID = []; + const groupsID = []; + + memberships.forEach(el => { + if(el.type == "friend") + usersID.push(el.friend.ID_friend); + else if(el.type == "group") + groupsID.push(el.id); + }); + + const usersInfo = await getUsers(usersID); + const groupsInfo = await getGroups(groupsID); + + // Get conversations name + const convNames = new Map() + for(const el of memberships.filter(el => el.type == "conversation")) + convNames.set(el.conv.ID, await getConvName(el.conv)) + + success(memberships, usersInfo, groupsInfo, convNames); + + } catch(e) { + console.error("Get memberships error", e) + err(); + } - } - ); }