Prevent opening same chat by t.me username link

This commit is contained in:
Alexander Zinchuk 2022-08-05 19:23:15 +02:00
parent 2a5eeb3111
commit a2c37ca00c

View File

@ -1469,23 +1469,26 @@ async function openChatByUsername(
startAttach?: string | boolean, startAttach?: string | boolean,
attach?: string, attach?: string,
) { ) {
let global = getGlobal();
const currentChat = selectCurrentChat(global);
// Attach in the current chat // Attach in the current chat
if (startAttach && !attach) { if (startAttach && !attach) {
const chat = await fetchChatByUsername(username); const chat = await fetchChatByUsername(username);
if (!chat) return; if (!chat) return;
const global = getGlobal();
const user = selectUser(global, chat.id);
global = getGlobal();
const user = selectUser(global, chat.id);
if (!user) return; if (!user) return;
const isBot = isUserBot(user); const isBot = isUserBot(user);
if (!isBot || !user.isAttachMenuBot) { if (!isBot || !user.isAttachMenuBot) {
actions.showNotification({ message: langProvider.getTranslation('WebApp.AddToAttachmentUnavailableError') }); actions.showNotification({ message: langProvider.getTranslation('WebApp.AddToAttachmentUnavailableError') });
return; return;
} }
const currentChat = selectCurrentChat(global);
if (!currentChat) return; if (!currentChat) return;
actions.callAttachMenuBot({ actions.callAttachMenuBot({
@ -1493,25 +1496,33 @@ async function openChatByUsername(
chatId: currentChat.id, chatId: currentChat.id,
...(typeof startAttach === 'string' && { startParam: startAttach }), ...(typeof startAttach === 'string' && { startParam: startAttach }),
}); });
return; return;
} }
// Open temporary empty chat to make the click response feel faster const isCurrentChat = currentChat?.username === username;
actions.openChat({ id: TMP_CHAT_ID });
if (!isCurrentChat) {
// Open temporary empty chat to make the click response feel faster
actions.openChat({ id: TMP_CHAT_ID });
}
const chat = await fetchChatByUsername(username); const chat = await fetchChatByUsername(username);
if (!chat) { if (!chat) {
actions.openPreviousChat(); if (!isCurrentChat) {
actions.showNotification({ message: 'User does not exist' }); actions.openPreviousChat();
actions.showNotification({ message: 'User does not exist' });
}
return; return;
} }
if (channelPostId) { if (channelPostId) {
actions.focusMessage({ chatId: chat.id, messageId: channelPostId }); actions.focusMessage({ chatId: chat.id, messageId: channelPostId });
} else { } else if (!isCurrentChat) {
actions.openChat({ id: chat.id }); actions.openChat({ id: chat.id });
} }
if (startParam) { if (startParam) {
actions.startBot({ botId: chat.id, param: startParam }); actions.startBot({ botId: chat.id, param: startParam });
} }