Searching: Add initial pagination for combined searches.

pull/21833/head
Damir Jelić 2020-05-29 16:32:57 +02:00
parent e94f3422df
commit c1ef193e3a
1 changed files with 114 additions and 8 deletions

View File

@ -71,6 +71,18 @@ async function combinedSearch(searchTerm) {
result.highlights = localResult.highlights.concat(
serverSideResult.highlights);
result.seshatQuery = localResult.seshatQuery;
result.serverSideNextBatch = serverSideResult.next_batch;
result._query = serverSideResult._query;
// We need the next batch to be set for the client to know that it can
// paginate further.
if (serverSideResult.next_batch) {
result.next_batch = serverSideResult.next_batch;
} else {
result.next_batch = localResult.next_batch;
}
return result;
}
@ -106,16 +118,16 @@ async function localSearch(searchTerm, roomId = undefined) {
},
};
return MatrixClientPeg.get()._processRoomEventsSearch(
emptyResult, response);
return MatrixClientPeg.get()._processRoomEventsSearch(emptyResult, response);
}
async function paginatedLocalSearch(searchResult) {
async function localPagination(searchResult) {
const eventIndex = EventIndexPeg.get();
let searchArgs = searchResult.seshatQuery;
const searchArgs = searchResult.seshatQuery;
const localResult = await eventIndex.search(searchArgs);
searchResult.seshatQuery.next_batch = localResult.next_batch;
const response = {
search_categories: {
@ -129,6 +141,92 @@ async function paginatedLocalSearch(searchResult) {
return result;
}
/**
* Combine the local and server search results
*/
function combineResults(previousSearchResult, localResult = undefined, serverSideResult = undefined) {
// // cachedResults = previousSearchResult.cachedResults;
// if (localResult) {
// previousSearchResult.seshatQuery.next_batch = localResult.next_batch;
// }
const compare = (a, b) => {
const aEvent = a.result;
const bEvent = b.result;
if (aEvent.origin_server_ts >
bEvent.origin_server_ts) return -1;
if (aEvent.origin_server_ts <
bEvent.origin_server_ts) return 1;
return 0;
};
const result = {};
result.count = previousSearchResult.count;
if (localResult && serverSideResult) {
result.results = localResult.results.concat(serverSideResult.results).sort(compare);
result.highlights = localResult.highlights.concat(serverSideResult.highlights);
} else if (localResult) {
result.results = localResult.results;
result.highlights = localResult.highlights;
} else {
result.results = serverSideResult.results;
result.highlights = serverSideResult.highlights;
}
if (localResult) {
previousSearchResult.seshatQuery.next_batch = localResult.next_batch;
result.next_batch = localResult.next_batch;
}
if (serverSideResult && serverSideResult.next_batch) {
previousSearchResult.serverSideNextBatch = serverSideResult.next_batch;
result.next_batch = serverSideResult.next_batch;
}
console.log("HELLOO COMBINING RESULTS", localResult, serverSideResult, result);
return result
}
async function combinedPagination(searchResult) {
const eventIndex = EventIndexPeg.get();
const client = MatrixClientPeg.get();
console.log("HELLOOO WORLD");
const searchArgs = searchResult.seshatQuery;
let localResult;
let serverSideResult;
if (searchArgs.next_batch) {
localResult = await eventIndex.search(searchArgs);
}
if (searchResult.serverSideNextBatch) {
const body = {body: searchResult._query, next_batch: searchResult.serverSideNextBatch};
serverSideResult = await client.search(body);
}
const combinedResult = combineResults(searchResult, localResult, serverSideResult.search_categories.room_events);
const response = {
search_categories: {
room_events: combinedResult,
},
};
const result = client._processRoomEventsSearch(searchResult, response);
console.log("HELLO NEW RESULT", searchResult);
searchResult.pendingRequest = null;
return result
}
function eventIndexSearch(term, roomId = undefined) {
let searchPromise;
@ -153,14 +251,22 @@ function eventIndexSearch(term, roomId = undefined) {
function eventIndexSearchPagination(searchResult) {
const client = MatrixClientPeg.get();
const query = searchResult.seshatQuery;
if (!query) {
const seshatQuery = searchResult.seshatQuery;
const serverQuery = searchResult._query;
if (!seshatQuery) {
return client.backPaginateRoomEventsSearch(searchResult);
} else {
const promise = paginatedLocalSearch(searchResult);
} else if (!serverQuery) {
const promise = localPagination(searchResult);
searchResult.pendingRequest = promise;
return promise;
} else {
const promise = combinedPagination(searchResult);
searchResult.pendingRequest = promise;
return promise
}
}