From 136db557039c1b4797b319c422cd64850adbd33f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 5 Mar 2021 15:26:45 +0100 Subject: [PATCH] Upgrade user memberships pane --- assets/js/components/calls/controller.js | 12 +++++----- .../js/components/conversations/interface.js | 2 +- assets/js/components/conversations/utils.js | 24 ++++++++----------- assets/js/components/sidebar/main.js | 20 ++++++++-------- assets/js/components/webApp/interface.js | 7 +++--- assets/js/typings/Conversations.d.ts | 1 + assets/js/typings/UserMembreship.d.ts | 13 ++++++++++ 7 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 assets/js/typings/UserMembreship.d.ts diff --git a/assets/js/components/calls/controller.js b/assets/js/components/calls/controller.js index 2986f013..d04a3832 100644 --- a/assets/js/components/calls/controller.js +++ b/assets/js/components/calls/controller.js @@ -17,19 +17,19 @@ class CallsController { * @param {Conversation} conv Information about the target conversation */ static Open(conv) { - if(OpenCalls.has(conv.ID) && OpenCalls.get(conv.ID).isOpen) + if(OpenCalls.has(conv.id) && OpenCalls.get(conv.id).isOpen) return; - console.info("Open call for conversation " + conv.ID); + console.info("Open call for conversation " + conv.id); // Create a new window for the conversation const window = new CallWindow(conv); - OpenCalls.set(conv.ID, window) - this.AddToLocalStorage(conv.ID); + OpenCalls.set(conv.id, window) + this.AddToLocalStorage(conv.id); window.on("close", () => { - OpenCalls.delete(conv.ID) - this.RemoveFromLocalStorage(conv.ID) + OpenCalls.delete(conv.id) + this.RemoveFromLocalStorage(conv.id) }) } diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index d9d8156c..2645e192 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -177,7 +177,7 @@ const ConversationsInterface = { } //Else, perform an API request - var apiURI = "conversations/getInfosOne"; + var apiURI = "conversations/get_single"; var params = { conversationID: conversationID, }; diff --git a/assets/js/components/conversations/utils.js b/assets/js/components/conversations/utils.js index a437a08f..41e53ad2 100644 --- a/assets/js/components/conversations/utils.js +++ b/assets/js/components/conversations/utils.js @@ -9,32 +9,28 @@ const ConversationsUtils = { /** * Given conversation informations, returns its name * - * @param {Object} infos Conversation informations + * @param {Conversation} info Conversation information * @param {Function} afterName What to do once we got conversation name * @return {Boolean} True for a success */ - getName: function(infos, afterName){ + getName: function(info, afterName){ //Check if the conversation has a name or not - if(infos.name) - afterName(infos.name); + if(info.name && info.name.length > 0) + afterName(info.name); else { //Get informations about the first two members var firstMembers = []; //Retrieve IDs - for(o in infos.members){ + for(o in info.members){ //Limit number to 2 - if(firstMembers.length < 2){ + if(firstMembers.length < 2){ + //Exclude current user ID + if(info.members[o].user_id != userID()) + firstMembers.push(info.members[o].user_id); - //Check this is a valid entry - if(infos.members[o]){ - - //Exclude current user ID - if(infos.members[o] != userID()) - firstMembers.push(infos.members[o]); - } } } @@ -42,7 +38,7 @@ const ConversationsUtils = { ComunicWeb.user.userInfos.getNames(firstMembers, function(usersName){ //For conversations with many members (more than 3 - we musn't forget current user) - if(infos.members.length > 3) + if(info.members.length > 3) usersName += ", ..."; //Peform next action now diff --git a/assets/js/components/sidebar/main.js b/assets/js/components/sidebar/main.js index 176fa720..ab658c92 100644 --- a/assets/js/components/sidebar/main.js +++ b/assets/js/components/sidebar/main.js @@ -309,7 +309,7 @@ const SidebarMain = { * @param {HTMLElement} target */ refreshMemberships: function(target){ - ComunicWeb.components.webApp.interface.getMemberships( + WebAppInterface.getMemberships( () => notify("Could not refresh your memberships!", "error"), (m, u, g, c) => this.applyMemberships(target, m, u, g, c) ); @@ -319,14 +319,14 @@ const SidebarMain = { * Apply memberships * * @param {HTMLElement} target - * @param {*} memberships - * @param {*} users + * @param {UserMembership[]} memberships + * @param {UsersList} users * @param {*} groups * @param {Map} convs */ applyMemberships: function(target, memberships, users, groups, convs) { - // Empty liste + // Empty list target.innerHTML = ""; let friendsTarget = createElem2({ @@ -344,7 +344,7 @@ const SidebarMain = { this.applyGroup(friendsTarget, groups.get(e.id), e.last_activity); if(e.type == "conversation") - this.applyConversation(friendsTarget, e.conv, convs.get(e.conv.ID)); + this.applyConversation(friendsTarget, e.conv, convs.get(e.conv.id)); }); createElem2({ @@ -532,7 +532,7 @@ const SidebarMain = { * Apply a conversation * * @param {HTMLElement} target - * @param {Array} conv + * @param {Conversation} conv * @param {String} name */ applyConversation: function(target, conv, name) { @@ -542,17 +542,17 @@ const SidebarMain = { type: "li", class: "conversation_memberhsip" }); - li.setAttribute("data-membership-conv-id", conv.ID) + li.setAttribute("data-membership-conv-id", conv.id) // Check for unread messages - if(!conv.saw_last_message) { + if(!conv.last_activity > conv.members.find(m => m.user_id == userID()).last_access) { li.classList.add("has-unread-msg"); } let a = createElem2({ appendTo: li, type: "a", - onclick: () => openConversation(conv.ID, true) + onclick: () => openConversation(conv.id, true) }); // Icon @@ -573,7 +573,7 @@ const SidebarMain = { appendTo: a, type: "div", class: "subinfo", - innerHTML: timeDiffToStr(conv.last_active) + innerHTML: timeDiffToStr(conv.last_activity) }); diff --git a/assets/js/components/webApp/interface.js b/assets/js/components/webApp/interface.js index 04a74a60..faa4e263 100644 --- a/assets/js/components/webApp/interface.js +++ b/assets/js/components/webApp/interface.js @@ -4,13 +4,13 @@ * @author Pierre HUBERT */ -ComunicWeb.components.webApp.interface = { +const WebAppInterface = { /** * Get all the membership of the user * * @param {Function} err Function called in case of errors - * @param {Function(memberships, usersInfo, groupsInfo, convNames) : void} success Function called in case of success + * @param {(UserMembership[], UsersList, groupsInfo, convNames) => void} success Function called in case of success */ getMemberships: async function(err, success) { @@ -26,6 +26,7 @@ ComunicWeb.components.webApp.interface = { memberships.forEach(el => { if(el.type == "friend") usersID.push(el.friend.ID_friend); + else if(el.type == "group") groupsID.push(el.id); }); @@ -36,7 +37,7 @@ ComunicWeb.components.webApp.interface = { // 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)) + convNames.set(el.conv.id, await getConvName(el.conv)) success(memberships, usersInfo, groupsInfo, convNames); diff --git a/assets/js/typings/Conversations.d.ts b/assets/js/typings/Conversations.d.ts index b67b8259..0ee00691 100644 --- a/assets/js/typings/Conversations.d.ts +++ b/assets/js/typings/Conversations.d.ts @@ -15,6 +15,7 @@ declare interface ConversationSettingsFormElements { declare interface ConversationMember { user_id: number, last_message_seen: number, + last_access: number, following: boolean, is_admin: boolean, } diff --git a/assets/js/typings/UserMembreship.d.ts b/assets/js/typings/UserMembreship.d.ts new file mode 100644 index 00000000..9be8b5e8 --- /dev/null +++ b/assets/js/typings/UserMembreship.d.ts @@ -0,0 +1,13 @@ +/** + * User membreship typings + * + * @author Pierre Hubert + */ + +declare interface UserMembership { + type: "group"|"friend"|"conversation", + id ?: number, + friend ?: Friend, + last_activity ?: number, + conv ?: Conversation +} \ No newline at end of file