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 {
if(this.lastUrl !== url) { if(this.lastUrl !== url) {
this.lastUrl = url; this.lastUrl = url;
// this.willSendWebPage = null; // this.willSendWebPage = null;
const promise = this.getWebPagePromise = apiManager.invokeApiHashable('messages.getWebPage', { const promise = this.getWebPagePromise = apiManager.invokeApiHashable({
url, method: 'messages.getWebPage',
processResult: (webPage) => {
return this.appWebPagesManager.saveWebPage(webPage);
},
params: {
url
},
}).then((webpage) => { }).then((webpage) => {
webpage = this.appWebPagesManager.saveWebPage(webpage);
if(this.getWebPagePromise === promise) this.getWebPagePromise = undefined; if(this.getWebPagePromise === promise) this.getWebPagePromise = undefined;
if(this.lastUrl !== url) return; if(this.lastUrl !== url) return;
if(webpage._ === 'webPage') { if(webpage._ === 'webPage') {

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

@ -85,7 +85,7 @@ export default class AppBackgroundTab extends SliderSuperTab {
rootScope.addEventListener('background_change', this.setActive); 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[]; const wallpapers = (accountWallpapers as AccountWallPapers.accountWallPapers).wallpapers as WallPaper.wallPaper[];
wallpapers.forEach((wallpaper) => { wallpapers.forEach((wallpaper) => {
this.addWallPaper(wallpaper); this.addWallPaper(wallpaper);

39
src/lib/appManagers/appStickersManager.ts

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

24
src/lib/mtproto/mtprotoworker.ts

@ -460,9 +460,20 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
return this.invokeApi(method, params, o); 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); //console.log('will invokeApi:', method, params, options);
const {params, options, method} = o;
const queryJSON = JSON.stringify(params); const queryJSON = JSON.stringify(params);
let cached: HashResult; let cached: HashResult;
if(this.hashes[method]) { if(this.hashes[method]) {
@ -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')) { if(result._.includes('NotModified')) {
this.debug && this.log.warn('NotModified saved!', method, queryJSON); this.debug && this.log.warn('NotModified saved!', method, queryJSON);
return cached.result; return cached.result;
@ -488,7 +501,14 @@ export class ApiManagerProxy extends CryptoWorkerMethods {
}; };
} }
if(o.processResult) {
return o.processResult(result);
}
return result; return result;
},
params,
options
}); });
} }

Loading…
Cancel
Save