Browse Source

Fix unneeded saving webPage and stickers

master
morethanwords 3 years ago
parent
commit
a9c70efda6
  1. 11
      src/components/chat/input.ts
  2. 2
      src/components/sidebarLeft/tabs/background.ts
  3. 39
      src/lib/appManagers/appStickersManager.ts
  4. 24
      src/lib/mtproto/mtprotoworker.ts

11
src/components/chat/input.ts

@ -1494,10 +1494,15 @@ export default class ChatInput { @@ -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') {

2
src/components/sidebarLeft/tabs/background.ts

@ -85,7 +85,7 @@ export default class AppBackgroundTab extends SliderSuperTab { @@ -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);

39
src/lib/appManagers/appStickersManager.ts

@ -18,6 +18,7 @@ import lottieLoader from '../rlottie/lottieLoader'; @@ -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 { @@ -137,9 +138,14 @@ export class AppStickersManager {
public async getRecentStickers(): Promise<Modify<MessagesRecentStickers.messagesRecentStickers, {
stickers: Document[]
}>> {
const res = await apiManager.invokeApiHashable('messages.getRecentStickers') as MessagesRecentStickers.messagesRecentStickers;
const res = await apiManager.invokeApiHashable({
method: 'messages.getRecentStickers',
processResult: (res) => {
assumeType<MessagesRecentStickers.messagesRecentStickers>(res);
this.saveStickers(res.stickers);
return res;
}
});
return res;
}
@ -260,12 +266,18 @@ export class AppStickersManager { @@ -260,12 +266,18 @@ export class AppStickersManager {
}
public async getFeaturedStickers() {
const res = await apiManager.invokeApiHashable('messages.getFeaturedStickers') as MessagesFeaturedStickers.messagesFeaturedStickers;
const res = await apiManager.invokeApiHashable({
method: 'messages.getFeaturedStickers',
processResult: (res) => {
assumeType<MessagesFeaturedStickers.messagesFeaturedStickers>(res);
res.sets.forEach(covered => {
this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id);
});
return res;
}
});
return res.sets;
}
@ -299,16 +311,24 @@ export class AppStickersManager { @@ -299,16 +311,24 @@ export class AppStickersManager {
public async searchStickerSets(query: string, excludeFeatured = true) {
const flags = excludeFeatured ? 1 : 0;
const res = await apiManager.invokeApiHashable('messages.searchStickerSets', {
const res = await apiManager.invokeApiHashable({
method: 'messages.searchStickerSets',
params: {
flags,
exclude_featured: excludeFeatured || undefined,
q: query
}) as MessagesFoundStickerSets.messagesFoundStickerSets;
},
processResult: (res) => {
assumeType<MessagesFoundStickerSets.messagesFoundStickerSets>(res);
res.sets.forEach(covered => {
this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id);
});
return res;
}
});
const foundSaved: StickerSetCovered[] = [];
const cache = this.storage.getCache();
for(let id in cache) {
@ -323,7 +343,7 @@ export class AppStickersManager { @@ -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 { @@ -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', {
apiManager.invokeApiHashable({
method: 'messages.getStickers',
params: {
emoticon
}
}),
includeOurStickers ? this.preloadStickerSets() : [],
includeOurStickers ? this.getRecentStickers() : undefined

24
src/lib/mtproto/mtprotoworker.ts

@ -460,9 +460,20 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -460,9 +460,20 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
return this.invokeApi(method, params, o);
}
public invokeApiHashable<T extends keyof MethodDeclMap>(method: T, params: Omit<MethodDeclMap[T]['req'], 'hash'> = {} as any, options: InvokeApiOptions = {}): Promise<MethodDeclMap[T]['res']> {
public invokeApiHashable<T extends keyof MethodDeclMap, R>(o: {
method: T,
processResult?: (response: MethodDeclMap[T]['res']) => R,
processError?: (error: ApiError) => any,
params?: Omit<MethodDeclMap[T]['req'], 'hash'>,
options?: InvokeApiOptions & {cacheKey?: string}
}): Promise<R> {
// @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,7 +483,9 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -472,7 +483,9 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}
}
return this.invokeApi(method, params, options).then((result: any) => {
return this.invokeApiSingleProcess({
method,
processResult: (result) => {
if(result._.includes('NotModified')) {
this.debug && this.log.warn('NotModified saved!', method, queryJSON);
return cached.result;
@ -488,7 +501,14 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -488,7 +501,14 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
};
}
if(o.processResult) {
return o.processResult(result);
}
return result;
},
params,
options
});
}

Loading…
Cancel
Save