From a5516426d0076bd948960e47398378fe8dce3409 Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Tue, 8 Jun 2021 17:49:41 -0700 Subject: [PATCH 1/5] Add User_IsFromPublicDomain api command --- src/libs/API.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libs/API.js b/src/libs/API.js index 5a3918173bbf..3761a642ccb7 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -658,6 +658,17 @@ function User_GetBetas() { return Network.post('User_GetBetas'); } +/** + * @param {Object} parameters + * @param {String} parameters.email + * @returns {Promise} + */ +function User_IsFromPublicDomain(parameters) { + const commandName = 'User_IsFromPublicDomain'; + requireParameters(['email'], parameters, commandName); + return Network.post(commandName, parameters); +} + /** * @param {Object} parameters * @param {String} parameters.email @@ -880,6 +891,7 @@ export { UpdateAccount, User_SignUp, User_GetBetas, + User_IsFromPublicDomain, User_ReopenAccount, User_SecondaryLogin_Send, User_UploadAvatar, From f5589e38f66515c0852a5029ddf3fc7674b16ab4 Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Tue, 8 Jun 2021 18:16:05 -0700 Subject: [PATCH 2/5] Create onyx action to fetch public domain info --- src/libs/actions/User.js | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index b7e7bdaf1b32..c6682bc5368f 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -1,6 +1,10 @@ import _ from 'underscore'; import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; +import { + Str, + PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS, +} from 'expensify-common/lib/CONST'; import ONYXKEYS from '../../ONYXKEYS'; import * as API from '../API'; import CONST from '../../CONST'; @@ -8,9 +12,13 @@ import Navigation from '../Navigation/Navigation'; import ROUTES from '../../ROUTES'; let sessionAuthToken = ''; +let sessionEmail = ''; Onyx.connect({ key: ONYXKEYS.SESSION, - callback: val => sessionAuthToken = val ? val.authToken : '', + callback: (val) => { + sessionAuthToken = lodashGet(val, 'authToken', ''); + sessionEmail = lodashGet(val, 'email', ''); + }, }); let currentlyViewedReportID = ''; @@ -168,6 +176,38 @@ function validateLogin(accountID, validateCode) { }); } +/** + * Fetch the public domain info for the current user. + * + * This API is a bit weird in that it sometimes depends on information being cached in bedrock. + * If the info for the domain is not in bedrock, then it creates an asynchronous bedrock job to gather domain info. + * If that happens, this function will automatically retry itself in 10 minutes. + */ +function getPublicDomainInfo() { + // If this command fails, we'll retry again in 10 minutes, + // arbitrarily chosen giving Bedrock time to resolve the ClearbitCheckPublicEmail job for this email. + const RETRY_TIMEOUT = 600000; + + // First check list of common public domains + if (_.contains(COMMON_PUBLIC_DOMAINS, sessionEmail)) { + Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain: true}); + return; + } + + // If it is not a common public domain, check the API + API.User_IsFromPublicDomain({email: sessionEmail}) + .then((response) => { + if (response.jsonCode === 200) { + const {isFromPublicDomain} = response; + Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain}); + } else { + // eslint-disable-next-line max-len + console.debug(`Command User_IsFromPublicDomain returned error code: ${response.jsonCode}. Most likely, this means that the domain ${Str.extractEmail(sessionEmail)} is not in the bedrock cache. Retrying in ${RETRY_TIMEOUT / 1000 / 60} minutes`); + setTimeout(getPublicDomainInfo, RETRY_TIMEOUT); + } + }); +} + export { changePassword, getBetas, @@ -176,4 +216,5 @@ export { setExpensifyNewsStatus, setSecondaryLogin, validateLogin, + getPublicDomainInfo, }; From 9b499fea3cddc171ce4d40f40dea2f36d5540ef1 Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Tue, 8 Jun 2021 18:17:45 -0700 Subject: [PATCH 3/5] Call user action in AuthScreens::componentDidMount --- src/libs/Navigation/AppNavigator/AuthScreens.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index a032193f97f8..5314b8b5dec7 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -120,6 +120,7 @@ class AuthScreens extends React.Component { PersonalDetails.fetchPersonalDetails(); User.getUserDetails(); User.getBetas(); + User.getPublicDomainInfo(); PersonalDetails.fetchCurrencyPreferences(); fetchAllReports(true, true, true); fetchCountryCodeByRequestIP(); From c23a17e24e8b917bc522074ef275edbee47fdf51 Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Wed, 9 Jun 2021 09:39:45 -0700 Subject: [PATCH 4/5] Add requireClearbitResponse parameter --- src/libs/API.js | 6 +++++- src/libs/actions/User.js | 6 ++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libs/API.js b/src/libs/API.js index 3761a642ccb7..c8b2b4b4144b 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -661,12 +661,16 @@ function User_GetBetas() { /** * @param {Object} parameters * @param {String} parameters.email + * @param {Boolean} [parameters.requireClearbitResponse] * @returns {Promise} */ function User_IsFromPublicDomain(parameters) { const commandName = 'User_IsFromPublicDomain'; requireParameters(['email'], parameters, commandName); - return Network.post(commandName, parameters); + return Network.post(commandName, { + ...{requireClearbitResponse: true}, + ...parameters, + }); } /** diff --git a/src/libs/actions/User.js b/src/libs/actions/User.js index c6682bc5368f..c43701d37c30 100644 --- a/src/libs/actions/User.js +++ b/src/libs/actions/User.js @@ -1,10 +1,8 @@ import _ from 'underscore'; import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; -import { - Str, - PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS, -} from 'expensify-common/lib/CONST'; +import Str from 'expensify-common/lib/str'; +import {PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS} from 'expensify-common/lib/CONST'; import ONYXKEYS from '../../ONYXKEYS'; import * as API from '../API'; import CONST from '../../CONST'; From 689cf93dc71cd8254eb4c791f138c4025721db91 Mon Sep 17 00:00:00 2001 From: Rory Abraham Date: Wed, 9 Jun 2021 10:24:13 -0700 Subject: [PATCH 5/5] Change requireClearbitResponse to requireCertainty --- src/libs/API.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/API.js b/src/libs/API.js index c8b2b4b4144b..07de975c2405 100644 --- a/src/libs/API.js +++ b/src/libs/API.js @@ -661,14 +661,14 @@ function User_GetBetas() { /** * @param {Object} parameters * @param {String} parameters.email - * @param {Boolean} [parameters.requireClearbitResponse] + * @param {Boolean} [parameters.requireCertainty] * @returns {Promise} */ function User_IsFromPublicDomain(parameters) { const commandName = 'User_IsFromPublicDomain'; requireParameters(['email'], parameters, commandName); return Network.post(commandName, { - ...{requireClearbitResponse: true}, + ...{requireCertainty: true}, ...parameters, }); }