mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 13:53:34 +00:00
[PM-5272] Migrate CollapsedGroupings to State Provider (#7954)
This commit is contained in:
@@ -607,12 +607,12 @@ export default class MainBackground {
|
|||||||
);
|
);
|
||||||
|
|
||||||
this.vaultFilterService = new VaultFilterService(
|
this.vaultFilterService = new VaultFilterService(
|
||||||
this.stateService,
|
|
||||||
this.organizationService,
|
this.organizationService,
|
||||||
this.folderService,
|
this.folderService,
|
||||||
this.cipherService,
|
this.cipherService,
|
||||||
this.collectionService,
|
this.collectionService,
|
||||||
this.policyService,
|
this.policyService,
|
||||||
|
this.stateProvider,
|
||||||
this.accountService,
|
this.accountService,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -427,28 +427,14 @@ function getBgService<T>(service: keyof MainBackground) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
provide: VaultFilterService,
|
provide: VaultFilterService,
|
||||||
useFactory: (
|
useClass: VaultFilterService,
|
||||||
stateService: StateServiceAbstraction,
|
|
||||||
organizationService: OrganizationService,
|
|
||||||
folderService: FolderServiceAbstraction,
|
|
||||||
policyService: PolicyService,
|
|
||||||
accountService: AccountServiceAbstraction,
|
|
||||||
) => {
|
|
||||||
return new VaultFilterService(
|
|
||||||
stateService,
|
|
||||||
organizationService,
|
|
||||||
folderService,
|
|
||||||
getBgService<CipherService>("cipherService")(),
|
|
||||||
getBgService<CollectionService>("collectionService")(),
|
|
||||||
policyService,
|
|
||||||
accountService,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
deps: [
|
deps: [
|
||||||
StateServiceAbstraction,
|
|
||||||
OrganizationService,
|
OrganizationService,
|
||||||
FolderServiceAbstraction,
|
FolderServiceAbstraction,
|
||||||
|
CipherService,
|
||||||
|
CollectionService,
|
||||||
PolicyService,
|
PolicyService,
|
||||||
|
StateProvider,
|
||||||
AccountServiceAbstraction,
|
AccountServiceAbstraction,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { VaultFilterService as BaseVaultFilterService } from "@bitwarden/angular
|
|||||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||||
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
||||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { StateProvider } from "@bitwarden/common/platform/state";
|
||||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||||
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
|
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
|
||||||
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||||
@@ -16,21 +16,21 @@ export class VaultFilterService extends BaseVaultFilterService {
|
|||||||
myVault = "myVault";
|
myVault = "myVault";
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
stateService: StateService,
|
|
||||||
organizationService: OrganizationService,
|
organizationService: OrganizationService,
|
||||||
folderService: FolderService,
|
folderService: FolderService,
|
||||||
cipherService: CipherService,
|
cipherService: CipherService,
|
||||||
collectionService: CollectionService,
|
collectionService: CollectionService,
|
||||||
policyService: PolicyService,
|
policyService: PolicyService,
|
||||||
|
stateProvider: StateProvider,
|
||||||
private accountService: AccountService,
|
private accountService: AccountService,
|
||||||
) {
|
) {
|
||||||
super(
|
super(
|
||||||
stateService,
|
|
||||||
organizationService,
|
organizationService,
|
||||||
folderService,
|
folderService,
|
||||||
cipherService,
|
cipherService,
|
||||||
collectionService,
|
collectionService,
|
||||||
policyService,
|
policyService,
|
||||||
|
stateProvider,
|
||||||
);
|
);
|
||||||
this.vaultFilter.myVaultOnly = false;
|
this.vaultFilter.myVaultOnly = false;
|
||||||
this.vaultFilter.selectedOrganizationId = null;
|
this.vaultFilter.selectedOrganizationId = null;
|
||||||
|
|||||||
@@ -1,24 +1,31 @@
|
|||||||
|
import {
|
||||||
|
FakeAccountService,
|
||||||
|
mockAccountServiceWith,
|
||||||
|
} from "@bitwarden/common/../spec/fake-account-service";
|
||||||
|
import { FakeActiveUserState } from "@bitwarden/common/../spec/fake-state";
|
||||||
|
import { FakeStateProvider } from "@bitwarden/common/../spec/fake-state-provider";
|
||||||
import { mock, MockProxy } from "jest-mock-extended";
|
import { mock, MockProxy } from "jest-mock-extended";
|
||||||
import { firstValueFrom, ReplaySubject, take } from "rxjs";
|
import { firstValueFrom, ReplaySubject } from "rxjs";
|
||||||
|
|
||||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||||
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
||||||
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
||||||
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
||||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
||||||
|
import { UserId } from "@bitwarden/common/types/guid";
|
||||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||||
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
||||||
import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
|
import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
|
||||||
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
|
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
|
||||||
|
import { COLLAPSED_GROUPINGS } from "@bitwarden/common/vault/services/key-state/collapsed-groupings.state";
|
||||||
|
|
||||||
import { VaultFilterService } from "./vault-filter.service";
|
import { VaultFilterService } from "./vault-filter.service";
|
||||||
|
|
||||||
describe("vault filter service", () => {
|
describe("vault filter service", () => {
|
||||||
let vaultFilterService: VaultFilterService;
|
let vaultFilterService: VaultFilterService;
|
||||||
|
|
||||||
let stateService: MockProxy<StateService>;
|
|
||||||
let organizationService: MockProxy<OrganizationService>;
|
let organizationService: MockProxy<OrganizationService>;
|
||||||
let folderService: MockProxy<FolderService>;
|
let folderService: MockProxy<FolderService>;
|
||||||
let cipherService: MockProxy<CipherService>;
|
let cipherService: MockProxy<CipherService>;
|
||||||
@@ -26,14 +33,20 @@ describe("vault filter service", () => {
|
|||||||
let i18nService: MockProxy<I18nService>;
|
let i18nService: MockProxy<I18nService>;
|
||||||
let organizations: ReplaySubject<Organization[]>;
|
let organizations: ReplaySubject<Organization[]>;
|
||||||
let folderViews: ReplaySubject<FolderView[]>;
|
let folderViews: ReplaySubject<FolderView[]>;
|
||||||
|
let stateProvider: FakeStateProvider;
|
||||||
|
|
||||||
|
const mockUserId = Utils.newGuid() as UserId;
|
||||||
|
let accountService: FakeAccountService;
|
||||||
|
let collapsedGroupingsState: FakeActiveUserState<string[]>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateService = mock<StateService>();
|
|
||||||
organizationService = mock<OrganizationService>();
|
organizationService = mock<OrganizationService>();
|
||||||
folderService = mock<FolderService>();
|
folderService = mock<FolderService>();
|
||||||
cipherService = mock<CipherService>();
|
cipherService = mock<CipherService>();
|
||||||
policyService = mock<PolicyService>();
|
policyService = mock<PolicyService>();
|
||||||
i18nService = mock<I18nService>();
|
i18nService = mock<I18nService>();
|
||||||
|
accountService = mockAccountServiceWith(mockUserId);
|
||||||
|
stateProvider = new FakeStateProvider(accountService);
|
||||||
i18nService.collator = new Intl.Collator("en-US");
|
i18nService.collator = new Intl.Collator("en-US");
|
||||||
|
|
||||||
organizations = new ReplaySubject<Organization[]>(1);
|
organizations = new ReplaySubject<Organization[]>(1);
|
||||||
@@ -43,31 +56,32 @@ describe("vault filter service", () => {
|
|||||||
folderService.folderViews$ = folderViews;
|
folderService.folderViews$ = folderViews;
|
||||||
|
|
||||||
vaultFilterService = new VaultFilterService(
|
vaultFilterService = new VaultFilterService(
|
||||||
stateService,
|
|
||||||
organizationService,
|
organizationService,
|
||||||
folderService,
|
folderService,
|
||||||
cipherService,
|
cipherService,
|
||||||
policyService,
|
policyService,
|
||||||
i18nService,
|
i18nService,
|
||||||
|
stateProvider,
|
||||||
);
|
);
|
||||||
|
collapsedGroupingsState = stateProvider.activeUser.getFake(COLLAPSED_GROUPINGS);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("collapsed filter nodes", () => {
|
describe("collapsed filter nodes", () => {
|
||||||
const nodes = new Set(["1", "2"]);
|
const nodes = new Set(["1", "2"]);
|
||||||
it("updates observable when saving", (complete) => {
|
|
||||||
vaultFilterService.collapsedFilterNodes$.pipe(take(1)).subscribe((value) => {
|
|
||||||
if (value === nodes) {
|
|
||||||
complete();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
|
it("should update the collapsedFilterNodes$", async () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
await vaultFilterService.setCollapsedFilterNodes(nodes);
|
||||||
vaultFilterService.setCollapsedFilterNodes(nodes);
|
|
||||||
|
const collapsedGroupingsState = stateProvider.activeUser.getFake(COLLAPSED_GROUPINGS);
|
||||||
|
expect(await firstValueFrom(collapsedGroupingsState.state$)).toEqual(Array.from(nodes));
|
||||||
|
expect(collapsedGroupingsState.nextMock).toHaveBeenCalledWith([
|
||||||
|
mockUserId,
|
||||||
|
Array.from(nodes),
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("loads from state on initialization", async () => {
|
it("loads from state on initialization", async () => {
|
||||||
stateService.getCollapsedGroupings.mockResolvedValue(["1", "2"]);
|
collapsedGroupingsState.nextState(["1", "2"]);
|
||||||
|
|
||||||
await expect(firstValueFrom(vaultFilterService.collapsedFilterNodes$)).resolves.toEqual(
|
await expect(firstValueFrom(vaultFilterService.collapsedFilterNodes$)).resolves.toEqual(
|
||||||
nodes,
|
nodes,
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli
|
|||||||
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
||||||
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
||||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state";
|
||||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||||
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||||
import { CipherType } from "@bitwarden/common/vault/enums";
|
import { CipherType } from "@bitwarden/common/vault/enums";
|
||||||
@@ -24,6 +24,7 @@ import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
|||||||
import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
|
import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view";
|
||||||
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
|
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
|
||||||
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
||||||
|
import { COLLAPSED_GROUPINGS } from "@bitwarden/common/vault/services/key-state/collapsed-groupings.state";
|
||||||
|
|
||||||
import { CollectionAdminView } from "../../../core/views/collection-admin.view";
|
import { CollectionAdminView } from "../../../core/views/collection-admin.view";
|
||||||
import {
|
import {
|
||||||
@@ -39,11 +40,6 @@ const NestingDelimiter = "/";
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class VaultFilterService implements VaultFilterServiceAbstraction {
|
export class VaultFilterService implements VaultFilterServiceAbstraction {
|
||||||
protected _collapsedFilterNodes = new BehaviorSubject<Set<string>>(null);
|
|
||||||
collapsedFilterNodes$: Observable<Set<string>> = this._collapsedFilterNodes.pipe(
|
|
||||||
switchMap(async (nodes) => nodes ?? (await this.getCollapsedFilterNodes())),
|
|
||||||
);
|
|
||||||
|
|
||||||
organizationTree$: Observable<TreeNode<OrganizationFilter>> =
|
organizationTree$: Observable<TreeNode<OrganizationFilter>> =
|
||||||
this.organizationService.memberOrganizations$.pipe(
|
this.organizationService.memberOrganizations$.pipe(
|
||||||
switchMap((orgs) => this.buildOrganizationTree(orgs)),
|
switchMap((orgs) => this.buildOrganizationTree(orgs)),
|
||||||
@@ -78,13 +74,19 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
|
|||||||
|
|
||||||
cipherTypeTree$: Observable<TreeNode<CipherTypeFilter>> = this.buildCipherTypeTree();
|
cipherTypeTree$: Observable<TreeNode<CipherTypeFilter>> = this.buildCipherTypeTree();
|
||||||
|
|
||||||
|
private collapsedGroupingsState: ActiveUserState<string[]> =
|
||||||
|
this.stateProvider.getActive(COLLAPSED_GROUPINGS);
|
||||||
|
|
||||||
|
readonly collapsedFilterNodes$: Observable<Set<string>> =
|
||||||
|
this.collapsedGroupingsState.state$.pipe(map((c) => new Set(c)));
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
protected stateService: StateService,
|
|
||||||
protected organizationService: OrganizationService,
|
protected organizationService: OrganizationService,
|
||||||
protected folderService: FolderService,
|
protected folderService: FolderService,
|
||||||
protected cipherService: CipherService,
|
protected cipherService: CipherService,
|
||||||
protected policyService: PolicyService,
|
protected policyService: PolicyService,
|
||||||
protected i18nService: I18nService,
|
protected i18nService: I18nService,
|
||||||
|
protected stateProvider: StateProvider,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async reloadCollections(collections: CollectionView[]) {
|
async reloadCollections(collections: CollectionView[]) {
|
||||||
@@ -97,13 +99,11 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async setCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> {
|
async setCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> {
|
||||||
await this.stateService.setCollapsedGroupings(Array.from(collapsedFilterNodes));
|
await this.collapsedGroupingsState.update(() => Array.from(collapsedFilterNodes));
|
||||||
this._collapsedFilterNodes.next(collapsedFilterNodes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async getCollapsedFilterNodes(): Promise<Set<string>> {
|
protected async getCollapsedFilterNodes(): Promise<Set<string>> {
|
||||||
const nodes = new Set(await this.stateService.getCollapsedGroupings());
|
return await firstValueFrom(this.collapsedFilterNodes$);
|
||||||
return nodes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getOrganizationFilter() {
|
getOrganizationFilter() {
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ import { map, Observable, ReplaySubject, Subject } from "rxjs";
|
|||||||
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
|
||||||
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
||||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
|
import { StateProvider } from "@bitwarden/common/platform/state";
|
||||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||||
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||||
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
||||||
|
|
||||||
import { StateService } from "../../../core";
|
|
||||||
import { CollectionAdminView } from "../../../vault/core/views/collection-admin.view";
|
import { CollectionAdminView } from "../../../vault/core/views/collection-admin.view";
|
||||||
import { CollectionAdminService } from "../../core/collection-admin.service";
|
import { CollectionAdminService } from "../../core/collection-admin.service";
|
||||||
import { VaultFilterService as BaseVaultFilterService } from "../../individual-vault/vault-filter/services/vault-filter.service";
|
import { VaultFilterService as BaseVaultFilterService } from "../../individual-vault/vault-filter/services/vault-filter.service";
|
||||||
@@ -26,21 +26,21 @@ export class VaultFilterService extends BaseVaultFilterService implements OnDest
|
|||||||
);
|
);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
stateService: StateService,
|
|
||||||
organizationService: OrganizationService,
|
organizationService: OrganizationService,
|
||||||
folderService: FolderService,
|
folderService: FolderService,
|
||||||
cipherService: CipherService,
|
cipherService: CipherService,
|
||||||
policyService: PolicyService,
|
policyService: PolicyService,
|
||||||
i18nService: I18nService,
|
i18nService: I18nService,
|
||||||
|
stateProvider: StateProvider,
|
||||||
protected collectionAdminService: CollectionAdminService,
|
protected collectionAdminService: CollectionAdminService,
|
||||||
) {
|
) {
|
||||||
super(
|
super(
|
||||||
stateService,
|
|
||||||
organizationService,
|
organizationService,
|
||||||
folderService,
|
folderService,
|
||||||
cipherService,
|
cipherService,
|
||||||
policyService,
|
policyService,
|
||||||
i18nService,
|
i18nService,
|
||||||
|
stateProvider,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { firstValueFrom, from, mergeMap, Observable } from "rxjs";
|
import { firstValueFrom, from, map, mergeMap, Observable } from "rxjs";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
isMember,
|
isMember,
|
||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
||||||
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
||||||
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
|
||||||
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state";
|
||||||
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
|
||||||
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
|
import { CollectionService } from "@bitwarden/common/vault/abstractions/collection.service";
|
||||||
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
|
||||||
@@ -20,25 +20,32 @@ import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
|||||||
import { DeprecatedVaultFilterService as DeprecatedVaultFilterServiceAbstraction } from "../../abstractions/deprecated-vault-filter.service";
|
import { DeprecatedVaultFilterService as DeprecatedVaultFilterServiceAbstraction } from "../../abstractions/deprecated-vault-filter.service";
|
||||||
import { DynamicTreeNode } from "../models/dynamic-tree-node.model";
|
import { DynamicTreeNode } from "../models/dynamic-tree-node.model";
|
||||||
|
|
||||||
|
import { COLLAPSED_GROUPINGS } from "./../../../../../common/src/vault/services/key-state/collapsed-groupings.state";
|
||||||
|
|
||||||
const NestingDelimiter = "/";
|
const NestingDelimiter = "/";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class VaultFilterService implements DeprecatedVaultFilterServiceAbstraction {
|
export class VaultFilterService implements DeprecatedVaultFilterServiceAbstraction {
|
||||||
|
private collapsedGroupingsState: ActiveUserState<string[]> =
|
||||||
|
this.stateProvider.getActive(COLLAPSED_GROUPINGS);
|
||||||
|
private readonly collapsedGroupings$: Observable<Set<string>> =
|
||||||
|
this.collapsedGroupingsState.state$.pipe(map((c) => new Set(c)));
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
protected stateService: StateService,
|
|
||||||
protected organizationService: OrganizationService,
|
protected organizationService: OrganizationService,
|
||||||
protected folderService: FolderService,
|
protected folderService: FolderService,
|
||||||
protected cipherService: CipherService,
|
protected cipherService: CipherService,
|
||||||
protected collectionService: CollectionService,
|
protected collectionService: CollectionService,
|
||||||
protected policyService: PolicyService,
|
protected policyService: PolicyService,
|
||||||
|
protected stateProvider: StateProvider,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async storeCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> {
|
async storeCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> {
|
||||||
await this.stateService.setCollapsedGroupings(Array.from(collapsedFilterNodes));
|
await this.collapsedGroupingsState.update(() => Array.from(collapsedFilterNodes));
|
||||||
}
|
}
|
||||||
|
|
||||||
async buildCollapsedFilterNodes(): Promise<Set<string>> {
|
async buildCollapsedFilterNodes(): Promise<Set<string>> {
|
||||||
return new Set(await this.stateService.getCollapsedGroupings());
|
return await firstValueFrom(this.collapsedGroupings$);
|
||||||
}
|
}
|
||||||
|
|
||||||
async buildOrganizations(): Promise<Organization[]> {
|
async buildOrganizations(): Promise<Organization[]> {
|
||||||
|
|||||||
@@ -80,8 +80,6 @@ export abstract class StateService<T extends Account = Account> {
|
|||||||
getHasPremiumFromOrganization: (options?: StorageOptions) => Promise<boolean>;
|
getHasPremiumFromOrganization: (options?: StorageOptions) => Promise<boolean>;
|
||||||
getClearClipboard: (options?: StorageOptions) => Promise<number>;
|
getClearClipboard: (options?: StorageOptions) => Promise<number>;
|
||||||
setClearClipboard: (value: number, options?: StorageOptions) => Promise<void>;
|
setClearClipboard: (value: number, options?: StorageOptions) => Promise<void>;
|
||||||
getCollapsedGroupings: (options?: StorageOptions) => Promise<string[]>;
|
|
||||||
setCollapsedGroupings: (value: string[], options?: StorageOptions) => Promise<void>;
|
|
||||||
getConvertAccountToKeyConnector: (options?: StorageOptions) => Promise<boolean>;
|
getConvertAccountToKeyConnector: (options?: StorageOptions) => Promise<boolean>;
|
||||||
setConvertAccountToKeyConnector: (value: boolean, options?: StorageOptions) => Promise<void>;
|
setConvertAccountToKeyConnector: (value: boolean, options?: StorageOptions) => Promise<void>;
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import { DeepJsonify } from "../../../types/deep-jsonify";
|
|||||||
import { MasterKey, UserKey } from "../../../types/key";
|
import { MasterKey, UserKey } from "../../../types/key";
|
||||||
import { UriMatchType } from "../../../vault/enums";
|
import { UriMatchType } from "../../../vault/enums";
|
||||||
import { CipherData } from "../../../vault/models/data/cipher.data";
|
import { CipherData } from "../../../vault/models/data/cipher.data";
|
||||||
import { FolderData } from "../../../vault/models/data/folder.data";
|
|
||||||
import { CipherView } from "../../../vault/models/view/cipher.view";
|
import { CipherView } from "../../../vault/models/view/cipher.view";
|
||||||
import { AddEditCipherInfo } from "../../../vault/types/add-edit-cipher-info";
|
import { AddEditCipherInfo } from "../../../vault/types/add-edit-cipher-info";
|
||||||
import { KdfType } from "../../enums";
|
import { KdfType } from "../../enums";
|
||||||
@@ -87,7 +86,6 @@ export class AccountData {
|
|||||||
CipherData,
|
CipherData,
|
||||||
CipherView
|
CipherView
|
||||||
>();
|
>();
|
||||||
folders? = new TemporaryDataEncryption<FolderData>();
|
|
||||||
localData?: any;
|
localData?: any;
|
||||||
sends?: DataEncryptionPair<SendData, SendView> = new DataEncryptionPair<SendData, SendView>();
|
sends?: DataEncryptionPair<SendData, SendView> = new DataEncryptionPair<SendData, SendView>();
|
||||||
policies?: DataEncryptionPair<PolicyData, Policy> = new DataEncryptionPair<PolicyData, Policy>();
|
policies?: DataEncryptionPair<PolicyData, Policy> = new DataEncryptionPair<PolicyData, Policy>();
|
||||||
@@ -206,7 +204,6 @@ export class AccountSettings {
|
|||||||
autoConfirmFingerPrints?: boolean;
|
autoConfirmFingerPrints?: boolean;
|
||||||
biometricUnlock?: boolean;
|
biometricUnlock?: boolean;
|
||||||
clearClipboard?: number;
|
clearClipboard?: number;
|
||||||
collapsedGroupings?: string[];
|
|
||||||
defaultUriMatch?: UriMatchType;
|
defaultUriMatch?: UriMatchType;
|
||||||
disableAutoBiometricsPrompt?: boolean;
|
disableAutoBiometricsPrompt?: boolean;
|
||||||
disableBadgeCounter?: boolean;
|
disableBadgeCounter?: boolean;
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ export class GlobalState {
|
|||||||
enableBrowserIntegrationFingerprint?: boolean;
|
enableBrowserIntegrationFingerprint?: boolean;
|
||||||
enableDuckDuckGoBrowserIntegration?: boolean;
|
enableDuckDuckGoBrowserIntegration?: boolean;
|
||||||
neverDomains?: { [id: string]: unknown };
|
neverDomains?: { [id: string]: unknown };
|
||||||
enablePasskeys?: boolean;
|
|
||||||
disableAddLoginNotification?: boolean;
|
disableAddLoginNotification?: boolean;
|
||||||
disableChangedPasswordNotification?: boolean;
|
disableChangedPasswordNotification?: boolean;
|
||||||
disableContextMenuItem?: boolean;
|
disableContextMenuItem?: boolean;
|
||||||
|
|||||||
@@ -495,23 +495,6 @@ export class StateService<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCollapsedGroupings(options?: StorageOptions): Promise<string[]> {
|
|
||||||
return (
|
|
||||||
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()))
|
|
||||||
)?.settings?.collapsedGroupings;
|
|
||||||
}
|
|
||||||
|
|
||||||
async setCollapsedGroupings(value: string[], options?: StorageOptions): Promise<void> {
|
|
||||||
const account = await this.getAccount(
|
|
||||||
this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()),
|
|
||||||
);
|
|
||||||
account.settings.collapsedGroupings = value;
|
|
||||||
await this.saveAccount(
|
|
||||||
account,
|
|
||||||
this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getConvertAccountToKeyConnector(options?: StorageOptions): Promise<boolean> {
|
async getConvertAccountToKeyConnector(options?: StorageOptions): Promise<boolean> {
|
||||||
return (
|
return (
|
||||||
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))
|
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))
|
||||||
|
|||||||
@@ -57,3 +57,7 @@ export const AUTOFILL_SETTINGS_DISK = new StateDefinition("autofillSettings", "d
|
|||||||
export const AUTOFILL_SETTINGS_DISK_LOCAL = new StateDefinition("autofillSettingsLocal", "disk", {
|
export const AUTOFILL_SETTINGS_DISK_LOCAL = new StateDefinition("autofillSettingsLocal", "disk", {
|
||||||
web: "disk-local",
|
web: "disk-local",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const VAULT_FILTER_DISK = new StateDefinition("vaultFilter", "disk", {
|
||||||
|
web: "disk-local",
|
||||||
|
});
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { AutofillSettingsKeyMigrator } from "./migrations/18-move-autofill-setti
|
|||||||
import { RequirePasswordOnStartMigrator } from "./migrations/19-migrate-require-password-on-start";
|
import { RequirePasswordOnStartMigrator } from "./migrations/19-migrate-require-password-on-start";
|
||||||
import { PrivateKeyMigrator } from "./migrations/20-move-private-key-to-state-providers";
|
import { PrivateKeyMigrator } from "./migrations/20-move-private-key-to-state-providers";
|
||||||
import { CollectionMigrator } from "./migrations/21-move-collections-state-to-state-provider";
|
import { CollectionMigrator } from "./migrations/21-move-collections-state-to-state-provider";
|
||||||
|
import { CollapsedGroupingsMigrator } from "./migrations/22-move-collapsed-groupings-to-state-provider";
|
||||||
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
|
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
|
||||||
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
||||||
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
||||||
@@ -26,7 +27,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
|
|||||||
import { MinVersionMigrator } from "./migrations/min-version";
|
import { MinVersionMigrator } from "./migrations/min-version";
|
||||||
|
|
||||||
export const MIN_VERSION = 2;
|
export const MIN_VERSION = 2;
|
||||||
export const CURRENT_VERSION = 21;
|
export const CURRENT_VERSION = 22;
|
||||||
export type MinVersion = typeof MIN_VERSION;
|
export type MinVersion = typeof MIN_VERSION;
|
||||||
|
|
||||||
export function createMigrationBuilder() {
|
export function createMigrationBuilder() {
|
||||||
@@ -50,7 +51,8 @@ export function createMigrationBuilder() {
|
|||||||
.with(AutofillSettingsKeyMigrator, 17, 18)
|
.with(AutofillSettingsKeyMigrator, 17, 18)
|
||||||
.with(RequirePasswordOnStartMigrator, 18, 19)
|
.with(RequirePasswordOnStartMigrator, 18, 19)
|
||||||
.with(PrivateKeyMigrator, 19, 20)
|
.with(PrivateKeyMigrator, 19, 20)
|
||||||
.with(CollectionMigrator, 20, CURRENT_VERSION);
|
.with(CollectionMigrator, 20, 21)
|
||||||
|
.with(CollapsedGroupingsMigrator, 21, CURRENT_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function currentVersion(
|
export async function currentVersion(
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
import { MockProxy, any } from "jest-mock-extended";
|
||||||
|
|
||||||
|
import { MigrationHelper } from "../migration-helper";
|
||||||
|
import { mockMigrationHelper } from "../migration-helper.spec";
|
||||||
|
|
||||||
|
import { CollapsedGroupingsMigrator } from "./22-move-collapsed-groupings-to-state-provider";
|
||||||
|
|
||||||
|
function exampleJSON() {
|
||||||
|
return {
|
||||||
|
global: {
|
||||||
|
otherStuff: "otherStuff1",
|
||||||
|
},
|
||||||
|
authenticatedAccounts: ["user-1", "user-2", "user-3"],
|
||||||
|
"user-1": {
|
||||||
|
settings: {
|
||||||
|
collapsedGroupings: ["grouping-1", "grouping-2"],
|
||||||
|
otherStuff: "otherStuff2",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff3",
|
||||||
|
},
|
||||||
|
"user-2": {
|
||||||
|
settings: {
|
||||||
|
otherStuff: "otherStuff4",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff5",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function rollbackJSON() {
|
||||||
|
return {
|
||||||
|
"user_user-1_vaultFilter_collapsedGroupings": ["grouping-1", "grouping-2"],
|
||||||
|
global: {
|
||||||
|
otherStuff: "otherStuff1",
|
||||||
|
},
|
||||||
|
authenticatedAccounts: ["user-1", "user-2"],
|
||||||
|
"user-1": {
|
||||||
|
settings: {
|
||||||
|
otherStuff: "otherStuff2",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff3",
|
||||||
|
},
|
||||||
|
"user-2": {
|
||||||
|
settings: {
|
||||||
|
otherStuff: "otherStuff4",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff5",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("CollapsedGroupingsMigrator", () => {
|
||||||
|
let helper: MockProxy<MigrationHelper>;
|
||||||
|
let sut: CollapsedGroupingsMigrator;
|
||||||
|
const keyDefinitionLike = {
|
||||||
|
key: "collapsedGroupings",
|
||||||
|
stateDefinition: {
|
||||||
|
name: "vaultFilter",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("migrate", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
helper = mockMigrationHelper(exampleJSON(), 21);
|
||||||
|
sut = new CollapsedGroupingsMigrator(21, 22);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should remove collapsedGroupings from all accounts", async () => {
|
||||||
|
await sut.migrate(helper);
|
||||||
|
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||||
|
settings: {
|
||||||
|
otherStuff: "otherStuff2",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff3",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set collapsedGroupings values for each account", async () => {
|
||||||
|
await sut.migrate(helper);
|
||||||
|
|
||||||
|
expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, [
|
||||||
|
"grouping-1",
|
||||||
|
"grouping-2",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("rollback", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
helper = mockMigrationHelper(rollbackJSON(), 22);
|
||||||
|
sut = new CollapsedGroupingsMigrator(21, 22);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["user-1", "user-2"])("should null out new values", async (userId) => {
|
||||||
|
await sut.rollback(helper);
|
||||||
|
expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should add explicit value back to accounts", async () => {
|
||||||
|
await sut.rollback(helper);
|
||||||
|
|
||||||
|
expect(helper.set).toHaveBeenCalledTimes(2);
|
||||||
|
expect(helper.set).toHaveBeenCalledWith("user-1", {
|
||||||
|
settings: {
|
||||||
|
collapsedGroupings: ["grouping-1", "grouping-2"],
|
||||||
|
otherStuff: "otherStuff2",
|
||||||
|
},
|
||||||
|
otherStuff: "otherStuff3",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not try to restore values to missing accounts", async () => {
|
||||||
|
await sut.rollback(helper);
|
||||||
|
|
||||||
|
expect(helper.set).not.toHaveBeenCalledWith("user-3", any());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||||
|
import { Migrator } from "../migrator";
|
||||||
|
|
||||||
|
type ExpectedAccountType = {
|
||||||
|
settings?: {
|
||||||
|
collapsedGroupings?: string[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const COLLAPSED_GROUPINGS: KeyDefinitionLike = {
|
||||||
|
key: "collapsedGroupings",
|
||||||
|
stateDefinition: {
|
||||||
|
name: "vaultFilter",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export class CollapsedGroupingsMigrator extends Migrator<21, 22> {
|
||||||
|
async migrate(helper: MigrationHelper): Promise<void> {
|
||||||
|
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||||
|
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
||||||
|
const value = account?.settings?.collapsedGroupings;
|
||||||
|
if (value != null) {
|
||||||
|
await helper.setToUser(userId, COLLAPSED_GROUPINGS, value);
|
||||||
|
delete account.settings.collapsedGroupings;
|
||||||
|
await helper.set(userId, account);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async rollback(helper: MigrationHelper): Promise<void> {
|
||||||
|
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||||
|
async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
||||||
|
const value = await helper.getFromUser(userId, COLLAPSED_GROUPINGS);
|
||||||
|
if (account) {
|
||||||
|
account.settings = Object.assign(account.settings ?? {}, {
|
||||||
|
collapsedGroupings: value,
|
||||||
|
});
|
||||||
|
await helper.set(userId, account);
|
||||||
|
}
|
||||||
|
await helper.setToUser(userId, COLLAPSED_GROUPINGS, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { KeyDefinition, VAULT_FILTER_DISK } from "../../../platform/state";
|
||||||
|
|
||||||
|
export const COLLAPSED_GROUPINGS = KeyDefinition.array<string>(
|
||||||
|
VAULT_FILTER_DISK,
|
||||||
|
"collapsedGroupings",
|
||||||
|
{
|
||||||
|
deserializer: (obj) => obj,
|
||||||
|
},
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user