Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display and autofill available papers #252

Merged
merged 8 commits into from
Sep 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ export type InlineMenuListPortMessageHandlers = {
message,
port,
}: PortOnMessageHandlerParams) => void;
fillAutofillInlineMenuCipherWithPaperField: ({
message,
port,
}: PortOnMessageHandlerParams) => void;
inlineMenuSearchContact: ({ message, port }: PortOnMessageHandlerParams) => void;
saveFieldToCozyDoctype: ({ message, port }: PortOnMessageHandlerParams) => void;
// Cozy customization end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,7 @@ describe("OverlayBackground", () => {
pageDetails: [pageDetailsForTab],
fillNewPassword: true,
allowTotpAutofill: true,
cozyAutofillOptions: {},
});
expect(overlayBackground["inlineMenuCiphers"].entries()).toStrictEqual(
new Map([
Expand Down
53 changes: 42 additions & 11 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 @@ -77,8 +77,9 @@ import { nameToColor } from "cozy-ui/transpiled/react/Avatar/helpers";
import { CozyClientService } from "../../popup/services/cozyClient.service";
import { AmbiguousContactFieldName, AmbiguousContactFieldValue } from "src/autofill/types";
import { COZY_ATTRIBUTES_MAPPING } from "../../../../../libs/cozy/mapping";
import { getCozyValue } from "../../../../../libs/cozy/getCozyValue";
import { createOrUpdateCozyDoctype } from "../../../../../libs/cozy/createOrUpdateCozyDoctype";
import { getCozyValue, getAllPapersFromContact } from "../../../../../libs/cozy/getCozyValue";
import _ from "lodash";
/* eslint-enable */
/* end Cozy imports */

Expand Down Expand Up @@ -170,6 +171,8 @@ export class OverlayBackground implements OverlayBackgroundInterface {
saveFieldToCozyDoctype: ({ message }) => this.saveFieldToCozyDoctype(message),
fillAutofillInlineMenuCipherWithAmbiguousField: ({ message, port }) =>
this.fillAutofillInlineMenuCipherWithAmbiguousField(message, port),
fillAutofillInlineMenuCipherWithPaperField: ({ message, port }) =>
this.fillAutofillInlineMenuCipherWithPaperField(message, port),
inlineMenuSearchContact: ({ message }) => this.searchContacts(message),
redirectToCozy: ({ message }) => this.redirectToCozy(message),
// Cozy customization end
Expand Down Expand Up @@ -834,7 +837,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 @@ -853,7 +856,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
pageDetails: Array.from(pageDetails.values()),
fillNewPassword: true,
allowTotpAutofill: true,
...(ambiguousValue ? { cozyProfile: ambiguousValue } : {}),
cozyAutofillOptions,
...(fieldHtmlIDToFill ? { fillOnlyThisFieldHtmlID: fieldHtmlIDToFill } : {}),
});

Expand All @@ -874,7 +877,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 @@ -897,7 +901,16 @@ export class OverlayBackground implements OverlayBackgroundInterface {
return;
}

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

private async fillAutofillInlineMenuCipherWithPaperField(
message: OverlayPortMessage,
port: chrome.runtime.Port,
) {
const { cozyAutofillOptions } = message;

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

/**
Expand All @@ -913,11 +926,12 @@ export class OverlayBackground implements OverlayBackgroundInterface {
executeFromStore: true,
})) as { data: IOCozyContact };

const focusedFieldModel = COZY_ATTRIBUTES_MAPPING[this.focusedFieldData?.fieldQualifier];

const ambiguousContactFields = getAmbiguousFieldsContact(ambiguousContactFieldNames, contact);

const isFocusedFieldAmbigous = ambiguousContactFieldNames.includes(
COZY_ATTRIBUTES_MAPPING[this.focusedFieldData?.fieldQualifier]
.name as AmbiguousContactFieldName,
focusedFieldModel.name as AmbiguousContactFieldName,
);
const hasMultipleAmbiguousValueInSameField = Object.values(ambiguousContactFields).some(
(item) => item.length > 1,
Expand All @@ -926,8 +940,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
// On an ambiguous form field, the associated contact values are kept.
const ambiguousFormFieldsOfFocusedField = Object.fromEntries(
Object.entries(ambiguousContactFields).filter(
([fieldName]) =>
fieldName === COZY_ATTRIBUTES_MAPPING[this.focusedFieldData?.fieldQualifier].name,
([fieldName]) => fieldName === focusedFieldModel.name,
),
);
// On an unambiguous form field, we keep only the multiple values of an ambiguous contact field.
Expand All @@ -938,13 +951,31 @@ export class OverlayBackground implements OverlayBackgroundInterface {
);

/*
On a form field with data coming from a paper :
- Display a menu to select or create a paper
On a form field other than ambiguous(phone/address/email):
- If the contact has one or less ambiguous value: autofill everything.
- If the contact has more than one ambiguous values: display a menu to choose which one.
On the ambiguous(phone/address/email) form field:
- Display a menu to select value.
*/
if (
if (focusedFieldModel.doctype === "io.cozy.files") {
const availablePapers = (
await getAllPapersFromContact({
client,
contactId: contact.id,
contactEmail: cipher.contact.primaryEmail,
me: cipher.contact.me,
cozyAttributeModel: focusedFieldModel,
})
).map((paper: any) => ({ name: paper.name, value: _.get(paper, focusedFieldModel.path) }));
this.inlineMenuListPort?.postMessage({
command: "paperList",
inlineMenuCipherId,
contactName: contact.displayName,
availablePapers,
});
} else if (
(!isFocusedFieldAmbigous && hasMultipleAmbiguousValueInSameField) ||
isFocusedFieldAmbigous
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InlineMenuCipherData } from "../../../background/abstractions/overlay.b

/* start Cozy imports */
/* eslint-disable */
import { AmbiguousContactFields } from "src/autofill/types";
import { AmbiguousContactFields, AvailablePapers } from "src/autofill/types";
import { AutofillFieldQualifierType } from "src/autofill/enums/autofill-field.enums";
import { IOCozyContact } from "cozy-client/types/types";
/* eslint-enable */
Expand All @@ -27,6 +27,12 @@ export type UpdateAutofillInlineMenuListAmbiguousMessage = AutofillInlineMenuLis
isFocusedFieldAmbigous: boolean;
fieldHtmlIDToFill: string;
};

export type UpdateAutofillInlineMenuListPaperMessage = AutofillInlineMenuListMessage & {
inlineMenuCipherId: string;
contactName: string;
availablePapers: AvailablePapers[];
};
// Cozy customization end

export type InitAutofillInlineMenuListMessage = AutofillInlineMenuListMessage & {
Expand Down Expand Up @@ -61,6 +67,7 @@ export type AutofillInlineMenuListWindowMessageHandlers = {
}: {
message: UpdateAutofillInlineMenuListAmbiguousMessage;
}) => void;
paperList: ({ message }: { message: UpdateAutofillInlineMenuListPaperMessage }) => void;
createEmptyNameList: ({
message,
}: {
Expand Down
Loading
Loading