diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index 37ca3177e..12a2ea9a3 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -28,6 +28,7 @@ import { updateChat, updateChatListSecondaryInfo, updateManagementProgress, + leaveChat, } from '../../reducers'; import { selectChat, @@ -299,78 +300,78 @@ addReducer('joinChannel', (global, actions, payload) => { }); addReducer('deleteChatUser', (global, actions, payload) => { - (async () => { - const { chatId, userId }: { chatId: string; userId: string } = payload!; - const chat = selectChat(global, chatId); - const user = selectUser(global, userId); - if (!chat || !user) { - return; - } - await callApi('deleteChatUser', { chat, user }); + const { chatId, userId }: { chatId: string; userId: string } = payload!; + const chat = selectChat(global, chatId); + const user = selectUser(global, userId); + if (!chat || !user) { + return; + } - const activeChat = selectCurrentMessageList(global); - if (activeChat && activeChat.chatId === chatId && global.currentUserId === userId) { - actions.openChat({ id: undefined }); - } - })(); + global = leaveChat(global, chatId); + setGlobal(global); + + if (selectCurrentMessageList(global)?.chatId === chatId) { + actions.openChat({ id: undefined }); + } + + void callApi('deleteChatUser', { chat, user }); }); addReducer('deleteChat', (global, actions, payload) => { - (async () => { - const { chatId }: { chatId: string } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } - await callApi('deleteChat', { chatId: chat.id }); + const { chatId }: { chatId: string } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - const activeChat = selectCurrentMessageList(global); - if (activeChat && activeChat.chatId === chatId) { - actions.openChat({ id: undefined }); - } - })(); + global = leaveChat(global, chatId); + setGlobal(global); + + if (selectCurrentMessageList(global)?.chatId === chatId) { + actions.openChat({ id: undefined }); + } + + void callApi('deleteChat', { chatId: chat.id }); }); addReducer('leaveChannel', (global, actions, payload) => { - (async () => { - const { chatId } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } + const { chatId } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - const { id: channelId, accessHash } = chat; + global = leaveChat(global, chatId); + setGlobal(global); - if (channelId && accessHash) { - await callApi('leaveChannel', { channelId, accessHash }); - } + if (selectCurrentMessageList(global)?.chatId === chatId) { + actions.openChat({ id: undefined }); + } - const activeChannel = selectCurrentMessageList(global); - if (activeChannel && activeChannel.chatId === chatId) { - actions.openChat({ id: undefined }); - } - })(); + const { id: channelId, accessHash } = chat; + if (channelId && accessHash) { + void callApi('leaveChannel', { channelId, accessHash }); + } }); addReducer('deleteChannel', (global, actions, payload) => { - (async () => { - const { chatId } = payload!; - const chat = selectChat(global, chatId); - if (!chat) { - return; - } + const { chatId } = payload!; + const chat = selectChat(global, chatId); + if (!chat) { + return; + } - const { id: channelId, accessHash } = chat; + global = leaveChat(global, chatId); + setGlobal(global); - if (channelId && accessHash) { - await callApi('deleteChannel', { channelId, accessHash }); - } + if (selectCurrentMessageList(global)?.chatId === chatId) { + actions.openChat({ id: undefined }); + } - const activeChannel = selectCurrentMessageList(global); - if (activeChannel && activeChannel.chatId === chatId) { - actions.openChat({ id: undefined }); - } - })(); + const { id: channelId, accessHash } = chat; + if (channelId && accessHash) { + void callApi('deleteChannel', { channelId, accessHash }); + } }); addReducer('createGroupChat', (global, actions, payload) => { diff --git a/src/modules/actions/apiUpdaters/chats.ts b/src/modules/actions/apiUpdaters/chats.ts index 55b4f4b7a..11e799dcd 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -8,10 +8,10 @@ import { closeMessageNotifications, showNewMessageNotification } from '../../../ import { updateAppBadge } from '../../../util/appBadge'; import { updateChat, - replaceChatListIds, updateChatListIds, updateChatListType, replaceThreadParam, + leaveChat, } from '../../reducers'; import { selectChat, @@ -70,19 +70,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { } case 'updateChatLeave': { - const listType = selectChatListType(global, update.id); - if (!listType) { - break; - } - - const { [listType]: listIds } = global.chats.listIds; - - if (listIds) { - global = replaceChatListIds(global, listType, listIds.filter((listId) => listId !== update.id)); - } - - global = updateChat(global, update.id, { isNotJoined: true }); - setGlobal(global); + setGlobal(leaveChat(global, update.id)); break; } diff --git a/src/modules/reducers/chats.ts b/src/modules/reducers/chats.ts index 2c1c94fc9..a4518515c 100644 --- a/src/modules/reducers/chats.ts +++ b/src/modules/reducers/chats.ts @@ -3,6 +3,7 @@ import { ApiChat, ApiPhoto } from '../../api/types'; import { ARCHIVED_FOLDER_ID } from '../../config'; import { omit } from '../../util/iteratees'; +import { selectChatListType } from '../selectors'; export function replaceChatListIds( global: GlobalState, @@ -193,3 +194,20 @@ export function updateChatListSecondaryInfo( }, }; } + +export function leaveChat(global: GlobalState, leftChatId: string): GlobalState { + const listType = selectChatListType(global, leftChatId); + if (!listType) { + return global; + } + + const { [listType]: listIds } = global.chats.listIds; + + if (listIds) { + global = replaceChatListIds(global, listType, listIds.filter((listId) => listId !== leftChatId)); + } + + global = updateChat(global, leftChatId, { isNotJoined: true }); + + return global; +}