diff --git a/src/CONST.js b/src/CONST.js index 48206b8f9755..753e47739c1a 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -799,6 +799,7 @@ const CONST = { AFTER_FIRST_LINE_BREAK: /\n.*/g, CODE_2FA: /^\d{6}$/, ATTACHMENT_ID: /chat-attachments\/(\d+)/, + MERGED_ACCOUNT_PREFIX: /^(MERGED_\d+@)/, }, PRONOUNS: { diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index bb67b2fb39b6..f260bbc4c58d 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -7,7 +7,6 @@ import moment from 'moment'; import * as CollectionUtils from './CollectionUtils'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; -import * as ReportUtils from './ReportUtils'; const allReportActions = {}; Onyx.connect({ @@ -170,7 +169,9 @@ function getLastVisibleMessageText(reportID, actionsToMerge = {}) { const parser = new ExpensiMark(); const messageText = parser.htmlToText(htmlText); - return ReportUtils.formatReportLastMessageText(messageText); + return String(messageText) + .replace(CONST.REGEX.AFTER_FIRST_LINE_BREAK, '') + .substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH); } export { diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index c2b6d107c326..d6acdb0ffb5d 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -14,6 +14,7 @@ import Navigation from './Navigation/Navigation'; import ROUTES from '../ROUTES'; import * as NumberUtils from './NumberUtils'; import * as NumberFormatUtils from './NumberFormatUtils'; +import * as ReportActionsUtils from './ReportActionsUtils'; import Permissions from './Permissions'; import DateUtils from './DateUtils'; import linkingConfig from './Navigation/linkingConfig'; @@ -72,7 +73,7 @@ let doesDomainHaveApprovedAccountant; Onyx.connect({ key: ONYXKEYS.ACCOUNT, waitForCollectionCallback: true, - callback: val => doesDomainHaveApprovedAccountant = val.doesDomainHaveApprovedAccountant, + callback: val => doesDomainHaveApprovedAccountant = val.doesDomainHaveApprovedAccountant || false, }); function getChatType(report) { @@ -654,6 +655,39 @@ function getDisplayNamesWithTooltips(participants, isMultipleParticipantReport) }); } +/** + * Get the title for a policy expense chat which depends on the role of the policy member seeing this report + * + * @param {Object} report + * @param {Object} [policies] + * @returns {String} + */ +function getPolicyExpenseChatName(report, policies = {}) { + const reportOwnerDisplayName = getDisplayNameForParticipant(report.ownerEmail) || report.ownerEmail || report.reportName; + + // If the policy expense chat is owned by this user, use the name of the policy as the report name. + if (report.isOwnPolicyExpenseChat) { + return getPolicyName(report, policies); + } + + const policyExpenseChatRole = lodashGet(policies, [ + `${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'role', + ]) || 'user'; + + // If this user is not admin and this policy expense chat has been archived because of account merging, this must be an old workspace chat + // of the account which was merged into the current user's account. Use the name of the policy as the name of the report. + if (isArchivedRoom(report)) { + const lastAction = ReportActionsUtils.getLastVisibleAction(report.reportID); + const archiveReason = (lastAction && lastAction.originalMessage && lastAction.originalMessage.reason) || CONST.REPORT.ARCHIVE_REASON.DEFAULT; + if (archiveReason === CONST.REPORT.ARCHIVE_REASON.ACCOUNT_MERGED && policyExpenseChatRole !== CONST.POLICY.ROLE.ADMIN) { + return getPolicyName(report, policies); + } + } + + // If user can see this report and they are not its owner, they must be an admin and the report name should be the name of the policy member + return reportOwnerDisplayName; +} + /** * Get the title for a report. * @@ -668,8 +702,7 @@ function getReportName(report, policies = {}) { } if (isPolicyExpenseChat(report)) { - const reportOwnerDisplayName = getDisplayNameForParticipant(report.ownerEmail) || report.ownerEmail || report.reportName; - formattedName = report.isOwnPolicyExpenseChat ? getPolicyName(report, policies) : reportOwnerDisplayName; + formattedName = getPolicyExpenseChatName(report, policies); } if (isArchivedRoom(report)) { diff --git a/src/pages/home/report/ReportActionItemSingle.js b/src/pages/home/report/ReportActionItemSingle.js index 1df707541092..19d2cc05f58c 100644 --- a/src/pages/home/report/ReportActionItemSingle.js +++ b/src/pages/home/report/ReportActionItemSingle.js @@ -19,6 +19,7 @@ import Tooltip from '../../../components/Tooltip'; import ControlSelection from '../../../libs/ControlSelection'; import * as ReportUtils from '../../../libs/ReportUtils'; import OfflineWithFeedback from '../../../components/OfflineWithFeedback'; +import CONST from '../../../CONST'; const propTypes = { /** All the data of the action */ @@ -51,13 +52,14 @@ const showUserDetails = (email) => { }; const ReportActionItemSingle = (props) => { + const actorEmail = props.action.actorEmail.replace(CONST.REGEX.MERGED_ACCOUNT_PREFIX, ''); const { avatar, displayName, login, pendingFields, - } = props.personalDetails[props.action.actorEmail] || {}; - const avatarSource = ReportUtils.getAvatar(avatar, props.action.actorEmail); + } = props.personalDetails[actorEmail] || {}; + const avatarSource = ReportUtils.getAvatar(avatar, actorEmail); // Since the display name for a report action message is delivered with the report history as an array of fragments // we'll need to take the displayName from personal details and have it be in the same format for now. Eventually, @@ -72,9 +74,9 @@ const ReportActionItemSingle = (props) => { style={[styles.alignSelfStart, styles.mr3]} onPressIn={ControlSelection.block} onPressOut={ControlSelection.unblock} - onPress={() => showUserDetails(props.action.actorEmail)} + onPress={() => showUserDetails(actorEmail)} > - + @@ -92,13 +94,13 @@ const ReportActionItemSingle = (props) => { style={[styles.flexShrink1, styles.mr1]} onPressIn={ControlSelection.block} onPressOut={ControlSelection.unblock} - onPress={() => showUserDetails(props.action.actorEmail)} + onPress={() => showUserDetails(actorEmail)} > {_.map(personArray, (fragment, index) => (