mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 13:23:34 +00:00
[PM-329] Detangle SearchService & CipherService (#4838)
* Remove Circular Dependency * Fix Vault Searching * Remove Unused cipherServiceOptions * Add searchService Parameter to CipherService * Fix instantiation of CipherService in test
This commit is contained in:
@@ -5,7 +5,7 @@ export abstract class SearchService {
|
||||
indexedEntityId?: string = null;
|
||||
clearIndex: () => void;
|
||||
isSearchable: (query: string) => boolean;
|
||||
indexCiphers: (indexedEntityGuid?: string, ciphersToIndex?: CipherView[]) => Promise<void>;
|
||||
indexCiphers: (ciphersToIndex: CipherView[], indexedEntityGuid?: string) => void;
|
||||
searchCiphers: (
|
||||
query: string,
|
||||
filter?: ((cipher: CipherView) => boolean) | ((cipher: CipherView) => boolean)[],
|
||||
|
||||
@@ -5,7 +5,6 @@ import { LogService } from "../abstractions/log.service";
|
||||
import { SearchService as SearchServiceAbstraction } from "../abstractions/search.service";
|
||||
import { FieldType, UriMatchType } from "../enums";
|
||||
import { SendView } from "../tools/send/models/view/send.view";
|
||||
import { CipherService } from "../vault/abstractions/cipher.service";
|
||||
import { CipherType } from "../vault/enums/cipher-type";
|
||||
import { CipherView } from "../vault/models/view/cipher.view";
|
||||
|
||||
@@ -19,11 +18,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
private readonly defaultSearchableMinLength: number = 2;
|
||||
private searchableMinLength: number = this.defaultSearchableMinLength;
|
||||
|
||||
constructor(
|
||||
private cipherService: CipherService,
|
||||
private logService: LogService,
|
||||
private i18nService: I18nService
|
||||
) {
|
||||
constructor(private logService: LogService, private i18nService: I18nService) {
|
||||
this.i18nService.locale$.subscribe((locale) => {
|
||||
if (this.immediateSearchLocales.indexOf(locale) !== -1) {
|
||||
this.searchableMinLength = 1;
|
||||
@@ -55,7 +50,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
return !notSearchable;
|
||||
}
|
||||
|
||||
async indexCiphers(indexedEntityId?: string, ciphers?: CipherView[]): Promise<void> {
|
||||
indexCiphers(ciphers: CipherView[], indexedEntityId?: string): void {
|
||||
if (this.indexing) {
|
||||
return;
|
||||
}
|
||||
@@ -94,7 +89,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
extractor: (c: CipherView) => this.attachmentExtractor(c, true),
|
||||
});
|
||||
builder.field("organizationid", { extractor: (c: CipherView) => c.organizationId });
|
||||
ciphers = ciphers || (await this.cipherService.getAllDecrypted());
|
||||
ciphers = ciphers || [];
|
||||
ciphers.forEach((c) => builder.add(c));
|
||||
this.index = builder.build();
|
||||
|
||||
@@ -106,7 +101,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
async searchCiphers(
|
||||
query: string,
|
||||
filter: ((cipher: CipherView) => boolean) | ((cipher: CipherView) => boolean)[] = null,
|
||||
ciphers: CipherView[] = null
|
||||
ciphers: CipherView[]
|
||||
): Promise<CipherView[]> {
|
||||
const results: CipherView[] = [];
|
||||
if (query != null) {
|
||||
@@ -117,7 +112,7 @@ export class SearchService implements SearchServiceAbstraction {
|
||||
}
|
||||
|
||||
if (ciphers == null) {
|
||||
ciphers = await this.cipherService.getAllDecrypted();
|
||||
ciphers = [];
|
||||
}
|
||||
|
||||
if (filter != null && Array.isArray(filter) && filter.length > 0) {
|
||||
|
||||
@@ -49,7 +49,7 @@ describe("Cipher Service", () => {
|
||||
settingsService,
|
||||
apiService,
|
||||
i18nService,
|
||||
() => searchService,
|
||||
searchService,
|
||||
stateService,
|
||||
encryptService,
|
||||
cipherFileUploadService
|
||||
|
||||
@@ -53,7 +53,7 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
private settingsService: SettingsService,
|
||||
private apiService: ApiService,
|
||||
private i18nService: I18nService,
|
||||
private searchService: () => SearchService,
|
||||
private searchService: SearchService,
|
||||
private stateService: StateService,
|
||||
private encryptService: EncryptService,
|
||||
private cipherFileUploadService: CipherFileUploadService
|
||||
@@ -68,9 +68,9 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
await this.stateService.setDecryptedCiphers(value);
|
||||
if (this.searchService != null) {
|
||||
if (value == null) {
|
||||
this.searchService().clearIndex();
|
||||
this.searchService.clearIndex();
|
||||
} else {
|
||||
this.searchService().indexCiphers();
|
||||
this.searchService.indexCiphers(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,9 +358,9 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
private async reindexCiphers() {
|
||||
const userId = await this.stateService.getUserId();
|
||||
const reindexRequired =
|
||||
this.searchService != null && (this.searchService().indexedEntityId ?? userId) !== userId;
|
||||
this.searchService != null && (this.searchService.indexedEntityId ?? userId) !== userId;
|
||||
if (reindexRequired) {
|
||||
await this.searchService().indexCiphers(userId, await this.getDecryptedCipherCache());
|
||||
this.searchService.indexCiphers(await this.getDecryptedCipherCache(), userId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user