From 16c732879d0e02a071117e048e5eb023818041e1 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 7 Jul 2021 23:14:59 +0300 Subject: [PATCH] Chat List: Fix active chat closing on leave/delete (#1249) --- src/api/gramjs/methods/chats.ts | 10 ++++---- src/components/common/DeleteChatModal.tsx | 6 ++++- src/modules/actions/api/chats.ts | 30 +++++++++++++++-------- src/modules/actions/api/messages.ts | 5 +++- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index a645db5e8..1e0cf1da7 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -476,14 +476,14 @@ export function joinChannel({ } export function deleteChatUser({ - chatId, user, + chat, user, }: { - chatId: number; user?: ApiUser; + chat: ApiChat; user: ApiUser; }) { - const userId = user ? buildInputEntity(user.id, user.accessHash) as GramJs.InputUser : new GramJs.InputUserSelf(); + if (chat.type !== 'chatTypeBasicGroup') return undefined; return invokeRequest(new GramJs.messages.DeleteChatUser({ - chatId: buildInputEntity(chatId) as number, - userId, + chatId: buildInputEntity(chat.id, chat.accessHash) as number, + userId: buildInputEntity(user.id, user.accessHash) as GramJs.InputUser, }), true); } diff --git a/src/components/common/DeleteChatModal.tsx b/src/components/common/DeleteChatModal.tsx index 685c32837..2c6c66835 100644 --- a/src/components/common/DeleteChatModal.tsx +++ b/src/components/common/DeleteChatModal.tsx @@ -37,6 +37,7 @@ type StateProps = { isPrivateChat: boolean; isBasicGroup: boolean; isSuperGroup: boolean; + currentUserId: number | undefined; canDeleteForAll?: boolean; contactName?: string; }; @@ -51,6 +52,7 @@ const DeleteChatModal: FC = ({ isChatWithSelf, isBasicGroup, isSuperGroup, + currentUserId, canDeleteForAll, contactName, onClose, @@ -73,7 +75,7 @@ const DeleteChatModal: FC = ({ if (isPrivateChat) { deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if (isBasicGroup) { - deleteChatUser({ chatId: chat.id }); + deleteChatUser({ chatId: chat.id, userId: currentUserId }); deleteHistory({ chatId: chat.id, shouldDeleteForAll: false }); } else if ((isChannel || isSuperGroup) && !chat.isCreator) { leaveChannel({ chatId: chat.id }); @@ -86,6 +88,7 @@ const DeleteChatModal: FC = ({ isBasicGroup, isChannel, isSuperGroup, + currentUserId, chat.isCreator, chat.id, onClose, @@ -188,6 +191,7 @@ export default memo(withGlobal( isChannel: isChatChannel(chat), isBasicGroup: isChatBasicGroup(chat), isSuperGroup: isChatSuperGroup(chat), + currentUserId: global.currentUserId, canDeleteForAll, contactName, }; diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index 331745a81..213cdb5f9 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -230,16 +230,18 @@ addReducer('joinChannel', (global, actions, payload) => { addReducer('deleteChatUser', (global, actions, payload) => { (async () => { - const { chatId, userId } : {chatId: number; userId?: number} = payload!; + const { chatId, userId } : {chatId: number; userId: number} = payload!; const chat = selectChat(global, chatId); - if (!chat) { + const user = selectUser(global, userId); + if (!chat || !user) { return; } + await callApi('deleteChatUser', { chat, user }); - const user = userId !== undefined ? selectUser(global, userId) : undefined; - await callApi('deleteChatUser', { chatId: chat.id, user }); - - actions.openChat({ id: undefined }); + const activeChat = selectCurrentMessageList(global); + if (activeChat && activeChat.chatId === chatId && global.currentUserId === userId) { + actions.openChat({ id: undefined }); + } })(); }); @@ -247,13 +249,15 @@ addReducer('deleteChat', (global, actions, payload) => { (async () => { const { chatId } : {chatId: number } = payload!; const chat = selectChat(global, chatId); - if (!chat) { return; } await callApi('deleteChat', { chatId: chat.id }); - actions.openChat({ id: undefined }); + const activeChat = selectCurrentMessageList(global); + if (activeChat && activeChat.chatId === chatId) { + actions.openChat({ id: undefined }); + } })(); }); @@ -271,7 +275,10 @@ addReducer('leaveChannel', (global, actions, payload) => { await callApi('leaveChannel', { channelId, accessHash }); } - actions.openChat({ id: undefined }); + const activeChannel = selectCurrentMessageList(global); + if (activeChannel && activeChannel.chatId === chatId) { + actions.openChat({ id: undefined }); + } })(); }); @@ -289,7 +296,10 @@ addReducer('deleteChannel', (global, actions, payload) => { await callApi('deleteChannel', { channelId, accessHash }); } - actions.openChat({ id: undefined }); + const activeChannel = selectCurrentMessageList(global); + if (activeChannel && activeChannel.chatId === chatId) { + actions.openChat({ id: undefined }); + } })(); }); diff --git a/src/modules/actions/api/messages.ts b/src/modules/actions/api/messages.ts index 0ae38016e..b528892c0 100644 --- a/src/modules/actions/api/messages.ts +++ b/src/modules/actions/api/messages.ts @@ -407,7 +407,10 @@ addReducer('deleteHistory', (global, actions, payload) => { await callApi('deleteHistory', { chat, shouldDeleteForAll, maxId }); - actions.openChat({ id: undefined }); + const activeChat = selectCurrentMessageList(global); + if (activeChat && activeChat.chatId === chatId) { + actions.openChat({ id: undefined }); + } })(); });