Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / helpers / address.ts
blob9fc0dfbfb7d03e5643b222ca9733ebf1103af390
1 import unary from '@proton/utils/unary';
3 import { ADDRESS_FLAGS, ADDRESS_RECEIVE, ADDRESS_SEND, ADDRESS_STATUS, ADDRESS_TYPE, MEMBER_TYPE } from '../constants';
4 import type { Address, Domain, Member, Recipient, UserModel } from '../interfaces';
5 import { AddressConfirmationState } from '../interfaces';
6 import type { ContactEmail } from '../interfaces/contacts';
7 import { getIsDomainActive } from '../organization/helper';
8 import { hasBit } from './bitset';
9 import { canonicalizeInternalEmail } from './email';
11 export const getIsAddressEnabled = (address: Address) => {
12     return address.Status === ADDRESS_STATUS.STATUS_ENABLED;
15 export const getIsAddressConfirmed = (address: Address) => {
16     return address.ConfirmationState === AddressConfirmationState.CONFIRMATION_CONFIRMED;
19 export const getIsAddressDisabled = (address: Address) => {
20     return address.Status === ADDRESS_STATUS.STATUS_DISABLED;
23 export const getIsAddressActive = (address: Address) => {
24     return (
25         address.Status === ADDRESS_STATUS.STATUS_ENABLED &&
26         address.Receive === ADDRESS_RECEIVE.RECEIVE_YES &&
27         address.Send === ADDRESS_SEND.SEND_YES
28     );
31 export const getActiveAddresses = (addresses: Address[]): Address[] => {
32     return addresses.filter(unary(getIsAddressActive));
35 export const hasAddresses = (addresses: Address[] | undefined): boolean => {
36     return Array.isArray(addresses) && addresses.length > 0;
39 export const getIsAddressExternal = ({ Type }: Address) => {
40     return Type === ADDRESS_TYPE.TYPE_EXTERNAL;
43 export const getHasOnlyExternalAddresses = (addresses: Address[]) => {
44     return addresses.length >= 1 && addresses.every((address) => getIsAddressExternal(address));
47 export const contactToRecipient = (contact: Partial<ContactEmail> = {}, groupPath?: string): Partial<Recipient> => ({
48     Name: contact.Name,
49     Address: contact.Email,
50     ContactID: contact.ContactID,
51     Group: groupPath,
52 });
54 export const findUserAddress = (userEmail?: string, addresses: Address[] = []) => {
55     if (!userEmail) {
56         return undefined;
57     }
58     const canonicalUserEmail = canonicalizeInternalEmail(userEmail);
59     return addresses.find(({ Email }) => canonicalizeInternalEmail(Email) === canonicalUserEmail);
62 export const getSelfSendAddresses = (ownAddresses: Address[]) => {
63     // For custom domains, Proton Mail allows to have multiple sub-users with the same email address
64     // as long as only one of them is enabled. This poses problems when a sub-user
65     // with a disabled address wants to send email to the same address enabled in another sub-user.
66     // Because of this case, it's better to consider disabled addresses as non self
67     return ownAddresses.filter(({ Receive }) => !!Receive);
70 export const getPrimaryAddress = (addresses: Address[]) => {
71     const [address] = getActiveAddresses(addresses);
73     if (!address) {
74         return undefined;
75     }
77     return address;
80 export const getAvailableAddressDomains = ({
81     member,
82     user,
83     premiumDomains,
84     protonDomains,
85     customDomains,
86 }: {
87     user: UserModel;
88     member: Member;
89     premiumDomains: string[];
90     protonDomains: string[];
91     customDomains: Domain[] | undefined;
92 }) => {
93     const hasProtonDomains = member.Type === MEMBER_TYPE.PROTON;
95     return [
96         ...(hasProtonDomains ? protonDomains : []),
97         ...(Array.isArray(customDomains)
98             ? customDomains.filter(getIsDomainActive).map(({ DomainName }) => DomainName)
99             : []),
100         ...(hasProtonDomains && user.hasPaidMail && Array.isArray(premiumDomains) ? premiumDomains : []),
101     ];
104 const { FLAG_DISABLE_E2EE, FLAG_DISABLE_EXPECTED_SIGNED } = ADDRESS_FLAGS;
105 export const encryptionDisabled = (address: Address) => hasBit(address.Flags, FLAG_DISABLE_E2EE);
106 export const expectSignatureDisabled = (address: Address) => hasBit(address.Flags, FLAG_DISABLE_EXPECTED_SIGNED);