From 0a0bf76cae03738a0b2863ddfcedbbb0a605e54c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 13 Dec 2024 16:47:42 -1000 Subject: [PATCH] Update all comparisons and checks for `hidden` to use isHiddenParticipant() instead --- src/libs/OptionsListUtils.ts | 4 +-- src/libs/ReportUtils.ts | 33 ++++++++++++++----- src/libs/SidebarUtils.ts | 2 +- src/libs/UnreadIndicatorUpdater/index.ts | 2 +- src/libs/actions/Report.ts | 19 +++++++---- src/libs/actions/Task.ts | 2 +- src/libs/actions/User.ts | 5 ++- src/pages/ProfilePage.tsx | 2 +- src/pages/ReportDetailsPage.tsx | 2 +- src/pages/RoomInvitePage.tsx | 2 +- src/pages/home/ReportScreen.tsx | 9 +---- .../Report/NotificationPreferencePage.tsx | 6 ++-- .../settings/Report/ReportSettingsPage.tsx | 4 +-- 13 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index a7f738790f92..8c78057936c9 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -767,7 +767,7 @@ function getPolicyExpenseReportOption(participant: Participant | ReportUtils.Opt const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? ReportUtils.getReportOrDraftReport(participant.reportID) : null; const visibleParticipantAccountIDs = Object.entries(expenseReport?.participants ?? {}) - .filter(([, reportParticipant]) => reportParticipant && reportParticipant.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) + .filter(([, reportParticipant]) => reportParticipant && !ReportUtils.isHiddenParticipant(reportParticipant.notificationPreference)) .map(([accountID]) => Number(accountID)); const option = createOption( @@ -1787,7 +1787,7 @@ function getEmptyOptions(): Options { function shouldUseBoldText(report: ReportUtils.OptionData): boolean { const notificationPreference = report.notificationPreference ?? ReportUtils.getReportNotificationPreference(report); - return report.isUnread === true && notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE && notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + return report.isUnread === true && notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE && !ReportUtils.isHiddenParticipant(notificationPreference); } export { diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index eb728586f8f2..e623c06e998a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1294,12 +1294,9 @@ function getDefaultNotificationPreferenceForReport(report: OnyxEntry): V } /** - * Get the notification preference given a report + * Get the notification preference given a report. This should ALWAYS default to 'hidden'. Do not change this! */ -function getReportNotificationPreference(report: OnyxEntry, shouldDefaltToHidden = true): ValueOf { - if (!shouldDefaltToHidden) { - return report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? getDefaultNotificationPreferenceForReport(report); - } +function getReportNotificationPreference(report: OnyxEntry): ValueOf { return report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; } @@ -1415,6 +1412,23 @@ function canCreateTaskInReport(report: OnyxEntry): boolean { return true; } +/** + * For all intents and purposes a report that has no notificationPreference at all should be considered "hidden". + * We will remove the 'hidden' field entirely once the backend changes for https://github.com/Expensify/Expensify/issues/450891 are done. + */ +function isHiddenParticipant(notificationPreference: string | null | undefined): boolean; +function isHiddenParticipant(report: OnyxEntry): boolean; +function isHiddenParticipant(reportOrPreference: OnyxEntry | string | null | undefined): boolean { + if (typeof reportOrPreference === 'object' && reportOrPreference !== null) { + const notificationPreference = getReportNotificationPreference(reportOrPreference); + return isHiddenParticipant(notificationPreference); + } + if (reportOrPreference === undefined || reportOrPreference == null) { + return true; + } + return reportOrPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; +} + /** * Returns true if there are any guides accounts (team.expensify.com) in a list of accountIDs * by cross-referencing the accountIDs with personalDetails since guides that are participants @@ -1426,7 +1440,7 @@ function hasExpensifyGuidesEmails(accountIDs: number[]): boolean { function getMostRecentlyVisitedReport(reports: Array>, reportMetadata: OnyxCollection): OnyxEntry { const filteredReports = reports.filter((report) => { - const shouldKeep = !isChatThread(report) || getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldKeep = !isChatThread(report) || !isHiddenParticipant(report); return shouldKeep && !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime); }); return lodashMaxBy(filteredReports, (a) => new Date(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? a?.lastReadTime ?? '').valueOf()); @@ -2233,7 +2247,7 @@ function getParticipantsAccountIDsForDisplay(report: OnyxEntry, shouldEx return false; } - if (shouldExcludeHidden && reportParticipants[accountID]?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (shouldExcludeHidden && isHiddenParticipant(reportParticipants[accountID]?.notificationPreference)) { return false; } @@ -8119,7 +8133,7 @@ function canJoinChat(report: OnyxEntry, parentReportAction: OnyxInputOrE } // If the notification preference of the chat is not hidden that means we have already joined the chat - if (getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (!isHiddenParticipant(report)) { return false; } @@ -8153,7 +8167,7 @@ function canLeaveChat(report: OnyxEntry, policy: OnyxEntry): boo return false; } - if (getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (isHiddenParticipant(report)) { return false; } @@ -8835,6 +8849,7 @@ export { getAllReportActionsErrorsAndReportActionThatRequiresAttention, hasInvoiceReports, getReportMetadata, + isHiddenParticipant, }; export type { diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 01cbad41b128..1bbd27593f0a 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -115,7 +115,7 @@ function getOrderedReportIDs( } const parentReportAction = ReportActionsUtils.getReportAction(report?.parentReportID ?? '-1', report?.parentReportActionID ?? '-1'); const doesReportHaveViolations = ReportUtils.shouldDisplayViolationsRBRInLHN(report, transactionViolations); - const isHidden = ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const isHidden = ReportUtils.isHiddenParticipant(report); const isFocused = report.reportID === currentReportId; const hasErrorsOtherThanFailedReceipt = ReportUtils.hasReportErrorsOtherThanFailedReceipt(report, doesReportHaveViolations, transactionViolations); const isReportInAccessible = report?.errorFields?.notFound; diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index 4d38d410cbfd..e8dfb5d1c9b4 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -40,7 +40,7 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection, cur * Furthermore, muted reports may or may not appear in the LHN depending on priority mode, * but they should not be considered in the unread indicator count. */ - notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && + !ReportUtils.isHiddenParticipant(notificationPreference) && notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE ); }); diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 7baf66adc5c5..02cd36e75da7 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -545,9 +545,8 @@ function addActions(reportID: string, text = '', file?: FileObject) { }; const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const shouldUpdateNotificationPrefernece = !isEmptyObject(report) && ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; - - if (shouldUpdateNotificationPrefernece) { + const shouldUpdateNotificationPreference = !isEmptyObject(report) && ReportUtils.isHiddenParticipant(report); + if (shouldUpdateNotificationPreference) { optimisticReport.participants = { [currentUserAccountID]: {notificationPreference: ReportUtils.getDefaultNotificationPreferenceForReport(report)}, }; @@ -1932,7 +1931,7 @@ function toggleSubscribeToChildReport(childReportID = '-1', parentReportAction: if (childReportID !== '-1') { openReport(childReportID); const parentReportActionID = parentReportAction?.reportActionID ?? '-1'; - if (!prevNotificationPreference || prevNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) { + if (!prevNotificationPreference || ReportUtils.isHiddenParticipant(prevNotificationPreference)) { updateNotificationPreference(childReportID, prevNotificationPreference, CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, parentReportID, parentReportActionID); } else { updateNotificationPreference(childReportID, prevNotificationPreference, CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, parentReportID, parentReportActionID); @@ -1957,8 +1956,9 @@ function toggleSubscribeToChildReport(childReportID = '-1', parentReportAction: const participantLogins = PersonalDetailsUtils.getLoginsByAccountIDs(participantAccountIDs); openReport(newChat.reportID, '', participantLogins, newChat, parentReportAction.reportActionID); - const notificationPreference = - prevNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN ? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS : CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const notificationPreference = ReportUtils.isHiddenParticipant(prevNotificationPreference) + ? CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS + : CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; updateNotificationPreference(newChat.reportID, prevNotificationPreference, notificationPreference, parentReportID, parentReportAction?.reportActionID); } } @@ -3062,7 +3062,12 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, - value: {[report.parentReportActionID]: {childReportNotificationPreference: ReportUtils.getReportNotificationPreference(report, false)}}, + value: { + [report.parentReportActionID]: { + childReportNotificationPreference: + report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? ReportUtils.getDefaultNotificationPreferenceForReport(report), + }, + }, }); } diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index df8c0474fbaa..c08b03585a4c 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -252,7 +252,7 @@ function createTaskAndNavigate( }, ); - const shouldUpdateNotificationPreference = !isEmptyObject(parentReport) && ReportUtils.getReportNotificationPreference(parentReport) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldUpdateNotificationPreference = !isEmptyObject(parentReport) && ReportUtils.isHiddenParticipant(parentReport); if (shouldUpdateNotificationPreference) { optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, diff --git a/src/libs/actions/User.ts b/src/libs/actions/User.ts index 885969857d72..7c2d011561e1 100644 --- a/src/libs/actions/User.ts +++ b/src/libs/actions/User.ts @@ -32,6 +32,7 @@ import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as Pusher from '@libs/Pusher/pusher'; import PusherUtils from '@libs/PusherUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; +import * as ReportUtils from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import playSoundExcludingMobile from '@libs/Sound/playSoundExcludingMobile'; import Visibility from '@libs/Visibility'; @@ -795,9 +796,7 @@ const isChannelMuted = (reportId: string) => Onyx.disconnect(connection); const notificationPreference = report?.participants?.[currentUserAccountID]?.notificationPreference; - resolve( - !notificationPreference || notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE || notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - ); + resolve(!notificationPreference || notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE || ReportUtils.isHiddenParticipant(notificationPreference)); }, }); }); diff --git a/src/pages/ProfilePage.tsx b/src/pages/ProfilePage.tsx index f0308301e142..7b42aba8bc56 100755 --- a/src/pages/ProfilePage.tsx +++ b/src/pages/ProfilePage.tsx @@ -158,7 +158,7 @@ function ProfilePage({route}: ProfilePageProps) { const notificationPreferenceValue = ReportUtils.getReportNotificationPreference(report); - const shouldShowNotificationPreference = !isEmptyObject(report) && !isCurrentUser && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldShowNotificationPreference = !isEmptyObject(report) && !isCurrentUser && !ReportUtils.isHiddenParticipant(notificationPreferenceValue); const notificationPreference = shouldShowNotificationPreference ? translate(`notificationPreferencesPage.notificationPreferences.${notificationPreferenceValue}` as TranslationPaths) : ''; diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 9d2d7e4ada75..3bc744e837e6 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -288,7 +288,7 @@ function ReportDetailsPage({policies, report, route, reportMetadata}: ReportDeta roomDescription = translate('newRoomPage.roomName'); } - const shouldShowNotificationPref = !isMoneyRequestReport && ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldShowNotificationPref = !isMoneyRequestReport && !ReportUtils.isHiddenParticipant(report); const shouldShowWriteCapability = !isMoneyRequestReport; const shouldShowMenuItem = shouldShowNotificationPref || shouldShowWriteCapability || (!!report?.visibility && report.chatType !== CONST.REPORT.CHAT_TYPE.INVOICE); diff --git a/src/pages/RoomInvitePage.tsx b/src/pages/RoomInvitePage.tsx index 79d7cfe4acc5..d4978ef8e795 100644 --- a/src/pages/RoomInvitePage.tsx +++ b/src/pages/RoomInvitePage.tsx @@ -63,7 +63,7 @@ function RoomInvitePage({ // Any existing participants and Expensify emails should not be eligible for invitation const excludedUsers = useMemo(() => { const visibleParticipantAccountIDs = Object.entries(report.participants ?? {}) - .filter(([, participant]) => participant && participant.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) + .filter(([, participant]) => participant && !ReportUtils.isHiddenParticipant(participant.notificationPreference)) .map(([accountID]) => Number(accountID)); return [...PersonalDetailsUtils.getLoginsByAccountIDs(visibleParticipantAccountIDs), ...CONST.EXPENSIFY_EMAILS].map((participant) => PhoneNumber.addSMSDomainIfPhoneNumber(participant), diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 47315b604f0b..bc21f768d4f2 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -557,14 +557,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro // If a user has chosen to leave a thread, and then returns to it (e.g. with the back button), we need to call `openReport` again in order to allow the user to rejoin and to receive real-time updates useEffect(() => { - if ( - !shouldUseNarrowLayout || - !isFocused || - prevIsFocused || - !ReportUtils.isChatThread(report) || - ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN || - isSingleTransactionView - ) { + if (!shouldUseNarrowLayout || !isFocused || prevIsFocused || !ReportUtils.isChatThread(report) || !ReportUtils.isHiddenParticipant(report) || isSingleTransactionView) { return; } Report.openReport(reportID ?? ''); diff --git a/src/pages/settings/Report/NotificationPreferencePage.tsx b/src/pages/settings/Report/NotificationPreferencePage.tsx index 50d445015f45..b07458fdd69a 100644 --- a/src/pages/settings/Report/NotificationPreferencePage.tsx +++ b/src/pages/settings/Report/NotificationPreferencePage.tsx @@ -27,11 +27,9 @@ function NotificationPreferencePage({report}: NotificationPreferencePageProps) { const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); const currentNotificationPreference = ReportUtils.getReportNotificationPreference(report); const shouldDisableNotificationPreferences = - ReportUtils.isArchivedRoom(report, reportNameValuePairs) || - ReportUtils.isSelfDM(report) || - (!isMoneyRequestReport && currentNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN); + ReportUtils.isArchivedRoom(report, reportNameValuePairs) || ReportUtils.isSelfDM(report) || (!isMoneyRequestReport && ReportUtils.isHiddenParticipant(currentNotificationPreference)); const notificationPreferenceOptions = Object.values(CONST.REPORT.NOTIFICATION_PREFERENCE) - .filter((pref) => pref !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) + .filter((pref) => !ReportUtils.isHiddenParticipant(pref)) .map((preference) => ({ value: preference, text: translate(`notificationPreferencesPage.notificationPreferences.${preference}`), diff --git a/src/pages/settings/Report/ReportSettingsPage.tsx b/src/pages/settings/Report/ReportSettingsPage.tsx index e943bc7b84a1..a01f12cd44e6 100644 --- a/src/pages/settings/Report/ReportSettingsPage.tsx +++ b/src/pages/settings/Report/ReportSettingsPage.tsx @@ -36,7 +36,7 @@ function ReportSettingsPage({report, policies, route}: ReportSettingsPageProps) const shouldDisableSettings = isEmptyObject(report) || ReportUtils.isArchivedRoom(report, reportNameValuePairs) || ReportUtils.isSelfDM(report); const notificationPreferenceValue = ReportUtils.getReportNotificationPreference(report); const notificationPreference = - notificationPreferenceValue && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN + notificationPreferenceValue && !ReportUtils.isHiddenParticipant(notificationPreferenceValue) ? translate(`notificationPreferencesPage.notificationPreferences.${notificationPreferenceValue}`) : ''; const writeCapability = ReportUtils.isAdminRoom(report) ? CONST.REPORT.WRITE_CAPABILITIES.ADMINS : report?.writeCapability ?? CONST.REPORT.WRITE_CAPABILITIES.ALL; @@ -45,7 +45,7 @@ function ReportSettingsPage({report, policies, route}: ReportSettingsPageProps) const shouldAllowWriteCapabilityEditing = useMemo(() => ReportUtils.canEditWriteCapability(report, linkedWorkspace), [report, linkedWorkspace]); const shouldAllowChangeVisibility = useMemo(() => ReportUtils.canEditRoomVisibility(report, linkedWorkspace), [report, linkedWorkspace]); - const shouldShowNotificationPref = !isMoneyRequestReport && notificationPreferenceValue !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const shouldShowNotificationPref = !isMoneyRequestReport && !ReportUtils.isHiddenParticipant(notificationPreferenceValue); const shouldShowWriteCapability = !isMoneyRequestReport;