Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jprusik committed Dec 17, 2024
1 parent bd3f573 commit 1dd180e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { JslibModule } from "@bitwarden/angular/jslib.module";
import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service";
import { NeverDomains } from "@bitwarden/common/models/domain/domain-service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import {
ButtonModule,
Expand All @@ -26,6 +25,7 @@ import {
LinkModule,
SectionComponent,
SectionHeaderComponent,
ToastService,
TypographyModule,
} from "@bitwarden/components";

Expand Down Expand Up @@ -59,7 +59,8 @@ import { PopupPageComponent } from "../../../platform/popup/layout/popup-page.co
],
})
export class BlockedDomainsComponent implements AfterViewInit, OnDestroy {
@ViewChildren("uriInput") uriInputElements: QueryList<ElementRef<HTMLInputElement>>;
@ViewChildren("uriInput") uriInputElements: QueryList<ElementRef<HTMLInputElement>> =
new QueryList();

dataIsPristine = true;
isLoading = false;
Expand All @@ -73,7 +74,7 @@ export class BlockedDomainsComponent implements AfterViewInit, OnDestroy {
constructor(
private domainSettingsService: DomainSettingsService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private toastService: ToastService,
) {}

async ngAfterViewInit() {
Expand Down Expand Up @@ -150,11 +151,11 @@ export class BlockedDomainsComponent implements AfterViewInit, OnDestroy {
const validatedHost = Utils.getHostname(uri);

if (!validatedHost) {
this.platformUtilsService.showToast(
"error",
null,
this.i18nService.t("excludedDomainsInvalidDomain", uri),
);
this.toastService.showToast({
message: this.i18nService.t("excludedDomainsInvalidDomain", uri),
title: "",
variant: "error",
});

// Don't reset via `handleStateUpdate` to allow existing input value correction
this.isLoading = false;
Expand All @@ -176,21 +177,25 @@ export class BlockedDomainsComponent implements AfterViewInit, OnDestroy {
if (stateIsUnchanged) {
// Reset UI state directly
const constructedNeverDomainsState = this.storedBlockedDomains.reduce(
(neverDomains, uri) => ({ ...neverDomains, [uri]: null }),
(neverDomains: NeverDomains, uri: string) => ({ ...neverDomains, [uri]: null }),
{},
);
this.handleStateUpdate(constructedNeverDomainsState);
} else {
await this.domainSettingsService.setBlockedInteractionsUris(newBlockedDomainsSaveState);
}

this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("blockedDomainsSavedSuccess"),
);
this.toastService.showToast({
message: this.i18nService.t("blockedDomainsSavedSuccess"),
title: "",
variant: "success",
});
} catch {
this.platformUtilsService.showToast("error", null, this.i18nService.t("unexpectedError"));
this.toastService.showToast({
message: this.i18nService.t("unexpectedError"),
title: "",
variant: "error",
});

// Don't reset via `handleStateUpdate` to preserve input values
this.isLoading = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { CommonModule } from "@angular/common";
import {
QueryList,
Expand All @@ -17,7 +15,6 @@ import { JslibModule } from "@bitwarden/angular/jslib.module";
import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service";
import { NeverDomains } from "@bitwarden/common/models/domain/domain-service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import {
ButtonModule,
Expand All @@ -28,6 +25,7 @@ import {
LinkModule,
SectionComponent,
SectionHeaderComponent,
ToastService,
TypographyModule,
} from "@bitwarden/components";

Expand Down Expand Up @@ -62,7 +60,8 @@ import { PopupPageComponent } from "../../../platform/popup/layout/popup-page.co
],
})
export class ExcludedDomainsComponent implements AfterViewInit, OnDestroy {
@ViewChildren("uriInput") uriInputElements: QueryList<ElementRef<HTMLInputElement>>;
@ViewChildren("uriInput") uriInputElements: QueryList<ElementRef<HTMLInputElement>> =
new QueryList();

accountSwitcherEnabled = false;
dataIsPristine = true;
Expand All @@ -77,7 +76,7 @@ export class ExcludedDomainsComponent implements AfterViewInit, OnDestroy {
constructor(
private domainSettingsService: DomainSettingsService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private toastService: ToastService,
) {
this.accountSwitcherEnabled = enableAccountSwitching();
}
Expand Down Expand Up @@ -156,11 +155,11 @@ export class ExcludedDomainsComponent implements AfterViewInit, OnDestroy {
const validatedHost = Utils.getHostname(uri);

if (!validatedHost) {
this.platformUtilsService.showToast(
"error",
null,
this.i18nService.t("excludedDomainsInvalidDomain", uri),
);
this.toastService.showToast({
message: this.i18nService.t("excludedDomainsInvalidDomain", uri),
title: "",
variant: "error",
});

// Don't reset via `handleStateUpdate` to allow existing input value correction
this.isLoading = false;
Expand All @@ -182,21 +181,25 @@ export class ExcludedDomainsComponent implements AfterViewInit, OnDestroy {
if (stateIsUnchanged) {
// Reset UI state directly
const constructedNeverDomainsState = this.storedExcludedDomains.reduce(
(neverDomains, uri) => ({ ...neverDomains, [uri]: null }),
(neverDomains: NeverDomains, uri: string) => ({ ...neverDomains, [uri]: null }),
{},
);
this.handleStateUpdate(constructedNeverDomainsState);
} else {
await this.domainSettingsService.setNeverDomains(newExcludedDomainsSaveState);
}

this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("excludedDomainsSavedSuccess"),
);
this.toastService.showToast({
message: this.i18nService.t("excludedDomainsSavedSuccess"),
title: "",
variant: "success",
});
} catch {
this.platformUtilsService.showToast("error", null, this.i18nService.t("unexpectedError"));
this.toastService.showToast({
message: this.i18nService.t("unexpectedError"),
title: "",
variant: "error",
});

// Don't reset via `handleStateUpdate` to preserve input values
this.isLoading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class AutofillVaultListItemsComponent {
/**
* Indicators for the section.
*/
@Input() sectionIndicators: string[];
@Input() sectionIndicators: string[] = [];

/**
* Observable that determines whether the empty autofill tip should be shown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
// @ts-strict-ignore
import { ScrollingModule } from "@angular/cdk/scrolling";
import { CommonModule } from "@angular/common";
import { booleanAttribute, Component, EventEmitter, inject, Input, Output, OnInit } from "@angular/core";
import {
booleanAttribute,
Component,
EventEmitter,
inject,
Input,
Output,
OnInit,
} from "@angular/core";
import { Router } from "@angular/router";
import { map } from "rxjs";

Expand Down Expand Up @@ -93,7 +101,7 @@ export class VaultListItemsContainerComponent implements OnInit {
/**
* Indicators for the section.
*/
@Input() sectionIndicators: string[];
@Input() sectionIndicators: string[] = [];

/**
* Optional description for the vault list item section. Will be shown below the title even when
Expand Down
32 changes: 18 additions & 14 deletions apps/browser/src/vault/popup/components/vault/vault-v2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class VaultV2Component implements OnInit, OnDestroy {
protected loading$ = this.vaultPopupItemsService.loading$;
protected scriptInjectionIsBlocked = false;
protected showScriptInjectionIsBlockedBanner = false;
protected autofillTabHostname: string = null;
protected autofillTabHostname: string | null = null;
protected sectionIndicators: string[] = [];

protected newItemItemValues$: Observable<NewItemInitialValues> =
Expand Down Expand Up @@ -151,20 +151,24 @@ export class VaultV2Component implements OnInit, OnDestroy {
ngOnDestroy(): void {}

handleScriptInjectionIsBlockedBannerDismiss() {
firstValueFrom(this.domainSettingsService.blockedInteractionsUris$)
.then((blockedURIs) => {
this.showScriptInjectionIsBlockedBanner = false;
this.domainSettingsService
.setBlockedInteractionsUris({
if (!this.autofillTabHostname) {
return;
}

try {
void firstValueFrom(this.domainSettingsService.blockedInteractionsUris$).then(
(blockedURIs) => {
this.showScriptInjectionIsBlockedBanner = false;
void this.domainSettingsService.setBlockedInteractionsUris({
...blockedURIs,
[this.autofillTabHostname]: { bannerIsDismissed: true },
})
.catch(() => {
/* no-op */
[this.autofillTabHostname as string]: { bannerIsDismissed: true },
});
})
.catch(() => {
/* no-op */
});
},
);
} catch (e) {
throw new Error(
"There was a problem dismissing the blocked interaction URI notification banner",
);
}
}
}
43 changes: 23 additions & 20 deletions apps/cli/src/service-container/service-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,29 @@ export class ServiceContainer {

this.containerService = new ContainerService(this.keyService, this.encryptService);

this.domainSettingsService = new DefaultDomainSettingsService(this.stateProvider);
this.configApiService = new ConfigApiService(this.apiService, this.tokenService);

this.authService = new AuthService(
this.accountService,
this.messagingService,
this.keyService,
this.apiService,
this.stateService,
this.tokenService,
);

this.configService = new DefaultConfigService(
this.configApiService,
this.environmentService,
this.logService,
this.stateProvider,
this.authService,
);

this.domainSettingsService = new DefaultDomainSettingsService(
this.stateProvider,
this.configService,
);

this.fileUploadService = new FileUploadService(this.logService, this.apiService);

Expand Down Expand Up @@ -580,25 +602,6 @@ export class ServiceContainer {

this.taskSchedulerService = new DefaultTaskSchedulerService(this.logService);

this.authService = new AuthService(
this.accountService,
this.messagingService,
this.keyService,
this.apiService,
this.stateService,
this.tokenService,
);

this.configApiService = new ConfigApiService(this.apiService, this.tokenService);

this.configService = new DefaultConfigService(
this.configApiService,
this.environmentService,
this.logService,
this.stateProvider,
this.authService,
);

this.devicesApiService = new DevicesApiServiceImplementation(this.apiService);
this.deviceTrustService = new DeviceTrustService(
this.keyGenerationService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { MockProxy, mock } from "jest-mock-extended";
import { firstValueFrom, of } from "rxjs";

import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";

import { FakeStateProvider, FakeAccountService, mockAccountServiceWith } from "../../../spec";
import { Utils } from "../../platform/misc/utils";
import { UserId } from "../../types/guid";
Expand All @@ -8,8 +11,10 @@ import { DefaultDomainSettingsService, DomainSettingsService } from "./domain-se

describe("DefaultDomainSettingsService", () => {
let domainSettingsService: DomainSettingsService;
const configServiceMock = mock<ConfigService>();
const mockUserId = Utils.newGuid() as UserId;
const accountService: FakeAccountService = mockAccountServiceWith(mockUserId);
let mockConfigService: MockProxy<ConfigService>;
const fakeStateProvider: FakeStateProvider = new FakeStateProvider(accountService);

const mockEquivalentDomains = [
Expand All @@ -19,10 +24,12 @@ describe("DefaultDomainSettingsService", () => {
];

beforeEach(() => {
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider);
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider, mockConfigService);
jest.spyOn(configServiceMock, "getFeatureFlag$").mockReturnValue(of(false));

jest.spyOn(domainSettingsService, "getUrlEquivalentDomains");
domainSettingsService.equivalentDomains$ = of(mockEquivalentDomains);
domainSettingsService.blockedInteractionsUris$ = of(null);
});

describe("getUrlEquivalentDomains", () => {
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/autofill/services/domain-settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { combineLatest, map, Observable } from "rxjs";

import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";

import {
NeverDomains,
EquivalentDomains,
UriMatchStrategySetting,
UriMatchStrategy,
} from "../../models/domain/domain-service";
import { ConfigService } from "../../platform/abstractions/config/config.service";
import { Utils } from "../../platform/misc/utils";
import {
DOMAIN_SETTINGS_DISK,
Expand Down Expand Up @@ -99,7 +99,7 @@ export class DefaultDomainSettingsService implements DomainSettingsService {

this.blockedInteractionsUris$ = combineLatest([
this.blockedInteractionsUrisState.state$,
this.configService.getFeatureFlag$(FeatureFlag.BlockBrowserInjectionsByDomain),
this.configService?.getFeatureFlag$(FeatureFlag.BlockBrowserInjectionsByDomain),
]).pipe(
map(([blockedUris, blockBrowserInjectionsByDomainEnabled]) => {
if (!blockBrowserInjectionsByDomainEnabled) {
Expand Down

0 comments on commit 1dd180e

Please sign in to comment.