2020-04-26 15:19:17 +03:00
|
|
|
|
//import apiManager from '../mtproto/apiManager';
|
2020-06-13 11:19:39 +03:00
|
|
|
|
import animationIntersector from '../../components/animationIntersector';
|
2020-10-10 01:36:06 +03:00
|
|
|
|
import appSidebarLeft from "../../components/sidebarLeft";
|
|
|
|
|
import appSidebarRight, { AppSidebarRight, RIGHT_COLUMN_ACTIVE_CLASSNAME } from '../../components/sidebarRight';
|
2020-10-31 17:58:13 +02:00
|
|
|
|
import mediaSizes, { ScreenSize } from '../../helpers/mediaSizes';
|
2020-10-10 01:36:06 +03:00
|
|
|
|
import { logger, LogLevels } from "../logger";
|
|
|
|
|
import apiManager from '../mtproto/mtprotoworker';
|
2020-09-29 05:03:26 +03:00
|
|
|
|
import { MOUNT_CLASS_TO } from '../mtproto/mtproto_config';
|
2020-11-15 05:33:47 +02:00
|
|
|
|
import rootScope from '../rootScope';
|
2020-10-02 23:33:32 +03:00
|
|
|
|
import apiUpdatesManager from './apiUpdatesManager';
|
2020-12-01 17:38:36 +02:00
|
|
|
|
import appUsersManager from "./appUsersManager";
|
2020-12-08 21:48:44 +02:00
|
|
|
|
import Chat, { ChatType } from '../../components/chat/chat';
|
2020-12-01 17:38:36 +02:00
|
|
|
|
import appChatsManager from './appChatsManager';
|
2020-10-10 01:36:06 +03:00
|
|
|
|
import appDocsManager from './appDocsManager';
|
2020-12-13 01:29:12 +02:00
|
|
|
|
import appInlineBotsManager from './appInlineBotsManager';
|
2020-12-01 17:38:36 +02:00
|
|
|
|
import appMessagesManager from './appMessagesManager';
|
|
|
|
|
import appPeersManager from './appPeersManager';
|
|
|
|
|
import appPhotosManager from './appPhotosManager';
|
|
|
|
|
import appProfileManager from './appProfileManager';
|
2020-10-10 01:36:06 +03:00
|
|
|
|
import appStickersManager from './appStickersManager';
|
2020-12-01 17:38:36 +02:00
|
|
|
|
import appWebPagesManager from './appWebPagesManager';
|
2020-12-16 05:22:58 +02:00
|
|
|
|
import { cancelEvent, getFilesFromEvent, placeCaretAtEnd } from '../../helpers/dom';
|
2020-12-18 05:07:32 +02:00
|
|
|
|
import PopupNewMedia from '../../components/popups/newMedia';
|
2021-01-31 03:52:14 +02:00
|
|
|
|
import { numberThousandSplitter } from '../../helpers/number';
|
2020-12-01 17:38:36 +02:00
|
|
|
|
import MarkupTooltip from '../../components/chat/markupTooltip';
|
|
|
|
|
import { isTouchSupported } from '../../helpers/touchSupport';
|
|
|
|
|
import appPollsManager from './appPollsManager';
|
2020-12-12 02:52:11 +02:00
|
|
|
|
import SetTransition from '../../components/singleTransition';
|
|
|
|
|
import ChatDragAndDrop from '../../components/chat/dragAndDrop';
|
2021-01-18 22:34:41 +04:00
|
|
|
|
import { debounce, pause } from '../../helpers/schedulers';
|
2021-01-04 14:09:48 +04:00
|
|
|
|
import lottieLoader from '../lottieLoader';
|
2021-01-18 22:34:41 +04:00
|
|
|
|
import useHeavyAnimationCheck, { dispatchHeavyAnimationEvent } from '../../hooks/useHeavyAnimationCheck';
|
2021-01-09 18:08:26 +04:00
|
|
|
|
import appDraftsManager from './appDraftsManager';
|
|
|
|
|
import serverTimeManager from '../mtproto/serverTimeManager';
|
2021-01-26 02:40:53 +02:00
|
|
|
|
import sessionStorage from '../sessionStorage';
|
2021-01-31 03:52:14 +02:00
|
|
|
|
import { renderImageFromUrl } from '../../components/misc';
|
|
|
|
|
import appDownloadManager from './appDownloadManager';
|
|
|
|
|
import appStateManager, { AppStateManager } from './appStateManager';
|
2020-02-06 22:43:07 +07:00
|
|
|
|
|
2020-06-21 15:25:17 +03:00
|
|
|
|
//console.log('appImManager included33!');
|
2020-02-06 22:43:07 +07:00
|
|
|
|
|
2020-04-24 03:48:48 +03:00
|
|
|
|
appSidebarLeft; // just to include
|
|
|
|
|
|
2020-09-26 01:47:43 +03:00
|
|
|
|
const LEFT_COLUMN_ACTIVE_CLASSNAME = 'is-left-column-shown';
|
|
|
|
|
|
2020-10-21 22:17:49 +03:00
|
|
|
|
export const CHAT_ANIMATION_GROUP = 'chat';
|
2020-12-24 07:26:29 +02:00
|
|
|
|
const FOCUS_EVENT_NAME = isTouchSupported ? 'touchstart' : 'mousemove';
|
2020-08-24 17:09:31 +03:00
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
|
export class AppImManager {
|
2020-06-05 19:01:06 +03:00
|
|
|
|
public columnEl = document.getElementById('column-center') as HTMLDivElement;
|
2020-12-01 17:38:36 +02:00
|
|
|
|
public chatsContainer: HTMLElement;
|
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
|
public offline = false;
|
|
|
|
|
public updateStatusInterval = 0;
|
2020-04-16 03:48:41 +03:00
|
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
|
public log: ReturnType<typeof logger>;
|
2020-04-16 03:48:41 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
public setPeerPromise: Promise<void> = null;
|
2020-05-30 09:44:54 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
public tabId = -1;
|
2020-09-26 01:47:43 +03:00
|
|
|
|
public hideRightSidebar = false;
|
2020-11-23 18:45:50 +02:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
private chats: Chat[] = [];
|
2020-12-21 05:11:26 +02:00
|
|
|
|
private prevTab: HTMLElement;
|
|
|
|
|
private chatsSelectTabDebounced: () => void;
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
public markupTooltip: MarkupTooltip;
|
2020-06-05 19:01:06 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
get myId() {
|
|
|
|
|
return rootScope.myId;
|
2020-10-02 23:33:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
get chat(): Chat {
|
|
|
|
|
return this.chats[this.chats.length - 1];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
|
constructor() {
|
2020-10-02 23:33:32 +03:00
|
|
|
|
apiUpdatesManager.attach();
|
2020-10-03 03:35:26 +03:00
|
|
|
|
|
2020-08-25 12:39:39 +03:00
|
|
|
|
this.log = logger('IM', LogLevels.log | LogLevels.warn | LogLevels.debug | LogLevels.error);
|
2020-09-26 01:47:43 +03:00
|
|
|
|
|
2020-06-05 19:01:06 +03:00
|
|
|
|
this.selectTab(0);
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
|
window.addEventListener('blur', () => {
|
2020-06-13 11:19:39 +03:00
|
|
|
|
animationIntersector.checkAnimations(true);
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-11-15 05:33:47 +02:00
|
|
|
|
this.offline = rootScope.idle.isIDLE = true;
|
2020-02-06 22:43:07 +07:00
|
|
|
|
this.updateStatus();
|
|
|
|
|
clearInterval(this.updateStatusInterval);
|
|
|
|
|
|
|
|
|
|
window.addEventListener('focus', () => {
|
2020-11-15 05:33:47 +02:00
|
|
|
|
this.offline = rootScope.idle.isIDLE = false;
|
2020-02-06 22:43:07 +07:00
|
|
|
|
this.updateStatus();
|
|
|
|
|
this.updateStatusInterval = window.setInterval(() => this.updateStatus(), 50e3);
|
2020-08-22 19:53:59 +03:00
|
|
|
|
|
|
|
|
|
// в обратном порядке
|
|
|
|
|
animationIntersector.checkAnimations(false);
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}, {once: true});
|
|
|
|
|
});
|
2020-09-26 01:47:43 +03:00
|
|
|
|
|
2020-12-24 07:26:29 +02:00
|
|
|
|
// * Prevent setting online after reloading page
|
|
|
|
|
window.addEventListener(FOCUS_EVENT_NAME, () => {
|
|
|
|
|
this.updateStatusInterval = window.setInterval(() => this.updateStatus(), 50e3);
|
|
|
|
|
this.updateStatus();
|
|
|
|
|
}, {once: true, passive: true});
|
2020-06-13 11:19:39 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.chatsContainer = document.createElement('div');
|
|
|
|
|
this.chatsContainer.classList.add('chats-container', 'tabs-container');
|
2021-01-18 22:34:41 +04:00
|
|
|
|
this.chatsContainer.dataset.animation = 'navigation';
|
2020-08-19 15:29:10 +03:00
|
|
|
|
|
2020-12-21 05:11:26 +02:00
|
|
|
|
this.columnEl.append(this.chatsContainer);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
|
|
|
|
|
this.createNewChat();
|
2020-12-21 05:11:26 +02:00
|
|
|
|
this.chatsSelectTab(this.chat.container);
|
2020-11-25 21:36:18 +02:00
|
|
|
|
|
2020-12-16 00:13:54 +02:00
|
|
|
|
window.addEventListener('hashchange', (e) => {
|
|
|
|
|
const hash = location.hash;
|
|
|
|
|
const splitted = hash.split('?');
|
|
|
|
|
|
2020-12-19 03:07:24 +02:00
|
|
|
|
if(!splitted[1]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 00:13:54 +02:00
|
|
|
|
const params: any = {};
|
|
|
|
|
splitted[1].split('&').forEach(item => {
|
|
|
|
|
params[item.split('=')[0]] = decodeURIComponent(item.split('=')[1]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-03 06:15:28 +02:00
|
|
|
|
this.log('hashchange', hash, splitted[0], params);
|
2020-12-16 00:13:54 +02:00
|
|
|
|
|
|
|
|
|
switch(splitted[0]) {
|
|
|
|
|
case '#/im': {
|
|
|
|
|
const p = params.p;
|
2021-02-03 06:15:28 +02:00
|
|
|
|
let postId = params.post !== undefined ? appMessagesManager.generateMessageId(+params.post) : undefined;
|
|
|
|
|
|
|
|
|
|
switch(p[0]) {
|
|
|
|
|
case '@': {
|
|
|
|
|
appUsersManager.resolveUsername(p).then(peer => {
|
2021-02-03 06:36:59 +02:00
|
|
|
|
const isUser = peer._ === 'user';
|
2021-02-03 06:15:28 +02:00
|
|
|
|
const peerId = isUser ? peer.id : -peer.id;
|
|
|
|
|
|
|
|
|
|
this.setInnerPeer(peerId, postId);
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: { // peerId
|
|
|
|
|
this.setInnerPeer(postId ? -+p : +p, postId);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-16 00:13:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location.hash = '';
|
|
|
|
|
});
|
2021-01-03 16:02:35 +04:00
|
|
|
|
|
|
|
|
|
this.setSettings();
|
|
|
|
|
rootScope.on('settings_updated', () => this.setSettings());
|
2021-01-07 16:36:21 +04:00
|
|
|
|
|
|
|
|
|
useHeavyAnimationCheck(() => {
|
|
|
|
|
animationIntersector.setOnlyOnePlayableGroup('lock');
|
|
|
|
|
animationIntersector.checkAnimations(true);
|
|
|
|
|
}, () => {
|
|
|
|
|
animationIntersector.setOnlyOnePlayableGroup('');
|
|
|
|
|
animationIntersector.checkAnimations(false);
|
|
|
|
|
});
|
2021-01-26 02:40:53 +02:00
|
|
|
|
|
2021-01-31 03:52:14 +02:00
|
|
|
|
const isDefaultBackground = rootScope.settings.background.blur === AppStateManager.STATE_INIT.settings.background.blur &&
|
|
|
|
|
rootScope.settings.background.slug === AppStateManager.STATE_INIT.settings.background.slug;
|
|
|
|
|
if(!isDefaultBackground) {
|
2021-02-06 19:18:53 +02:00
|
|
|
|
appDownloadManager.cacheStorage.getFile('backgrounds/' + rootScope.settings.background.slug).then(blob => {
|
2021-01-31 03:52:14 +02:00
|
|
|
|
this.setBackground(URL.createObjectURL(blob), false);
|
2021-02-03 23:48:01 +02:00
|
|
|
|
}, () => { // * if NO_ENTRY_FOUND
|
|
|
|
|
this.setBackground('');
|
|
|
|
|
appStateManager.setByKey('settings.background', AppStateManager.STATE_INIT.settings.background); // * reset background
|
2021-01-31 03:52:14 +02:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setBackground('');
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 02:40:53 +02:00
|
|
|
|
/* rootScope.on('peer_changing', (chat) => {
|
|
|
|
|
this.saveChatPosition(chat);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sessionStorage.get('chatPositions').then((c) => {
|
|
|
|
|
sessionStorage.setToCache('chatPositions', c || {});
|
|
|
|
|
}); */
|
2021-01-03 16:02:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 03:52:14 +02:00
|
|
|
|
public setBackground(url: string, broadcastEvent = true): Promise<void> {
|
|
|
|
|
const promises = this.chats.map(chat => chat.setBackground(url));
|
|
|
|
|
return promises[promises.length - 1].then(() => {
|
|
|
|
|
if(broadcastEvent) {
|
|
|
|
|
rootScope.broadcast('background_change');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 02:40:53 +02:00
|
|
|
|
/* public saveChatPosition(chat: Chat) {
|
|
|
|
|
const bubble = chat.bubbles.getBubbleByPoint('top');
|
|
|
|
|
if(bubble) {
|
|
|
|
|
const top = bubble.getBoundingClientRect().top;
|
|
|
|
|
|
|
|
|
|
this.log('saving position by bubble:', bubble, top);
|
|
|
|
|
|
|
|
|
|
const key = chat.peerId + (chat.threadId ? '_' + chat.threadId : '');
|
|
|
|
|
|
|
|
|
|
const chatPositions = sessionStorage.getFromCache('chatPositions');
|
|
|
|
|
chatPositions[key] = {
|
|
|
|
|
mid: +bubble.dataset.mid,
|
|
|
|
|
top
|
|
|
|
|
};
|
|
|
|
|
sessionStorage.set({chatPositions});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getChatSavedPosition(chat: Chat) {
|
|
|
|
|
const key = chat.peerId + (chat.threadId ? '_' + chat.threadId : '');
|
|
|
|
|
return sessionStorage.getFromCache('chatPositions')[key];
|
|
|
|
|
} */
|
|
|
|
|
|
2021-01-03 16:02:35 +04:00
|
|
|
|
private setSettings() {
|
|
|
|
|
document.documentElement.style.setProperty('--messages-text-size', rootScope.settings.messagesTextSize + 'px');
|
2021-01-03 19:59:13 +04:00
|
|
|
|
|
2021-02-06 19:18:53 +02:00
|
|
|
|
if(rootScope.settings.background.highlightningColor) {
|
|
|
|
|
document.documentElement.style.setProperty('--message-highlightning-color', rootScope.settings.background.highlightningColor);
|
|
|
|
|
} else {
|
|
|
|
|
document.documentElement.style.removeProperty('--message-highlightning-color');
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 19:59:13 +04:00
|
|
|
|
document.body.classList.toggle('animation-level-0', !rootScope.settings.animationsEnabled);
|
|
|
|
|
document.body.classList.toggle('animation-level-1', false);
|
|
|
|
|
document.body.classList.toggle('animation-level-2', rootScope.settings.animationsEnabled);
|
|
|
|
|
|
|
|
|
|
this.chatsSelectTabDebounced = debounce(() => {
|
|
|
|
|
const topbar = this.chat.topbar;
|
|
|
|
|
if(topbar.pinnedMessage) { // * буду молиться богам, чтобы это ничего не сломало, но это исправляет получение пиннеда после анимации
|
|
|
|
|
topbar.pinnedMessage.setCorrectIndex(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiManager.setQueueId(this.chat.bubbles.lazyLoadQueue.queueId);
|
|
|
|
|
}, rootScope.settings.animationsEnabled ? 250 : 0, false, true);
|
2021-01-04 14:09:48 +04:00
|
|
|
|
|
|
|
|
|
lottieLoader.setLoop(rootScope.settings.stickers.loop);
|
|
|
|
|
animationIntersector.checkAnimations(false);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
}
|
2020-06-13 11:19:39 +03:00
|
|
|
|
|
2021-01-18 22:34:41 +04:00
|
|
|
|
// * не могу использовать тут TransitionSlider, так как мне нужен отрисованный блок рядом
|
|
|
|
|
// * (или под текущим чатом) чтобы правильно отрендерить чат (напр. scrollTop)
|
2020-12-21 05:11:26 +02:00
|
|
|
|
private chatsSelectTab(tab: HTMLElement) {
|
|
|
|
|
if(this.prevTab === tab) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.prevTab) {
|
|
|
|
|
this.prevTab.classList.remove('active');
|
|
|
|
|
this.chatsSelectTabDebounced();
|
2021-01-18 22:34:41 +04:00
|
|
|
|
|
|
|
|
|
if(rootScope.settings.animationsEnabled) { // ! нужно переделать на animation, так как при лаге анимация будет длиться не 250мс
|
2021-02-07 05:35:29 +02:00
|
|
|
|
dispatchHeavyAnimationEvent(pause(250 + 150), 250 + 150);
|
2021-01-18 22:34:41 +04:00
|
|
|
|
}
|
2020-12-21 05:11:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tab.classList.add('active');
|
|
|
|
|
this.prevTab = tab;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
private init() {
|
|
|
|
|
document.addEventListener('paste', this.onDocumentPaste, true);
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-11-07 05:48:07 +02:00
|
|
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
2020-11-15 05:33:47 +02:00
|
|
|
|
if(rootScope.overlayIsActive) return;
|
2020-10-13 00:06:02 +03:00
|
|
|
|
|
2020-11-07 05:48:07 +02:00
|
|
|
|
const target = e.target as HTMLElement;
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2021-02-04 02:30:23 +02:00
|
|
|
|
//if(target.tagName === 'INPUT') return;
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-12-08 21:48:44 +02:00
|
|
|
|
//this.log('onkeydown', e, document.activeElement);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
|
|
|
|
|
const chat = this.chat;
|
2020-10-13 00:06:02 +03:00
|
|
|
|
|
2020-12-24 07:26:29 +02:00
|
|
|
|
if(e.key === 'Escape') {
|
2020-12-08 21:48:44 +02:00
|
|
|
|
let cancel = true;
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(this.markupTooltip?.container?.classList.contains('is-visible')) {
|
|
|
|
|
this.markupTooltip.hide();
|
|
|
|
|
} else if(chat.selection.isSelecting) {
|
|
|
|
|
chat.selection.cancelSelection();
|
|
|
|
|
} else if(chat.container.classList.contains('is-helper-active')) {
|
|
|
|
|
chat.input.replyElements.cancelBtn.click();
|
2020-12-24 07:26:29 +02:00
|
|
|
|
} else if(chat.peerId !== 0) { // hide current dialog
|
2020-05-30 09:44:54 +03:00
|
|
|
|
this.setPeer(0);
|
2020-12-08 21:48:44 +02:00
|
|
|
|
} else {
|
|
|
|
|
cancel = false;
|
2020-11-21 15:13:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * cancel event for safari, because if application is in fullscreen, browser will try to exit fullscreen
|
2020-12-08 21:48:44 +02:00
|
|
|
|
if(cancel) {
|
2020-11-21 15:13:23 +02:00
|
|
|
|
cancelEvent(e);
|
2020-05-30 09:44:54 +03:00
|
|
|
|
}
|
2020-12-24 07:26:29 +02:00
|
|
|
|
} else if(e.key === 'Meta' || e.key === 'Control') {
|
2020-02-13 22:42:39 +07:00
|
|
|
|
return;
|
2020-12-24 07:26:29 +02:00
|
|
|
|
} else if(e.code === "KeyC" && (e.ctrlKey || e.metaKey) && target.tagName !== 'INPUT') {
|
2020-02-13 22:42:39 +07:00
|
|
|
|
return;
|
2020-12-24 07:26:29 +02:00
|
|
|
|
} else if(e.code === 'ArrowUp') {
|
2021-01-23 00:12:57 +02:00
|
|
|
|
if(!chat.input.editMsgId && chat.input.isInputEmpty()) {
|
2021-01-03 16:02:35 +04:00
|
|
|
|
const history = appMessagesManager.getHistoryStorage(chat.peerId, chat.threadId);
|
2020-12-18 05:07:32 +02:00
|
|
|
|
if(history.history.length) {
|
2020-11-19 01:51:39 +02:00
|
|
|
|
let goodMid: number;
|
|
|
|
|
for(const mid of history.history) {
|
2021-01-03 16:02:35 +04:00
|
|
|
|
const message = chat.getMessage(mid);
|
2020-12-24 07:26:29 +02:00
|
|
|
|
const good = this.myId === chat.peerId ? message.fromId === this.myId : message.pFlags.out;
|
2020-11-19 01:51:39 +02:00
|
|
|
|
|
|
|
|
|
if(good) {
|
2020-12-24 07:26:29 +02:00
|
|
|
|
if(appMessagesManager.canEditMessage(chat.getMessage(mid), 'text')) {
|
2020-11-19 01:51:39 +02:00
|
|
|
|
goodMid = mid;
|
2020-12-24 07:26:29 +02:00
|
|
|
|
break;
|
2020-11-19 01:51:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 07:26:29 +02:00
|
|
|
|
// * this check will allow editing only last message
|
|
|
|
|
//break;
|
2020-11-19 01:51:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(goodMid) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
chat.input.initMessageEditing(goodMid);
|
2020-12-08 21:48:44 +02:00
|
|
|
|
cancelEvent(e); // * prevent from scrolling
|
2020-11-19 01:51:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-01 17:38:36 +02:00
|
|
|
|
}
|
2020-02-13 22:42:39 +07:00
|
|
|
|
}
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2021-01-25 21:06:44 +02:00
|
|
|
|
if(chat.input.messageInput && e.target !== chat.input.messageInput && target.tagName !== 'INPUT' && !target.hasAttribute('contenteditable') && !isTouchSupported) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
chat.input.messageInput.focus();
|
|
|
|
|
placeCaretAtEnd(chat.input.messageInput);
|
2020-02-11 13:22:12 +07:00
|
|
|
|
}
|
|
|
|
|
};
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-02-11 13:22:12 +07:00
|
|
|
|
document.body.addEventListener('keydown', onKeyDown);
|
2020-04-22 22:10:09 +03:00
|
|
|
|
|
2020-12-18 05:07:32 +02:00
|
|
|
|
rootScope.addEventListener('history_multiappend', (e) => {
|
2021-01-08 13:55:37 +04:00
|
|
|
|
const msgIdsByPeer = e;
|
2020-12-18 05:07:32 +02:00
|
|
|
|
|
|
|
|
|
for(const peerId in msgIdsByPeer) {
|
|
|
|
|
appSidebarRight.sharedMediaTab.renderNewMessages(+peerId, msgIdsByPeer[peerId]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rootScope.addEventListener('history_delete', (e) => {
|
2021-01-08 13:55:37 +04:00
|
|
|
|
const {peerId, msgs} = e;
|
2020-12-18 05:07:32 +02:00
|
|
|
|
|
|
|
|
|
const mids = Object.keys(msgs).map(s => +s);
|
|
|
|
|
appSidebarRight.sharedMediaTab.deleteDeletedMessages(peerId, mids);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Calls when message successfully sent and we have an id
|
|
|
|
|
rootScope.addEventListener('message_sent', (e) => {
|
2021-01-08 13:55:37 +04:00
|
|
|
|
const {storage, tempId, mid} = e;
|
2020-12-18 05:07:32 +02:00
|
|
|
|
const message = appMessagesManager.getMessageFromStorage(storage, mid);
|
|
|
|
|
appSidebarRight.sharedMediaTab.renderNewMessages(message.peerId, [mid]);
|
|
|
|
|
});
|
|
|
|
|
|
2020-12-12 02:52:11 +02:00
|
|
|
|
if(!isTouchSupported) {
|
|
|
|
|
this.attachDragAndDropListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 01:29:12 +02:00
|
|
|
|
//if(!isTouchSupported) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.markupTooltip = new MarkupTooltip(this);
|
|
|
|
|
this.markupTooltip.handleSelection();
|
2020-12-13 01:29:12 +02:00
|
|
|
|
//}
|
2020-12-01 17:38:36 +02:00
|
|
|
|
}
|
2020-05-03 15:46:05 +03:00
|
|
|
|
|
2020-12-12 02:52:11 +02:00
|
|
|
|
private attachDragAndDropListeners() {
|
|
|
|
|
const drops: ChatDragAndDrop[] = [];
|
|
|
|
|
let mounted = false;
|
|
|
|
|
const toggle = async(e: DragEvent, mount: boolean) => {
|
2021-02-03 06:36:59 +02:00
|
|
|
|
if(mount === mounted) return;
|
2020-12-12 02:52:11 +02:00
|
|
|
|
|
2020-12-12 03:04:58 +02:00
|
|
|
|
const _types = e.dataTransfer.types;
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const isFiles = _types.contains ? _types.contains('Files') : _types.indexOf('Files') >= 0;
|
|
|
|
|
|
2020-12-12 17:06:19 +02:00
|
|
|
|
if(!isFiles || !this.canDrag()) { // * skip dragging text case
|
2020-12-12 03:04:58 +02:00
|
|
|
|
counter = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 02:52:11 +02:00
|
|
|
|
if(mount && !drops.length) {
|
|
|
|
|
const types: string[] = await getFilesFromEvent(e, true)
|
2020-12-12 03:04:58 +02:00
|
|
|
|
const force = isFiles && !types.length; // * can't get file items not from 'drop' on Safari
|
2020-12-12 02:52:11 +02:00
|
|
|
|
|
|
|
|
|
const foundMedia = types.filter(t => ['image', 'video'].includes(t.split('/')[0])).length;
|
|
|
|
|
const foundDocuments = types.length - foundMedia;
|
|
|
|
|
|
|
|
|
|
this.log('drag files', types);
|
|
|
|
|
|
2020-12-12 03:04:58 +02:00
|
|
|
|
if(types.length || force) {
|
2020-12-12 02:52:11 +02:00
|
|
|
|
drops.push(new ChatDragAndDrop(dropsContainer, {
|
|
|
|
|
icon: 'dragfiles',
|
|
|
|
|
header: 'Drop files here to send them',
|
|
|
|
|
subtitle: 'without compression',
|
|
|
|
|
onDrop: (e: DragEvent) => {
|
|
|
|
|
toggle(e, false);
|
|
|
|
|
appImManager.log('drop', e);
|
|
|
|
|
appImManager.onDocumentPaste(e, 'document');
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 03:04:58 +02:00
|
|
|
|
if((foundMedia && !foundDocuments) || force) {
|
2020-12-12 02:52:11 +02:00
|
|
|
|
drops.push(new ChatDragAndDrop(dropsContainer, {
|
|
|
|
|
icon: 'dragmedia',
|
|
|
|
|
header: 'Drop files here to send them',
|
|
|
|
|
subtitle: 'in a quick way',
|
|
|
|
|
onDrop: (e: DragEvent) => {
|
|
|
|
|
toggle(e, false);
|
|
|
|
|
appImManager.log('drop', e);
|
|
|
|
|
appImManager.onDocumentPaste(e, 'media');
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.chat.container.append(dropsContainer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//if(!mount) return;
|
|
|
|
|
|
|
|
|
|
SetTransition(dropsContainer, 'is-visible', mount, 200, () => {
|
|
|
|
|
if(!mount) {
|
|
|
|
|
drops.forEach(drop => {
|
|
|
|
|
drop.destroy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
drops.length = 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(mount) {
|
|
|
|
|
drops.forEach(drop => {
|
|
|
|
|
drop.setPath();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
counter = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.body.classList.toggle('is-dragging', mount);
|
|
|
|
|
mounted = mount;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* document.body.addEventListener('dragover', (e) => {
|
|
|
|
|
cancelEvent(e);
|
|
|
|
|
}); */
|
|
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
|
document.body.addEventListener('dragenter', (e) => {
|
|
|
|
|
counter++;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.body.addEventListener('dragover', (e) => {
|
|
|
|
|
//this.log('dragover', e/* , e.dataTransfer.types[0] */);
|
|
|
|
|
toggle(e, true);
|
|
|
|
|
cancelEvent(e);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.body.addEventListener('dragleave', (e) => {
|
|
|
|
|
//this.log('dragleave', e, counter);
|
|
|
|
|
//if((e.pageX <= 0 || e.pageX >= appPhotosManager.windowW) || (e.pageY <= 0 || e.pageY >= appPhotosManager.windowH)) {
|
|
|
|
|
counter--;
|
|
|
|
|
if(counter === 0) {
|
|
|
|
|
//if(!findUpClassName(e.target, 'drops-container')) {
|
|
|
|
|
toggle(e, false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const dropsContainer = document.createElement('div');
|
|
|
|
|
dropsContainer.classList.add('drops-container');
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 17:06:19 +02:00
|
|
|
|
private canDrag() {
|
2020-12-11 04:06:16 +02:00
|
|
|
|
const peerId = this.chat?.peerId;
|
2020-12-12 17:06:19 +02:00
|
|
|
|
return !(!peerId || rootScope.overlayIsActive || (peerId < 0 && !appChatsManager.hasRights(peerId, 'send', 'send_media')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onDocumentPaste = (e: ClipboardEvent | DragEvent, attachType?: 'media' | 'document') => {
|
|
|
|
|
if(!this.canDrag()) return;
|
2020-08-19 15:29:10 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
//console.log('document paste');
|
|
|
|
|
//console.log('item', event.clipboardData.getData());
|
2020-12-12 02:52:11 +02:00
|
|
|
|
|
2020-12-13 04:32:55 +02:00
|
|
|
|
if(e instanceof DragEvent) {
|
|
|
|
|
const _types = e.dataTransfer.types;
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const isFiles = _types.contains ? _types.contains('Files') : _types.indexOf('Files') >= 0;
|
|
|
|
|
if(isFiles) {
|
|
|
|
|
cancelEvent(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 02:52:11 +02:00
|
|
|
|
getFilesFromEvent(e).then((files: File[]) => {
|
|
|
|
|
if(files.length) {
|
2021-02-03 06:36:59 +02:00
|
|
|
|
if(attachType === 'media' && files.find(file => !['image', 'video'].includes(file.type.split('/')[0]))) {
|
2020-12-12 02:52:11 +02:00
|
|
|
|
attachType = 'document';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chatInput = this.chat.input;
|
|
|
|
|
chatInput.willAttachType = attachType || (files[0].type.indexOf('image/') === 0 ? 'media' : "document");
|
2020-12-18 05:07:32 +02:00
|
|
|
|
new PopupNewMedia(this.chat, files, chatInput.willAttachType);
|
2020-05-03 15:46:05 +03:00
|
|
|
|
}
|
2020-12-12 02:52:11 +02:00
|
|
|
|
});
|
2020-11-23 18:45:50 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-26 01:47:43 +03:00
|
|
|
|
public selectTab(id: number) {
|
2020-12-24 07:26:29 +02:00
|
|
|
|
document.body.classList.toggle(LEFT_COLUMN_ACTIVE_CLASSNAME, id === 0);
|
2020-09-26 01:47:43 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
const prevTabId = this.tabId;
|
2021-01-18 22:34:41 +04:00
|
|
|
|
|
|
|
|
|
this.log('selectTab', id, prevTabId);
|
|
|
|
|
|
|
|
|
|
if(prevTabId !== -1 && prevTabId !== id && rootScope.settings.animationsEnabled) {
|
2021-02-07 05:35:29 +02:00
|
|
|
|
const transitionTime = (mediaSizes.isMobile ? 250 : 200) + 100; // * cause transition time could be > 250ms
|
2021-01-18 22:34:41 +04:00
|
|
|
|
dispatchHeavyAnimationEvent(pause(transitionTime), transitionTime);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
this.tabId = id;
|
2020-12-24 07:26:29 +02:00
|
|
|
|
if(mediaSizes.isMobile && prevTabId === 2 && id === 1) {
|
2020-10-16 18:16:05 +03:00
|
|
|
|
//appSidebarRight.toggleSidebar(false);
|
|
|
|
|
document.body.classList.remove(RIGHT_COLUMN_ACTIVE_CLASSNAME);
|
|
|
|
|
}
|
2020-10-16 19:53:57 +03:00
|
|
|
|
|
2021-01-25 21:06:44 +02:00
|
|
|
|
rootScope.broadcast('im_tab_change', id);
|
|
|
|
|
|
2020-12-21 02:38:21 +02:00
|
|
|
|
//this._selectTab(id, mediaSizes.isMobile);
|
2021-02-04 02:30:23 +02:00
|
|
|
|
//document.body.classList.toggle(RIGHT_COLUMN_ACTIVE_CLASSNAME, id === 2);
|
2020-09-26 01:47:43 +03:00
|
|
|
|
}
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
|
public updateStatus() {
|
2020-12-11 04:06:16 +02:00
|
|
|
|
if(!this.myId) return Promise.resolve();
|
2020-03-02 00:26:25 +07:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
appUsersManager.setUserStatus(this.myId, this.offline);
|
2020-02-22 23:00:17 +07:00
|
|
|
|
return apiManager.invokeApi('account.updateStatus', {offline: this.offline});
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
private createNewChat() {
|
2021-01-26 02:40:53 +02:00
|
|
|
|
const chat = new Chat(this, appChatsManager, appDocsManager, appInlineBotsManager, appMessagesManager, appPeersManager, appPhotosManager, appProfileManager, appStickersManager, appUsersManager, appWebPagesManager, appPollsManager, apiManager, appDraftsManager, serverTimeManager, sessionStorage);
|
2020-05-02 00:28:40 +03:00
|
|
|
|
|
2021-01-31 03:52:14 +02:00
|
|
|
|
if(this.chats.length) {
|
|
|
|
|
chat.backgroundEl.append(this.chat.backgroundEl.lastElementChild.cloneNode(true));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.chats.push(chat);
|
2020-05-02 00:28:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
private spliceChats(fromIndex: number, justReturn = true) {
|
|
|
|
|
if(fromIndex >= this.chats.length) return;
|
2020-11-09 22:57:06 +03:00
|
|
|
|
|
2021-01-09 18:08:26 +04:00
|
|
|
|
if(this.chats.length > 1 && justReturn) {
|
|
|
|
|
rootScope.broadcast('peer_changing', this.chat);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
const spliced = this.chats.splice(fromIndex, this.chats.length - fromIndex);
|
2020-04-08 18:46:43 +03:00
|
|
|
|
|
2020-12-24 00:03:34 +02:00
|
|
|
|
// * fix middle chat z-index on animation
|
|
|
|
|
if(spliced.length > 1) {
|
|
|
|
|
spliced.slice(0, -1).forEach(chat => {
|
|
|
|
|
chat.container.remove();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.chatsSelectTab(this.chat.container);
|
2020-04-08 18:46:43 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(justReturn) {
|
2020-12-11 04:06:16 +02:00
|
|
|
|
rootScope.broadcast('peer_changed', this.chat.peerId);
|
2020-04-22 22:10:09 +03:00
|
|
|
|
|
2020-12-24 07:26:29 +02:00
|
|
|
|
if(appSidebarRight.historyTabIds[appSidebarRight.historyTabIds.length - 1] === AppSidebarRight.SLIDERITEMSIDS.search) {
|
2020-12-24 11:13:21 +02:00
|
|
|
|
appSidebarRight.closeTab(AppSidebarRight.SLIDERITEMSIDS.search);
|
2020-04-22 22:10:09 +03:00
|
|
|
|
}
|
2020-11-25 21:36:18 +02:00
|
|
|
|
|
2020-12-23 04:18:24 +02:00
|
|
|
|
appSidebarRight.sharedMediaTab.setPeer(this.chat.peerId, this.chat.threadId);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
appSidebarRight.sharedMediaTab.loadSidebarMedia(true);
|
|
|
|
|
appSidebarRight.sharedMediaTab.fillProfileElements();
|
2020-12-14 18:21:12 +02:00
|
|
|
|
|
2020-12-20 05:54:35 +02:00
|
|
|
|
/* setTimeout(() => {
|
2020-12-14 18:21:12 +02:00
|
|
|
|
appSidebarRight.sharedMediaTab.loadSidebarMedia(false);
|
2020-12-20 05:54:35 +02:00
|
|
|
|
}); */
|
2020-11-25 21:36:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
//chat.setPeer(0);
|
|
|
|
|
spliced.forEach(chat => {
|
|
|
|
|
chat.destroy();
|
|
|
|
|
});
|
2021-02-07 05:35:29 +02:00
|
|
|
|
}, 250 + 100);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
}
|
2020-04-22 22:10:09 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
public setPeer(peerId: number, lastMsgId?: number): boolean {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(this.init) {
|
|
|
|
|
this.init();
|
|
|
|
|
this.init = null;
|
|
|
|
|
}
|
2020-06-16 23:48:08 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
const chat = this.chat;
|
|
|
|
|
const chatIndex = this.chats.indexOf(chat);
|
2020-06-16 23:48:08 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
if(!peerId) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(chatIndex > 0) {
|
|
|
|
|
this.spliceChats(chatIndex);
|
|
|
|
|
return;
|
2020-12-23 05:40:30 +02:00
|
|
|
|
} else if(mediaSizes.activeScreen === ScreenSize.medium) { // * floating sidebar case
|
2020-12-01 17:38:36 +02:00
|
|
|
|
const isNowOpen = document.body.classList.toggle(LEFT_COLUMN_ACTIVE_CLASSNAME);
|
2020-06-16 23:48:08 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(isNowOpen && document.body.classList.contains(RIGHT_COLUMN_ACTIVE_CLASSNAME)) {
|
|
|
|
|
appSidebarRight.toggleSidebar(false, false);
|
2020-12-23 05:40:30 +02:00
|
|
|
|
this.selectTab(0);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.hideRightSidebar = isNowOpen;
|
|
|
|
|
} else if(this.hideRightSidebar) {
|
|
|
|
|
appSidebarRight.toggleSidebar(true);
|
2020-02-10 21:51:24 +07:00
|
|
|
|
}
|
2020-04-14 18:46:31 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
return;
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}
|
2020-12-24 07:26:29 +02:00
|
|
|
|
} else if(chatIndex > 0 && chat.peerId && chat.peerId !== peerId) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
this.spliceChats(1, false);
|
2020-12-11 04:06:16 +02:00
|
|
|
|
return this.setPeer(peerId, lastMsgId);
|
2020-10-30 02:23:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 05:11:26 +02:00
|
|
|
|
// * don't reset peer if returning
|
|
|
|
|
if(peerId === chat.peerId && mediaSizes.activeScreen === ScreenSize.mobile && document.body.classList.contains(LEFT_COLUMN_ACTIVE_CLASSNAME)) {
|
|
|
|
|
this.selectTab(1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 02:38:21 +02:00
|
|
|
|
if(peerId || mediaSizes.activeScreen !== ScreenSize.mobile) {
|
|
|
|
|
const result = chat.setPeer(peerId, lastMsgId);
|
|
|
|
|
|
2020-12-21 05:11:26 +02:00
|
|
|
|
// * wait for cached render
|
2020-12-21 02:38:21 +02:00
|
|
|
|
const promise = result?.cached ? result.promise : Promise.resolve();
|
|
|
|
|
if(peerId) {
|
|
|
|
|
promise.then(() => {
|
|
|
|
|
//window.requestAnimationFrame(() => {
|
|
|
|
|
setTimeout(() => { // * setTimeout is better here
|
|
|
|
|
if(this.hideRightSidebar) {
|
|
|
|
|
appSidebarRight.toggleSidebar(true);
|
|
|
|
|
this.hideRightSidebar = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 05:11:26 +02:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.chatsSelectTab(this.chat.container);
|
|
|
|
|
}, 0);
|
2020-12-21 02:38:21 +02:00
|
|
|
|
this.selectTab(1);
|
|
|
|
|
}, 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-18 04:21:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 02:38:21 +02:00
|
|
|
|
if(!peerId) {
|
2020-09-26 01:47:43 +03:00
|
|
|
|
this.selectTab(0);
|
2020-04-14 18:46:31 +03:00
|
|
|
|
return false;
|
2020-02-08 13:03:09 +07:00
|
|
|
|
}
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}
|
2020-04-22 22:10:09 +03:00
|
|
|
|
|
2020-12-23 03:18:07 +02:00
|
|
|
|
public setInnerPeer(peerId: number, lastMsgId?: number, type: ChatType = 'chat', threadId?: number) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
// * prevent opening already opened peer
|
2021-01-09 18:08:26 +04:00
|
|
|
|
const existingIndex = this.chats.findIndex(chat => chat.peerId === peerId && chat.type === type);
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(existingIndex !== -1) {
|
|
|
|
|
this.spliceChats(existingIndex + 1);
|
2020-12-11 04:06:16 +02:00
|
|
|
|
return this.setPeer(peerId, lastMsgId);
|
2020-04-22 22:10:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 10:08:50 +02:00
|
|
|
|
const chat = this.chat;
|
|
|
|
|
if(!chat.init) { // * use first not inited chat
|
|
|
|
|
this.createNewChat();
|
|
|
|
|
}
|
2020-05-18 04:21:58 +03:00
|
|
|
|
|
2020-12-08 21:48:44 +02:00
|
|
|
|
if(type) {
|
2020-12-16 05:22:58 +02:00
|
|
|
|
this.chat.setType(type);
|
2020-12-23 03:18:07 +02:00
|
|
|
|
|
|
|
|
|
if(threadId) {
|
|
|
|
|
this.chat.threadId = threadId;
|
|
|
|
|
}
|
2020-12-08 21:48:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 05:11:26 +02:00
|
|
|
|
//this.chatsSelectTab(this.chat.container);
|
2020-05-30 09:44:54 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
return this.setPeer(peerId, lastMsgId);
|
2020-05-30 09:44:54 +03:00
|
|
|
|
}
|
2020-05-18 04:21:58 +03:00
|
|
|
|
|
2020-12-18 05:07:32 +02:00
|
|
|
|
public openScheduled(peerId: number) {
|
|
|
|
|
this.setInnerPeer(peerId, undefined, 'scheduled');
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
public async getPeerStatus(peerId: number) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
let subtitle = '';
|
2020-12-11 04:06:16 +02:00
|
|
|
|
if(!peerId) return subtitle;
|
2020-05-18 04:21:58 +03:00
|
|
|
|
|
2020-12-11 04:06:16 +02:00
|
|
|
|
if(peerId < 0) { // not human
|
|
|
|
|
const chat = appPeersManager.getPeer(peerId);
|
2020-05-18 04:21:58 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
const chatInfo = await appProfileManager.getChatFull(chat.id) as any;
|
|
|
|
|
this.chat.log('chatInfo res:', chatInfo);
|
2020-05-18 04:21:58 +03:00
|
|
|
|
|
2020-12-22 00:38:26 +02:00
|
|
|
|
const participants_count = chatInfo.participants_count || (chatInfo.participants && chatInfo.participants.participants && chatInfo.participants.participants.length) || 1;
|
|
|
|
|
//if(participants_count) {
|
|
|
|
|
subtitle = appChatsManager.getChatMembersString(-peerId);
|
2020-05-27 09:21:16 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
if(participants_count < 2) return subtitle;
|
|
|
|
|
const onlines = await appChatsManager.getOnlines(chat.id);
|
|
|
|
|
if(onlines > 1) {
|
2021-01-31 03:52:14 +02:00
|
|
|
|
subtitle += ', ' + numberThousandSplitter(onlines, ' ') + ' online';
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}
|
2020-05-02 00:28:40 +03:00
|
|
|
|
|
2020-12-01 17:38:36 +02:00
|
|
|
|
return subtitle;
|
2020-12-22 00:38:26 +02:00
|
|
|
|
//}
|
2020-12-23 03:18:07 +02:00
|
|
|
|
} else { // user
|
2020-12-11 04:06:16 +02:00
|
|
|
|
const user = appUsersManager.getUser(peerId);
|
2020-10-30 02:23:10 +02:00
|
|
|
|
|
2021-02-03 06:36:59 +02:00
|
|
|
|
if(rootScope.myId === peerId) {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
return '';
|
|
|
|
|
} else if(user) {
|
|
|
|
|
subtitle = appUsersManager.getUserStatusString(user.id);
|
2020-05-03 15:46:05 +03:00
|
|
|
|
|
2020-12-23 03:18:07 +02:00
|
|
|
|
if(!appUsersManager.isBot(peerId)) {
|
|
|
|
|
const typings = appChatsManager.typingsInPeer[peerId];
|
|
|
|
|
if(typings && typings.length) {
|
|
|
|
|
return '<span class="online">typing...</span>';
|
2021-02-03 06:36:59 +02:00
|
|
|
|
} else if(subtitle === 'online') {
|
2020-12-23 03:18:07 +02:00
|
|
|
|
return `<span class="online">${subtitle}</span>`;
|
|
|
|
|
} else {
|
|
|
|
|
return subtitle;
|
|
|
|
|
}
|
2020-05-03 15:46:05 +03:00
|
|
|
|
} else {
|
2020-12-01 17:38:36 +02:00
|
|
|
|
return subtitle;
|
2020-05-03 15:46:05 +03:00
|
|
|
|
}
|
2020-05-18 04:21:58 +03:00
|
|
|
|
}
|
2020-02-09 13:45:43 +07:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 22:43:07 +07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:56:15 +07:00
|
|
|
|
const appImManager = new AppImManager();
|
2020-09-29 05:03:26 +03:00
|
|
|
|
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.appImManager = appImManager);
|
2020-02-10 14:56:15 +07:00
|
|
|
|
export default appImManager;
|