diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index 0774a263..d9d8156c 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -319,16 +319,10 @@ const ConversationsInterface = { /** * Get the list of unread conversations * - * @param {function} callback + * @returns {Promise} */ - getUnreadConversations: function(callback){ - - //Perform a request on the API - var apiURI = "conversations/get_list_unread"; - var params = {}; - - ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); - + getUnreadConversations: async function() { + return await api("conversations/get_list_unread", null, true); }, /** diff --git a/assets/js/components/conversations/unreadDropdown.js b/assets/js/components/conversations/unreadDropdown.js index 9bca73c8..d8331ecf 100644 --- a/assets/js/components/conversations/unreadDropdown.js +++ b/assets/js/components/conversations/unreadDropdown.js @@ -99,67 +99,49 @@ ComunicWeb.components.conversations.unreadDropdown = { * * @param {HTMLElement} target The target to display the conversations */ - refresh_list_conversations: function(target){ + refresh_list_conversations: async function(target) { - //Perform a request through the interface - ComunicWeb.components.conversations.interface.getUnreadConversations(function(conversations){ + try { - //Check for errors - if(conversations.error){ - //Display an error - ComunicWeb.common.notificationSystem.showNotification(lang("conversations_dropdown_err_get_list"), "danger"); - return; - } + //Perform a request through the interface + const list = await ConversationsInterface.getUnreadConversations(); //Get the list of users ID - var usersID = []; + let usersID = new Set(); //Process the list of conversations - for (var index = 0; index < conversations.length; index++) { - const entry = conversations[index]; - - var userID = entry.userID; - if(!usersID.includes(userID)) - usersID.push(userID); + for (let entry of list) { + ConversationsUtils.getUsersIDForMessage(entry.message).forEach(id => usersID.add(id)) } - //Get informations about the users - ComunicWeb.user.userInfos.getMultipleUsersInfo(usersID, function(usersInfos){ + const users = await getUsers([...usersID]); - //Check for errors - if(usersInfos.error){ - //Display an error - ComunicWeb.common.notificationSystem.showNotification(lang("conversations_dropdown_err_get_user_info"), "danger"); - return; - } - - //Display the list of conversations - ComunicWeb.components.conversations.unreadDropdown._display_list(target, conversations, usersInfos); - }); - }); + this._display_list(target, list, users); + } + catch(e) { + console.error(e); + ComunicWeb.common.notificationSystem.showNotification(lang("conversations_dropdown_err_get_list"), "danger"); + } }, /** * Display the list of conversations * * @param {HTMLElement} target The target for the fields - * @param {array} conversations The list of conversations - * @param {object} usersInfos Informations about related users + * @param {UnreadConversation[]} conversations The list of conversations + * @param {UsersList} usersInfo Information about related users */ - _display_list: function(target, conversations, usersInfos){ + _display_list: function(target, conversations, usersInfo){ //Empty the target target.innerHTML = ""; //Process each conversation - for (var index = 0; index < conversations.length; index++) { - - //Get the conversation - const conversation = conversations[index]; + for (let conversation of conversations) { //Get informations about the user - const userInfos = usersInfos["user-" + conversation.userID]; + const user = usersInfo.get(ConversationsUtils.getMainUserForMessage(conversation.message)) //Create list element var convLi = createElem2({ @@ -170,10 +152,9 @@ ComunicWeb.components.conversations.unreadDropdown = { //Create the conversation link element var convLink = createElem2({ appendTo: convLi, - type: "a", - href: "#" + type: "a" }); - convLink.setAttribute("data-conversation-id", conversation.id); + convLink.setAttribute("data-conversation-id", conversation.conv.id); //Add left elements var leftElems = createElem2({ @@ -187,14 +168,14 @@ ComunicWeb.components.conversations.unreadDropdown = { appendTo: leftElems, type: "img", class: "img-circle", - src: userInfos.accountImage + src: conversation.conv.logo ? conversation.conv.logo : user.image }); //Add item top informations var liTop = createElem2({ appendTo: convLink, type: "h4", - innerHTML: userFullName(userInfos) + innerHTML: user.fullName }); //Add item top small informations @@ -204,30 +185,42 @@ ComunicWeb.components.conversations.unreadDropdown = { }); //Add the message post time - var conversationLastActive = createElem2({ + createElem2({ appendTo: liTopSmall, type: "span", - innerHTML: ' ' + ComunicWeb.common.date.timeDiffToStr(conversation.last_active) + " ago" + innerHTML: ' ' + ComunicWeb.common.date.timeDiffToStr(conversation.conv.last_activity) + " ago" }); //Add conversation name (if available) - if(conversation.conv_name != ""){ + if(conversation.conv.name != "" && conversation.conv.name != null){ - var targetConversation = createElem2({ + createElem2({ appendTo: convLink, type: "p", - innerHTML: conversation.conv_name, + innerHTML: conversation.conv.name, }); } - //Add the message - var conversationMessage = createElem2({ - appendTo: convLink, - type: "p", - class: "message-content", - innerHTML: removeHtmlTags(conversation.message) - }); + // Add the message + if (conversation.message.message) { + createElem2({ + appendTo: convLink, + type: "p", + class: "message-content", + innerHTML: removeHtmlTags(conversation.message.message) + }); + } + + // In case of server message + if (conversation.message.server_message) { + createElem2({ + appendTo: convLink, + type: "p", + class: "message-content", + innerHTML: ConversationsUtils.getServerMessage(conversation.message, usersInfo) + }); + } //Make the conversation link lives convLink.onclick = function(){ diff --git a/assets/js/components/conversations/utils.js b/assets/js/components/conversations/utils.js index 2bdd846f..a437a08f 100644 --- a/assets/js/components/conversations/utils.js +++ b/assets/js/components/conversations/utils.js @@ -136,6 +136,85 @@ const ConversationsUtils = { //Return result return form; }, + + /** + * Get the ID of the users for a message + * + * @param {ConversationMessage} msg + */ + getUsersIDForMessage: function(msg) { + if (msg.user_id != null && msg.user_id > 0) + return [msg.user_id]; + + switch (msg.server_message.type) { + case "user_created_conv": + case "user_left": + return [msg.server_message.user_id]; + + case "user_added_another": + return [msg.server_message.user_who_added, msg.server_message.user_added]; + + case "user_removed_another": + return [msg.server_message.user_who_removed, msg.server_message.user_removed]; + } + }, + + /** + * Get the ID of the main user for a given message + * + * @param {ConversationMessage} msg + */ + getMainUserForMessage: function(msg) { + if (msg.user_id != null && msg.user_id > 0) + return msg.user_id; + + switch (msg.server_message.type) { + case "user_created_conv": + case "user_left": + return msg.server_message.user_id; + + case "user_added_another": + return msg.server_message.user_who_added; + + case "user_removed_another": + return msg.server_message.user_who_removed; + } + }, + + /** + * Generate a message of the server + * + * @param {ConversationMessage} msg + * @param {UsersList} users + */ + getServerMessage: function(msg, users) { + if (msg.server_message == null) + return ""; + + switch (msg.server_message.type) { + case "user_created_conv": + return tr("%1% created the conversation", { + "1": users.get(msg.server_message.user_id).fullName + }); + + case "user_added_another": + return tr("%1% added %2% to the conversation", { + "1": users.get(msg.server_message.user_who_added).fullName, + "2": users.get(msg.server_message.user_added).fullName + }) + + case "user_left": + return tr("%1% left the conversation", { + "1": users.get(msg.server_message.user_id).fullName + }); + + case "user_removed_another": + return tr("%1% removed %2% from the conversation", { + "1": users.get(msg.server_message.user_who_removed).fullName, + "2": users.get(msg.server_message.user_removed).fullName + }); + } + } } ComunicWeb.components.conversations.utils = ConversationsUtils; diff --git a/assets/js/langs/en.inc.js b/assets/js/langs/en.inc.js index 10159ced..f5d429a0 100644 --- a/assets/js/langs/en.inc.js +++ b/assets/js/langs/en.inc.js @@ -130,7 +130,6 @@ ComunicWeb.common.langs.en = { //Conversations - unread dropdown conversations_dropdown_header: "Unread conversations", conversations_dropdown_err_get_list: "Could not retrieve the list of unread conversations !", - conversations_dropdown_err_get_user_info: "Could not get informations about some users !", conversations_dropdown_no_unread_notice: "You do not have any unread messages in the conversations you are following...", diff --git a/assets/js/langs/fr.inc.js b/assets/js/langs/fr.inc.js index 3e7501fa..22f175de 100644 --- a/assets/js/langs/fr.inc.js +++ b/assets/js/langs/fr.inc.js @@ -130,7 +130,6 @@ ComunicWeb.common.langs.fr = { //Conversations - unread dropdown conversations_dropdown_header: "Conversations non lues", conversations_dropdown_err_get_list: "Une erreur a survenue lors de la récupération des conversations non lues !", - conversations_dropdown_err_get_user_info: "Une erreur a survenue lors de la récupération d'informations de certains utilisateurs !", conversations_dropdown_no_unread_notice: "Vous n'avez aucun message non lu dans les conversations que vous suivez...", //Notifications - dropdown diff --git a/assets/js/typings/Conversations.d.ts b/assets/js/typings/Conversations.d.ts index aecbf5b4..b67b8259 100644 --- a/assets/js/typings/Conversations.d.ts +++ b/assets/js/typings/Conversations.d.ts @@ -10,4 +10,57 @@ declare interface ConversationSettingsFormElements { conversationNameInput: HTMLElement, allowEveryoneToAddMembers: HTMLElement, followConversationInput: HTMLElement, +} + +declare interface ConversationMember { + user_id: number, + last_message_seen: number, + following: boolean, + is_admin: boolean, +} + +declare interface Conversation { + id: number, + last_activity: number, + name: string, + color?: string, + logo?: string, + group_id?: number, + members: ConversationMember[], + can_everyone_add_members: boolean, + can_have_call: boolean, + can_have_video_call: boolean, + has_call_now: boolean, +} + +declare interface ConversationServerMessage { + type: "user_created_conv"|"user_added_another"|"user_left"|"user_removed_another", + user_id?: number, + user_who_added?: number, + user_added?: number, + user_who_removed?: number, + user_removed?: number, +} + +declare interface ConversationMessageFile { + url: string, + size: number, + name: string, + thumbnail?: string, + type: string +} + +declare interface ConversationMessage { + id: number, + conv_id: number, + user_id: number, + time_sent: number, + message?: string, + file?: ConversationMessageFile, + server_message?: ConversationServerMessage, +} + +declare interface UnreadConversation { + conv: Conversation, + message: ConversationMessage } \ No newline at end of file