From 7ea9dd116f93cabfaede10987bf68331ea8cd2a1 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 19 Nov 2021 03:22:30 +0300 Subject: [PATCH] New Chat: Fix freezing when users disable adding them to groups (#1552) --- src/api/gramjs/methods/chats.ts | 2 +- src/components/left/newChat/NewChatStep2.tsx | 2 +- src/modules/actions/api/chats.ts | 61 ++++++++++++++------ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/api/gramjs/methods/chats.ts b/src/api/gramjs/methods/chats.ts index 2d80e84bd..aae826131 100644 --- a/src/api/gramjs/methods/chats.ts +++ b/src/api/gramjs/methods/chats.ts @@ -567,7 +567,7 @@ export async function createGroupChat({ const result = await invokeRequest(new GramJs.messages.CreateChat({ title, users: users.map(({ id, accessHash }) => buildInputEntity(id, accessHash)) as GramJs.InputUser[], - }), true); + }), true, true); // `createChat` can return a lot of different update types according to docs, // but currently chat creation returns only `Updates` type. diff --git a/src/components/left/newChat/NewChatStep2.tsx b/src/components/left/newChat/NewChatStep2.tsx index 92e7b02f3..e5a4a7478 100644 --- a/src/components/left/newChat/NewChatStep2.tsx +++ b/src/components/left/newChat/NewChatStep2.tsx @@ -113,7 +113,7 @@ const NewChatStep2: FC = ({ } }, [creationProgress, onReset]); - const renderedError = creationError || ( + const renderedError = (creationError && lang(creationError)) || ( error !== chatTitleEmptyError && error !== channelTitleEmptyError ? error : undefined diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index 12a2ea9a3..5c359efec 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -1088,27 +1088,52 @@ async function createGroupChat(title: string, users: ApiUser[], photo?: File) { }, }); - const createdChat = await callApi('createGroupChat', { title, users }); - if (!createdChat) { - return; - } + try { + const createdChat = await callApi('createGroupChat', { + title, + users, + }); - const { id: chatId } = createdChat; + if (!createdChat) { + return; + } - let global = getGlobal(); - global = updateChat(global, chatId, createdChat); - global = { - ...global, - chatCreation: { - ...global.chatCreation, - progress: createdChat ? ChatCreationProgress.Complete : ChatCreationProgress.Error, - }, - }; - setGlobal(global); - getDispatch().openChat({ id: chatId, shouldReplaceHistory: true }); + const { id: chatId } = createdChat; - if (chatId && photo) { - await callApi('editChatPhoto', { chatId, photo }); + let global = getGlobal(); + global = updateChat(global, chatId, createdChat); + global = { + ...global, + chatCreation: { + ...global.chatCreation, + progress: createdChat ? ChatCreationProgress.Complete : ChatCreationProgress.Error, + }, + }; + setGlobal(global); + getDispatch() + .openChat({ + id: chatId, + shouldReplaceHistory: true, + }); + + if (chatId && photo) { + await callApi('editChatPhoto', { + chatId, + photo, + }); + } + } catch (e) { + if (e.message === 'USERS_TOO_FEW') { + const global = getGlobal(); + setGlobal({ + ...global, + chatCreation: { + ...global.chatCreation, + progress: ChatCreationProgress.Error, + error: 'CreateGroupError', + }, + }); + } } }