mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-11 19:40:47 +01:00
allow ciphers to be passed into search service
This commit is contained in:
parent
f16fc58d70
commit
9ba3c17626
@ -4,6 +4,7 @@ export abstract class SearchService {
|
||||
clearIndex: () => void;
|
||||
isSearchable: (query: string) => boolean;
|
||||
indexCiphers: () => Promise<void>;
|
||||
searchCiphers: (query: string, filter?: (cipher: CipherView) => boolean) => Promise<CipherView[]>;
|
||||
searchCiphers: (query: string, filter?: (cipher: CipherView) => boolean,
|
||||
ciphers?: CipherView[]) => Promise<CipherView[]>;
|
||||
searchCiphersBasic: (ciphers: CipherView[], query: string) => CipherView[];
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
console.timeEnd('search indexing');
|
||||
}
|
||||
|
||||
async searchCiphers(query: string, filter: (cipher: CipherView) => boolean = null):
|
||||
async searchCiphers(query: string, filter: (cipher: CipherView) => boolean = null, ciphers: CipherView[] = null):
|
||||
Promise<CipherView[]> {
|
||||
const results: CipherView[] = [];
|
||||
if (query != null) {
|
||||
@ -73,7 +73,9 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
query = null;
|
||||
}
|
||||
|
||||
let ciphers = await this.cipherService.getAllDecrypted();
|
||||
if (ciphers == null) {
|
||||
ciphers = await this.cipherService.getAllDecrypted();
|
||||
}
|
||||
if (filter != null) {
|
||||
ciphers = ciphers.filter(filter);
|
||||
}
|
||||
@ -82,7 +84,8 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
return ciphers;
|
||||
}
|
||||
|
||||
if (this.index == null) {
|
||||
const index = this.getIndexForSearch();
|
||||
if (index == null) {
|
||||
// Fall back to basic search if index is not available
|
||||
return this.searchCiphersBasic(ciphers, query);
|
||||
}
|
||||
@ -94,12 +97,12 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
const isQueryString = query != null && query.length > 1 && query.indexOf('>') === 0;
|
||||
if (isQueryString) {
|
||||
try {
|
||||
searchResults = this.index.search(query.substr(1));
|
||||
searchResults = index.search(query.substr(1));
|
||||
} catch { }
|
||||
} else {
|
||||
// tslint:disable-next-line
|
||||
const soWild = lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING;
|
||||
searchResults = this.index.query((q) => {
|
||||
searchResults = index.query((q) => {
|
||||
q.term(query, { fields: ['name'], wildcard: soWild });
|
||||
q.term(query, { fields: ['subTitle'], wildcard: soWild });
|
||||
q.term(query, { fields: ['login.uris'], wildcard: soWild });
|
||||
@ -141,6 +144,10 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
});
|
||||
}
|
||||
|
||||
getIndexForSearch(): lunr.Index {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
private fieldExtractor(c: CipherView, joined: boolean) {
|
||||
if (!c.hasFields) {
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user