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

fix: Drop domain name in mentions if chat members are on the same domain #34013

Merged
merged 14 commits into from
Jan 25, 2024
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {cloneDeep} from 'lodash';
import lodashGet from 'lodash/get';
import React from 'react';
import {TNodeChildrenRenderer} from 'react-native-render-html';
Expand All @@ -16,6 +17,7 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import personalDetailsPropType from '@pages/personalDetailsPropType';
import CONST from '@src/CONST';
import * as LoginUtils from '@src/libs/LoginUtils';
import ROUTES from '@src/ROUTES';
import htmlRendererPropTypes from './htmlRendererPropTypes';

Expand All @@ -37,15 +39,28 @@ function MentionUserRenderer(props) {
let accountID;
let displayNameOrLogin;
let navigationRoute;
const tnode = cloneDeep(props.tnode);
const getMentionDisplayText = (displayText, accountId, userLogin = '') => {
if (accountId && userLogin !== displayText) {
tienifr marked this conversation as resolved.
Show resolved Hide resolved
return displayText;
}
if (!LoginUtils.areEmailsFromSamePrivateDomain(displayText, props.currentUserPersonalDetails.login)) {
return displayText;
}
tienifr marked this conversation as resolved.
Show resolved Hide resolved

return displayText.split('@')[0];
};

if (!_.isEmpty(htmlAttribAccountID)) {
const user = lodashGet(personalDetails, htmlAttribAccountID);
accountID = parseInt(htmlAttribAccountID, 10);
displayNameOrLogin = LocalePhoneNumber.formatPhoneNumber(lodashGet(user, 'login', '')) || lodashGet(user, 'displayName', '') || translate('common.hidden');
displayNameOrLogin = getMentionDisplayText(displayNameOrLogin, htmlAttribAccountID, lodashGet(user, 'login', ''));
navigationRoute = ROUTES.PROFILE.getRoute(htmlAttribAccountID);
} else if (!_.isEmpty(props.tnode.data)) {
} else if (!_.isEmpty(tnode.data)) {
// We need to remove the LTR unicode and leading @ from data as it is not part of the login
displayNameOrLogin = props.tnode.data.replace(CONST.UNICODE.LTR, '').slice(1);
Copy link
Contributor

@sobitneupane sobitneupane Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will probably cause issue. We are passing displayNameOrLogin to tooltip and and is also used in navigationRoute. As mentioned in the comment above, we need to remove the LTR unicode and leading @ from data as it is not part of the login.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I updated the PR

displayNameOrLogin = tnode.data.replace(CONST.UNICODE.LTR, '').slice(1);
tnode.data = tnode.data.replace(displayNameOrLogin, getMentionDisplayText(displayNameOrLogin, htmlAttribAccountID));
tienifr marked this conversation as resolved.
Show resolved Hide resolved

accountID = _.first(PersonalDetailsUtils.getAccountIDsByLogins([displayNameOrLogin]));
navigationRoute = ROUTES.DETAILS.getRoute(displayNameOrLogin);
Expand Down Expand Up @@ -83,7 +98,7 @@ function MentionUserRenderer(props) {
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
{!_.isEmpty(htmlAttribAccountID) ? `@${displayNameOrLogin}` : <TNodeChildrenRenderer tnode={props.tnode} />}
{!_.isEmpty(htmlAttribAccountID) ? `@${displayNameOrLogin}` : <TNodeChildrenRenderer tnode={tnode} />}
</Text>
</UserDetailsTooltip>
</Text>
Expand Down
14 changes: 13 additions & 1 deletion src/libs/LoginUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ function getPhoneLogin(partnerUserID: string): string {
return appendCountryCode(getPhoneNumberWithoutSpecialChars(partnerUserID));
}

export {getPhoneNumberWithoutSpecialChars, appendCountryCode, isEmailPublicDomain, validateNumber, getPhoneLogin};
/**
* Check whether 2 emails have the same private domain
*/
function areEmailsFromSamePrivateDomain(email1: string, email2: string): boolean {
if (isEmailPublicDomain(email1) || isEmailPublicDomain(email2)) {
return false;
}
const emailDomain1 = Str.extractEmailDomain(email1).toLowerCase();
const emailDomain2 = Str.extractEmailDomain(email2).toLowerCase();
return emailDomain1 === emailDomain2;
tienifr marked this conversation as resolved.
Show resolved Hide resolved
}

export {getPhoneNumberWithoutSpecialChars, appendCountryCode, isEmailPublicDomain, validateNumber, getPhoneLogin, areEmailsFromSamePrivateDomain};
Loading