From 77cdea4f757db4a45d5550e6b807922ee5d8a306 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 25 Jan 2022 03:24:43 +0100 Subject: [PATCH] [Refactoring] API: Ignore redundant updates from `fetchNotificationExceptions` --- src/api/gramjs/methods/client.ts | 6 +++++- src/api/gramjs/methods/messages.ts | 15 +++------------ src/api/gramjs/methods/settings.ts | 20 +++++++------------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 21705fa7..42d512a9 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -169,12 +169,14 @@ export async function invokeRequest( request: T, shouldReturnTrue?: boolean, shouldThrow?: boolean, + shouldIgnoreUpdates?: boolean, ): Promise; export async function invokeRequest( request: T, shouldReturnTrue = false, shouldThrow = false, + shouldIgnoreUpdates = false, ) { if (!isConnected) { if (DEBUG) { @@ -198,7 +200,9 @@ export async function invokeRequest( console.log(`[GramJs/client] INVOKE RESPONSE ${request.className}`, result); } - handleUpdatesFromRequest(request, result); + if (!shouldIgnoreUpdates) { + handleUpdatesFromRequest(request, result); + } return shouldReturnTrue ? result && true : result; } catch (err) { diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index 93af244f..23f724ce 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -61,7 +61,7 @@ import { } from '../helpers'; import { interpolateArray } from '../../../util/waveform'; import { requestChatUpdate } from './chats'; -import { buildApiPeerId, getApiChatIdFromMtpPeer } from '../apiBuilders/peers'; +import { getApiChatIdFromMtpPeer } from '../apiBuilders/peers'; const FAST_SEND_TIMEOUT = 1000; const INPUT_WAVEFORM_LENGTH = 63; @@ -1172,17 +1172,8 @@ function updateLocalDb(result: ( GramJs.messages.MessagesSlice | GramJs.messages.Messages | GramJs.messages.ChannelMessages | GramJs.messages.DiscussionMessage | GramJs.messages.SponsoredMessages )) { - result.users.forEach((user) => { - if (user instanceof GramJs.User) { - localDb.users[buildApiPeerId(user.id, 'user')] = user; - } - }); - - result.chats.forEach((chat) => { - if (chat instanceof GramJs.Chat || chat instanceof GramJs.Channel) { - localDb.chats[buildApiPeerId(chat.id, chat instanceof GramJs.Chat ? 'chat' : 'channel')] = chat; - } - }); + addEntitiesWithPhotosToLocalDb(result.users); + addEntitiesWithPhotosToLocalDb(result.chats); result.messages.forEach((message) => { if ((message instanceof GramJs.Message && isMessageWithMedia(message)) diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index 4839de37..f8fa14f6 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -28,9 +28,10 @@ import { getClient, invokeRequest, uploadFile } from './client'; import { omitVirtualClassFields } from '../apiBuilders/helpers'; import { buildCollectionByKey } from '../../../util/iteratees'; import { getServerTime } from '../../../util/serverTime'; -import { buildApiPeerId, getApiChatIdFromMtpPeer } from '../apiBuilders/peers'; +import { getApiChatIdFromMtpPeer } from '../apiBuilders/peers'; import localDb from '../localDb'; import { buildApiConfig } from '../apiBuilders/appConfig'; +import { addEntitiesWithPhotosToLocalDb } from '../helpers'; const MAX_INT_32 = 2 ** 31 - 1; const BETA_LANG_CODES = ['ar', 'fa', 'id', 'ko', 'uz']; @@ -173,7 +174,9 @@ export function terminateAllAuthorizations() { export async function fetchNotificationExceptions({ serverTimeOffset, }: { serverTimeOffset: number }) { - const result = await invokeRequest(new GramJs.account.GetNotifyExceptions({ compareSound: true })); + const result = await invokeRequest(new GramJs.account.GetNotifyExceptions({ + compareSound: true, + }), undefined, undefined, true); if (!(result instanceof GramJs.Updates || result instanceof GramJs.UpdatesCombined)) { return undefined; @@ -453,17 +456,8 @@ function updateLocalDb( GramJs.Updates | GramJs.UpdatesCombined ), ) { - result.users.forEach((user) => { - if (user instanceof GramJs.User) { - localDb.users[buildApiPeerId(user.id, 'user')] = user; - } - }); - - result.chats.forEach((chat) => { - if (chat instanceof GramJs.Chat || chat instanceof GramJs.Channel) { - localDb.chats[buildApiPeerId(chat.id, chat instanceof GramJs.Chat ? 'chat' : 'channel')] = chat; - } - }); + addEntitiesWithPhotosToLocalDb(result.users); + addEntitiesWithPhotosToLocalDb(result.chats); } export async function fetchCountryList({ langCode = 'en' }: { langCode?: LangCode }) {