From 86f8ad9e2fd0d21765a24a70d2a91b4bc8c43b9f Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 18 Jun 2023 12:03:57 +0200 Subject: [PATCH] [Refactoring] Simplify `min` constructor handling (#3343) --- src/api/gramjs/apiBuilders/users.ts | 13 ++++++++----- src/global/reducers/chats.ts | 25 ++++++++----------------- src/global/reducers/users.ts | 25 ++++++++----------------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/api/gramjs/apiBuilders/users.ts b/src/api/gramjs/apiBuilders/users.ts index d624f9982..18f111b58 100644 --- a/src/api/gramjs/apiBuilders/users.ts +++ b/src/api/gramjs/apiBuilders/users.ts @@ -64,9 +64,9 @@ export function buildApiUser(mtpUser: GramJs.TypeUser): ApiUser | undefined { ...(mtpUser.verified && { isVerified: true }), ...((mtpUser.contact || mtpUser.mutualContact) && { isContact: true }), type: userType, - ...(firstName && { firstName }), + firstName, + lastName, ...(userType === 'userTypeBot' && { canBeInvitedToGroup: !mtpUser.botNochats }), - ...(lastName && { lastName }), ...(usernames && { usernames }), phoneNumber: mtpUser.phone || '', noStatus: !mtpUser.status, @@ -120,7 +120,7 @@ export function buildApiUserEmojiStatus(mtpEmojiStatus: GramJs.TypeEmojiStatus): export function buildApiUsersAndStatuses(mtpUsers: GramJs.TypeUser[]) { const userStatusesById: Record = {}; - const users: ApiUser[] = []; + const usersById: Record = {}; mtpUsers.forEach((mtpUser) => { const user = buildApiUser(mtpUser); @@ -128,14 +128,17 @@ export function buildApiUsersAndStatuses(mtpUsers: GramJs.TypeUser[]) { return; } - users.push(user); + const duplicateUser = usersById[user.id]; + if (!duplicateUser || duplicateUser.isMin) { + usersById[user.id] = user; + } if ('status' in mtpUser) { userStatusesById[user.id] = buildApiUserStatus(mtpUser.status); } }); - return { users, userStatusesById }; + return { users: Object.values(usersById), userStatusesById }; } export function buildApiPremiumGiftOption(option: GramJs.TypePremiumGiftOption): ApiPremiumGiftOption { diff --git a/src/global/reducers/chats.ts b/src/global/reducers/chats.ts index f5ba76d85..ffc2fa865 100644 --- a/src/global/reducers/chats.ts +++ b/src/global/reducers/chats.ts @@ -141,13 +141,11 @@ export function addChats(global: T, newById: Record>((acc, id) => { - if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { - const updatedChat = getUpdatedChat(global, id, newById[id]); - if (updatedChat) { - acc[id] = updatedChat; - if (!isUpdated) { - isUpdated = true; - } + const updatedChat = getUpdatedChat(global, id, newById[id]); + if (updatedChat) { + acc[id] = updatedChat; + if (!isUpdated) { + isUpdated = true; } } return acc; @@ -174,15 +172,8 @@ function getUpdatedChat( const chat = byId[chatId]; const omitProps: (keyof ApiChat)[] = []; - const shouldIgnoreUndefinedFields = chatUpdate.isMin && chat && !chat.isMin; - if (shouldIgnoreUndefinedFields) { - omitProps.push('isMin', 'accessHash'); - Object.keys(chatUpdate).forEach((key) => { - const prop = key as keyof ApiChat; - if (chatUpdate[prop] === undefined) { - omitProps.push(prop); - } - }); + if (chatUpdate.isMin && chat && !chat.isMin) { + return undefined; // Do not apply updates from min constructor } if (!noOmitUnreadReactionCount) { @@ -197,7 +188,7 @@ function getUpdatedChat( ...chat, ...omit(chatUpdate, omitProps), ...(photo && { photos: [photo, ...(chat.photos || [])] }), - }; + } as ApiChat; if (!updatedChat.id || !updatedChat.type) { return undefined; diff --git a/src/global/reducers/users.ts b/src/global/reducers/users.ts index 6e14b5ba8..725fd2a9e 100644 --- a/src/global/reducers/users.ts +++ b/src/global/reducers/users.ts @@ -83,13 +83,11 @@ export function addUsers(global: T, newById: Record>((acc, id) => { - if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { - const updatedUser = getUpdatedUser(global, id, newById[id]); - if (updatedUser) { - acc[id] = updatedUser; - if (!isUpdated) { - isUpdated = true; - } + const updatedUser = getUpdatedUser(global, id, newById[id]); + if (updatedUser) { + acc[id] = updatedUser; + if (!isUpdated) { + isUpdated = true; } } return acc; @@ -115,15 +113,8 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial const user = byId[userId]; const omitProps: (keyof ApiUser)[] = []; - const shouldIgnoreUndefinedFields = userUpdate.isMin && user && !user.isMin; - if (shouldIgnoreUndefinedFields) { - omitProps.push('isMin', 'accessHash'); - Object.keys(userUpdate).forEach((key) => { - const prop = key as keyof ApiUser; - if (userUpdate[prop] === undefined) { - omitProps.push(prop); - } - }); + if (userUpdate.isMin && user && !user.isMin) { + return undefined; // Do not apply updates from min constructor } if (areDeepEqual(user?.usernames, userUpdate.usernames)) { @@ -133,7 +124,7 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial const updatedUser = { ...user, ...omit(userUpdate, omitProps), - }; + } as ApiUser; if (!updatedUser.id || !updatedUser.type) { return undefined;