Browse Source

Select peers:

Fix infinity load bug
Fix search peer by text
Fix search Saved Messages
master
morethanwords 4 years ago
parent
commit
8a0f1ac794
  1. 58
      src/components/appSelectPeers.ts
  2. 6
      src/components/sidebarRight/tabs/forward.ts
  3. 24
      src/lib/appManagers/appMessagesManager.ts
  4. 82
      src/lib/searchIndexManager.ts

58
src/components/appSelectPeers.ts

@ -12,7 +12,7 @@ type PeerType = 'contacts' | 'dialogs'; @@ -12,7 +12,7 @@ type PeerType = 'contacts' | 'dialogs';
// TODO: правильная сортировка для addMembers, т.е. для peerType: 'contacts', потому что там идут сначала контакты - потом неконтакты, а должно всё сортироваться по имени
let loadedAllDialogs = false;
let loadedAllDialogs = false, loadAllDialogsPromise: Promise<any>;
export class AppSelectPeers {
public container = document.createElement('div');
public list = document.createElement('ul');
@ -141,7 +141,9 @@ export class AppSelectPeers { @@ -141,7 +141,9 @@ export class AppSelectPeers {
private renderSaved() {
if(!this.offsetIndex && this.folderID == 0 &&
(!this.query || 'saved messages'.includes(this.query.toLowerCase())) &&
(!this.query
|| 'saved messages'.includes(this.query.toLowerCase())
|| appUsersManager.getUser($rootScope.myID).sortName.includes(this.query.toLowerCase())) &&
this.peerType.includes('dialogs')) {
this.renderResultsFunc([$rootScope.myID]);
}
@ -223,7 +225,7 @@ export class AppSelectPeers { @@ -223,7 +225,7 @@ export class AppSelectPeers {
const pageCount = appPhotosManager.windowH / 72 * 1.25 | 0;
const arr = this.cachedContacts.splice(0, pageCount);
this.renderResultsFunc(arr);
}
}
if(!this.cachedContacts.length) {
this.loadedWhat.contacts = true;
@ -235,26 +237,46 @@ export class AppSelectPeers { @@ -235,26 +237,46 @@ export class AppSelectPeers {
}
}
private getMoreResults() {
const promises: Promise<any>[] = [];
if(!loadedAllDialogs) {
promises.push(appMessagesManager.getConversationsAll());
}
checkForTriggers = () => {
this.scrollable.checkForTriggers();
};
if((this.peerType.includes('dialogs') || this.loadedWhat.contacts) && !this.loadedWhat.archived) { // to load non-contacts
promises.push(this.getMoreDialogs());
private getMoreResults() {
const get = () => {
const promises: Promise<any>[] = [];
if(!loadedAllDialogs && !loadAllDialogsPromise) {
loadAllDialogsPromise = appMessagesManager.getConversationsAll()
.then(() => {
loadedAllDialogs = true;
}, () => {
loadAllDialogsPromise = null;
});
if(!this.loadedWhat.archived) {
return Promise.all(promises);
promises.push(loadAllDialogsPromise);
}
}
if((this.peerType.includes('dialogs') || this.loadedWhat.contacts) && !this.loadedWhat.archived) { // to load non-contacts
promises.push(this.getMoreDialogs());
if(!this.loadedWhat.archived) {
return promises;
}
}
if(this.peerType.includes('contacts') && !this.loadedWhat.contacts) {
promises.push(this.getMoreContacts());
}
return promises;
};
if(this.peerType.includes('contacts') && !this.loadedWhat.contacts) {
promises.push(this.getMoreContacts());
const promises = get();
const promise = Promise.all(promises);
if(promises.length) {
promise.then(this.checkForTriggers);
}
return Promise.all(promises);
return promise
}
private renderResults(peerIDs: number[]) {

6
src/components/sidebarRight/tabs/forward.ts

@ -79,7 +79,11 @@ export default class AppForwardTab implements SliderTab { @@ -79,7 +79,11 @@ export default class AppForwardTab implements SliderTab {
}, ['dialogs', 'contacts'], () => {
//console.log('forward rendered:', this.container.querySelector('.selector ul').childElementCount);
appSidebarRight.selectTab(AppSidebarRight.SLIDERITEMSIDS.forward);
appSidebarRight.toggleSidebar(true);
appSidebarRight.toggleSidebar(true).then(() => {
if(this.selector) {
this.selector.checkForTriggers();
}
});
document.body.classList.add('is-forward-active');
}, null, 'send');
}

24
src/lib/appManagers/appMessagesManager.ts

@ -516,11 +516,13 @@ export class AppMessagesManager { @@ -516,11 +516,13 @@ export class AppMessagesManager {
private cachedResults: {
query: string,
count: number,
dialogs: Dialog[]
dialogs: Dialog[],
folderID: number
} = {
query: '',
count: 0,
dialogs: []
dialogs: [],
folderID: 0
};
private log = logger('MESSAGES'/* , LogLevels.error */);
@ -1900,27 +1902,23 @@ export class AppMessagesManager { @@ -1900,27 +1902,23 @@ export class AppMessagesManager {
let curDialogStorage = this.dialogsStorage.getFolder(folderID);
if(query) {
if(!limit || this.cachedResults.query !== query) {
this.cachedResults.query = query
if(!limit || this.cachedResults.query !== query || this.cachedResults.folderID != folderID) {
this.cachedResults.query = query;
this.cachedResults.folderID = folderID;
const results = searchIndexManager.search(query, this.dialogsIndex);
this.cachedResults.dialogs = [];
/* for(const folderID in this.dialogsStorage) {
const dialogs = this.dialogsStorage[folderID];
dialogs.forEach(dialog => {
if(results[dialog.peerID]) {
this.cachedResults.dialogs.push(dialog);
}
});
} */
for(const peerID in this.dialogsStorage.dialogs) {
const dialog = this.dialogsStorage.dialogs[peerID];
if(results[dialog.peerID]) {
if(results[dialog.peerID] && dialog.folder_id == folderID) {
this.cachedResults.dialogs.push(dialog);
}
}
this.cachedResults.dialogs.sort((d1, d2) => d2.index - d1.index);
this.cachedResults.count = this.cachedResults.dialogs.length;
}

82
src/lib/searchIndexManager.ts

@ -1,13 +1,22 @@ @@ -1,13 +1,22 @@
import Config from './config';
export type SearchIndex = {
fullTexts: {
[peerID: string]: string
}/* ,
shortIndexes: {
[shortStr: string]: number[]
} */
};
class SearchIndexManager {
public static badCharsRe = /[`~!@#$%^&*()\-_=+\[\]\\|{}'";:\/?.>,<]+/g;
public static trimRe = /^\s+|\s$/g;
public createIndex() {
public createIndex(): SearchIndex {
return {
shortIndexes: {},
fullTexts: {}
fullTexts: {}/* ,
shortIndexes: {} */
};
}
@ -33,21 +42,19 @@ class SearchIndexManager { @@ -33,21 +42,19 @@ class SearchIndexManager {
return username && username.toLowerCase() || '';
}
public indexObject(id: number, searchText: string, searchIndex: any) {
if(searchIndex.fullTexts[id] !== undefined) {
public indexObject(id: number, searchText: string, searchIndex: SearchIndex) {
if(searchIndex.fullTexts.hasOwnProperty(id)) {
return false;
}
searchText = this.cleanSearchText(searchText);
if(!searchText.length) {
return false;
}
const shortIndexes = searchIndex.shortIndexes;
searchIndex.fullTexts[id] = searchText;
/* const shortIndexes = searchIndex.shortIndexes;
searchText.split(' ').forEach((searchWord) => {
let len = Math.min(searchWord.length, 3),
wordPart, i;
@ -59,39 +66,52 @@ class SearchIndexManager { @@ -59,39 +66,52 @@ class SearchIndexManager {
shortIndexes[wordPart].push(id);
}
}
});
}); */
}
public search(query: string, searchIndex: any) {
const shortIndexes = searchIndex.shortIndexes;
public search(query: string, searchIndex: SearchIndex) {
const fullTexts = searchIndex.fullTexts;
//const shortIndexes = searchIndex.shortIndexes;
query = this.cleanSearchText(query);
const newFoundObjs: {[peerID: string]: true} = {};
const queryWords = query.split(' ');
let foundObjs: any = false,
newFoundObjs: any, i: number;
let j: number, searchText: string;
let found: boolean;
for(i = 0; i < queryWords.length; i++) {
newFoundObjs = shortIndexes[queryWords[i].substr(0, 3)];
if(!newFoundObjs) {
foundObjs = [];
for(const peerID in fullTexts) {
const fullText = fullTexts[peerID];
let found = true;
for(const word of queryWords) {
if(fullText.indexOf(word) === -1) {
found = false;
break;
}
}
if(found) {
newFoundObjs[peerID] = true;
}
}
/* const queryWords = query.split(' ');
let foundArr: number[];
for(let i = 0; i < queryWords.length; i++) {
const newFound = shortIndexes[queryWords[i].substr(0, 3)];
if(!newFound) {
foundArr = [];
break;
}
if(foundObjs === false || foundObjs.length > newFoundObjs.length) {
foundObjs = newFoundObjs;
if(foundArr === undefined || foundArr.length > newFound.length) {
foundArr = newFound;
}
}
newFoundObjs = {};
for(j = 0; j < foundObjs.length; j++) {
found = true;
searchText = fullTexts[foundObjs[j]];
for(i = 0; i < queryWords.length; i++) {
for(let j = 0; j < foundArr.length; j++) {
let found = true;
let searchText = fullTexts[foundArr[j]];
for(let i = 0; i < queryWords.length; i++) {
if(searchText.indexOf(queryWords[i]) == -1) {
found = false;
break;
@ -99,9 +119,9 @@ class SearchIndexManager { @@ -99,9 +119,9 @@ class SearchIndexManager {
}
if(found) {
newFoundObjs[foundObjs[j]] = true;
newFoundObjs[foundArr[j]] = true;
}
}
} */
return newFoundObjs;
}

Loading…
Cancel
Save