Fix leaving/deleting chats

This commit is contained in:
Alexander Zinchuk 2021-11-12 18:45:49 +03:00
parent ac8eee48ec
commit 7620774dc4
3 changed files with 76 additions and 69 deletions

View File

@ -28,6 +28,7 @@ import {
updateChat, updateChat,
updateChatListSecondaryInfo, updateChatListSecondaryInfo,
updateManagementProgress, updateManagementProgress,
leaveChat,
} from '../../reducers'; } from '../../reducers';
import { import {
selectChat, selectChat,
@ -299,78 +300,78 @@ addReducer('joinChannel', (global, actions, payload) => {
}); });
addReducer('deleteChatUser', (global, actions, payload) => { addReducer('deleteChatUser', (global, actions, payload) => {
(async () => { const { chatId, userId }: { chatId: string; userId: string } = payload!;
const { chatId, userId }: { chatId: string; userId: string } = payload!; const chat = selectChat(global, chatId);
const chat = selectChat(global, chatId); const user = selectUser(global, userId);
const user = selectUser(global, userId); if (!chat || !user) {
if (!chat || !user) { return;
return; }
}
await callApi('deleteChatUser', { chat, user });
const activeChat = selectCurrentMessageList(global); global = leaveChat(global, chatId);
if (activeChat && activeChat.chatId === chatId && global.currentUserId === userId) { setGlobal(global);
actions.openChat({ id: undefined });
} if (selectCurrentMessageList(global)?.chatId === chatId) {
})(); actions.openChat({ id: undefined });
}
void callApi('deleteChatUser', { chat, user });
}); });
addReducer('deleteChat', (global, actions, payload) => { addReducer('deleteChat', (global, actions, payload) => {
(async () => { const { chatId }: { chatId: string } = payload!;
const { chatId }: { chatId: string } = payload!; const chat = selectChat(global, chatId);
const chat = selectChat(global, chatId); if (!chat) {
if (!chat) { return;
return; }
}
await callApi('deleteChat', { chatId: chat.id });
const activeChat = selectCurrentMessageList(global); global = leaveChat(global, chatId);
if (activeChat && activeChat.chatId === chatId) { setGlobal(global);
actions.openChat({ id: undefined });
} if (selectCurrentMessageList(global)?.chatId === chatId) {
})(); actions.openChat({ id: undefined });
}
void callApi('deleteChat', { chatId: chat.id });
}); });
addReducer('leaveChannel', (global, actions, payload) => { addReducer('leaveChannel', (global, actions, payload) => {
(async () => { const { chatId } = payload!;
const { chatId } = payload!; const chat = selectChat(global, chatId);
const chat = selectChat(global, chatId); if (!chat) {
if (!chat) { return;
return; }
}
const { id: channelId, accessHash } = chat; global = leaveChat(global, chatId);
setGlobal(global);
if (channelId && accessHash) { if (selectCurrentMessageList(global)?.chatId === chatId) {
await callApi('leaveChannel', { channelId, accessHash }); actions.openChat({ id: undefined });
} }
const activeChannel = selectCurrentMessageList(global); const { id: channelId, accessHash } = chat;
if (activeChannel && activeChannel.chatId === chatId) { if (channelId && accessHash) {
actions.openChat({ id: undefined }); void callApi('leaveChannel', { channelId, accessHash });
} }
})();
}); });
addReducer('deleteChannel', (global, actions, payload) => { addReducer('deleteChannel', (global, actions, payload) => {
(async () => { const { chatId } = payload!;
const { chatId } = payload!; const chat = selectChat(global, chatId);
const chat = selectChat(global, chatId); if (!chat) {
if (!chat) { return;
return; }
}
const { id: channelId, accessHash } = chat; global = leaveChat(global, chatId);
setGlobal(global);
if (channelId && accessHash) { if (selectCurrentMessageList(global)?.chatId === chatId) {
await callApi('deleteChannel', { channelId, accessHash }); actions.openChat({ id: undefined });
} }
const activeChannel = selectCurrentMessageList(global); const { id: channelId, accessHash } = chat;
if (activeChannel && activeChannel.chatId === chatId) { if (channelId && accessHash) {
actions.openChat({ id: undefined }); void callApi('deleteChannel', { channelId, accessHash });
} }
})();
}); });
addReducer('createGroupChat', (global, actions, payload) => { addReducer('createGroupChat', (global, actions, payload) => {

View File

@ -8,10 +8,10 @@ import { closeMessageNotifications, showNewMessageNotification } from '../../../
import { updateAppBadge } from '../../../util/appBadge'; import { updateAppBadge } from '../../../util/appBadge';
import { import {
updateChat, updateChat,
replaceChatListIds,
updateChatListIds, updateChatListIds,
updateChatListType, updateChatListType,
replaceThreadParam, replaceThreadParam,
leaveChat,
} from '../../reducers'; } from '../../reducers';
import { import {
selectChat, selectChat,
@ -70,19 +70,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => {
} }
case 'updateChatLeave': { case 'updateChatLeave': {
const listType = selectChatListType(global, update.id); setGlobal(leaveChat(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);
break; break;
} }

View File

@ -3,6 +3,7 @@ import { ApiChat, ApiPhoto } from '../../api/types';
import { ARCHIVED_FOLDER_ID } from '../../config'; import { ARCHIVED_FOLDER_ID } from '../../config';
import { omit } from '../../util/iteratees'; import { omit } from '../../util/iteratees';
import { selectChatListType } from '../selectors';
export function replaceChatListIds( export function replaceChatListIds(
global: GlobalState, 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;
}