From a2c37ca00cb1e26c52b14b9f74cffdb698ec18e5 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 5 Aug 2022 19:23:15 +0200 Subject: [PATCH] Prevent opening same chat by t.me username link --- src/global/actions/api/chats.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index f6be39575..ee5466696 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -1469,23 +1469,26 @@ async function openChatByUsername( startAttach?: string | boolean, attach?: string, ) { + let global = getGlobal(); + const currentChat = selectCurrentChat(global); + // Attach in the current chat if (startAttach && !attach) { const chat = await fetchChatByUsername(username); - if (!chat) return; - const global = getGlobal(); - const user = selectUser(global, chat.id); + global = getGlobal(); + + const user = selectUser(global, chat.id); if (!user) return; + const isBot = isUserBot(user); if (!isBot || !user.isAttachMenuBot) { actions.showNotification({ message: langProvider.getTranslation('WebApp.AddToAttachmentUnavailableError') }); + return; } - const currentChat = selectCurrentChat(global); - if (!currentChat) return; actions.callAttachMenuBot({ @@ -1493,25 +1496,33 @@ async function openChatByUsername( chatId: currentChat.id, ...(typeof startAttach === 'string' && { startParam: startAttach }), }); + return; } - // Open temporary empty chat to make the click response feel faster - actions.openChat({ id: TMP_CHAT_ID }); + const isCurrentChat = currentChat?.username === username; + + if (!isCurrentChat) { + // Open temporary empty chat to make the click response feel faster + actions.openChat({ id: TMP_CHAT_ID }); + } const chat = await fetchChatByUsername(username); - if (!chat) { - actions.openPreviousChat(); - actions.showNotification({ message: 'User does not exist' }); + if (!isCurrentChat) { + actions.openPreviousChat(); + actions.showNotification({ message: 'User does not exist' }); + } + return; } if (channelPostId) { actions.focusMessage({ chatId: chat.id, messageId: channelPostId }); - } else { + } else if (!isCurrentChat) { actions.openChat({ id: chat.id }); } + if (startParam) { actions.startBot({ botId: chat.id, param: startParam }); }