Skip to content

Commit

Permalink
refactor: Rename autofill options and variables to make them more gen…
Browse files Browse the repository at this point in the history
…eric

Previously, we added a CozyProfile option to the Bitwarden  autofill method to take into account the "Pro" or "Perso" profile when autofilling a phone for example. Variables were named in link with "ambiguous values".

But we will soon introduce the ability to autofill a paper from a list of paper which has no "Pro" or "Perso" profile and is not linked with "ambiguous values".

So here I renamed these options and variables to make them more generic.
  • Loading branch information
zatteo committed Sep 12, 2024
1 parent 85537e7 commit e800280
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
11 changes: 6 additions & 5 deletions apps/browser/src/autofill/background/overlay.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
AutofillOverlayPort,
MAX_SUB_FRAME_DEPTH,
} from "../enums/autofill-overlay.enum";
import { AutofillService } from "../services/abstractions/autofill.service";
import { AutofillService, CozyAutofillOptions } from "../services/abstractions/autofill.service";
import {
ambiguousContactFieldNames,
generateRandomChars,
Expand Down Expand Up @@ -869,7 +869,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
private async fillInlineMenuCipher(
{ inlineMenuCipherId }: OverlayPortMessage,
{ sender }: chrome.runtime.Port,
ambiguousValue?: AmbiguousContactFieldValue[0],
cozyAutofillOptions: CozyAutofillOptions = {},
fieldHtmlIDToFill?: string,
) {
const pageDetails = this.pageDetailsForTab[sender.tab.id];
Expand All @@ -888,7 +888,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
pageDetails: Array.from(pageDetails.values()),
fillNewPassword: true,
allowTotpAutofill: true,
...(ambiguousValue ? { cozyProfile: ambiguousValue } : {}),
cozyAutofillOptions,
...(fieldHtmlIDToFill ? { fillOnlyThisFieldHtmlID: fieldHtmlIDToFill } : {}),
});

Expand All @@ -909,7 +909,8 @@ export class OverlayBackground implements OverlayBackgroundInterface {
message: OverlayPortMessage,
port: chrome.runtime.Port,
) {
const { ambiguousValue, fieldHtmlIDToFill, inlineMenuCipherId } = message;
const { cozyAutofillOptions, fieldHtmlIDToFill, inlineMenuCipherId } = message;

const client = await this.cozyClientService.getClientInstance();
const cipher = this.inlineMenuCiphers.get(inlineMenuCipherId);

Expand All @@ -932,7 +933,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
return;
}

this.fillInlineMenuCipher(message, port, ambiguousValue, fieldHtmlIDToFill);
this.fillInlineMenuCipher(message, port, cozyAutofillOptions, fieldHtmlIDToFill);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
AvailablePapers,
} from "src/autofill/types";
import { COZY_ATTRIBUTES_MAPPING } from "../../../../../../../../libs/cozy/mapping";
import { CozyAutofillOptions } from "src/autofill/services/abstractions/autofill.service";
/* eslint-enable */
/* end Cozy imports */

Expand Down Expand Up @@ -450,6 +451,12 @@ export class AutofillInlineMenuList extends AutofillInlineMenuPageElement {
// @ts-ignore
const currentListItemValue = ambiguousValue[getAmbiguousValueKey(ambiguousKey)];

const cozyAutofillOptions = {
value: currentListItemValue,
label: ambiguousValue.label,
type: ambiguousValue.type,
};

const listItem = document.createElement("li");
listItem.setAttribute("role", "listitem");
listItem.classList.add("inline-menu-list-actions-item");
Expand All @@ -465,7 +472,7 @@ export class AutofillInlineMenuList extends AutofillInlineMenuPageElement {
EVENTS.CLICK,
this.handleFillCipherAmbiguousClickEvent(
inlineMenuCipherId,
ambiguousValue,
cozyAutofillOptions,
fieldHtmlIDToFill,
uniqueId(),
),
Expand Down Expand Up @@ -851,12 +858,13 @@ export class AutofillInlineMenuList extends AutofillInlineMenuPageElement {

/**
* @param inlineMenuCipherId
* @param ambiguousValue
* @param cozyAutofillOptions
* @param fieldHtmlIDToFill
* @param UID
*/
private handleFillCipherAmbiguousClickEvent = (
inlineMenuCipherId: string,
ambiguousValue: AmbiguousContactFieldValue[0],
cozyAutofillOptions: CozyAutofillOptions,
fieldHtmlIDToFill: string,
UID: string,
) => {
Expand All @@ -865,7 +873,7 @@ export class AutofillInlineMenuList extends AutofillInlineMenuPageElement {
this.postMessageToParent({
command: "fillAutofillInlineMenuCipherWithAmbiguousField",
inlineMenuCipherId,
ambiguousValue,
cozyAutofillOptions,
fieldHtmlIDToFill,
}),
`${UID}-fill-cipher-button-click-handler`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ export interface PageDetail {
}

// Cozy customization
export interface CozyProfile {
export interface CozyAutofillOptions {
type?: string;
label?: "work" | "home";
number?: string; // the phone value we want to enforce if it has no type and label
address?: string; // the email value we want to enforce if it has no type and label
formattedAddress?: string; // the address value we want to enforce if it has no type and label
value?: string;
}
// Cozy customization end

Expand All @@ -40,7 +38,7 @@ export interface AutoFillOptions {
allowUntrustedIframe?: boolean;
allowTotpAutofill?: boolean;
// Cozy customization
cozyProfile?: CozyProfile;
cozyAutofillOptions?: CozyAutofillOptions;
fillOnlyThisFieldHtmlID?: string;
// Cozy customization end
}
Expand All @@ -62,7 +60,7 @@ export interface GenerateFillScriptOptions {
tabUrl: string;
defaultUriMatch: UriMatchStrategySetting;
// Cozy customization
cozyProfile?: CozyProfile;
cozyAutofillOptions?: CozyAutofillOptions;
fillOnlyThisFieldHtmlID?: string;
// Cozy customization end
}
Expand Down
4 changes: 2 additions & 2 deletions apps/browser/src/autofill/services/autofill.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export default class AutofillService implements AutofillServiceInterface {
cipher: options.cipher,
tabUrl: tab.url,
defaultUriMatch: defaultUriMatch,
cozyProfile: options.cozyProfile, // Cozy customization
cozyAutofillOptions: options.cozyAutofillOptions, // Cozy customization
fillOnlyThisFieldHtmlID: options.fillOnlyThisFieldHtmlID, // Cozy customization
});

Expand Down Expand Up @@ -717,7 +717,7 @@ export default class AutofillService implements AutofillServiceInterface {
options.cipher.identity = await generateIdentityViewFromContactId(
client,
options.cipher.id,
options.cozyProfile,
options.cozyAutofillOptions,
);
} catch (e) {
// eslint-disable-next-line no-console
Expand Down
18 changes: 9 additions & 9 deletions libs/cozy/contact.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IdentityView } from "@bitwarden/common/vault/models/view/identity.view"
const { getInitials } = models.contact;

import { AutofillFieldQualifier } from "../../apps/browser/src/autofill/enums/autofill-field.enums";
import { CozyProfile } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";
import { CozyAutofillOptions } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";

import { buildFieldsFromContact } from "./fields.helper";
import { getCozyValue } from "./getCozyValue";
Expand Down Expand Up @@ -70,7 +70,7 @@ export const convertContactToCipherData = async (
export const generateIdentityViewFromContactId = async (
client: CozyClient,
contactId: string,
cozyProfile?: CozyProfile,
cozyAutofillOptions?: CozyAutofillOptions,
): Promise<IdentityView> => {
const identity = new IdentityView();

Expand Down Expand Up @@ -99,44 +99,44 @@ export const generateIdentityViewFromContactId = async (
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityPhone,
cozyProfile,
cozyAutofillOptions,
});
identity.email = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityEmail,
cozyProfile,
cozyAutofillOptions,
});

identity.address1 = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityAddress1,
cozyProfile,
cozyAutofillOptions,
});
identity.city = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityCity,
cozyProfile,
cozyAutofillOptions,
});
identity.state = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityState,
cozyProfile,
cozyAutofillOptions,
});
identity.postalCode = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityPostalCode,
cozyProfile,
cozyAutofillOptions,
});
identity.country = await getCozyValue({
client,
contactId,
fieldQualifier: AutofillFieldQualifier.identityCountry,
cozyProfile,
cozyAutofillOptions,
});

return identity;
Expand Down
16 changes: 8 additions & 8 deletions libs/cozy/getCozyValue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { CozyProfile } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";
import { CozyAutofillOptions } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";

import { selectDataWithCozyProfile } from "./getCozyValue";

// PROFILES

const EMPTY_PROFILE: CozyProfile = {};
const EMPTY_PROFILE: CozyAutofillOptions = {};

const HOME_ONLY_PROFILE: CozyProfile = {
const HOME_ONLY_PROFILE: CozyAutofillOptions = {
label: "home",
};

const HOME_AND_TYPE_PROFILE: CozyProfile = {
const HOME_AND_TYPE_PROFILE: CozyAutofillOptions = {
label: "home",
type: "iPhone",
};

const WORK_ONLY_PROFILE: CozyProfile = {
const WORK_ONLY_PROFILE: CozyAutofillOptions = {
label: "work",
};

const WORK_AND_TYPE_PROFILE: CozyProfile = {
const WORK_AND_TYPE_PROFILE: CozyAutofillOptions = {
label: "work",
type: "Cozy Cloud",
};

const EMPTY_PROFILE_WITH_PHONE: CozyProfile = {
number: "0",
const EMPTY_PROFILE_WITH_PHONE: CozyAutofillOptions = {
value: "0",
};

// ELEMENTS
Expand Down
32 changes: 18 additions & 14 deletions libs/cozy/getCozyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from "lodash";

import { AutofillFieldQualifierType } from "../../apps/browser/src/autofill/enums/autofill-field.enums";
import AutofillField from "../../apps/browser/src/autofill/models/autofill-field";
import { CozyProfile } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";
import { CozyAutofillOptions } from "../../apps/browser/src/autofill/services/abstractions/autofill.service";
import { PaperAutoFillConstants } from "../../apps/browser/src/autofill/services/autofill-constants";

import { COZY_ATTRIBUTES_MAPPING, CozyAttributesModel, FILTERS } from "./mapping";
Expand All @@ -16,7 +16,7 @@ interface GetCozyValueType {
me?: boolean;
field?: AutofillField;
fieldQualifier: AutofillFieldQualifierType;
cozyProfile?: CozyProfile;
cozyAutofillOptions?: CozyAutofillOptions;
filterName?: string;
}

Expand All @@ -27,7 +27,7 @@ export const getCozyValue = async ({
me,
field,
fieldQualifier,
cozyProfile,
cozyAutofillOptions,
filterName,
}: GetCozyValueType): Promise<string | undefined> => {
const cozyAttributeModel = COZY_ATTRIBUTES_MAPPING[fieldQualifier];
Expand All @@ -41,7 +41,7 @@ export const getCozyValue = async ({
client,
contactId,
cozyAttributeModel,
cozyProfile,
cozyAutofillOptions,
});
} else if (cozyAttributeModel.doctype === "io.cozy.files") {
return await getCozyValueInPaper({
Expand All @@ -50,7 +50,7 @@ export const getCozyValue = async ({
contactEmail,
me,
cozyAttributeModel,
cozyProfile,
cozyAutofillOptions,
field,
filterName,
});
Expand All @@ -63,7 +63,7 @@ interface GetCozyValueInDataType {
contactEmail?: string;
me?: boolean;
cozyAttributeModel: CozyAttributesModel;
cozyProfile?: CozyProfile;
cozyAutofillOptions?: CozyAutofillOptions;
field?: AutofillField;
filterName?: string;
}
Expand All @@ -72,7 +72,7 @@ const getCozyValueInContact = async ({
client,
contactId,
cozyAttributeModel,
cozyProfile,
cozyAutofillOptions,
}: GetCozyValueInDataType) => {
const { data: contact } = await client.query(Q("io.cozy.contacts").getById(contactId), {
executeFromStore: true,
Expand All @@ -81,7 +81,7 @@ const getCozyValueInContact = async ({
if (cozyAttributeModel.isPathArray) {
const dataArray = _.get(contact, cozyAttributeModel.path);

const selectedData = selectDataWithCozyProfile(dataArray, cozyProfile);
const selectedData = selectDataWithCozyProfile(dataArray, cozyAutofillOptions);
const selectedValue = cozyAttributeModel.pathAttributes
.map((pathAttribute) => _.get(selectedData, pathAttribute))
.join(" ");
Expand Down Expand Up @@ -151,7 +151,10 @@ export const getAllPapersFromContact = async ({
return papersFromContact;
};

export const selectDataWithCozyProfile = (data: any[] | undefined, cozyProfile?: CozyProfile) => {
export const selectDataWithCozyProfile = (
data: any[] | undefined,
cozyAutofillOptions?: CozyAutofillOptions,
) => {
if (!data || data.length === 0) {
return;
}
Expand All @@ -160,17 +163,18 @@ export const selectDataWithCozyProfile = (data: any[] | undefined, cozyProfile?:
return data[0];
}

const type = cozyProfile?.type;
const label = cozyProfile?.label;
const type = cozyAutofillOptions?.type;
const label = cozyAutofillOptions?.label;

// If we clicked on a phone number with no type and label,
// we want to autofill with this phone number and not evaluate the select data logic
// that will finish by return the first phone number and not the phone number we clicked
const matchingValueData = data.find(
(d) =>
(cozyProfile.number && cozyProfile.number === d.number) ||
(cozyProfile.formattedAddress && cozyProfile.formattedAddress === d.formattedAddress) ||
(cozyProfile.address && cozyProfile.address === d.address),
cozyAutofillOptions?.value &&
(cozyAutofillOptions.value === d.number ||
cozyAutofillOptions.value === d.formattedAddress ||
cozyAutofillOptions.value === d.address),
);

if (!type && !label && matchingValueData) {
Expand Down

0 comments on commit e800280

Please sign in to comment.