Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.2 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*
* Originally from:
* https://github.com/zhukov/webogram
* Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
* https://github.com/zhukov/webogram/blob/master/LICENSE
*/
import { ReferenceContext } from "../mtproto/referenceDatabase";
import { WebPage } from "../../layer";
import safeReplaceObject from "../../helpers/object/safeReplaceObject";
3 years ago
import { AppManager } from "./manager";
3 years ago
const photoTypeSet = new Set(['photo', 'video', 'gif', 'document']);
3 years ago
type WebPageMessageKey = `${PeerId}_${number}`;
3 years ago
export class AppWebPagesManager extends AppManager {
3 years ago
private webpages: {
[webPageId: string]: WebPage
} = {};
private pendingWebPages: {
3 years ago
[webPageId: string]: Set<WebPageMessageKey>
3 years ago
} = {};
protected after() {
this.apiUpdatesManager.addMultipleEventsListeners({
3 years ago
updateWebPage: (update) => {
this.saveWebPage(update.webpage);
}
});
}
3 years ago
public saveWebPage(apiWebPage: WebPage, messageKey?: WebPageMessageKey, mediaContext?: ReferenceContext) {
3 years ago
if(apiWebPage._ === 'webPageNotModified') return;
const {id} = apiWebPage;
const oldWebPage = this.webpages[id];
const isUpdated = oldWebPage &&
oldWebPage._ === apiWebPage._ &&
(oldWebPage as WebPage.webPage).hash === (oldWebPage as WebPage.webPage).hash;
3 years ago
if(apiWebPage._ === 'webPage') {
if(apiWebPage.photo?._ === 'photo') {
3 years ago
apiWebPage.photo = this.appPhotosManager.savePhoto(apiWebPage.photo, mediaContext);
3 years ago
} else {
delete apiWebPage.photo;
}
if(apiWebPage.document?._ === 'document') {
3 years ago
apiWebPage.document = this.appDocsManager.saveDoc(apiWebPage.document, mediaContext);
3 years ago
} else {
if(apiWebPage.type === 'document') {
delete apiWebPage.type;
}
delete apiWebPage.document;
}
const siteName = apiWebPage.site_name;
let shortTitle = apiWebPage.title || apiWebPage.author || siteName || '';
if(siteName && shortTitle === siteName) {
delete apiWebPage.site_name;
}
// delete apiWebPage.description
if(!photoTypeSet.has(apiWebPage.type) &&
!apiWebPage.description &&
apiWebPage.photo) {
apiWebPage.type = 'photo';
}
}
let pendingSet = this.pendingWebPages[id];
if(messageKey) {
if(!pendingSet) pendingSet = this.pendingWebPages[id] = new Set();
pendingSet.add(messageKey);
}
if(oldWebPage === undefined) {
3 years ago
this.webpages[id] = apiWebPage;
} else {
safeReplaceObject(oldWebPage, apiWebPage);
3 years ago
}
if(!messageKey && pendingSet !== undefined && isUpdated) {
3 years ago
const msgs: {peerId: PeerId, mid: number, isScheduled: boolean}[] = [];
3 years ago
pendingSet.forEach((value) => {
3 years ago
const [peerId, mid, isScheduled] = value.split('_');
3 years ago
msgs.push({
3 years ago
peerId: peerId.toPeerId(),
mid: +mid,
isScheduled: !!isScheduled
3 years ago
});
});
this.rootScope.dispatchEvent('webpage_updated', {
3 years ago
id,
msgs
});
}
return apiWebPage;
}
3 years ago
public getMessageKeyForPendingWebPage(peerId: PeerId, mid: number, isScheduled?: boolean): WebPageMessageKey {
return peerId + '_' + mid + (isScheduled ? '_s' : '') as any;
3 years ago
}
3 years ago
public deleteWebPageFromPending(webPage: WebPage, messageKey: WebPageMessageKey) {
3 years ago
const id = (webPage as WebPage.webPage).id;
if(!id) return;
const set = this.pendingWebPages[id];
if(set && set.has(messageKey)) {
set.delete(messageKey);
if(!set.size) {
delete this.pendingWebPages[id];
}
}
}
public getCachedWebPage(id: WebPage.webPage['id']) {
3 years ago
return this.webpages[id];
}
public getWebPage(url: string) {
return this.apiManager.invokeApiHashable({
method: 'messages.getWebPage',
processResult: (webPage) => {
return this.saveWebPage(webPage);
},
params: {
url
},
});
}
3 years ago
}