[Refactoring] Simplify min constructor handling (#3343)
This commit is contained in:
parent
2dcca26aba
commit
86f8ad9e2f
@ -64,9 +64,9 @@ export function buildApiUser(mtpUser: GramJs.TypeUser): ApiUser | undefined {
|
|||||||
...(mtpUser.verified && { isVerified: true }),
|
...(mtpUser.verified && { isVerified: true }),
|
||||||
...((mtpUser.contact || mtpUser.mutualContact) && { isContact: true }),
|
...((mtpUser.contact || mtpUser.mutualContact) && { isContact: true }),
|
||||||
type: userType,
|
type: userType,
|
||||||
...(firstName && { firstName }),
|
firstName,
|
||||||
|
lastName,
|
||||||
...(userType === 'userTypeBot' && { canBeInvitedToGroup: !mtpUser.botNochats }),
|
...(userType === 'userTypeBot' && { canBeInvitedToGroup: !mtpUser.botNochats }),
|
||||||
...(lastName && { lastName }),
|
|
||||||
...(usernames && { usernames }),
|
...(usernames && { usernames }),
|
||||||
phoneNumber: mtpUser.phone || '',
|
phoneNumber: mtpUser.phone || '',
|
||||||
noStatus: !mtpUser.status,
|
noStatus: !mtpUser.status,
|
||||||
@ -120,7 +120,7 @@ export function buildApiUserEmojiStatus(mtpEmojiStatus: GramJs.TypeEmojiStatus):
|
|||||||
|
|
||||||
export function buildApiUsersAndStatuses(mtpUsers: GramJs.TypeUser[]) {
|
export function buildApiUsersAndStatuses(mtpUsers: GramJs.TypeUser[]) {
|
||||||
const userStatusesById: Record<string, ApiUserStatus> = {};
|
const userStatusesById: Record<string, ApiUserStatus> = {};
|
||||||
const users: ApiUser[] = [];
|
const usersById: Record<string, ApiUser> = {};
|
||||||
|
|
||||||
mtpUsers.forEach((mtpUser) => {
|
mtpUsers.forEach((mtpUser) => {
|
||||||
const user = buildApiUser(mtpUser);
|
const user = buildApiUser(mtpUser);
|
||||||
@ -128,14 +128,17 @@ export function buildApiUsersAndStatuses(mtpUsers: GramJs.TypeUser[]) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
users.push(user);
|
const duplicateUser = usersById[user.id];
|
||||||
|
if (!duplicateUser || duplicateUser.isMin) {
|
||||||
|
usersById[user.id] = user;
|
||||||
|
}
|
||||||
|
|
||||||
if ('status' in mtpUser) {
|
if ('status' in mtpUser) {
|
||||||
userStatusesById[user.id] = buildApiUserStatus(mtpUser.status);
|
userStatusesById[user.id] = buildApiUserStatus(mtpUser.status);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return { users, userStatusesById };
|
return { users: Object.values(usersById), userStatusesById };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildApiPremiumGiftOption(option: GramJs.TypePremiumGiftOption): ApiPremiumGiftOption {
|
export function buildApiPremiumGiftOption(option: GramJs.TypePremiumGiftOption): ApiPremiumGiftOption {
|
||||||
|
|||||||
@ -141,13 +141,11 @@ export function addChats<T extends GlobalState>(global: T, newById: Record<strin
|
|||||||
let isUpdated = false;
|
let isUpdated = false;
|
||||||
|
|
||||||
const addedById = Object.keys(newById).reduce<Record<string, ApiChat>>((acc, id) => {
|
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]);
|
||||||
const updatedChat = getUpdatedChat(global, id, newById[id]);
|
if (updatedChat) {
|
||||||
if (updatedChat) {
|
acc[id] = updatedChat;
|
||||||
acc[id] = updatedChat;
|
if (!isUpdated) {
|
||||||
if (!isUpdated) {
|
isUpdated = true;
|
||||||
isUpdated = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
@ -174,15 +172,8 @@ function getUpdatedChat<T extends GlobalState>(
|
|||||||
const chat = byId[chatId];
|
const chat = byId[chatId];
|
||||||
const omitProps: (keyof ApiChat)[] = [];
|
const omitProps: (keyof ApiChat)[] = [];
|
||||||
|
|
||||||
const shouldIgnoreUndefinedFields = chatUpdate.isMin && chat && !chat.isMin;
|
if (chatUpdate.isMin && chat && !chat.isMin) {
|
||||||
if (shouldIgnoreUndefinedFields) {
|
return undefined; // Do not apply updates from min constructor
|
||||||
omitProps.push('isMin', 'accessHash');
|
|
||||||
Object.keys(chatUpdate).forEach((key) => {
|
|
||||||
const prop = key as keyof ApiChat;
|
|
||||||
if (chatUpdate[prop] === undefined) {
|
|
||||||
omitProps.push(prop);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!noOmitUnreadReactionCount) {
|
if (!noOmitUnreadReactionCount) {
|
||||||
@ -197,7 +188,7 @@ function getUpdatedChat<T extends GlobalState>(
|
|||||||
...chat,
|
...chat,
|
||||||
...omit(chatUpdate, omitProps),
|
...omit(chatUpdate, omitProps),
|
||||||
...(photo && { photos: [photo, ...(chat.photos || [])] }),
|
...(photo && { photos: [photo, ...(chat.photos || [])] }),
|
||||||
};
|
} as ApiChat;
|
||||||
|
|
||||||
if (!updatedChat.id || !updatedChat.type) {
|
if (!updatedChat.id || !updatedChat.type) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
@ -83,13 +83,11 @@ export function addUsers<T extends GlobalState>(global: T, newById: Record<strin
|
|||||||
let isUpdated = false;
|
let isUpdated = false;
|
||||||
|
|
||||||
const addedById = Object.keys(newById).reduce<Record<string, ApiUser>>((acc, id) => {
|
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]);
|
||||||
const updatedUser = getUpdatedUser(global, id, newById[id]);
|
if (updatedUser) {
|
||||||
if (updatedUser) {
|
acc[id] = updatedUser;
|
||||||
acc[id] = updatedUser;
|
if (!isUpdated) {
|
||||||
if (!isUpdated) {
|
isUpdated = true;
|
||||||
isUpdated = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
@ -115,15 +113,8 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial
|
|||||||
const user = byId[userId];
|
const user = byId[userId];
|
||||||
const omitProps: (keyof ApiUser)[] = [];
|
const omitProps: (keyof ApiUser)[] = [];
|
||||||
|
|
||||||
const shouldIgnoreUndefinedFields = userUpdate.isMin && user && !user.isMin;
|
if (userUpdate.isMin && user && !user.isMin) {
|
||||||
if (shouldIgnoreUndefinedFields) {
|
return undefined; // Do not apply updates from min constructor
|
||||||
omitProps.push('isMin', 'accessHash');
|
|
||||||
Object.keys(userUpdate).forEach((key) => {
|
|
||||||
const prop = key as keyof ApiUser;
|
|
||||||
if (userUpdate[prop] === undefined) {
|
|
||||||
omitProps.push(prop);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (areDeepEqual(user?.usernames, userUpdate.usernames)) {
|
if (areDeepEqual(user?.usernames, userUpdate.usernames)) {
|
||||||
@ -133,7 +124,7 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial
|
|||||||
const updatedUser = {
|
const updatedUser = {
|
||||||
...user,
|
...user,
|
||||||
...omit(userUpdate, omitProps),
|
...omit(userUpdate, omitProps),
|
||||||
};
|
} as ApiUser;
|
||||||
|
|
||||||
if (!updatedUser.id || !updatedUser.type) {
|
if (!updatedUser.id || !updatedUser.type) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user