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

Stop sending <mention-report> when not in a policy room #40685

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"date-fns-tz": "^2.0.0",
"dom-serializer": "^0.2.2",
"domhandler": "^4.3.0",
"expensify-common": "git+ssh://[email protected]/Expensify/expensify-common.git#c0f7f3b6558fbeda0527c80d68460d418afef219",
"expensify-common": "git+ssh://[email protected]/Expensify/expensify-common.git#9a68635cdcef4c81593c0f816a007bc9c707d46a",
"expo": "^50.0.3",
"expo-av": "~13.10.4",
"expo-image": "1.11.0",
Expand Down
39 changes: 32 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ type OutstandingChildRequest = {
hasOutstandingChildRequest?: boolean;
};

type ParsingDetails = {
shouldEscapeText?: boolean;
reportID?: string;
policyID?: string;
};

let currentUserEmail: string | undefined;
let currentUserPrivateDomain: string | undefined;
let currentUserAccountID: number | undefined;
Expand Down Expand Up @@ -3168,15 +3174,27 @@ function addDomainToShortMention(mention: string): string | undefined {
* For comments shorter than or equal to 10k chars, convert the comment from MD into HTML because that's how it is stored in the database
* For longer comments, skip parsing, but still escape the text, and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!!
*/
function getParsedComment(text: string, shouldEscapeText?: boolean): string {
function getParsedComment(text: string, parsingDetails?: ParsingDetails): string {
let isGroupPolicyReport = false;
if (parsingDetails?.reportID) {
const currentReport = getReport(parsingDetails?.reportID);
isGroupPolicyReport = currentReport && !isEmptyObject(currentReport) ? isGroupPolicy(currentReport) : false;
war-in marked this conversation as resolved.
Show resolved Hide resolved
}
if (parsingDetails?.policyID) {
war-in marked this conversation as resolved.
Show resolved Hide resolved
const policyType = getPolicy(parsingDetails?.policyID).type;
isGroupPolicyReport = policyType === CONST.POLICY.TYPE.CORPORATE || policyType === CONST.POLICY.TYPE.TEAM || policyType === CONST.POLICY.TYPE.FREE;
war-in marked this conversation as resolved.
Show resolved Hide resolved
}

const parser = new ExpensiMark();
const textWithMention = text.replace(CONST.REGEX.SHORT_MENTION, (match) => {
const mention = match.substring(1);
const mentionWithDomain = addDomainToShortMention(mention);
return mentionWithDomain ? `@${mentionWithDomain}` : match;
});

return text.length <= CONST.MAX_MARKUP_LENGTH ? parser.replace(textWithMention, {shouldEscapeText}) : lodashEscape(text);
return text.length <= CONST.MAX_MARKUP_LENGTH
? parser.replace(textWithMention, {shouldEscapeText: parsingDetails?.shouldEscapeText, disabledRules: isGroupPolicyReport ? [] : ['reportMentions']})
: lodashEscape(text);
}

function getReportDescriptionText(report: Report): string {
Expand All @@ -3197,9 +3215,16 @@ function getPolicyDescriptionText(policy: OnyxEntry<Policy>): string {
return parser.htmlToText(policy.description);
}

function buildOptimisticAddCommentReportAction(text?: string, file?: FileObject, actorAccountID?: number, createdOffset = 0, shouldEscapeText?: boolean): OptimisticReportAction {
function buildOptimisticAddCommentReportAction(
text?: string,
file?: FileObject,
actorAccountID?: number,
createdOffset = 0,
shouldEscapeText?: boolean,
reportID?: string,
): OptimisticReportAction {
const parser = new ExpensiMark();
const commentText = getParsedComment(text ?? '', shouldEscapeText);
const commentText = getParsedComment(text ?? '', {shouldEscapeText, reportID});
const isAttachmentOnly = file && !text;
const isTextOnly = text && !file;

Expand Down Expand Up @@ -3321,7 +3346,7 @@ function buildOptimisticTaskCommentReportAction(
childOldestFourAccountIDs?: string;
},
): OptimisticReportAction {
const reportAction = buildOptimisticAddCommentReportAction(text, undefined, undefined, createdOffset);
const reportAction = buildOptimisticAddCommentReportAction(text, undefined, undefined, createdOffset, undefined, taskReportID);
if (reportAction.reportAction.message?.[0]) {
reportAction.reportAction.message[0].taskReportID = taskReportID;
}
Expand Down Expand Up @@ -4996,8 +5021,8 @@ function getNewMarkerReportActionID(report: OnyxEntry<Report>, sortedAndFiltered
* Used for compatibility with the backend auth validator for AddComment, and to account for MD in comments
* @returns The comment's total length as seen from the backend
*/
function getCommentLength(textComment: string): number {
return getParsedComment(textComment)
function getCommentLength(textComment: string, parsingDetails?: ParsingDetails): number {
return getParsedComment(textComment, parsingDetails)
.replace(/[^ -~]/g, '\\u????')
.trim().length;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ function updateWorkspaceDescription(policyID: string, description: string, curre
if (description === currentDescription) {
return;
}
const parsedDescription = ReportUtils.getParsedComment(description);
const parsedDescription = ReportUtils.getParsedComment(description, {policyID});

const optimisticData: OnyxUpdate[] = [
{
Expand Down
8 changes: 4 additions & 4 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function addActions(reportID: string, text = '', file?: FileObject) {
let commandName: typeof WRITE_COMMANDS.ADD_COMMENT | typeof WRITE_COMMANDS.ADD_ATTACHMENT | typeof WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT = WRITE_COMMANDS.ADD_COMMENT;

if (text && !file) {
const reportComment = ReportUtils.buildOptimisticAddCommentReportAction(text);
const reportComment = ReportUtils.buildOptimisticAddCommentReportAction(text, undefined, undefined, undefined, undefined, reportID);
reportCommentAction = reportComment.reportAction;
reportCommentText = reportComment.commentText;
}
Expand All @@ -455,13 +455,13 @@ function addActions(reportID: string, text = '', file?: FileObject) {
// When we are adding an attachment we will call AddAttachment.
// It supports sending an attachment with an optional comment and AddComment supports adding a single text comment only.
commandName = WRITE_COMMANDS.ADD_ATTACHMENT;
const attachment = ReportUtils.buildOptimisticAddCommentReportAction(text, file);
const attachment = ReportUtils.buildOptimisticAddCommentReportAction(text, file, undefined, undefined, undefined, reportID);
attachmentAction = attachment.reportAction;
}

if (text && file) {
// When there is both text and a file, the text for the report comment needs to be parsed)
reportCommentText = ReportUtils.getParsedComment(text ?? '');
reportCommentText = ReportUtils.getParsedComment(text ?? '', {reportID});

// And the API command needs to go to the new API which supports combining both text and attachments in a single report action
commandName = WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT;
Expand Down Expand Up @@ -1848,7 +1848,7 @@ function updateDescription(reportID: string, previousValue: string, newValue: st
return;
}

const parsedDescription = ReportUtils.getParsedComment(newValue);
const parsedDescription = ReportUtils.getParsedComment(newValue, {reportID});

const optimisticData: OnyxUpdate[] = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ function ComposerWithSuggestions(

const prepareCommentAndResetComposer = useCallback((): string => {
const trimmedComment = commentRef.current.trim();
const commentLength = ReportUtils.getCommentLength(trimmedComment);
const commentLength = ReportUtils.getCommentLength(trimmedComment, {reportID});

// Don't submit empty comments or comments that exceed the character limit
if (!commentLength || commentLength > CONST.MAX_COMMENT_LENGTH) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function ReportActionItemMessageEdit(
*/
const publishDraft = useCallback(() => {
// Do nothing if draft exceed the character limit
if (ReportUtils.getCommentLength(draft) > CONST.MAX_COMMENT_LENGTH) {
if (ReportUtils.getCommentLength(draft, {reportID}) > CONST.MAX_COMMENT_LENGTH) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/pages/workspace/WorkspaceNewRoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function WorkspaceNewRoomPage({policies, reports, formState, session, activePoli
*/
const submit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_ROOM_FORM>) => {
const participants = [session?.accountID ?? 0];
const parsedDescription = ReportUtils.getParsedComment(values.reportDescription ?? '');
const parsedDescription = ReportUtils.getParsedComment(values.reportDescription ?? '', {policyID});
const policyReport = ReportUtils.buildOptimisticChatReport(
participants,
values.roomName,
Expand Down Expand Up @@ -183,7 +183,7 @@ function WorkspaceNewRoomPage({policies, reports, formState, session, activePoli
ErrorUtils.addErrorMessage(errors, 'roomName', ['common.error.characterLimitExceedCounter', {length: values.roomName.length, limit: CONST.TITLE_CHARACTER_LIMIT}]);
}

const descriptionLength = ReportUtils.getCommentLength(values.reportDescription);
const descriptionLength = ReportUtils.getCommentLength(values.reportDescription, {policyID});
if (descriptionLength > CONST.REPORT_DESCRIPTION.MAX_LENGTH) {
ErrorUtils.addErrorMessage(errors, 'reportDescription', [
'common.error.characterLimitExceedCounter',
Expand All @@ -197,7 +197,7 @@ function WorkspaceNewRoomPage({policies, reports, formState, session, activePoli

return errors;
},
[reports],
[reports, policyID],
);

const writeCapabilityOptions = useMemo(
Expand Down
Loading