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

454 lines
14 KiB
TypeScript
Raw Normal View History

2021-04-08 15:52:31 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2021-10-05 22:40:07 +02:00
import appMessagesManager from "../../../lib/appManagers/appMessagesManager";
import appUsersManager from "../../../lib/appManagers/appUsersManager";
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 SidebarSlider, { SliderSuperTab } from "../../slider";
import { TransitionSlider } from "../../transition";
2021-05-05 12:50:32 +02:00
import AppEditChatTab from "./editChat";
2021-03-13 03:22:42 +01:00
import PeerTitle from "../../peerTitle";
2021-03-13 04:03:59 +01:00
import AppEditContactTab from "./editContact";
2021-10-05 22:40:07 +02:00
import appChatsManager from "../../../lib/appManagers/appChatsManager";
2021-03-28 20:37:11 +02:00
import Button from "../../button";
import ButtonIcon from "../../buttonIcon";
2021-10-05 22:40:07 +02:00
import { i18n, LangPackKey } from "../../../lib/langPack";
import { toastNew } from "../../toast";
2021-04-10 15:52:21 +02:00
import AppAddMembersTab from "../../sidebarLeft/tabs/addMembers";
import PopupPickUser from "../../popups/pickUser";
import PopupPeer, { PopupPeerButtonCallbackCheckboxes, PopupPeerCheckboxOptions } from "../../popups/peer";
2021-05-03 21:17:50 +02:00
import ButtonCorner from "../../buttonCorner";
import { attachClickEvent } from "../../../helpers/dom/clickEvent";
2021-10-05 22:40:07 +02:00
import PeerProfile from "../../peerProfile";
// TODO: отредактированное сообщение не изменится
export default class AppSharedMediaTab extends SliderSuperTab {
2021-04-09 18:44:43 +02:00
private editBtn: HTMLElement;
2021-10-21 15:16:43 +02:00
private peerId: PeerId;
private threadId = 0;
2021-04-09 18:44:43 +02:00
private historiesStorage: {
2021-10-21 15:16:43 +02:00
[peerId: PeerId]: Partial<{
[type in SearchSuperType]: {mid: number, peerId: PeerId}[]
}>
} = {};
private searchSuper: AppSearchSuper;
2021-04-09 18:44:43 +02:00
private profile: PeerProfile;
private peerChanged: boolean;
constructor(slider: SidebarSlider) {
super(slider, false);
}
2021-04-09 18:44:43 +02:00
public init() {
//const perf = performance.now();
2021-04-09 18:44:43 +02:00
this.container.classList.add('shared-media-container', '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('Profile'));
this.editBtn = ButtonIcon('edit');
//const moreBtn = ButtonIcon('more');
transitionFirstItem.append(this.title, this.editBtn/* , moreBtn */);
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);
this.header.append(transitionContainer);
// * body
this.profile = new PeerProfile(this.scrollable);
this.profile.init();
this.scrollable.append(this.profile.element);
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 - 1;
const isSharedMedia = top <= HEADER_HEIGHT;
2021-03-28 20:37:11 +02:00
animatedCloseIcon.classList.toggle('state-back', isSharedMedia);
this.searchSuper.container.classList.toggle('is-full-viewport', isSharedMedia);
transition(+isSharedMedia);
2021-03-11 23:39:57 +01:00
if(!isSharedMedia) {
this.searchSuper.cleanScrollPositions();
2021-03-11 23:39:57 +01:00
}
};
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) {
2021-10-05 22:40:07 +02:00
this.slider.onCloseBtnClick();
}
});
2021-03-12 20:02:05 +01:00
attachClickEvent(this.editBtn, (e) => {
2021-05-05 12:50:32 +02:00
let tab: AppEditChatTab | AppEditContactTab;
2021-10-21 15:16:43 +02:00
if(this.peerId.isAnyChat()) {
2021-10-05 22:40:07 +02:00
tab = new AppEditChatTab(this.slider);
2021-03-12 20:02:05 +01:00
} else {
2021-10-05 22:40:07 +02:00
tab = new AppEditContactTab(this.slider);
2021-03-12 20:02:05 +01:00
}
if(tab) {
2021-05-05 12:50:32 +02:00
if(tab instanceof AppEditChatTab) {
2021-10-21 15:16:43 +02:00
tab.chatId = this.peerId.toChatId();
2021-03-17 14:05:32 +01:00
} else {
tab.peerId = this.peerId;
}
tab.open();
}
2021-03-12 20:02:05 +01:00
});
2021-09-15 07:02:03 +02:00
rootScope.addEventListener('contacts_update', (userId) => {
if(this.peerId === userId) {
this.toggleEditBtn();
}
});
rootScope.addEventListener('chat_update', (chatId) => {
2021-10-21 15:16:43 +02:00
if(this.peerId === chatId.toPeerId(true)) {
this.toggleEditBtn();
2021-09-15 07:02:03 +02:00
}
});
2021-10-05 22:40:07 +02:00
rootScope.addEventListener('history_multiappend', (msgIdsByPeer) => {
for(const peerId in msgIdsByPeer) {
2021-10-21 15:16:43 +02:00
this.renderNewMessages(peerId.toPeerId(), Array.from(msgIdsByPeer[peerId]));
2021-10-05 22:40:07 +02:00
}
});
rootScope.addEventListener('history_delete', ({peerId, msgs}) => {
this.deleteDeletedMessages(peerId, Array.from(msgs));
});
// Calls when message successfully sent and we have an id
rootScope.addEventListener('message_sent', ({message}) => {
this.renderNewMessages(message.peerId, [message.mid]);
});
2021-03-28 20:37:11 +02:00
//this.container.prepend(this.closeBtn.parentElement);
this.searchSuper = new AppSearchSuper({
mediaTabs: [{
inputFilter: 'inputMessagesFilterEmpty',
name: 'PeerMedia.Members',
type: 'members'
}, {
inputFilter: 'inputMessagesFilterPhotoVideo',
name: 'SharedMediaTab2',
type: 'media'
}, {
inputFilter: 'inputMessagesFilterDocument',
name: 'SharedFilesTab2',
type: 'files'
}, {
inputFilter: 'inputMessagesFilterUrl',
name: 'SharedLinksTab2',
type: 'links'
}, {
inputFilter: 'inputMessagesFilterMusic',
name: 'SharedMusicTab2',
type: 'music'
}, {
inputFilter: 'inputMessagesFilterRoundVoice',
name: 'SharedVoiceTab2',
type: 'voice'
}],
2021-04-10 15:52:21 +02:00
scrollable: this.scrollable,
onChangeTab: (mediaTab) => {
let timeout = mediaTab.type === 'members' && rootScope.settings.animationsEnabled ? 250 : 0;
setTimeout(() => {
btnAddMembers.classList.toggle('is-hidden', mediaTab.type !== 'members');
}, timeout);
}
});
2021-04-09 18:44:43 +02:00
this.profile.element.append(this.searchSuper.container);
2021-05-03 21:17:50 +02:00
const btnAddMembers = ButtonCorner({icon: 'addmember_filled'});
2021-04-09 18:44:43 +02:00
this.content.append(btnAddMembers);
2021-04-10 15:52:21 +02:00
btnAddMembers.addEventListener('click', () => {
2021-10-21 15:16:43 +02:00
const peerId = this.peerId;
const id = this.peerId.toChatId();
2021-04-10 15:52:21 +02:00
const isChannel = appChatsManager.isChannel(id);
2021-10-21 15:16:43 +02:00
const showConfirmation = (peerIds: PeerId[], callback: (checked: PopupPeerButtonCallbackCheckboxes) => void) => {
let titleLangKey: LangPackKey, titleLangArgs: any[],
descriptionLangKey: LangPackKey, descriptionLangArgs: any[],
checkboxes: PopupPeerCheckboxOptions[];
2021-04-10 15:52:21 +02:00
if(peerIds.length > 1) {
titleLangKey = 'AddMembersAlertTitle';
titleLangArgs = [i18n('Members', [peerIds.length])];
descriptionLangKey = 'AddMembersAlertCountText';
descriptionLangArgs = peerIds.map(peerId => {
const b = document.createElement('b');
b.append(new PeerTitle({peerId}).element);
return b;
});
if(!isChannel) {
checkboxes = [{
text: 'AddMembersForwardMessages',
checked: true
}];
}
2021-04-10 15:52:21 +02:00
} else {
titleLangKey = 'AddOneMemberAlertTitle';
descriptionLangKey = 'AddMembersAlertNamesText';
const b = document.createElement('b');
b.append(new PeerTitle({
peerId: peerIds[0]
}).element);
descriptionLangArgs = [b];
if(!isChannel) {
checkboxes = [{
text: 'AddOneMemberForwardMessages',
2021-08-28 00:01:24 +02:00
textArgs: [new PeerTitle({peerId: peerIds[0]}).element],
checked: true
}];
}
2021-04-10 15:52:21 +02:00
}
descriptionLangArgs.push(new PeerTitle({
2021-10-21 15:16:43 +02:00
peerId
}).element);
2021-04-10 15:52:21 +02:00
new PopupPeer('popup-add-members', {
2021-10-21 15:16:43 +02:00
peerId,
2021-04-10 15:52:21 +02:00
titleLangKey,
descriptionLangKey,
descriptionLangArgs,
buttons: [{
langKey: 'Add',
callback
}],
checkboxes
2021-04-10 15:52:21 +02:00
}).show();
};
const onError = (err: any) => {
if(err.type === 'USER_PRIVACY_RESTRICTED') {
toastNew({langPackKey: 'InviteToGroupError'});
}
};
2021-04-10 15:52:21 +02:00
if(isChannel) {
const tab = new AppAddMembersTab(this.slider);
tab.open({
type: 'channel',
skippable: false,
takeOut: (peerIds) => {
showConfirmation(peerIds, () => {
const promise = appChatsManager.inviteToChannel(id, peerIds);
promise.catch(onError);
tab.attachToPromise(promise);
2021-04-10 15:52:21 +02:00
});
return false;
},
title: 'GroupAddMembers',
placeholder: 'SendMessageTo'
});
} else {
new PopupPickUser({
peerTypes: ['contacts'],
placeholder: 'Search',
onSelect: (peerId) => {
setTimeout(() => {
showConfirmation([peerId], (checked) => {
appChatsManager.addChatUser(id, peerId, checked.size ? undefined : 0)
.catch(onError);
2021-04-10 15:52:21 +02:00
});
}, 0);
},
});
}
});
//console.log('construct shared media time:', performance.now() - perf);
}
2021-10-21 15:16:43 +02:00
public renderNewMessages(peerId: PeerId, 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
for(const mediaTab of this.searchSuper.mediaTabs) {
const inputFilter = mediaTab.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) {
const history = this.historiesStorage[peerId][inputFilter];
if(history) {
history.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, mediaTab, false);
}
}
}
}
2021-10-21 15:16:43 +02:00
public deleteDeletedMessages(peerId: PeerId, mids: number[]) {
if(this.init) return; // * not inited yet
if(!this.historiesStorage[peerId]) return;
for(const mid of mids) {
for(const type of this.searchSuper.mediaTabs) {
2020-12-25 00:19:34 +01:00
const inputFilter = type.inputFilter;
const history = this.historiesStorage[peerId][inputFilter];
if(!history) continue;
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];
const div = container.querySelector(`div[data-mid="${mid}"][data-peer-id="${peerId}"]`) as HTMLElement;
if(div) {
if(this.searchSuper.selection.isSelecting) {
this.searchSuper.selection.toggleByElement(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() {
// const perf = performance.now();
this.profile.cleanupHTML();
2021-09-15 07:02:03 +02:00
this.editBtn.classList.add('hide');
2021-04-09 18:44:43 +02:00
this.searchSuper.cleanupHTML(true);
2021-10-21 15:16:43 +02:00
this.container.classList.toggle('can-add-members', this.searchSuper.canViewMembers() && appChatsManager.hasRights(this.peerId.toChatId(), 'invite_users'));
2021-04-09 18:44:43 +02:00
// console.log('cleanupHTML shared media time:', performance.now() - perf);
}
public setLoadMutex(promise: Promise<any>) {
2020-12-25 00:19:34 +01:00
this.searchSuper.loadMutex = promise;
}
2021-10-21 15:16:43 +02:00
public setPeer(peerId: PeerId, threadId = 0) {
if(this.peerId === peerId && this.threadId === threadId) return false;
2021-04-09 18:44:43 +02:00
this.peerId = peerId;
this.threadId = threadId;
this.peerChanged = true;
2021-04-09 18:44:43 +02:00
if(this.init) {
this.init();
this.init = null;
}
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.profile.setPeer(peerId, threadId);
return true;
}
public fillProfileElements() {
if(!this.peerChanged) {
return;
}
this.peerChanged = false;
this.cleanupHTML();
this.profile.fillProfileElements();
2021-03-13 03:22:42 +01:00
this.toggleEditBtn();
}
private toggleEditBtn() {
let show: boolean;
2021-10-21 15:16:43 +02:00
if(this.peerId.isUser()) {
show = this.peerId !== rootScope.myId && appUsersManager.isContact(this.peerId);
2021-03-13 13:24:00 +01:00
} else {
2021-10-21 15:16:43 +02:00
show = appChatsManager.hasRights(this.peerId.toChatId(), 'change_info');
2021-03-13 13:24:00 +01:00
}
this.editBtn.classList.toggle('hide', !show);
2021-03-13 03:22:42 +01:00
}
public loadSidebarMedia(single: boolean, justLoad = false) {
this.searchSuper.load(single, justLoad);
}
onOpenAfterTimeout() {
2021-03-28 20:37:11 +02:00
this.scrollable.onScroll();
}
}
2021-04-09 18:44:43 +02:00
2021-10-05 22:40:07 +02:00
// MOUNT_CLASS_TO && (MOUNT_CLASS_TO.AppSharedMediaTab = AppSharedMediaTab);