Skip to content

Commit

Permalink
Merge pull request #49685 from bernhardoj/fix/49454-task-assignee-regex
Browse files Browse the repository at this point in the history
Fix task assignee regex can't accept multi-level domains
  • Loading branch information
iwiznia authored Sep 30, 2024
2 parents 36032e0 + 6ed09b9 commit 0e39ff9
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Str} from 'expensify-common';
import lodashIsEqual from 'lodash/isEqual';
import React, {memo, useCallback} from 'react';
import {Keyboard, View} from 'react-native';
Expand Down Expand Up @@ -129,29 +130,39 @@ function ReportFooter({
* Group 2: Optional email group between \s+....\s* start rule with @+valid email or short mention
* Group 3: Title is remaining characters
*/
const taskRegex = /^\[\]\s+(?:@([^\s@]+(?:@\w+\.\w+)?))?\s*([\s\S]*)/;
// The regex is copied from the expensify-common CONST file, but the domain is optional to accept short mention
const emailWithOptionalDomainRegex =
/(?=((?=[\w'#%+-]+(?:\.[\w'#%+-]+)*@?)[\w.'#%+-]{1,64}(?:@(?:(?=[a-z\d]+(?:-+[a-z\d]+)*\.)(?:[a-z\d-]{1,63}\.)+[a-z]{2,63}))?(?= |_|\b))(?<end>.*))\S{3,254}(?=\k<end>$)/;
const taskRegex = `^\\[\\]\\s+(?:@(?:${emailWithOptionalDomainRegex.source}))?\\s*([\\s\\S]*)`;

const match = text.match(taskRegex);
if (!match) {
return false;
}
const title = match[2] ? match[2].trim().replace(/\n/g, ' ') : undefined;
let title = match[3] ? match[3].trim().replace(/\n/g, ' ') : undefined;
if (!title) {
return false;
}

const mention = match[1] ? match[1].trim() : undefined;
const mentionWithDomain = ReportUtils.addDomainToShortMention(mention ?? '') ?? mention;
const mention = match[1] ? match[1].trim() : '';
const mentionWithDomain = ReportUtils.addDomainToShortMention(mention) ?? mention;
const isValidMention = Str.isValidEmail(mentionWithDomain);

let assignee: OnyxEntry<OnyxTypes.PersonalDetails>;
let assigneeChatReport;
if (mentionWithDomain) {
assignee = Object.values(allPersonalDetails).find((value) => value?.login === mentionWithDomain) ?? undefined;
if (!Object.keys(assignee ?? {}).length) {
const assigneeAccountID = UserUtils.generateAccountID(mentionWithDomain);
const optimisticDataForNewAssignee = Task.setNewOptimisticAssignee(mentionWithDomain, assigneeAccountID);
assignee = optimisticDataForNewAssignee.assignee;
assigneeChatReport = optimisticDataForNewAssignee.assigneeReport;
if (isValidMention) {
assignee = Object.values(allPersonalDetails).find((value) => value?.login === mentionWithDomain) ?? undefined;
if (!Object.keys(assignee ?? {}).length) {
const assigneeAccountID = UserUtils.generateAccountID(mentionWithDomain);
const optimisticDataForNewAssignee = Task.setNewOptimisticAssignee(mentionWithDomain, assigneeAccountID);
assignee = optimisticDataForNewAssignee.assignee;
assigneeChatReport = optimisticDataForNewAssignee.assigneeReport;
}
} else {
// If the mention is not valid, include it on the title.
// The mention could be invalid if it's a short mention and failed to be converted to a full mention.
title = `@${mentionWithDomain} ${title}`;
}
}
Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee?.accountID, assigneeChatReport, report.policyID);
Expand Down

0 comments on commit 0e39ff9

Please sign in to comment.