From 57bb83497f174d405ba695a3719f9e39d89170ed Mon Sep 17 00:00:00 2001 From: Perry Mitchell Date: Tue, 6 Feb 2024 21:41:11 +0200 Subject: [PATCH] Add vault source entry search --- source/index.common.ts | 1 + source/search/BaseSearch.ts | 14 +++++++-- source/search/VaultSourceEntrySearch.ts | 39 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 source/search/VaultSourceEntrySearch.ts diff --git a/source/index.common.ts b/source/index.common.ts index ee1c121a..6594a6f8 100644 --- a/source/index.common.ts +++ b/source/index.common.ts @@ -80,6 +80,7 @@ export { SearchResult } from "./search/BaseSearch.js"; export { VaultEntrySearch as Search } from "./search/VaultEntrySearch.js"; // compat @todo remove export { VaultEntrySearch } from "./search/VaultEntrySearch.js"; export { VaultFacadeEntrySearch } from "./search/VaultFacadeEntrySearch.js"; +export { VaultSourceEntrySearch } from "./search/VaultSourceEntrySearch.js"; export { SearchKey, buildSearcher } from "./search/searcher.js"; export { AppEnv, AppEnvGetPropertyOptions } from "./env/core/appEnv.js"; diff --git a/source/search/BaseSearch.ts b/source/search/BaseSearch.ts index bf06f6a6..6079af0d 100644 --- a/source/search/BaseSearch.ts +++ b/source/search/BaseSearch.ts @@ -2,8 +2,8 @@ import levenshtein from "fast-levenshtein"; import { StorageInterface } from "../storage/StorageInterface.js"; import { buildSearcher } from "./searcher.js"; import { Vault } from "../core/Vault.js"; -import { EntryID, EntryType, GroupID, VaultFacade, VaultID } from "../types.js"; import { extractTagsFromSearchTerm, tagsMatchSearch } from "./tags.js"; +import { EntryID, EntryType, GroupID, VaultFacade, VaultID, VaultSourceID } from "../types.js"; interface DomainScores { [domain: string]: number; @@ -28,6 +28,7 @@ export interface SearchResult { id: EntryID; properties: { [property: string]: string }; tags: Array; + sourceID?: VaultSourceID; urls: Array; vaultID: VaultID; } @@ -78,11 +79,20 @@ export class BaseSearch { /** * Last search results + * @deprecated Use `getResults` instead */ get results(): Array { return this._results; } + /** + * Get last search results + * @returns An array of results + */ + getResults(): Array { + return this._results; + } + /** * Increment the score of a URL in an entry * @param vaultID The vault ID @@ -110,7 +120,7 @@ export class BaseSearch { * Prepare the search instance by processing * entries */ - async prepare() { + async prepare(): Promise { this._entries = []; this._scores = {}; for (const target of this._targets) { diff --git a/source/search/VaultSourceEntrySearch.ts b/source/search/VaultSourceEntrySearch.ts new file mode 100644 index 00000000..928b6cdb --- /dev/null +++ b/source/search/VaultSourceEntrySearch.ts @@ -0,0 +1,39 @@ +import { SearchResult, SearcherFactory } from "./BaseSearch.js"; +import { VaultSource } from "../core/VaultSource.js"; +import { Vault } from "../core/Vault.js"; +import { StorageInterface } from "../storage/StorageInterface.js"; +import { VaultEntrySearch } from "./VaultEntrySearch.js"; +import { VaultSourceStatus } from "../types.js"; + +export class VaultSourceEntrySearch extends VaultEntrySearch { + _sources: Array; + + constructor( + sources: Array, + memory?: StorageInterface, + searcherFactory?: SearcherFactory + ) { + const vaults: Array = sources.reduce((output, source) => { + if (source.status === VaultSourceStatus.Unlocked) { + return [...output, source.vault]; + } + return output; + }, []); + super(vaults, memory, searcherFactory); + this._sources = [...sources]; + } + + /** + * Last search results + */ + get results(): Array { + return this._results.map((res) => { + const output = res; + const source = this._sources.find((src) => src?.vault?.id === output.vaultID); + if (source) { + output.sourceID = source.id; + } + return output; + }); + } +}