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.

81 lines
2.4 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2 years ago
import {UserProfilePhoto, ChatPhoto, InputFileLocation} from '../../layer';
import {DownloadOptions} from '../mtproto/apiFileManager';
import {AppManager} from './manager';
3 years ago
2 years ago
export type PeerPhotoSize = 'photo_small' | 'photo_big';
3 years ago
2 years ago
export class AppAvatarsManager extends AppManager {
3 years ago
private savedAvatarURLs: {
3 years ago
[peerId: PeerId]: {
3 years ago
[size in PeerPhotoSize]?: string | Promise<string>
}
} = {};
2 years ago
protected after() {
this.rootScope.addEventListener('avatar_update', (peerId) => {
2 years ago
this.removeFromAvatarsCache(peerId);
});
}
3 years ago
public isAvatarCached(peerId: PeerId, size?: PeerPhotoSize) {
const saved = this.savedAvatarURLs[peerId];
if(size === undefined) {
return !!saved;
}
return !!(saved && saved[size] && !(saved[size] instanceof Promise));
3 years ago
}
2 years ago
3 years ago
public removeFromAvatarsCache(peerId: PeerId) {
3 years ago
if(this.savedAvatarURLs[peerId]) {
delete this.savedAvatarURLs[peerId];
}
}
3 years ago
public loadAvatar(peerId: PeerId, photo: UserProfilePhoto.userProfilePhoto | ChatPhoto.chatPhoto, size: PeerPhotoSize) {
3 years ago
let saved = this.savedAvatarURLs[peerId];
if(!saved || !saved[size]) {
if(!saved) {
saved = this.savedAvatarURLs[peerId] = {};
}
2 years ago
// console.warn('will invoke downloadSmallFile:', peerId);
3 years ago
const peerPhotoFileLocation: InputFileLocation.inputPeerPhotoFileLocation = {
2 years ago
_: 'inputPeerPhotoFileLocation',
3 years ago
pFlags: {},
2 years ago
peer: this.appPeersManager.getInputPeerById(peerId),
3 years ago
photo_id: photo.photo_id
};
3 years ago
const downloadOptions: DownloadOptions = {dcId: photo.dc_id, location: peerPhotoFileLocation};
3 years ago
if(size === 'photo_big') {
peerPhotoFileLocation.pFlags.big = true;
3 years ago
downloadOptions.limitPart = 512 * 1024;
3 years ago
}
/* let str: string;
const time = Date.now();
if(peerId === 0) {
str = `download avatar ${peerId}`;
} */
const promise = this.apiFileManager.download(downloadOptions);
return saved[size] = promise.then((blob) => {
3 years ago
return saved[size] = URL.createObjectURL(blob);
/* if(str) {
console.log(str, Date.now() / 1000, Date.now() - time);
} */
});
} else {
return saved[size];
3 years ago
}
}
}