Telegram Web K with changes to work inside I2P
https://web.telegram.i2p/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.9 KiB
106 lines
2.9 KiB
4 years ago
|
/*
|
||
|
* https://github.com/morethanwords/tweb
|
||
|
* Copyright (C) 2019-2021 Eduard Kuzmenko
|
||
|
* https://github.com/morethanwords/tweb/blob/master/LICENSE
|
||
|
*
|
||
|
* Originally from:
|
||
|
* https://github.com/zhukov/webogram
|
||
|
* Copyright (C) 2014 Igor Zhukov <igor.beatle@gmail.com>
|
||
|
* https://github.com/zhukov/webogram/blob/master/LICENSE
|
||
|
*/
|
||
|
|
||
3 years ago
|
import cleanSearchText from '../helpers/cleanSearchText';
|
||
4 years ago
|
|
||
3 years ago
|
export default class SearchIndex<SearchWhat> {
|
||
|
private fullTexts: Map<SearchWhat, string> = new Map();
|
||
4 years ago
|
|
||
3 years ago
|
public indexObject(id: SearchWhat, searchText: string) {
|
||
4 years ago
|
/* if(searchIndex.fullTexts.hasOwnProperty(id)) {
|
||
4 years ago
|
return false;
|
||
4 years ago
|
} */
|
||
|
|
||
|
if(searchText.trim()) {
|
||
3 years ago
|
searchText = cleanSearchText(searchText);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
if(!searchText) {
|
||
3 years ago
|
this.fullTexts.delete(id);
|
||
4 years ago
|
return false;
|
||
|
}
|
||
|
|
||
3 years ago
|
this.fullTexts.set(id, searchText);
|
||
4 years ago
|
|
||
|
/* const shortIndexes = searchIndex.shortIndexes;
|
||
4 years ago
|
searchText.split(' ').forEach((searchWord) => {
|
||
|
let len = Math.min(searchWord.length, 3),
|
||
|
wordPart, i;
|
||
|
for(i = 1; i <= len; i++) {
|
||
|
wordPart = searchWord.substr(0, i);
|
||
|
if(shortIndexes[wordPart] === undefined) {
|
||
|
shortIndexes[wordPart] = [id];
|
||
|
} else {
|
||
|
shortIndexes[wordPart].push(id);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}); */
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
public search(query: string) {
|
||
|
const fullTexts = this.fullTexts;
|
||
4 years ago
|
//const shortIndexes = searchIndex.shortIndexes;
|
||
4 years ago
|
|
||
3 years ago
|
query = cleanSearchText(query);
|
||
4 years ago
|
|
||
3 years ago
|
const newFoundObjs: Array<{fullText: string, what: SearchWhat}> = [];
|
||
4 years ago
|
const queryWords = query.split(' ');
|
||
3 years ago
|
fullTexts.forEach((fullText, what) => {
|
||
4 years ago
|
let found = true;
|
||
4 years ago
|
for(const word of queryWords) { // * verify that all words are found
|
||
|
const idx = fullText.indexOf(word);
|
||
|
if(idx === -1 || (idx !== 0 && fullText[idx - 1] !== ' ')) { // * search only from word beginning
|
||
4 years ago
|
found = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(found) {
|
||
3 years ago
|
newFoundObjs.push({fullText, what});
|
||
4 years ago
|
}
|
||
3 years ago
|
});
|
||
|
|
||
|
//newFoundObjs.sort((a, b) => a.fullText.localeCompare(b.fullText));
|
||
|
const newFoundObjs2: Set<SearchWhat> = new Set(newFoundObjs.map(o => o.what));
|
||
4 years ago
|
|
||
|
/* 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 = [];
|
||
4 years ago
|
break;
|
||
|
}
|
||
|
|
||
4 years ago
|
if(foundArr === undefined || foundArr.length > newFound.length) {
|
||
|
foundArr = newFound;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
for(let j = 0; j < foundArr.length; j++) {
|
||
|
let found = true;
|
||
|
let searchText = fullTexts[foundArr[j]];
|
||
|
for(let i = 0; i < queryWords.length; i++) {
|
||
4 years ago
|
if(searchText.indexOf(queryWords[i]) === -1) {
|
||
4 years ago
|
found = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(found) {
|
||
4 years ago
|
newFoundObjs[foundArr[j]] = true;
|
||
4 years ago
|
}
|
||
4 years ago
|
} */
|
||
4 years ago
|
|
||
3 years ago
|
return newFoundObjs2;
|
||
4 years ago
|
}
|
||
|
}
|