Skip to content

Commit

Permalink
Merge pull request Expensify#12701 from tienifr/fix/11810-old-dot-lin…
Browse files Browse the repository at this point in the history
…k-without-w3-not-auto-signin

Fix/11810 old dot link without w3 not auto signin
  • Loading branch information
MonilBhavsar authored Dec 15, 2022
2 parents 21d9ad7 + be66a92 commit 1dd41bb
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/CONFIG.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const secureExpensifyUrl = Url.addTrailingForwardSlash(lodashGet(
const useNgrok = lodashGet(Config, 'USE_NGROK', 'false') === 'true';
const useWebProxy = lodashGet(Config, 'USE_WEB_PROXY', 'true') === 'true';
const expensifyComWithProxy = getPlatform() === 'web' && useWebProxy ? '/' : expensifyURL;
const conciergeUrl = `${expensifyURL}concierge/`;

// Throw errors on dev if config variables are not set correctly
if (ENVIRONMENT === CONST.ENVIRONMENT.DEV) {
Expand Down Expand Up @@ -57,7 +56,8 @@ export default {
PARTNER_NAME: lodashGet(Config, 'EXPENSIFY_PARTNER_NAME', 'chat-expensify-com'),
PARTNER_PASSWORD: lodashGet(Config, 'EXPENSIFY_PARTNER_PASSWORD', 'e21965746fd75f82bb66'),
EXPENSIFY_CASH_REFERER: 'ecash',
CONCIERGE_URL: conciergeUrl,
CONCIERGE_URL_PATHNAME: 'concierge/',
CONCIERGE_URL: `${expensifyURL}concierge/`,
},
IS_IN_PRODUCTION: Platform.OS === 'web' ? process.env.NODE_ENV === 'production' : !__DEV__,
IS_IN_STAGING: ENVIRONMENT === CONST.ENVIRONMENT.STAGING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import styles from '../../../styles/styles';
import Navigation from '../../../libs/Navigation/Navigation';
import AnchorForCommentsOnly from '../../AnchorForCommentsOnly';
import AnchorForAttachmentsOnly from '../../AnchorForAttachmentsOnly';
import * as Url from '../../../libs/Url';
import ROUTES from '../../../ROUTES';

const AnchorRenderer = (props) => {
Expand All @@ -24,19 +25,18 @@ const AnchorRenderer = (props) => {
const displayName = lodashGet(props.tnode, 'domNode.children[0].data', '');
const parentStyle = lodashGet(props.tnode, 'parent.styles.nativeTextRet', {});
const attrHref = htmlAttribs.href || '';
const internalNewExpensifyPath = (attrHref.startsWith(CONST.NEW_EXPENSIFY_URL) && attrHref.replace(CONST.NEW_EXPENSIFY_URL, ''))
|| (attrHref.startsWith(CONST.STAGING_NEW_EXPENSIFY_URL) && attrHref.replace(CONST.STAGING_NEW_EXPENSIFY_URL, ''));
const internalExpensifyPath = attrHref.startsWith(CONFIG.EXPENSIFY.EXPENSIFY_URL)
&& !attrHref.startsWith(CONFIG.EXPENSIFY.CONCIERGE_URL)
&& attrHref.replace(CONFIG.EXPENSIFY.EXPENSIFY_URL, '');

const attrPath = lodashGet(Url.getURLObject(attrHref), 'path', '').replace('/', '');
const internalNewExpensifyPath = (Url.hasSameExpensifyOrigin(attrHref, CONST.NEW_EXPENSIFY_URL) || Url.hasSameExpensifyOrigin(attrHref, CONST.STAGING_NEW_EXPENSIFY_URL)) && attrPath;
const internalExpensifyPath = Url.hasSameExpensifyOrigin(attrHref, CONFIG.EXPENSIFY.EXPENSIFY_URL)
&& !attrPath.startsWith(CONFIG.EXPENSIFY.CONCIERGE_URL_PATHNAME)
&& attrPath;
const navigateToLink = () => {
// There can be messages from Concierge with links to specific NewDot reports. Those URLs look like this:
// https://www.expensify.com.dev/newdotreport?reportID=3429600449838908 and they have a target="_blank" attribute. This is so that when a user is on OldDot,
// clicking on the link will open the chat in NewDot. However, when a user is in NewDot and clicks on the concierge link, the link needs to be handled differently.
// Normally, the link would be sent to Link.openOldDotLink() and opened in a new tab, and that's jarring to the user. Since the intention is to link to a specific NewDot chat,
// the reportID is extracted from the URL and then opened as an internal link, taking the user straight to the chat in the same tab.
if (attrHref.startsWith(CONFIG.EXPENSIFY.EXPENSIFY_URL) && attrHref.indexOf('newdotreport?reportID=') > -1) {
if (Url.hasSameExpensifyOrigin(attrHref, CONFIG.EXPENSIFY.EXPENSIFY_URL) && attrHref.indexOf('newdotreport?reportID=') > -1) {
const reportID = attrHref.split('newdotreport?reportID=').pop();
const reportRoute = ROUTES.getReportRoute(reportID);
Navigation.navigate(reportRoute);
Expand Down
64 changes: 64 additions & 0 deletions src/libs/Url.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {URL_WEBSITE_REGEX} from 'expensify-common/lib/Url';

/**
* Add / to the end of any URL if not present
* @param {String} url
Expand All @@ -10,7 +12,69 @@ function addTrailingForwardSlash(url) {
return url;
}

/**
* Parse href to URL object
* @param {String} href
* @returns {Object}
*/
function getURLObject(href) {
const urlRegex = new RegExp(URL_WEBSITE_REGEX, 'gi');
const match = urlRegex.exec(href);
if (!match) {
return {
href: undefined,
protocol: undefined,
hostname: undefined,
path: undefined,
};
}
const baseUrl = match[0];
const protocol = match[1];
return {
href,
protocol,
hostname: baseUrl.replace(protocol, ''),
path: href.startsWith(baseUrl) ? href.replace(baseUrl, '') : '',
};
}

/**
* Determine if we should remove w3 from hostname
* E.g www.expensify.com should be the same as expensify.com
* @param {String} hostname
* @returns {Boolean}
*/
function shouldRemoveW3FromExpensifyUrl(hostname) {
// Since expensify.com.dev is accessible with and without www subdomain
if (hostname === 'www.expensify.com.dev') {
return true;
}
const parts = hostname.split('.').reverse();
const subDomain = parts[2];
return subDomain === 'www';
}

/**
* Determine if two urls have the same origin
* Just care about expensify url to avoid the second-level domain (www.example.co.uk)
* @param {String} url1
* @param {String} url2
* @returns {Boolean}
*/
function hasSameExpensifyOrigin(url1, url2) {
const host1 = getURLObject(url1).hostname;
const host2 = getURLObject(url2).hostname;
if (!host1 || !host2) {
return false;
}
const host1WithoutW3 = shouldRemoveW3FromExpensifyUrl(host1) ? host1.replace('www.', '') : host1;
const host2WithoutW3 = shouldRemoveW3FromExpensifyUrl(host2) ? host2.replace('www.', '') : host2;
return host1WithoutW3 === host2WithoutW3;
}

export {
// eslint-disable-next-line import/prefer-default-export
addTrailingForwardSlash,
hasSameExpensifyOrigin,
getURLObject,
};
Loading

0 comments on commit 1dd41bb

Please sign in to comment.