From 9dbbf0eb5fb692f887dff4ca28978856bf690670 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 7 Mar 2021 14:28:38 +0100 Subject: [PATCH] Can leave / delete a conversation --- .../components/conversations/chatWindows.js | 32 ++++++++++++++++++- .../js/components/conversations/interface.js | 12 +++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/assets/js/components/conversations/chatWindows.js b/assets/js/components/conversations/chatWindows.js index 05974e56..3928602c 100644 --- a/assets/js/components/conversations/chatWindows.js +++ b/assets/js/components/conversations/chatWindows.js @@ -571,7 +571,7 @@ const ConvChatWindow = { removeLink.addEventListener("click", async e => { e.preventDefault(); - if(!await showConfirmDialog(tr("Do you really want to remove %1% from the conversation ?", {"1": user.fullName}))) + if(!await showConfirmDialog(tr("Do you really want to remove %1% from the conversation?", {"1": user.fullName}))) return; try { @@ -584,6 +584,36 @@ const ConvChatWindow = { } }) } + + // Leave conversation + if(member.user_id == userID()) { + let removeLink = createElem2({ + type: "a", + appendTo: status, + innerHTML: tr("Leave") + }) + + removeLink.addEventListener("click", async e => { + e.preventDefault(); + + const isLastAdmin = conv.members.filter(m => m.is_admin && m.user_id != userID()).length == 0; + const msg = isLastAdmin ? tr("As you are its last admin, if you leave this conversation, it will be permanently deleted!") + : tr("Do you really want to leave this conversation?"); + + if(!await showConfirmDialog(msg)) + return; + + try { + await ConversationsInterface.leaveConversation(conv.id, member.user_id); + + // Close the conversation + info.box.closeFunction(); + } catch(e) { + console.error(e); + notify(tr("Failed to leave conversation!"), "danger"); + } + }) + } } diff --git a/assets/js/components/conversations/interface.js b/assets/js/components/conversations/interface.js index 64c73dbe..32ec044b 100644 --- a/assets/js/components/conversations/interface.js +++ b/assets/js/components/conversations/interface.js @@ -483,6 +483,18 @@ const ConversationsInterface = { }, true); }, + /** + * Leave a conversation + * + * @param {number} convID Conversation ID + * @param {number} userID Target user + */ + leaveConversation: async function(convID, userID) { + await api("conversations/delete", { + conversationID: convID, + userID: userID + }, true); + }, /** * Empty conversations cache