tweb/src/components/peerProfile.ts

359 lines
10 KiB
TypeScript
Raw Normal View History

2021-10-05 22:40:07 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import IS_PARALLAX_SUPPORTED from "../environment/parallaxSupport";
2021-10-05 22:40:07 +02:00
import { copyTextToClipboard } from "../helpers/clipboard";
import replaceContent from "../helpers/dom/replaceContent";
import { fastRaf } from "../helpers/schedulers";
2022-02-11 23:17:35 +01:00
import { ChatFull, User } from "../layer";
2021-10-05 22:40:07 +02:00
import { Channel } from "../lib/appManagers/appChatsManager";
import appImManager from "../lib/appManagers/appImManager";
import appMessagesManager from "../lib/appManagers/appMessagesManager";
import appNotificationsManager from "../lib/appManagers/appNotificationsManager";
import appPeersManager from "../lib/appManagers/appPeersManager";
import appProfileManager from "../lib/appManagers/appProfileManager";
import appUsersManager from "../lib/appManagers/appUsersManager";
import I18n from "../lib/langPack";
import RichTextProcessor from "../lib/richtextprocessor";
import rootScope from "../lib/rootScope";
import AvatarElement from "./avatar";
import CheckboxField from "./checkboxField";
import generateVerifiedIcon from "./generateVerifiedIcon";
import PeerProfileAvatars from "./peerProfileAvatars";
import PeerTitle from "./peerTitle";
import Row from "./row";
import Scrollable from "./scrollable";
import { SettingSection, generateDelimiter } from "./sidebarLeft";
import { toast } from "./toast";
let setText = (text: string, row: Row) => {
//fastRaf(() => {
row.title.innerHTML = text;
row.container.style.display = '';
//});
};
export default class PeerProfile {
public element: HTMLElement;
public avatars: PeerProfileAvatars;
private avatar: AvatarElement;
private section: SettingSection;
private name: HTMLDivElement;
private subtitle: HTMLDivElement;
private bio: Row;
private username: Row;
private phone: Row;
private notifications: Row;
2022-01-01 12:28:21 +01:00
private location: Row;
2021-10-05 22:40:07 +02:00
private cleaned: boolean;
2022-01-01 12:28:21 +01:00
private setMoreDetailsTimeout: number;
2021-10-05 22:40:07 +02:00
private setPeerStatusInterval: number;
2021-10-21 15:16:43 +02:00
private peerId: PeerId;
2021-10-05 22:40:07 +02:00
private threadId: number;
constructor(public scrollable: Scrollable) {
if(!IS_PARALLAX_SUPPORTED) {
2021-10-05 22:40:07 +02:00
this.scrollable.container.classList.add('no-parallax');
}
}
public init() {
this.init = null;
2022-01-01 12:28:21 +01:00
2021-10-05 22:40:07 +02:00
this.element = document.createElement('div');
this.element.classList.add('profile-content');
this.section = new SettingSection({
noDelimiter: true
});
this.avatar = new AvatarElement();
this.avatar.classList.add('profile-avatar', 'avatar-120');
this.avatar.setAttribute('dialog', '1');
this.avatar.setAttribute('clickable', '');
this.name = document.createElement('div');
this.name.classList.add('profile-name');
this.subtitle = document.createElement('div');
this.subtitle.classList.add('profile-subtitle');
this.bio = new Row({
title: ' ',
subtitleLangKey: 'UserBio',
icon: 'info',
clickable: (e) => {
if((e.target as HTMLElement).tagName === 'A') {
return;
}
2022-02-08 20:18:01 +01:00
Promise.resolve(appProfileManager.getProfileByPeerId(this.peerId)).then(full => {
2021-10-05 22:40:07 +02:00
copyTextToClipboard(full.about);
toast(I18n.format('BioCopied', true));
});
}
});
this.bio.title.classList.add('pre-wrap');
this.username = new Row({
title: ' ',
subtitleLangKey: 'Username',
icon: 'username',
clickable: () => {
const peer: Channel | User.user = appPeersManager.getPeer(this.peerId);
copyTextToClipboard('@' + peer.username);
toast(I18n.format('UsernameCopied', true));
}
});
this.phone = new Row({
title: ' ',
subtitleLangKey: 'Phone',
icon: 'phone',
clickable: () => {
const peer: User = appUsersManager.getUser(this.peerId);
copyTextToClipboard('+' + peer.phone);
toast(I18n.format('PhoneCopied', true));
}
});
2022-01-01 12:28:21 +01:00
this.location = new Row({
title: ' ',
2022-01-02 19:43:27 +01:00
subtitleLangKey: 'ChatLocation',
2022-01-01 12:28:21 +01:00
icon: 'location'
});
2021-10-05 22:40:07 +02:00
this.notifications = new Row({
checkboxField: new CheckboxField({toggle: true}),
titleLangKey: 'Notifications',
icon: 'unmute'
});
2022-01-01 12:28:21 +01:00
this.section.content.append(
this.phone.container,
this.username.container,
this.location.container,
this.bio.container,
this.notifications.container
);
2021-10-05 22:40:07 +02:00
this.element.append(this.section.container);
if(IS_PARALLAX_SUPPORTED) {
this.element.append(generateDelimiter());
}
2021-10-05 22:40:07 +02:00
this.notifications.checkboxField.input.addEventListener('change', (e) => {
if(!e.isTrusted) {
return;
}
//let checked = this.notificationsCheckbox.checked;
appMessagesManager.togglePeerMute(this.peerId);
2021-10-05 22:40:07 +02:00
});
rootScope.addEventListener('dialog_notify_settings', (dialog) => {
if(this.peerId === dialog.peerId) {
const muted = appNotificationsManager.isPeerLocalMuted(this.peerId, false);
this.notifications.checkboxField.checked = !muted;
}
});
rootScope.addEventListener('peer_typings', ({peerId}) => {
if(this.peerId === peerId) {
this.setPeerStatus();
}
});
rootScope.addEventListener('peer_bio_edit', (peerId) => {
if(peerId === this.peerId) {
2022-01-01 12:28:21 +01:00
this.setMoreDetails(true);
2021-10-05 22:40:07 +02:00
}
});
rootScope.addEventListener('user_update', (userId) => {
if(this.peerId === userId) {
this.setPeerStatus();
}
});
rootScope.addEventListener('contacts_update', (userId) => {
if(this.peerId === userId) {
const user = appUsersManager.getUser(userId);
if(!user.pFlags.self) {
if(user.phone) {
setText(appUsersManager.formatUserPhone(user.phone), this.phone);
} else {
this.phone.container.style.display = 'none';
}
}
}
});
this.setPeerStatusInterval = window.setInterval(this.setPeerStatus, 60e3);
}
public setPeerStatus = (needClear = false) => {
if(!this.peerId) return;
const peerId = this.peerId;
appImManager.setPeerStatus(this.peerId, this.subtitle, needClear, true, () => peerId === this.peerId);
};
public cleanupHTML() {
this.bio.container.style.display = 'none';
this.phone.container.style.display = 'none';
this.username.container.style.display = 'none';
2022-01-01 12:28:21 +01:00
this.location.container.style.display = 'none';
2021-10-05 22:40:07 +02:00
this.notifications.container.style.display = '';
this.notifications.checkboxField.checked = true;
2022-01-01 12:28:21 +01:00
if(this.setMoreDetailsTimeout) {
window.clearTimeout(this.setMoreDetailsTimeout);
this.setMoreDetailsTimeout = 0;
2021-10-05 22:40:07 +02:00
}
}
public setAvatar() {
if(this.peerId !== rootScope.myId) {
const photo = appPeersManager.getPeerPhoto(this.peerId);
if(photo) {
const oldAvatars = this.avatars;
this.avatars = new PeerProfileAvatars(this.scrollable);
this.avatars.setPeer(this.peerId);
this.avatars.info.append(this.name, this.subtitle);
this.avatar.remove();
if(oldAvatars) oldAvatars.container.replaceWith(this.avatars.container);
else this.element.prepend(this.avatars.container);
if(IS_PARALLAX_SUPPORTED) {
2021-10-05 22:40:07 +02:00
this.scrollable.container.classList.add('parallax');
}
return;
}
}
if(IS_PARALLAX_SUPPORTED) {
2021-10-05 22:40:07 +02:00
this.scrollable.container.classList.remove('parallax');
}
if(this.avatars) {
this.avatars.container.remove();
this.avatars = undefined;
}
this.avatar.setAttribute('peer', '' + this.peerId);
this.section.content.prepend(this.avatar, this.name, this.subtitle);
}
public fillProfileElements() {
if(!this.cleaned) return;
this.cleaned = false;
const peerId = this.peerId;
this.cleanupHTML();
this.setAvatar();
// username
if(peerId !== rootScope.myId) {
let username = appPeersManager.getPeerUsername(peerId);
if(username) {
setText(appPeersManager.getPeerUsername(peerId), this.username);
}
const muted = appNotificationsManager.isPeerLocalMuted(peerId, false);
this.notifications.checkboxField.checked = !muted;
} else {
fastRaf(() => {
this.notifications.container.style.display = 'none';
});
}
//let membersLi = this.profileTabs.firstElementChild.children[0] as HTMLLIElement;
2021-10-21 15:16:43 +02:00
if(peerId.isUser()) {
2021-10-05 22:40:07 +02:00
//membersLi.style.display = 'none';
let user = appUsersManager.getUser(peerId);
if(user.phone && peerId !== rootScope.myId) {
setText(appUsersManager.formatUserPhone(user.phone), this.phone);
}
}/* else {
//membersLi.style.display = appPeersManager.isBroadcast(peerId) ? 'none' : '';
} */
2022-01-01 12:28:21 +01:00
this.setMoreDetails();
2021-10-05 22:40:07 +02:00
replaceContent(this.name, new PeerTitle({
peerId,
dialog: true,
}).element);
const peer = appPeersManager.getPeer(peerId);
if(peer?.pFlags?.verified) {
this.name.append(generateVerifiedIcon());
}
this.setPeerStatus(true);
}
2022-01-01 12:28:21 +01:00
public setMoreDetails(override?: true) {
if(this.setMoreDetailsTimeout) {
window.clearTimeout(this.setMoreDetailsTimeout);
this.setMoreDetailsTimeout = 0;
2021-10-05 22:40:07 +02:00
}
const peerId = this.peerId;
const threadId = this.threadId;
if(!peerId || appPeersManager.isRestricted(peerId) || peerId === rootScope.myId) {
2021-10-05 22:40:07 +02:00
return;
}
2022-02-11 23:17:35 +01:00
Promise.resolve(appProfileManager.getProfileByPeerId(peerId, override)).then((peerFull) => {
if(this.peerId !== peerId || this.threadId !== threadId || appPeersManager.isRestricted(peerId)) {
//this.log.warn('peer changed');
return;
}
//this.log('chatInfo res 2:', chatFull);
if(peerFull.about) {
setText(RichTextProcessor.wrapRichText(peerFull.about), this.bio);
}
2021-10-05 22:40:07 +02:00
2022-02-11 23:17:35 +01:00
if((peerFull as ChatFull.channelFull)?.location?._ == 'channelLocation') {
2022-01-01 12:28:21 +01:00
// @ts-ignore
2022-02-11 23:17:35 +01:00
setText(chatFull.location.address, this.location);
2021-10-05 22:40:07 +02:00
}
2022-02-11 23:17:35 +01:00
this.setMoreDetailsTimeout = window.setTimeout(() => this.setMoreDetails(true), 60e3);
2021-10-05 22:40:07 +02:00
});
}
2021-10-21 15:16:43 +02:00
public setPeer(peerId: PeerId, threadId = 0) {
if(this.peerId === peerId && this.threadId === threadId) return;
2021-10-05 22:40:07 +02:00
if(this.init) {
this.init();
}
this.peerId = peerId;
this.threadId = threadId;
this.cleaned = true;
}
}