2020-02-06 22:43:07 +07:00
|
|
|
import { logger } from "../polyfill";
|
2020-02-11 22:35:57 +07:00
|
|
|
import { putPreloader, formatPhoneNumber } from "../../components/misc";
|
2020-02-09 13:45:43 +07:00
|
|
|
import Scrollable from '../../components/scrollable';
|
2020-02-14 23:15:41 +07:00
|
|
|
import appMessagesManager, { AppMessagesManager } from "./appMessagesManager";
|
2020-02-06 22:43:07 +07:00
|
|
|
import appDialogsManager from "./appDialogsManager";
|
2020-02-22 18:20:43 +07:00
|
|
|
import { isElementInViewport, numberWithCommas } from "../utils";
|
2020-02-07 13:38:55 +07:00
|
|
|
import appMessagesIDsManager from "./appMessagesIDsManager";
|
2020-02-08 13:03:09 +07:00
|
|
|
import appImManager from "./appImManager";
|
2020-02-11 22:35:57 +07:00
|
|
|
import appUsersManager from "./appUsersManager";
|
|
|
|
import { appPeersManager } from "../services";
|
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
let testScroll = false;
|
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
class SearchGroup {
|
|
|
|
container: HTMLDivElement;
|
|
|
|
nameEl: HTMLDivElement;
|
|
|
|
list: HTMLUListElement;
|
|
|
|
|
|
|
|
constructor(public name: string, public type: string) {
|
|
|
|
this.list = document.createElement('ul');
|
|
|
|
this.container = document.createElement('div');
|
|
|
|
this.nameEl = document.createElement('div');
|
|
|
|
this.nameEl.classList.add('search-group__name');
|
|
|
|
this.nameEl.innerText = name;
|
|
|
|
|
|
|
|
this.container.classList.add('search-group');
|
|
|
|
this.container.append(this.nameEl, this.list);
|
|
|
|
this.container.style.display = 'none';
|
|
|
|
|
2020-02-22 18:20:43 +07:00
|
|
|
//appDialogsManager.setListClickListener(this.list);
|
2020-02-11 22:35:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.container.style.display = 'none';
|
|
|
|
this.list.innerHTML = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
setActive() {
|
|
|
|
this.container.style.display = '';
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 22:43:07 +07:00
|
|
|
|
|
|
|
class AppSidebarLeft {
|
|
|
|
private sidebarEl = document.querySelector('.page-chats .chats-container') as HTMLDivElement;
|
|
|
|
private searchInput = document.getElementById('global-search') as HTMLInputElement;
|
|
|
|
private toolsBtn = this.sidebarEl.querySelector('.sidebar-tools-button') as HTMLButtonElement;
|
2020-02-15 22:20:38 +07:00
|
|
|
private backBtn = this.sidebarEl.querySelector('.sidebar-back-button') as HTMLButtonElement;
|
2020-02-06 22:43:07 +07:00
|
|
|
private searchContainer = this.sidebarEl.querySelector('#search-container') as HTMLDivElement;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-08 13:03:09 +07:00
|
|
|
private menuEl = this.toolsBtn.querySelector('.btn-menu');
|
|
|
|
private savedBtn = this.menuEl.querySelector('.menu-saved');
|
2020-02-11 22:35:57 +07:00
|
|
|
private archivedBtn = this.menuEl.querySelector('.menu-archive');
|
2020-02-15 22:20:38 +07:00
|
|
|
public archivedCount = this.archivedBtn.querySelector('.archived-count') as HTMLSpanElement;
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
private listsContainer: HTMLDivElement = null;
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
private chatsArchivedContainer = document.getElementById('chats-archived-container') as HTMLDivElement;
|
2020-02-08 18:58:22 +07:00
|
|
|
private chatsContainer = document.getElementById('chats-container') as HTMLDivElement;
|
2020-02-12 20:48:30 +07:00
|
|
|
private chatsArchivedOffsetIndex = 0;
|
2020-02-08 18:58:22 +07:00
|
|
|
private chatsOffsetIndex = 0;
|
|
|
|
private chatsPreloader: HTMLDivElement;
|
2020-02-27 00:52:59 +07:00
|
|
|
//private chatsLoadCount = 0;
|
2020-02-14 23:15:41 +07:00
|
|
|
//private loadDialogsPromise: Promise<any>;
|
|
|
|
private loadDialogsPromise: ReturnType<AppMessagesManager["getConversations"]>;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
private log = logger('SL');
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
private peerID = 0;
|
2020-02-07 13:38:55 +07:00
|
|
|
private minMsgID = 0;
|
|
|
|
private loadedCount = 0;
|
|
|
|
private foundCount = 0;
|
|
|
|
private offsetRate = 0;
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
private searchPromise: Promise<void> = null;
|
2020-02-07 13:38:55 +07:00
|
|
|
private searchTimeout: number = 0;
|
|
|
|
|
2020-02-08 18:58:22 +07:00
|
|
|
private query = '';
|
2020-02-09 13:45:43 +07:00
|
|
|
|
|
|
|
public scroll: Scrollable = null;
|
2020-02-12 20:48:30 +07:00
|
|
|
public scrollArchived: Scrollable = null;
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
public searchGroups: {[group: string]: SearchGroup} = {
|
|
|
|
contacts: new SearchGroup('Contacts and Chats', 'contacts'),
|
|
|
|
globalContacts: new SearchGroup('Global Search', 'contacts'),
|
|
|
|
globalMessages: new SearchGroup('Global Search', 'messages'),
|
|
|
|
privateMessages: new SearchGroup('Private Search', 'messages')
|
|
|
|
};
|
2020-02-21 15:25:19 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
constructor() {
|
2020-02-08 18:58:22 +07:00
|
|
|
this.chatsPreloader = document.createElement('div');
|
|
|
|
this.chatsPreloader.classList.add('preloader');
|
|
|
|
putPreloader(this.chatsPreloader);
|
2020-02-22 18:20:43 +07:00
|
|
|
//this.chatsContainer.append(this.chatsPreloader);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
//this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
this.scroll = new Scrollable(this.chatsContainer as HTMLDivElement, false, true, 300, 'CL');
|
2020-02-09 13:45:43 +07:00
|
|
|
this.scroll.setVirtualContainer(appDialogsManager.chatList);
|
2020-02-21 15:25:19 +07:00
|
|
|
this.scroll.onScrolledBottom = this.onChatsScroll.bind(this);
|
2020-02-09 16:22:15 +07:00
|
|
|
appDialogsManager.chatsHidden = this.scroll.hiddenElements;
|
2020-02-21 15:25:19 +07:00
|
|
|
//this.scroll.container.addEventListener('scroll', this.onChatsScroll.bind(this));
|
2020-02-12 20:48:30 +07:00
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
this.scrollArchived = new Scrollable(this.chatsArchivedContainer as HTMLDivElement, false, true, 300, 'CLA');
|
2020-02-12 20:48:30 +07:00
|
|
|
this.scrollArchived.setVirtualContainer(appDialogsManager.chatListArchived);
|
|
|
|
appDialogsManager.chatsArchivedHidden = this.scrollArchived.hiddenElements;
|
2020-02-21 15:25:19 +07:00
|
|
|
//this.scrollArchived.container.addEventListener('scroll', this.onChatsArchivedScroll.bind(this));
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-09 13:45:43 +07:00
|
|
|
this.listsContainer = new Scrollable(this.searchContainer).container;
|
2020-02-11 22:35:57 +07:00
|
|
|
for(let i in this.searchGroups) {
|
|
|
|
this.listsContainer.append(this.searchGroups[i].container);
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-10 14:56:15 +07:00
|
|
|
this.savedBtn.addEventListener('click', (e) => {
|
2020-02-17 19:18:06 +07:00
|
|
|
///////this.log('savedbtn click');
|
2020-02-10 14:56:15 +07:00
|
|
|
setTimeout(() => { // menu doesn't close if no timeout (lol)
|
2020-02-13 22:42:39 +07:00
|
|
|
let dom = appDialogsManager.getDialogDom(appImManager.myID);
|
|
|
|
if(dom) {
|
|
|
|
dom.listEl.click();
|
|
|
|
} else {
|
|
|
|
appImManager.setPeer(appImManager.myID);
|
|
|
|
}
|
2020-02-10 14:56:15 +07:00
|
|
|
}, 0);
|
2020-02-08 13:03:09 +07:00
|
|
|
});
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
this.archivedBtn.addEventListener('click', (e) => {
|
|
|
|
this.chatsArchivedContainer.classList.add('active');
|
2020-02-15 22:20:38 +07:00
|
|
|
this.toolsBtn.classList.remove('active');
|
|
|
|
this.backBtn.classList.add('active');
|
|
|
|
//this.toolsBtn.classList.remove('tgico-menu', 'btn-menu-toggle');
|
|
|
|
//this.toolsBtn.classList.add('tgico-back');
|
2020-02-11 22:35:57 +07:00
|
|
|
});
|
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
if(testScroll) {
|
|
|
|
for(let i = 0; i < 1000; ++i) {
|
|
|
|
let li = document.createElement('li');
|
|
|
|
li.dataset.id = '' + i;
|
|
|
|
li.innerHTML = `<div class="rp"><div class="user-avatar" style="background-color: rgb(166, 149, 231); font-size: 0px;"><img src="blob:https://localhost:9000/ce99a2a3-f34b-4ca1-a09e-f716f89930d8"></div><div class="user-caption"><p><span class="user-title">${i}</span><span><span class="message-status"></span><span class="message-time">18:33</span></span></p><p><span class="user-last-message"><b>Ильяс: </b>Гагагагга</span><span></span></p></div></div>`;
|
|
|
|
this.scroll.append(li);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 21:17:39 +07:00
|
|
|
this.listsContainer.addEventListener('scroll', this.onSidebarScroll.bind(this));
|
2020-02-22 18:20:43 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchInput.addEventListener('focus', (e) => {
|
2020-02-15 22:20:38 +07:00
|
|
|
this.toolsBtn.classList.remove('active');
|
|
|
|
this.backBtn.classList.add('active');
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchContainer.classList.add('active');
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
if(!this.searchInput.value) {
|
2020-02-11 22:35:57 +07:00
|
|
|
for(let i in this.searchGroups) {
|
|
|
|
this.searchGroups[i].clear();
|
|
|
|
}
|
2020-02-06 22:43:07 +07:00
|
|
|
}
|
2020-02-09 17:30:24 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchInput.addEventListener('blur', (e) => {
|
|
|
|
if(!this.searchInput.value) {
|
2020-02-15 22:20:38 +07:00
|
|
|
this.toolsBtn.classList.add('active');
|
|
|
|
this.backBtn.classList.remove('active');
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchContainer.classList.remove('active');
|
2020-02-17 19:18:06 +07:00
|
|
|
this.backBtn.click();
|
2020-02-06 22:43:07 +07:00
|
|
|
}
|
2020-02-07 13:38:55 +07:00
|
|
|
|
|
|
|
/* this.peerID = 0;
|
|
|
|
this.loadedCount = 0;
|
|
|
|
this.minMsgID = 0; */
|
2020-02-06 22:43:07 +07:00
|
|
|
}, {once: true});
|
|
|
|
});
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchInput.addEventListener('input', (e) => {
|
|
|
|
//console.log('messageInput input', this.innerText, serializeNodes(Array.from(messageInput.childNodes)));
|
|
|
|
let value = this.searchInput.value;
|
2020-02-17 19:18:06 +07:00
|
|
|
////////this.log('input', value);
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
if(!value.trim()) {
|
2020-02-17 19:18:06 +07:00
|
|
|
//this.peerID = 0;
|
2020-02-06 22:43:07 +07:00
|
|
|
return;
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
this.query = value;
|
|
|
|
this.minMsgID = 0;
|
|
|
|
this.loadedCount = 0;
|
|
|
|
this.foundCount = 0;
|
|
|
|
this.offsetRate = 0;
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
for(let i in this.searchGroups) {
|
|
|
|
this.searchGroups[i].clear();
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
this.searchPromise = null;
|
2020-02-11 22:35:57 +07:00
|
|
|
this.searchMore();
|
2020-02-06 22:43:07 +07:00
|
|
|
});
|
2020-02-10 14:56:15 +07:00
|
|
|
|
2020-02-15 22:20:38 +07:00
|
|
|
this.backBtn.addEventListener('click', (e) => {
|
|
|
|
this.chatsArchivedContainer.classList.remove('active');
|
|
|
|
this.toolsBtn.classList.add('active');
|
|
|
|
this.backBtn.classList.remove('active');
|
2020-02-17 19:18:06 +07:00
|
|
|
this.searchInput.value = '';
|
|
|
|
this.searchContainer.classList.remove('active');
|
|
|
|
this.peerID = 0;
|
2020-02-15 22:20:38 +07:00
|
|
|
});
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
window.addEventListener('resize', () => {
|
2020-02-27 00:52:59 +07:00
|
|
|
//this.chatsLoadCount = Math.round(document.body.scrollHeight / 70 * 1.5);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.onSidebarScroll();
|
2020-02-12 20:48:30 +07:00
|
|
|
this.scroll.onScroll();
|
2020-02-21 15:25:19 +07:00
|
|
|
//this.onChatsScroll();
|
2020-02-12 20:48:30 +07:00
|
|
|
this.onChatsArchivedScroll();
|
2020-02-08 18:58:22 +07:00
|
|
|
}, 0);
|
2020-02-07 13:38:55 +07:00
|
|
|
});
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
/* appUsersManager.getTopPeers().then(categories => {
|
|
|
|
this.log('got top categories:', categories);
|
|
|
|
}); */
|
2020-02-07 13:38:55 +07:00
|
|
|
}
|
|
|
|
|
2020-02-12 20:48:30 +07:00
|
|
|
public async loadDialogs(archived = false) {
|
2020-02-27 00:52:59 +07:00
|
|
|
if(testScroll) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-22 18:20:43 +07:00
|
|
|
|
2020-02-08 18:58:22 +07:00
|
|
|
if(this.loadDialogsPromise/* || 1 == 1 */) return this.loadDialogsPromise;
|
|
|
|
|
2020-02-12 20:48:30 +07:00
|
|
|
(archived ? this.chatsArchivedContainer : this.chatsContainer).append(this.chatsPreloader);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
//let offset = appMessagesManager.generateDialogIndex();/* appMessagesManager.dialogsNum */;
|
2020-02-12 20:48:30 +07:00
|
|
|
|
|
|
|
let offset = archived ? this.chatsArchivedOffsetIndex : this.chatsOffsetIndex;
|
|
|
|
//let offset = 0;
|
|
|
|
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
try {
|
2020-02-27 00:52:59 +07:00
|
|
|
console.time('getDialogs time');
|
|
|
|
this.loadDialogsPromise = appMessagesManager.getConversations('', offset, 50/*this.chatsLoadCount */, +archived);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
let result = await this.loadDialogsPromise;
|
2020-02-27 00:52:59 +07:00
|
|
|
|
|
|
|
console.timeEnd('getDialogs time');
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
if(result && result.dialogs && result.dialogs.length) {
|
2020-02-12 20:48:30 +07:00
|
|
|
let index = result.dialogs[result.dialogs.length - 1].index;
|
|
|
|
|
|
|
|
if(archived) this.chatsArchivedOffsetIndex = index;
|
|
|
|
else this.chatsOffsetIndex = index;
|
|
|
|
|
2020-02-08 18:58:22 +07:00
|
|
|
result.dialogs.forEach((dialog: any) => {
|
|
|
|
appDialogsManager.addDialog(dialog);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:20:38 +07:00
|
|
|
/* if(archived) {
|
2020-02-14 23:15:41 +07:00
|
|
|
let count = result.count;
|
|
|
|
this.archivedCount.innerText = '' + count;
|
2020-02-15 22:20:38 +07:00
|
|
|
} */
|
2020-02-14 23:15:41 +07:00
|
|
|
|
2020-02-27 00:52:59 +07:00
|
|
|
//this.log('getDialogs ' + this.chatsLoadCount + ' dialogs by offset:', offset, result, this.scroll.hiddenElements);
|
2020-02-09 13:45:43 +07:00
|
|
|
this.scroll.onScroll();
|
2020-02-08 18:58:22 +07:00
|
|
|
} catch(err) {
|
|
|
|
this.log.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.chatsPreloader.remove();
|
|
|
|
this.loadDialogsPromise = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
public onChatsScroll() {
|
2020-02-12 20:48:30 +07:00
|
|
|
//this.log(this.scroll.hiddenElements.down.length, this.loadDialogsPromise, appDialogsManager.chatList.childNodes);
|
2020-02-21 15:25:19 +07:00
|
|
|
if(this.scroll.hiddenElements.down.length > 0 || this.loadDialogsPromise/* || 1 == 1 */) return;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-21 15:25:19 +07:00
|
|
|
this.loadDialogs();
|
2020-02-08 18:58:22 +07:00
|
|
|
}
|
2020-02-12 20:48:30 +07:00
|
|
|
|
|
|
|
public onChatsArchivedScroll() {
|
|
|
|
//this.log(this.scrollArchived.hiddenElements.down.length, this.loadDialogsPromise, appDialogsManager.chatListArchived.childNodes);
|
|
|
|
if(this.scrollArchived.hiddenElements.down.length > 0/* || 1 == 1 */) return;
|
|
|
|
|
|
|
|
if(!this.loadDialogsPromise) {
|
|
|
|
let d = Array.from(appDialogsManager.chatListArchived.childNodes).slice(-5);
|
|
|
|
for(let node of d) {
|
|
|
|
if(isElementInViewport(node)) {
|
|
|
|
this.loadDialogs(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log('last 5 dialogs:', d);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
public onSidebarScroll() {
|
2020-02-08 18:58:22 +07:00
|
|
|
if(!this.query.trim()) return;
|
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
let elements = Array.from(this.searchGroups[this.peerID ? 'privateMessages' : 'globalMessages'].list.childNodes).slice(-5);
|
2020-02-07 13:38:55 +07:00
|
|
|
for(let li of elements) {
|
|
|
|
if(isElementInViewport(li)) {
|
|
|
|
this.log('Will load more search');
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
if(!this.searchTimeout) {
|
|
|
|
this.searchTimeout = setTimeout(() => {
|
|
|
|
this.searchMore();
|
|
|
|
this.searchTimeout = 0;
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
public beginSearch(peerID?: number) {
|
|
|
|
if(peerID) {
|
|
|
|
this.peerID = peerID;
|
|
|
|
}
|
2020-02-07 13:38:55 +07:00
|
|
|
|
2020-02-06 22:43:07 +07:00
|
|
|
this.searchInput.focus();
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
private searchMore() {
|
|
|
|
if(this.searchPromise) return this.searchPromise;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
let query = this.query;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
|
|
|
if(!query.trim()) return;
|
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
if(this.loadedCount != 0 && this.loadedCount >= this.foundCount) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:39:00 +07:00
|
|
|
let maxID = appMessagesIDsManager.getMessageIDInfo(this.minMsgID)[0];
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
if(!this.peerID && !maxID) {
|
|
|
|
appUsersManager.searchContacts(query, 20).then((contacts: any) => {
|
|
|
|
if(this.searchInput.value != query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 19:18:06 +07:00
|
|
|
///////this.log('input search contacts result:', contacts);
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
let setResults = (results: any, group: SearchGroup, showMembersCount = false) => {
|
|
|
|
results.forEach((inputPeer: any) => {
|
|
|
|
let peerID = appPeersManager.getPeerID(inputPeer);
|
|
|
|
let peer = appPeersManager.getPeer(peerID);
|
|
|
|
let originalDialog = appMessagesManager.getDialogByPeerID(peerID)[0];
|
|
|
|
|
2020-02-17 19:18:06 +07:00
|
|
|
//////////this.log('contacts peer', peer);
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
if(!originalDialog) {
|
2020-02-17 19:18:06 +07:00
|
|
|
/////////this.log('no original dialog by peerID:', peerID);
|
2020-02-11 22:35:57 +07:00
|
|
|
|
|
|
|
originalDialog = {
|
|
|
|
peerID: peerID,
|
|
|
|
pFlags: {},
|
|
|
|
peer: peer
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let {dialog, dom} = appDialogsManager.addDialog(originalDialog, group.list, false);
|
|
|
|
|
|
|
|
if(showMembersCount && (peer.participants_count || peer.participants)) {
|
|
|
|
let isChannel = appPeersManager.isChannel(peerID) && !appPeersManager.isMegagroup(peerID);
|
|
|
|
let participants_count = peer.participants_count || peer.participants.participants.length;
|
|
|
|
let subtitle = numberWithCommas(participants_count) + ' ' + (isChannel ? 'subscribers' : 'members');
|
|
|
|
dom.lastMessageSpan.innerText = subtitle;
|
|
|
|
} else {
|
|
|
|
let username = appPeersManager.getPeerUsername(peerID);
|
|
|
|
if(!username) {
|
|
|
|
let user = appUsersManager.getUser(peerID);
|
|
|
|
if(user && user.phone) {
|
|
|
|
username = '+' + formatPhoneNumber(user.phone).formatted;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
username = '@' + username;
|
|
|
|
}
|
2020-02-12 20:48:30 +07:00
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
dom.lastMessageSpan.innerText = username;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(results.length) {
|
|
|
|
group.setActive();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
setResults(contacts.my_results, this.searchGroups.contacts, true);
|
|
|
|
setResults(contacts.results, this.searchGroups.globalContacts);
|
|
|
|
});
|
|
|
|
}
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
return this.searchPromise = appMessagesManager.getSearch(this.peerID, query, null, maxID, 20, this.offsetRate).then(res => {
|
|
|
|
this.searchPromise = null;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
if(this.searchInput.value != query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 19:18:06 +07:00
|
|
|
/////////this.log('input search result:', this.peerID, query, null, maxID, 20, res);
|
2020-02-07 13:38:55 +07:00
|
|
|
|
|
|
|
let {count, history, next_rate} = res;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
if(history[0] == this.minMsgID) {
|
|
|
|
history.shift();
|
|
|
|
}
|
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
let searchGroup = this.searchGroups[this.peerID ? 'privateMessages' : 'globalMessages'];
|
|
|
|
searchGroup.setActive();
|
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
history.forEach((msgID: number) => {
|
|
|
|
let message = appMessagesManager.getMessage(msgID);
|
|
|
|
let originalDialog = appMessagesManager.getDialogByPeerID(message.peerID)[0];
|
|
|
|
|
|
|
|
if(!originalDialog) {
|
2020-02-17 19:18:06 +07:00
|
|
|
////////this.log('no original dialog by message:', message);
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 14:39:00 +07:00
|
|
|
originalDialog = {
|
|
|
|
peerID: message.peerID,
|
|
|
|
pFlags: {},
|
|
|
|
peer: message.to_id
|
|
|
|
};
|
2020-02-07 13:38:55 +07:00
|
|
|
}
|
|
|
|
|
2020-02-11 22:35:57 +07:00
|
|
|
let {dialog, dom} = appDialogsManager.addDialog(originalDialog, searchGroup.list, false);
|
2020-02-07 13:38:55 +07:00
|
|
|
appDialogsManager.setLastMessage(dialog, message, dom);
|
|
|
|
});
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
this.minMsgID = history[history.length - 1];
|
|
|
|
this.offsetRate = next_rate;
|
|
|
|
this.loadedCount += history.length;
|
2020-02-08 18:58:22 +07:00
|
|
|
|
2020-02-07 13:38:55 +07:00
|
|
|
if(!this.foundCount) {
|
|
|
|
this.foundCount = count;
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
this.log.error('search error', err);
|
|
|
|
this.searchPromise = null;
|
|
|
|
});
|
|
|
|
}
|
2020-02-06 22:43:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new AppSidebarLeft();
|