Merge pull request #206 from keerthi16/SEARCH-273-new

Search 273 (Electron bindings for attachment search)
This commit is contained in:
Vikas Shashidhar
2017-10-23 14:17:09 +05:30
committed by GitHub
8 changed files with 58 additions and 23 deletions

View File

@@ -42,6 +42,10 @@
<label for="query">Query:</label><input id="query" size=60> <label for="query">Query:</label><input id="query" size=60>
</div> </div>
<br> <br>
<div>
<label for="has">Has:</label><input id="has">
</div>
<br>
<div> <div>
<label for="offset">Offset:</label><input id="offset" type="number" value="0" size=5> <label for="offset">Offset:</label><input id="offset" type="number" value="0" size=5>
<label for="limit">Limit:</label><input id="limit" type="number" value="25" size=5> <label for="limit">Limit:</label><input id="limit" type="number" value="25" size=5>
@@ -99,7 +103,7 @@
</div> </div>
<script> <script>
var search = new ssf.Search("7078106230763", "7078106230763"); var search = new ssf.Search("7078106230763", "k2rbHBd5CgU3/PKLFUKUYkmuhCiL2Gw2bnsD7MdaZrU=");
var buttonEl = document.getElementById('search'); var buttonEl = document.getElementById('search');
var merge = document.getElementById('merge'); var merge = document.getElementById('merge');
var buttonIndex = document.getElementById('index'); var buttonIndex = document.getElementById('index');
@@ -117,6 +121,7 @@
var realTimeIndexing = document.getElementById('realTimeIndexing'); var realTimeIndexing = document.getElementById('realTimeIndexing');
var batchNumber = document.getElementById('batchNumber'); var batchNumber = document.getElementById('batchNumber');
var timestamp = document.getElementById('getLatestMessageTimestamp'); var timestamp = document.getElementById('getLatestMessageTimestamp');
var has = document.getElementById('has');
buttonIndex.addEventListener('click', function () { buttonIndex.addEventListener('click', function () {
@@ -146,7 +151,8 @@
if (threadIdEl.value && threadIdEl.value !== "" && threadIdEl.value.replace(/ /g, "").length > 0) { if (threadIdEl.value && threadIdEl.value !== "" && threadIdEl.value.replace(/ /g, "").length > 0) {
threadIdObj = JSON.parse(threadIdEl.value); threadIdObj = JSON.parse(threadIdEl.value);
} }
search.searchQuery(queryEl.value, senderIdObj, threadIdObj, null, startDate, endDate, limitEl.value, offsetEl.value, 0).then(function (result) { let _has = has.value || null;
search.searchQuery(queryEl.value, senderIdObj, threadIdObj, _has, startDate, endDate, limitEl.value, offsetEl.value, 0).then(function (result) {
if (result.messages.length < 1) { if (result.messages.length < 1) {
resultsEl.innerHTML = "No results found" resultsEl.innerHTML = "No results found"
} }
@@ -194,7 +200,9 @@
merge.addEventListener('click', function () { merge.addEventListener('click', function () {
search.mergeIndexBatches().then(function () { search.mergeIndexBatches().then(function () {
search.encryptIndex(); search.encryptIndex().then(function () {
resultsEl.innerHTML = "Merging and Encrypting index completed"
});
}).catch(function (err) { }).catch(function (err) {
resultsEl.innerHTML = 'Error: ' + err; resultsEl.innerHTML = 'Error: ' + err;
}); });

View File

@@ -54,6 +54,7 @@ class Search {
* @param key * @param key
*/ */
constructor(userId, key) { constructor(userId, key) {
console.time('Decrypting');
this.isInitialized = false; this.isInitialized = false;
this.userId = userId; this.userId = userId;
this.key = key; this.key = key;
@@ -68,6 +69,7 @@ class Search {
decryptAndInit() { decryptAndInit() {
this.crypto.decryption().then(() => { this.crypto.decryption().then(() => {
console.timeEnd('Decrypting');
this.init(); this.init();
}).catch(() => { }).catch(() => {
this.init(); this.init();
@@ -206,10 +208,10 @@ class Search {
* to the main user index * to the main user index
*/ */
encryptIndex() { encryptIndex() {
this.crypto.encryption().then(() => { return this.crypto.encryption().then(() => {
return 'Success' return 'Success'
}).catch((e) => { }).catch((e) => {
throw new Error(e) return (new Error(e));
}); });
} }
@@ -219,7 +221,7 @@ class Search {
* @param {String} query * @param {String} query
* @param {Array} senderIds * @param {Array} senderIds
* @param {Array} threadIds * @param {Array} threadIds
* @param {String} attachments * @param {String} fileType
* @param {String} startDate * @param {String} startDate
* @param {String} endDate * @param {String} endDate
* @param {Number} limit * @param {Number} limit
@@ -227,7 +229,7 @@ class Search {
* @param {Number} sortOrder * @param {Number} sortOrder
* @returns {Promise} * @returns {Promise}
*/ */
searchQuery(query, senderIds, threadIds, attachments, startDate, searchQuery(query, senderIds, threadIds, fileType, startDate,
endDate, limit, offset, sortOrder) { endDate, limit, offset, sortOrder) {
let _limit = limit; let _limit = limit;
@@ -245,7 +247,7 @@ class Search {
return; return;
} }
let q = Search.constructQuery(query, senderIds, threadIds); let q = Search.constructQuery(query, senderIds, threadIds, fileType);
if (q === undefined) { if (q === undefined) {
reject(new Error('Search query error')); reject(new Error('Search query error'));
@@ -326,16 +328,17 @@ class Search {
* @param {String} searchQuery * @param {String} searchQuery
* @param {Array} senderId * @param {Array} senderId
* @param {Array} threadId * @param {Array} threadId
* @param {String} fileType
* @returns {string} * @returns {string}
*/ */
static constructQuery(searchQuery, senderId, threadId) { static constructQuery(searchQuery, senderId, threadId, fileType) {
let query = ""; let searchText = "";
if(searchQuery !== undefined) { if(searchQuery !== undefined) {
query = searchQuery.trim().toLowerCase(); //to prevent injection of AND and ORs searchText = searchQuery.trim().toLowerCase(); //to prevent injection of AND and ORs
} }
let q = ""; let q = "";
let hashTags = Search.getHashTags(query); let hashTags = Search.getHashTags(searchText);
let hashCashTagQuery = ""; let hashCashTagQuery = "";
if(hashTags.length > 0) { if(hashTags.length > 0) {
@@ -346,27 +349,51 @@ class Search {
hashCashTagQuery += ")"; hashCashTagQuery += ")";
} }
if (query.length > 0 ) { let hasAttachments = false;
q = "(text:(" + query + ")" + hashCashTagQuery + ")"; let additionalAttachmentQuery = "";
if(fileType) {
hasAttachments = true;
if(fileType === "attachment") {
additionalAttachmentQuery = "(hasfiles:true)";
} else {
additionalAttachmentQuery = "(filetype:(" + fileType +"))";
}
}
if (searchText.length > 0 ) {
q = "((text:(" + searchText + "))" + hashCashTagQuery ;
if(hasAttachments) {
q += " OR (filename:(" + searchText + "))" ;
}
q = q + ")";
} }
q = Search.appendFilterQuery(q, "senderId", senderId); q = Search.appendFilterQuery(q, "senderId", senderId);
q = Search.appendFilterQuery(q, "threadId", threadId); q = Search.appendFilterQuery(q, "threadId", threadId);
if(q === "") { if(q === "") {
q = undefined; //will be handled in the search function if(hasAttachments) {
q = additionalAttachmentQuery;
} else {
q = undefined; //will be handled in the search function
}
} else {
if(hasAttachments){
q = q + " AND " + additionalAttachmentQuery
}
} }
return q; return q;
} }
/** /**
* appending the senderId and threadId for the query * appending the senderId and threadId for the query
* @param {String} query * @param {String} searchText
* @param {String} fieldName * @param {String} fieldName
* @param {Array} valueArray * @param {Array} valueArray
* @returns {string} * @returns {string}
*/ */
static appendFilterQuery(query, fieldName, valueArray) { static appendFilterQuery(searchText, fieldName, valueArray) {
let q = ""; let q = "";
if (valueArray && valueArray.length > 0 ) { if (valueArray && valueArray.length > 0 ) {
@@ -375,12 +402,12 @@ class Search {
q+= "\"" + item + "\" "; q+= "\"" + item + "\" ";
}); });
q += "))"; q += "))";
if(query.length > 0 ) { if(searchText.length > 0 ) {
q = query + " AND " + q; q = searchText + " AND " + q;
} }
} else { } else {
q = query; q = searchText;
} }
return q; return q;
@@ -392,12 +419,12 @@ class Search {
/** /**
* return the hash cash * return the hash cash
* tags from the query * tags from the query
* @param {String} query * @param {String} searchText
* @returns {Array} * @returns {Array}
*/ */
static getHashTags(query) { static getHashTags(searchText) {
let hashTags = []; let hashTags = [];
let tokens = query.toLowerCase() let tokens = searchText.toLowerCase()
.trim() .trim()
.replace(/\s\s+/g, ' ') .replace(/\s\s+/g, ' ')
.split(' ').filter((el) => {return el.length !== 0}); .split(' ').filter((el) => {return el.length !== 0});

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.