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. 59
      src/lib/appManagers/appStickersManager.ts
  4. 52
      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);

59
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;
this.saveStickers(res.stickers);
const res = await apiManager.invokeApiHashable({
method: 'messages.getRecentStickers',
processResult: (res) => {
assumeType<MessagesRecentStickers.messagesRecentStickers>(res);
this.saveStickers(res.stickers);
return res;
}
});
return res;
}
@ -260,10 +266,16 @@ export class AppStickersManager { @@ -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<MessagesFeaturedStickers.messagesFeaturedStickers>(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 { @@ -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<MessagesFoundStickerSets.messagesFoundStickerSets>(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 { @@ -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', {
emoticon
apiManager.invokeApiHashable({
method: 'messages.getStickers',
params: {
emoticon
}
}),
includeOurStickers ? this.preloadStickerSets() : [],
includeOurStickers ? this.getRecentStickers() : undefined

52
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,23 +483,32 @@ export class ApiManagerProxy extends CryptoWorkerMethods { @@ -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
});
}

Loading…
Cancel
Save