From 9710b716dae9078aff6e36cb32624af6f7511dc2 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 16 Jan 2024 16:15:13 -1000 Subject: [PATCH 1/4] Update comment Add some methods to check if the report is valid Fix ts stuff --- src/libs/ReportUtils.ts | 25 +++++++++++++++++++++++++ src/libs/actions/PriorityMode.ts | 13 ++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e4b82aa36c7a..317ace1d7d4e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4317,6 +4317,29 @@ function isGroupChat(report: OnyxEntry): boolean { ); } +/** + * Assume any report without a reportID is unusable. + */ +function isValidReport(report?: OnyxEntry): boolean { + return Boolean(!report?.reportID); +} + +/** + * Check to see if we are a participant of this report. + */ +function isReportParticipant(report?: OnyxEntry, accountID: number): boolean { + if (!accountID) { + return false; + } + + // We are not the owner or the manager or a participant + if (report?.ownerAccountID !== accountID && report?.managerID !== accountID && !(report?.participantAccountIDs ?? []).includes(accountID)) { + return false; + } + + return true; +} + function shouldUseFullTitleToDisplay(report: OnyxEntry): boolean { return isMoneyRequestReport(report) || isPolicyExpenseChat(report) || isChatRoom(report) || isChatThread(report) || isTaskReport(report); } @@ -4613,6 +4636,8 @@ export { shouldDisplayThreadReplies, shouldDisableThread, getChildReportNotificationPreference, + isReportParticipant, + isValidReport, }; export type {ExpenseOriginalMessage, OptionData, OptimisticChatReport, OptimisticCreatedReportAction}; diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 1d38d09e08a1..1e86c8dd8f1a 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -6,6 +6,7 @@ import Log from '@libs/Log'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; +import * as ReportUtils from '@libs/ReportUtils'; /** * This actions file is used to automatically switch a user into #focus mode when they exceed a certain number of reports. We do this primarily for performance reasons. @@ -120,7 +121,17 @@ function tryFocusModeUpdate() { return; } - const reportCount = Object.keys(allReports ?? {}).length; + const validReports = []; + Object.keys(allReports ?? {}).forEach((key) => { + const report = allReports?.[key]; + if (ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { + return; + } + + validReports.push(report); + }); + + const reportCount = validReports.length; if (reportCount < CONST.REPORT.MAX_COUNT_BEFORE_FOCUS_UPDATE) { Log.info('Not switching user to optimized focus mode as they do not have enough reports', false, {reportCount}); return; From 3c242ba0986a9e6365b7ecb1ea688a8774a37e52 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 16 Jan 2024 16:39:06 -1000 Subject: [PATCH 2/4] Fix incorrect logic --- src/libs/ReportUtils.ts | 4 ++-- src/libs/actions/PriorityMode.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 317ace1d7d4e..659decf83bb4 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4321,13 +4321,13 @@ function isGroupChat(report: OnyxEntry): boolean { * Assume any report without a reportID is unusable. */ function isValidReport(report?: OnyxEntry): boolean { - return Boolean(!report?.reportID); + return Boolean(report?.reportID); } /** * Check to see if we are a participant of this report. */ -function isReportParticipant(report?: OnyxEntry, accountID: number): boolean { +function isReportParticipant(accountID: number, report?: OnyxEntry): boolean { if (!accountID) { return false; } diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 1e86c8dd8f1a..a36178c7c7c1 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -124,7 +124,7 @@ function tryFocusModeUpdate() { const validReports = []; Object.keys(allReports ?? {}).forEach((key) => { const report = allReports?.[key]; - if (ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { + if (!ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { return; } From fa4259948cbf008b7395b44fc2f954193fe032d3 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 16 Jan 2024 16:39:59 -1000 Subject: [PATCH 3/4] Run prettier --- src/libs/actions/PriorityMode.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index a36178c7c7c1..52ec43279e66 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -3,10 +3,10 @@ import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as CollectionUtils from '@libs/CollectionUtils'; import Log from '@libs/Log'; +import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; -import * as ReportUtils from '@libs/ReportUtils'; /** * This actions file is used to automatically switch a user into #focus mode when they exceed a certain number of reports. We do this primarily for performance reasons. @@ -121,7 +121,7 @@ function tryFocusModeUpdate() { return; } - const validReports = []; + const validReports = []; Object.keys(allReports ?? {}).forEach((key) => { const report = allReports?.[key]; if (!ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { From bcacc06e13d131c9e3657aff375e3dce7689b6d6 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Tue, 23 Jan 2024 17:08:24 -1000 Subject: [PATCH 4/4] Fix isReportParticipant() to handle DM case --- src/libs/ReportUtils.ts | 17 ++++++++++++----- src/libs/actions/PriorityMode.ts | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 65e8e572d97d..c1842ffa801d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4496,17 +4496,24 @@ function isValidReport(report?: OnyxEntry): boolean { /** * Check to see if we are a participant of this report. */ -function isReportParticipant(accountID: number, report?: OnyxEntry): boolean { +function isReportParticipant(accountID: number, report: OnyxEntry): boolean { if (!accountID) { return false; } - // We are not the owner or the manager or a participant - if (report?.ownerAccountID !== accountID && report?.managerID !== accountID && !(report?.participantAccountIDs ?? []).includes(accountID)) { - return false; + // If we have a DM AND the accountID we are checking is the current user THEN we won't find them as a participant and must assume they are a participant + if (isDM(report) && accountID === currentUserAccountID) { + return true; } - return true; + const possibleAccountIDs = report?.participantAccountIDs ?? []; + if (report?.ownerAccountID) { + possibleAccountIDs.push(report?.ownerAccountID); + } + if (report?.managerID) { + possibleAccountIDs.push(report?.managerID); + } + return possibleAccountIDs.includes(accountID); } function shouldUseFullTitleToDisplay(report: OnyxEntry): boolean { diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 52ec43279e66..7ae174ac8606 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -124,6 +124,10 @@ function tryFocusModeUpdate() { const validReports = []; Object.keys(allReports ?? {}).forEach((key) => { const report = allReports?.[key]; + if (!report) { + return; + } + if (!ReportUtils.isValidReport(report) || !ReportUtils.isReportParticipant(currentUserAccountID ?? 0, report)) { return; }