[Refactoring] Simplify min constructor handling (#3343)

This commit is contained in:
Alexander Zinchuk 2023-06-18 12:03:57 +02:00
parent 2dcca26aba
commit 86f8ad9e2f
3 changed files with 24 additions and 39 deletions

View File

@ -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<string, ApiUserStatus> = {};
const users: ApiUser[] = [];
const usersById: Record<string, ApiUser> = {};
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 {

View File

@ -141,13 +141,11 @@ export function addChats<T extends GlobalState>(global: T, newById: Record<strin
let isUpdated = false;
const addedById = Object.keys(newById).reduce<Record<string, ApiChat>>((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<T extends GlobalState>(
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<T extends GlobalState>(
...chat,
...omit(chatUpdate, omitProps),
...(photo && { photos: [photo, ...(chat.photos || [])] }),
};
} as ApiChat;
if (!updatedChat.id || !updatedChat.type) {
return undefined;

View File

@ -83,13 +83,11 @@ export function addUsers<T extends GlobalState>(global: T, newById: Record<strin
let isUpdated = false;
const addedById = Object.keys(newById).reduce<Record<string, ApiUser>>((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;