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.

224 lines
6.9 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import { renderImageFromUrlPromise } from "../../helpers/dom/renderImageFromUrl";
3 years ago
import replaceContent from "../../helpers/dom/replaceContent";
import sequentialDom from "../../helpers/sequentialDom";
import { UserProfilePhoto, ChatPhoto, InputFileLocation } from "../../layer";
3 years ago
import { NULL_PEER_ID, REPLIES_PEER_ID } from "../mtproto/mtproto_config";
3 years ago
import RichTextProcessor from "../richtextprocessor";
import rootScope from "../rootScope";
import appDownloadManager from "./appDownloadManager";
import appPeersManager from "./appPeersManager";
import appPhotosManager from "./appPhotosManager";
import appUsersManager from "./appUsersManager";
type PeerPhotoSize = 'photo_small' | 'photo_big';
export class AppAvatarsManager {
private savedAvatarURLs: {
3 years ago
[peerId: PeerId]: {
3 years ago
[size in PeerPhotoSize]?: string | Promise<string>
}
} = {};
3 years ago
3 years ago
public isAvatarCached(peerId: PeerId) {
3 years ago
return !!this.savedAvatarURLs[peerId];
}
3 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
const inputPeer = appPeersManager.getInputPeerById(peerId);
let cached = false;
let getAvatarPromise: Promise<string>;
let saved = this.savedAvatarURLs[peerId];
if(!saved || !saved[size]) {
if(!saved) {
saved = this.savedAvatarURLs[peerId] = {};
}
//console.warn('will invoke downloadSmallFile:', peerId);
const peerPhotoFileLocation: InputFileLocation.inputPeerPhotoFileLocation = {
_: 'inputPeerPhotoFileLocation',
pFlags: {},
peer: inputPeer,
photo_id: photo.photo_id
};
if(size === 'photo_big') {
peerPhotoFileLocation.pFlags.big = true;
}
const downloadOptions = {dcId: photo.dc_id, location: peerPhotoFileLocation};
/* let str: string;
const time = Date.now();
if(peerId === 0) {
str = `download avatar ${peerId}`;
} */
const promise = appDownloadManager.download(downloadOptions);
getAvatarPromise = saved[size] = promise.then(blob => {
return saved[size] = URL.createObjectURL(blob);
/* if(str) {
console.log(str, Date.now() / 1000, Date.now() - time);
} */
});
} else if(typeof(saved[size]) !== 'string') {
getAvatarPromise = saved[size] as Promise<any>;
} else {
getAvatarPromise = Promise.resolve(saved[size]);
cached = true;
}
return {cached, loadPromise: getAvatarPromise};
}
public putAvatar(
div: HTMLElement,
peerId: PeerId,
photo: UserProfilePhoto.userProfilePhoto | ChatPhoto.chatPhoto,
size: PeerPhotoSize,
img = new Image(),
onlyThumb = false
) {
3 years ago
let {cached, loadPromise} = this.loadAvatar(peerId, photo, size);
img.classList.add('avatar-photo');
3 years ago
let renderThumbPromise: Promise<void>;
let callback: () => void;
let thumbImage: HTMLImageElement;
3 years ago
if(cached) {
// смотри в misc.ts: renderImageFromUrl
callback = () => {
replaceContent(div, img);
div.dataset.color = '';
};
} else {
const animate = rootScope.settings.animationsEnabled;
if(animate) {
img.classList.add('fade-in');
}
if(size === 'photo_big') { // let's load small photo first
const res = this.putAvatar(div, peerId, photo, 'photo_small');
renderThumbPromise = res.loadPromise;
thumbImage = res.thumbImage;
} else if(photo.stripped_thumb) {
3 years ago
thumbImage = new Image();
div.classList.add('avatar-relative');
thumbImage.classList.add('avatar-photo', 'avatar-photo-thumbnail');
const url = appPhotosManager.getPreviewURLFromBytes(photo.stripped_thumb);
renderThumbPromise = renderImageFromUrlPromise(thumbImage, url).then(() => {
3 years ago
replaceContent(div, thumbImage);
});
}
callback = () => {
if(thumbImage) {
3 years ago
div.append(img);
} else {
replaceContent(div, img);
}
setTimeout(() => {
if(div.childElementCount) {
sequentialDom.mutateElement(img, () => {
div.dataset.color = '';
if(animate) {
img.classList.remove('fade-in');
}
if(thumbImage) {
thumbImage.remove();
}
});
}
}, animate ? 200 : 0);
};
}
const renderPromise = loadPromise
.then((url) => renderImageFromUrlPromise(img, url/* , false */))
.then(callback);
3 years ago
return {
cached,
loadPromise: renderThumbPromise || renderPromise,
thumbImage
};
3 years ago
}
3 years ago
public s(div: HTMLElement, innerHTML: string, color: string, icon: string) {
div.innerHTML = innerHTML;
div.dataset.color = color;
div.classList.remove('tgico-saved', 'tgico-deletedaccount', 'tgico-reply_filled');
icon && div.classList.add(icon);
}
3 years ago
3 years ago
// peerId === peerId || title
public putPhoto(div: HTMLElement, peerId: PeerId, isDialog = false, title = '', onlyThumb = false) {
3 years ago
const myId = rootScope.myId;
3 years ago
3 years ago
//console.log('loadDialogPhoto location:', location, inputPeer);
if(peerId === myId && isDialog) {
3 years ago
this.s(div, '', '', 'tgico-saved');
3 years ago
return;
}
3 years ago
if(peerId !== NULL_PEER_ID && peerId.isUser()) {
3 years ago
const user = appUsersManager.getUser(peerId);
if(user && user.pFlags && user.pFlags.deleted) {
3 years ago
this.s(div, '', appPeersManager.getPeerColorById(peerId), 'tgico-deletedaccount');
3 years ago
return;
}
}
3 years ago
const photo = appPeersManager.getPeerPhoto(peerId);
const avatarAvailable = !!photo;
const avatarRendered = !!div.firstElementChild && !(div.firstElementChild as HTMLElement).classList.contains('emoji');
3 years ago
if(!avatarAvailable || !avatarRendered || !this.savedAvatarURLs[peerId]) {
let color = '';
if(peerId && (peerId !== myId || !isDialog)) {
color = appPeersManager.getPeerColorById(peerId);
}
3 years ago
if(peerId === REPLIES_PEER_ID) {
this.s(div, '', color, 'tgico-reply_filled');
return;
}
3 years ago
let abbr: string;
if(!title) {
const peer = appPeersManager.getPeer(peerId);
abbr = peer.initials ?? '';
} else {
abbr = RichTextProcessor.getAbbreviation(title);
}
3 years ago
this.s(div, abbr, color, '');
3 years ago
//return Promise.resolve(true);
}
if(avatarAvailable/* && false */) {
3 years ago
const size: PeerPhotoSize = 'photo_small';
3 years ago
return this.putAvatar(div, peerId, photo, size, undefined, onlyThumb);
}
}
}
const appAvatarsManager = new AppAvatarsManager();
export default appAvatarsManager;