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 user are able to flag their own comment #21771

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Onyx.connect({
},
});

let loginList;
Onyx.connect({
key: ONYXKEYS.LOGIN_LIST,
callback: (val) => (loginList = _.isEmpty(val) ? [] : _.keys(val)),
});

let preferredLocale = CONST.LOCALES.DEFAULT;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
Expand Down Expand Up @@ -220,7 +226,7 @@ function canEditReportAction(reportAction) {
*/
function canFlagReportAction(reportAction) {
return (
reportAction.actorEmail !== sessionEmail &&
!loginList.includes(reportAction.actorEmail) &&
reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT &&
!ReportActionsUtils.isDeletedAction(reportAction) &&
!ReportActionsUtils.isCreatedTaskReportAction(reportAction)
Expand Down Expand Up @@ -2029,6 +2035,24 @@ function chatIncludesChronos(report) {
return report.participantAccountIDs && _.contains(report.participantAccountIDs, CONST.ACCOUNT_ID.CHRONOS);
}

/**
* Whether flag comment page should show
*
* @param {Object} reportAction
* @param {Object} report
* @returns {Boolean}
*/

function shouldShowFlagComment(reportAction, report) {
return (
canFlagReportAction(reportAction) &&
!isArchivedRoom(report) &&
!chatIncludesChronos(report) &&
!isConciergeChatReport(report.reportID) &&
reportAction.actorEmail !== CONST.EMAIL.CONCIERGE
);
}

/**
* @param {Object} report
* @param {String} report.lastReadTime
Expand Down Expand Up @@ -2275,6 +2299,7 @@ export {
findLastAccessedReport,
canEditReportAction,
canFlagReportAction,
shouldShowFlagComment,
canDeleteReportAction,
canLeaveRoom,
sortReportsByLastRead,
Expand Down
24 changes: 21 additions & 3 deletions src/pages/FlagCommentPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import CONST from '../CONST';
import * as ReportUtils from '../libs/ReportUtils';
import * as ReportActionsUtils from '../libs/ReportActionsUtils';
import * as Session from '../libs/actions/Session';
import FullPageNotFoundView from '../components/BlockingViews/FullPageNotFoundView';
import FullscreenLoadingIndicator from '../components/FullscreenLoadingIndicator';

const propTypes = {
/** Array of report actions for this report */
Expand All @@ -39,12 +41,16 @@ const propTypes = {
}),
}).isRequired,

/** Indicates whether the report data is loading */
isLoadingReportData: PropTypes.bool,

...withLocalizePropTypes,
};

const defaultProps = {
reportActions: {},
report: {},
isLoadingReportData: true,
};

/**
Expand All @@ -61,6 +67,11 @@ function getReportID(route) {

function FlagCommentPage(props) {
let reportAction = props.reportActions[`${props.route.params.reportActionID.toString()}`];

// Handle threads if needed
if (reportAction === undefined || reportAction.reportActionID === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make any difference if we move this if block to shouldShowFlagComment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we should get the correct reportAction here to pass it into Report.flagComment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay

reportAction = ReportActionsUtils.getParentReportAction(props.report);
}
const severities = [
{
severity: CONST.MODERATION.FLAG_SEVERITY_SPAM,
Expand Down Expand Up @@ -118,7 +129,6 @@ function FlagCommentPage(props) {
// Handle threads if needed
if (reportAction === undefined || reportAction.reportActionID === undefined) {
reportID = ReportUtils.getParentReport(props.report).reportID;
reportAction = ReportActionsUtils.getParentReportAction(props.report);
}
Report.flagComment(reportID, reportAction, severity);
Navigation.dismissModal();
Expand All @@ -138,10 +148,15 @@ function FlagCommentPage(props) {
/>
));

const shouldShowLoading = props.isLoadingReportData || props.report.isLoadingReportActions;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am a bit concerned that we're using props.report directly without confirming if it is undefined? I think it makes sense to add. the following:

if (isEmpty(props.report)) {
 Navigation.dismissModal();
return;
}


Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. We defined defaultProps for report is an empty object and that work as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was concerned about non-existent report, but I tested it shows up the invalid access page. Thanks for the patience here.

if (shouldShowLoading) {
return <FullscreenLoadingIndicator />;
}
Comment on lines +152 to +154
Copy link
Contributor

Choose a reason for hiding this comment

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

@dukenv0307 Why this code is needed? I couldn’t find any reference and it seems to be causing a regression in #23125

Copy link
Contributor Author

@dukenv0307 dukenv0307 Jul 27, 2023

Choose a reason for hiding this comment

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

@fedirjh We need it to wait the report data and report action data are loaded completely before we should not found page or flag page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we remove this code, when we open the page by deep link or reload the page we will see page not found will display briefly before flag page is displayed.

Copy link
Contributor

Choose a reason for hiding this comment

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

We need it to wait the report data and report action data are loaded completely before we should not found page or flag page.

This approach will not work in offline mode. Instead, we should check if report is empty.

Copy link
Contributor Author

@dukenv0307 dukenv0307 Jul 27, 2023

Choose a reason for hiding this comment

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

I think updating the check to this will be fine

const shouldShowLoading = (props.isLoadingReportData && _.isEmpty(props.report) || (_.isEmpty(props.reportActions) && props.report.isLoadingReportActions) 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach will not work in offline mode. Instead, we should check if report is empty.

This will not work in offline mode when we do something to make the openReport API is called

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's continue the discussion on the linked issue #23125


return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
{({safeAreaPaddingBottomStyle}) => (
<>
<FullPageNotFoundView shouldShow={!shouldShowLoading && !ReportUtils.shouldShowFlagComment(reportAction, props.report)}>
<HeaderWithBackButton title={props.translate('reportActionContextMenu.flagAsOffensive')} />
<ScrollView
contentContainerStyle={safeAreaPaddingBottomStyle}
Expand All @@ -155,7 +170,7 @@ function FlagCommentPage(props) {
<Text style={[styles.ph5, styles.textLabelSupporting, styles.mb1]}>{props.translate('moderation.chooseAReason')}</Text>
{severityMenuItems}
</ScrollView>
</>
</FullPageNotFoundView>
)}
</ScreenWrapper>
);
Expand All @@ -175,5 +190,8 @@ export default compose(
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${getReportID(route)}`,
},
isLoadingReportData: {
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
},
}),
)(FlagCommentPage);