From a9c70efda6c14b27b80174f171cc740d0bae0e55 Mon Sep 17 00:00:00 2001 From: morethanwords Date: Fri, 14 Jan 2022 18:24:09 +0400 Subject: [PATCH] Fix unneeded saving webPage and stickers --- src/components/chat/input.ts | 11 +++- src/components/sidebarLeft/tabs/background.ts | 2 +- src/lib/appManagers/appStickersManager.ts | 59 +++++++++++++------ src/lib/mtproto/mtprotoworker.ts | 52 +++++++++++----- 4 files changed, 86 insertions(+), 38 deletions(-) diff --git a/src/components/chat/input.ts b/src/components/chat/input.ts index bd1230c6..c2ac2972 100644 --- a/src/components/chat/input.ts +++ b/src/components/chat/input.ts @@ -1494,10 +1494,15 @@ export default class ChatInput { if(this.lastUrl !== url) { this.lastUrl = url; // this.willSendWebPage = null; - const promise = this.getWebPagePromise = apiManager.invokeApiHashable('messages.getWebPage', { - url, + const promise = this.getWebPagePromise = apiManager.invokeApiHashable({ + method: 'messages.getWebPage', + processResult: (webPage) => { + return this.appWebPagesManager.saveWebPage(webPage); + }, + params: { + url + }, }).then((webpage) => { - webpage = this.appWebPagesManager.saveWebPage(webpage); if(this.getWebPagePromise === promise) this.getWebPagePromise = undefined; if(this.lastUrl !== url) return; if(webpage._ === 'webPage') { diff --git a/src/components/sidebarLeft/tabs/background.ts b/src/components/sidebarLeft/tabs/background.ts index 7a97863e..287a83b0 100644 --- a/src/components/sidebarLeft/tabs/background.ts +++ b/src/components/sidebarLeft/tabs/background.ts @@ -85,7 +85,7 @@ export default class AppBackgroundTab extends SliderSuperTab { rootScope.addEventListener('background_change', this.setActive); - apiManager.invokeApiHashable('account.getWallPapers').then((accountWallpapers) => { + apiManager.invokeApiHashable({method: 'account.getWallPapers'}).then((accountWallpapers) => { const wallpapers = (accountWallpapers as AccountWallPapers.accountWallPapers).wallpapers as WallPaper.wallPaper[]; wallpapers.forEach((wallpaper) => { this.addWallPaper(wallpaper); diff --git a/src/lib/appManagers/appStickersManager.ts b/src/lib/appManagers/appStickersManager.ts index eb924a38..385b1322 100644 --- a/src/lib/appManagers/appStickersManager.ts +++ b/src/lib/appManagers/appStickersManager.ts @@ -18,6 +18,7 @@ import lottieLoader from '../rlottie/lottieLoader'; import mediaSizes from '../../helpers/mediaSizes'; import { getEmojiToneIndex } from '../../vendor/emoji'; import RichTextProcessor from '../richtextprocessor'; +import assumeType from '../../helpers/assumeType'; const CACHE_TIME = 3600e3; @@ -137,9 +138,14 @@ export class AppStickersManager { public async getRecentStickers(): Promise> { - const res = await apiManager.invokeApiHashable('messages.getRecentStickers') as MessagesRecentStickers.messagesRecentStickers; - - this.saveStickers(res.stickers); + const res = await apiManager.invokeApiHashable({ + method: 'messages.getRecentStickers', + processResult: (res) => { + assumeType(res); + this.saveStickers(res.stickers); + return res; + } + }); return res; } @@ -260,10 +266,16 @@ export class AppStickersManager { } public async getFeaturedStickers() { - const res = await apiManager.invokeApiHashable('messages.getFeaturedStickers') as MessagesFeaturedStickers.messagesFeaturedStickers; - - res.sets.forEach(covered => { - this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); + const res = await apiManager.invokeApiHashable({ + method: 'messages.getFeaturedStickers', + processResult: (res) => { + assumeType(res); + res.sets.forEach(covered => { + this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); + }); + + return res; + } }); return res.sets; @@ -299,14 +311,22 @@ export class AppStickersManager { public async searchStickerSets(query: string, excludeFeatured = true) { const flags = excludeFeatured ? 1 : 0; - const res = await apiManager.invokeApiHashable('messages.searchStickerSets', { - flags, - exclude_featured: excludeFeatured || undefined, - q: query - }) as MessagesFoundStickerSets.messagesFoundStickerSets; - - res.sets.forEach(covered => { - this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); + const res = await apiManager.invokeApiHashable({ + method: 'messages.searchStickerSets', + params: { + flags, + exclude_featured: excludeFeatured || undefined, + q: query + }, + processResult: (res) => { + assumeType(res); + + res.sets.forEach(covered => { + this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); + }); + + return res; + } }); const foundSaved: StickerSetCovered[] = []; @@ -323,7 +343,7 @@ export class AppStickersManager { } public getAllStickers() { - return apiManager.invokeApiHashable('messages.getAllStickers'); + return apiManager.invokeApiHashable({method: 'messages.getAllStickers'}); } public preloadStickerSets() { @@ -338,8 +358,11 @@ export class AppStickersManager { if(this.getStickersByEmoticonsPromises[emoticon]) return this.getStickersByEmoticonsPromises[emoticon]; return this.getStickersByEmoticonsPromises[emoticon] = Promise.all([ - apiManager.invokeApiHashable('messages.getStickers', { - emoticon + apiManager.invokeApiHashable({ + method: 'messages.getStickers', + params: { + emoticon + } }), includeOurStickers ? this.preloadStickerSets() : [], includeOurStickers ? this.getRecentStickers() : undefined diff --git a/src/lib/mtproto/mtprotoworker.ts b/src/lib/mtproto/mtprotoworker.ts index 162b0f8f..f7e9e984 100644 --- a/src/lib/mtproto/mtprotoworker.ts +++ b/src/lib/mtproto/mtprotoworker.ts @@ -460,9 +460,20 @@ export class ApiManagerProxy extends CryptoWorkerMethods { return this.invokeApi(method, params, o); } - public invokeApiHashable(method: T, params: Omit = {} as any, options: InvokeApiOptions = {}): Promise { + public invokeApiHashable(o: { + method: T, + processResult?: (response: MethodDeclMap[T]['res']) => R, + processError?: (error: ApiError) => any, + params?: Omit, + options?: InvokeApiOptions & {cacheKey?: string} + }): Promise { + // @ts-ignore + o.params ??= {}; + o.options ??= {}; //console.log('will invokeApi:', method, params, options); + const {params, options, method} = o; + const queryJSON = JSON.stringify(params); let cached: HashResult; if(this.hashes[method]) { @@ -472,23 +483,32 @@ export class ApiManagerProxy extends CryptoWorkerMethods { } } - return this.invokeApi(method, params, options).then((result: any) => { - if(result._.includes('NotModified')) { - this.debug && this.log.warn('NotModified saved!', method, queryJSON); - return cached.result; - } - - if(result.hash/* || result.messages */) { - const hash = result.hash/* || this.computeHash(result.messages) */; + return this.invokeApiSingleProcess({ + method, + processResult: (result) => { + if(result._.includes('NotModified')) { + this.debug && this.log.warn('NotModified saved!', method, queryJSON); + return cached.result; + } - if(!this.hashes[method]) this.hashes[method] = {}; - this.hashes[method][queryJSON] = { - hash, - result - }; - } + if(result.hash/* || result.messages */) { + const hash = result.hash/* || this.computeHash(result.messages) */; + + if(!this.hashes[method]) this.hashes[method] = {}; + this.hashes[method][queryJSON] = { + hash, + result + }; + } - return result; + if(o.processResult) { + return o.processResult(result); + } + + return result; + }, + params, + options }); }