tweb/src/components/sidebarRight/tabs/sharedMedia.ts

528 lines
16 KiB
TypeScript
Raw Normal View History

import appImManager from "../../../lib/appManagers/appImManager";
2021-01-20 02:38:59 +01:00
import appMessagesManager from "../../../lib/appManagers/appMessagesManager";
import appPeersManager from "../../../lib/appManagers/appPeersManager";
import appProfileManager from "../../../lib/appManagers/appProfileManager";
2021-03-28 20:37:11 +02:00
import appUsersManager, { User } from "../../../lib/appManagers/appUsersManager";
import { logger } from "../../../lib/logger";
import { RichTextProcessor } from "../../../lib/richtextprocessor";
2020-11-15 04:33:47 +01:00
import rootScope from "../../../lib/rootScope";
2020-12-25 13:53:20 +01:00
import AppSearchSuper, { SearchSuperType } from "../../appSearchSuper.";
import AvatarElement from "../../avatar";
import Scrollable from "../../scrollable";
2021-03-28 20:37:11 +02:00
import SidebarSlider, { SliderSuperTab, SliderTab } from "../../slider";
import CheckboxField from "../../checkboxField";
2021-03-13 03:22:42 +01:00
import { attachClickEvent } from "../../../helpers/dom";
import appSidebarRight from "..";
import { TransitionSlider } from "../../transition";
2021-03-11 04:06:44 +01:00
import appNotificationsManager from "../../../lib/appManagers/appNotificationsManager";
2021-03-12 20:02:05 +01:00
import AppEditGroupTab from "./editGroup";
2021-03-13 03:22:42 +01:00
import PeerTitle from "../../peerTitle";
2021-03-13 04:03:59 +01:00
import AppEditChannelTab from "./editChannel";
import AppEditContactTab from "./editContact";
2021-03-28 20:37:11 +02:00
import appChatsManager, { Channel } from "../../../lib/appManagers/appChatsManager";
2021-03-13 13:24:00 +01:00
import { Chat } from "../../../layer";
2021-03-28 20:37:11 +02:00
import Button from "../../button";
import ButtonIcon from "../../buttonIcon";
import I18n, { i18n } from "../../../lib/langPack";
import { SettingSection } from "../../sidebarLeft";
import Row from "../../row";
import { copyTextToClipboard } from "../../../helpers/clipboard";
import { toast } from "../../toast";
let setText = (text: string, row: Row) => {
window.requestAnimationFrame(() => {
2021-03-28 20:37:11 +02:00
row.title.innerHTML = text;
row.container.style.display = '';
});
};
// TODO: отредактированное сообщение не изменится
2021-03-28 20:37:11 +02:00
export default class AppSharedMediaTab extends SliderSuperTab {
2021-03-12 20:02:05 +01:00
public editBtn: HTMLElement;
private peerId = 0;
2020-12-23 03:18:24 +01:00
private threadId = 0;
public profileContentEl: HTMLDivElement;
public profileElements: {
avatar: AvatarElement,
name: HTMLDivElement,
subtitle: HTMLDivElement,
2021-03-28 20:37:11 +02:00
bio: Row,
username: Row,
phone: Row,
notifications: Row
} = {} as any;
2020-12-25 00:19:34 +01:00
public historiesStorage: {
[peerId: number]: Partial<{
2020-12-25 13:53:20 +01:00
[type in SearchSuperType]: {mid: number, peerId: number}[]
}>
} = {};
private log = logger('SM'/* , LogLevels.error */);
setPeerStatusInterval: number;
cleaned: boolean;
2020-12-25 00:19:34 +01:00
searchSuper: AppSearchSuper;
2021-03-13 03:22:42 +01:00
private setBioTimeout: number;
2021-03-28 20:37:11 +02:00
constructor(slider: SidebarSlider) {
super(slider, false);
}
protected init() {
this.container.id = 'shared-media-container';
this.container.classList.add('profile-container');
// * header
const newCloseBtn = Button('btn-icon sidebar-close-button', {noRipple: true});
this.closeBtn.replaceWith(newCloseBtn);
this.closeBtn = newCloseBtn;
const animatedCloseIcon = document.createElement('div');
animatedCloseIcon.classList.add('animated-close-icon');
newCloseBtn.append(animatedCloseIcon);
const transitionContainer = document.createElement('div');
transitionContainer.className = 'transition slide-fade';
const transitionFirstItem = document.createElement('div');
transitionFirstItem.classList.add('transition-item');
this.title.append(i18n('Telegram.PeerInfoController'));
this.editBtn = ButtonIcon('edit');
2021-04-04 17:39:17 +02:00
//const moreBtn = ButtonIcon('more');
2021-03-28 20:37:11 +02:00
2021-04-04 17:39:17 +02:00
transitionFirstItem.append(this.title, this.editBtn/* , moreBtn */);
2021-03-28 20:37:11 +02:00
const transitionLastItem = document.createElement('div');
transitionLastItem.classList.add('transition-item');
const secondTitle: HTMLElement = this.title.cloneNode() as any;
secondTitle.append(i18n('PeerInfo.SharedMedia'));
transitionLastItem.append(secondTitle);
transitionContainer.append(transitionFirstItem, transitionLastItem);
2021-03-28 20:37:11 +02:00
this.header.append(transitionContainer);
// * body
this.profileContentEl = document.createElement('div');
this.profileContentEl.classList.add('profile-content');
const section = new SettingSection({
noDelimiter: true
2021-02-01 04:07:44 +01:00
});
2021-01-20 02:38:59 +01:00
2021-03-28 20:37:11 +02:00
this.profileElements.avatar = new AvatarElement();
this.profileElements.avatar.classList.add('profile-avatar', 'avatar-120');
this.profileElements.avatar.setAttribute('dialog', '1');
this.profileElements.avatar.setAttribute('clickable', '');
this.profileElements.name = document.createElement('div');
this.profileElements.name.classList.add('profile-name');
this.profileElements.subtitle = document.createElement('div');
this.profileElements.subtitle.classList.add('profile-subtitle');
this.profileElements.bio = new Row({
title: ' ',
subtitleLangKey: 'UserBio',
icon: 'info',
2021-04-02 23:45:51 +02:00
clickable: (e) => {
if((e.target as HTMLElement).tagName === 'A') {
return;
}
2021-03-28 20:37:11 +02:00
appProfileManager.getProfileByPeerId(this.peerId).then(full => {
copyTextToClipboard(full.about);
toast(I18n.format('BioCopied', true));
});
}
});
2021-04-02 23:45:51 +02:00
this.profileElements.bio.title.classList.add('pre-wrap');
2021-03-28 20:37:11 +02:00
this.profileElements.username = new Row({
title: ' ',
subtitleLangKey: 'Username',
icon: 'username',
clickable: () => {
const peer: Channel | User = appPeersManager.getPeer(this.peerId);
copyTextToClipboard('@' + peer.username);
toast(I18n.format('UsernameCopied', true));
}
});
this.profileElements.phone = new Row({
title: ' ',
subtitleLangKey: 'Phone',
icon: 'phone',
clickable: () => {
const peer: User = appUsersManager.getUser(this.peerId);
copyTextToClipboard('+' + peer.phone);
toast(I18n.format('PhoneCopied', true));
}
});
this.profileElements.notifications = new Row({
checkboxField: new CheckboxField({text: 'Notifications'})
});
section.content.append(this.profileElements.avatar, this.profileElements.name, this.profileElements.subtitle,
this.profileElements.bio.container, this.profileElements.username.container, this.profileElements.phone.container, this.profileElements.notifications.container);
this.profileContentEl.append(section.container);
this.scrollable.append(this.profileContentEl);
const HEADER_HEIGHT = 56;
2021-03-28 20:37:11 +02:00
this.scrollable.onAdditionalScroll = () => {
const rect = this.searchSuper.nav.getBoundingClientRect();
if(!rect.width) return;
const top = rect.top;
const isSharedMedia = top <= HEADER_HEIGHT;
2021-03-28 20:37:11 +02:00
animatedCloseIcon.classList.toggle('state-back', isSharedMedia);
transition(+isSharedMedia);
2021-03-11 23:39:57 +01:00
if(!isSharedMedia) {
this.searchSuper.goingHard = {};
}
};
2021-03-28 20:37:11 +02:00
const transition = TransitionSlider(transitionContainer, 'slide-fade', 400, null, false);
transition(0);
attachClickEvent(this.closeBtn, (e) => {
if(this.closeBtn.firstElementChild.classList.contains('state-back')) {
2021-03-28 20:37:11 +02:00
this.scrollable.scrollIntoViewNew(this.scrollable.container.firstElementChild as HTMLElement, 'start');
transition(0);
2021-03-28 20:37:11 +02:00
animatedCloseIcon.classList.remove('state-back');
} else if(!this.scrollable.isHeavyAnimationInProgress) {
appSidebarRight.onCloseBtnClick();
}
});
2021-03-12 20:02:05 +01:00
attachClickEvent(this.editBtn, (e) => {
2021-03-13 04:03:59 +01:00
let tab: AppEditGroupTab | AppEditChannelTab | AppEditContactTab;
2021-03-12 20:02:05 +01:00
if(appPeersManager.isAnyGroup(this.peerId)) {
tab = new AppEditGroupTab(appSidebarRight);
2021-03-12 20:02:05 +01:00
} else if(this.peerId > 0) {
2021-03-13 04:03:59 +01:00
tab = new AppEditContactTab(appSidebarRight);
2021-03-12 20:02:05 +01:00
} else {
2021-03-13 04:03:59 +01:00
tab = new AppEditChannelTab(appSidebarRight);
2021-03-12 20:02:05 +01:00
}
if(tab) {
2021-03-17 14:05:32 +01:00
if(tab instanceof AppEditGroupTab) {
tab.chatId = -this.peerId;
} else {
tab.peerId = this.peerId;
}
tab.open();
}
2021-03-12 20:02:05 +01:00
});
2021-03-28 20:37:11 +02:00
//this.container.prepend(this.closeBtn.parentElement);
this.profileElements.notifications.checkboxField.input.addEventListener('change', (e) => {
if(!e.isTrusted) {
return;
}
//let checked = this.profileElements.notificationsCheckbox.checked;
appMessagesManager.mutePeer(this.peerId);
});
2021-03-11 04:06:44 +01:00
rootScope.on('dialog_notify_settings', (dialog) => {
if(this.peerId === dialog.peerId) {
const muted = appNotificationsManager.isPeerLocalMuted(this.peerId, false);
2021-03-28 20:37:11 +02:00
this.profileElements.notifications.checkboxField.checked = !muted;
}
});
rootScope.on('peer_typings', (e) => {
const {peerId} = e;
2021-02-04 01:30:23 +01:00
if(this.peerId === peerId) {
this.setPeerStatus();
}
});
2021-03-13 03:22:42 +01:00
rootScope.on('peer_bio_edit', (peerId) => {
if(peerId === this.peerId) {
this.setBio(true);
}
});
rootScope.on('user_update', (e) => {
const userId = e;
2021-02-04 01:30:23 +01:00
if(this.peerId === userId) {
this.setPeerStatus();
}
});
this.setPeerStatusInterval = window.setInterval(this.setPeerStatus, 60e3);
2020-12-25 00:19:34 +01:00
this.searchSuper = new AppSearchSuper([{
inputFilter: 'inputMessagesFilterPhotoVideo',
2021-03-26 18:49:29 +01:00
name: 'SharedMediaTab2',
type: 'media'
2020-12-25 00:19:34 +01:00
}, {
inputFilter: 'inputMessagesFilterDocument',
2021-03-26 18:49:29 +01:00
name: 'SharedFilesTab2',
type: 'files'
2020-12-25 00:19:34 +01:00
}, {
inputFilter: 'inputMessagesFilterUrl',
2021-03-26 18:49:29 +01:00
name: 'SharedLinksTab2',
type: 'links'
2020-12-25 00:19:34 +01:00
}, {
inputFilter: 'inputMessagesFilterMusic',
2021-03-26 18:49:29 +01:00
name: 'SharedMusicTab2',
type: 'music'
2021-03-28 20:37:11 +02:00
}], this.scrollable/* , undefined, undefined, false */);
2020-12-25 00:19:34 +01:00
this.profileContentEl.append(this.searchSuper.container);
}
public setPeerStatus = (needClear = false) => {
if(!this.peerId) return;
const peerId = this.peerId;
if(needClear) {
2021-03-13 13:24:00 +01:00
this.profileElements.subtitle.innerHTML = ''; // ! HERE U CAN FIND WHITESPACE
}
appImManager.getPeerStatus(this.peerId).then((subtitle) => {
2021-02-04 01:30:23 +01:00
if(peerId !== this.peerId) {
return;
}
2021-03-28 20:37:11 +02:00
this.profileElements.subtitle.textContent = '';
this.profileElements.subtitle.append(subtitle || '');
});
};
public renderNewMessages(peerId: number, mids: number[]) {
if(this.init) return; // * not inited yet
if(!this.historiesStorage[peerId]) return;
mids = mids.slice().reverse(); // ! because it will be ascend sorted array
2020-12-25 00:19:34 +01:00
for(const type of this.searchSuper.types) {
const inputFilter = type.inputFilter;
2020-12-25 13:53:20 +01:00
const filtered = this.searchSuper.filterMessagesByType(mids.map(mid => appMessagesManager.getMessageByPeer(peerId, mid)), inputFilter);
if(filtered.length) {
2020-12-25 00:19:34 +01:00
if(this.historiesStorage[peerId][inputFilter]) {
2020-12-25 13:53:20 +01:00
this.historiesStorage[peerId][inputFilter].unshift(...filtered.map(message => ({mid: message.mid, peerId: message.peerId})));
}
2021-02-04 01:30:23 +01:00
if(this.peerId === peerId && this.searchSuper.usedFromHistory[inputFilter] !== -1) {
2020-12-25 00:19:34 +01:00
this.searchSuper.usedFromHistory[inputFilter] += filtered.length;
this.searchSuper.performSearchResult(filtered, inputFilter, false);
}
break;
}
}
}
public deleteDeletedMessages(peerId: number, mids: number[]) {
if(this.init) return; // * not inited yet
if(!this.historiesStorage[peerId]) return;
for(const mid of mids) {
2020-12-25 00:19:34 +01:00
for(const type of this.searchSuper.types) {
const inputFilter = type.inputFilter;
if(!this.historiesStorage[peerId][inputFilter]) continue;
2020-12-25 00:19:34 +01:00
const history = this.historiesStorage[peerId][inputFilter];
2020-12-25 13:53:20 +01:00
const idx = history.findIndex(m => m.mid === mid);
if(idx !== -1) {
history.splice(idx, 1);
2021-02-04 01:30:23 +01:00
if(this.peerId === peerId) {
2020-12-25 00:19:34 +01:00
const container = this.searchSuper.tabs[inputFilter];
2020-12-25 13:53:20 +01:00
const div = container.querySelector(`div[data-mid="${mid}"][data-peer-id="${peerId}"]`);
if(div) {
div.remove();
}
2020-12-25 00:19:34 +01:00
if(this.searchSuper.usedFromHistory[inputFilter] >= (idx + 1)) {
this.searchSuper.usedFromHistory[inputFilter]--;
}
}
break;
}
}
}
2021-03-28 20:37:11 +02:00
this.scrollable.onScroll();
}
public cleanupHTML() {
2021-03-28 20:37:11 +02:00
this.profileElements.bio.container.style.display = 'none';
this.profileElements.phone.container.style.display = 'none';
this.profileElements.username.container.style.display = 'none';
this.profileElements.notifications.container.style.display = '';
this.profileElements.notifications.checkboxField.checked = true;
2021-03-12 20:02:05 +01:00
this.editBtn.style.display = 'none';
2021-03-13 03:22:42 +01:00
if(this.setBioTimeout) {
window.clearTimeout(this.setBioTimeout);
this.setBioTimeout = 0;
}
2020-12-25 00:19:34 +01:00
this.searchSuper.cleanupHTML();
2020-12-25 13:53:20 +01:00
this.searchSuper.selectTab(0, false);
}
public setLoadMutex(promise: Promise<any>) {
2020-12-25 00:19:34 +01:00
this.searchSuper.loadMutex = promise;
}
2020-12-23 03:18:24 +01:00
public setPeer(peerId: number, threadId = 0) {
if(this.peerId === peerId && this.threadId === peerId) return;
if(this.init) {
this.init();
this.init = null;
}
this.peerId = peerId;
2020-12-23 03:18:24 +01:00
this.threadId = threadId;
2020-12-25 13:53:20 +01:00
this.searchSuper.setQuery({
peerId,
//threadId,
historyStorage: this.historiesStorage[peerId] ?? (this.historiesStorage[peerId] = {})
});
2020-12-25 00:19:34 +01:00
this.cleaned = true;
}
public loadSidebarMedia(single: boolean) {
this.searchSuper.load(single);
}
public fillProfileElements() {
if(!this.cleaned) return;
this.cleaned = false;
const peerId = this.peerId;
this.cleanupHTML();
this.profileElements.avatar.setAttribute('peer', '' + peerId);
// username
2021-02-04 01:30:23 +01:00
if(peerId !== rootScope.myId) {
let username = appPeersManager.getPeerUsername(peerId);
if(username) {
setText(appPeersManager.getPeerUsername(peerId), this.profileElements.username);
}
2021-03-11 04:06:44 +01:00
const muted = appNotificationsManager.isPeerLocalMuted(peerId, false);
2021-03-28 20:37:11 +02:00
this.profileElements.notifications.checkboxField.checked = !muted;
} else {
window.requestAnimationFrame(() => {
2021-03-28 20:37:11 +02:00
this.profileElements.notifications.container.style.display = 'none';
});
}
//let membersLi = this.profileTabs.firstElementChild.children[0] as HTMLLIElement;
if(peerId > 0) {
//membersLi.style.display = 'none';
let user = appUsersManager.getUser(peerId);
2021-02-04 01:30:23 +01:00
if(user.phone && peerId !== rootScope.myId) {
setText(user.rPhone, this.profileElements.phone);
}
2021-03-13 03:22:42 +01:00
}/* else {
//membersLi.style.display = appPeersManager.isBroadcast(peerId) ? 'none' : '';
} */
this.setBio();
this.profileElements.name.innerHTML = '';
this.profileElements.name.append(new PeerTitle({
peerId,
dialog: true
}).element);
2021-03-13 13:24:00 +01:00
if(peerId > 0) {
if(peerId !== rootScope.myId && appUsersManager.isContact(peerId)) {
this.editBtn.style.display = '';
}
} else {
const chat: Chat = appChatsManager.getChat(-peerId);
if(chat._ === 'chat' || (chat as Chat.channel).admin_rights) {
this.editBtn.style.display = '';
}
}
2021-03-13 03:22:42 +01:00
this.setPeerStatus(true);
}
public setBio(override?: true) {
if(this.setBioTimeout) {
window.clearTimeout(this.setBioTimeout);
this.setBioTimeout = 0;
}
const peerId = this.peerId;
const threadId = this.threadId;
if(!peerId) {
return;
}
let promise: Promise<boolean>;
if(peerId > 0) {
promise = appProfileManager.getProfile(peerId, override).then(userFull => {
2020-12-23 03:18:24 +01:00
if(this.peerId !== peerId || this.threadId !== threadId) {
this.log.warn('peer changed');
2021-03-13 03:22:42 +01:00
return false;
}
2021-02-04 01:30:23 +01:00
if(userFull.rAbout && peerId !== rootScope.myId) {
setText(userFull.rAbout, this.profileElements.bio);
}
//this.log('userFull', userFull);
2021-03-13 03:22:42 +01:00
return true;
});
} else {
2021-03-13 03:22:42 +01:00
promise = appProfileManager.getChatFull(-peerId, override).then((chatFull) => {
2020-12-23 03:18:24 +01:00
if(this.peerId !== peerId || this.threadId !== threadId) {
this.log.warn('peer changed');
2021-03-13 03:22:42 +01:00
return false;
}
//this.log('chatInfo res 2:', chatFull);
if(chatFull.about) {
setText(RichTextProcessor.wrapRichText(chatFull.about), this.profileElements.bio);
}
2021-03-13 03:22:42 +01:00
return true;
});
}
2021-03-13 03:22:42 +01:00
promise.then((canSetNext) => {
if(canSetNext) {
this.setBioTimeout = window.setTimeout(() => this.setBio(true), 60e3);
}
});
}
onOpenAfterTimeout() {
2021-03-28 20:37:11 +02:00
this.scrollable.onScroll();
}
}