Skip to content

Commit

Permalink
Merge pull request #20574 from tienifr/fix/19363-unmatched-parent-rep…
Browse files Browse the repository at this point in the history
…ort-and-action

Fix: Unmatched parent report ID and report action ID
  • Loading branch information
cristipaval authored Jun 26, 2023
2 parents 5c6b751 + 5b37315 commit 4ba4d5d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
14 changes: 13 additions & 1 deletion src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ function isThreadParent(reportAction) {
* Returns true if reportAction is the first chat preview of a Thread
*
* @param {Object} reportAction
* @param {String} reportID
* @param {String} reportID
* @returns {Boolean}
*/
function isThreadFirstChat(reportAction, reportID) {
Expand Down Expand Up @@ -2258,6 +2258,17 @@ function getParentReport(report) {
return lodashGet(allReports, `${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`, {});
}

/**
* Returns ID of the original report from which the given reportAction is first created.
*
* @param {String} reportID
* @param {Object} reportAction
* @returns {String}
*/
function getOriginalReportID(reportID, reportAction) {
return isThreadFirstChat(reportAction, reportID) ? lodashGet(allReports, [`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, 'parentReportID']) : reportID;
}

export {
getReportParticipantsTitle,
isReportMessageAttachment,
Expand Down Expand Up @@ -2350,4 +2361,5 @@ export {
getMoneyRequestAction,
getBankAccountRoute,
getParentReport,
getOriginalReportID,
};
49 changes: 27 additions & 22 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ Onyx.connect({
* @param {Object} reportAction
*/
function deleteReportComment(reportID, reportAction) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, reportAction);
const reportActionID = reportAction.reportActionID;
const deletedMessage = [
{
Expand All @@ -856,9 +857,9 @@ function deleteReportComment(reportID, reportAction) {
lastMessageText: '',
lastVisibleActionCreated: '',
};
const lastMessageText = ReportActionsUtils.getLastVisibleMessageText(reportID, optimisticReportActions);
const lastMessageText = ReportActionsUtils.getLastVisibleMessageText(originalReportID, optimisticReportActions);
if (lastMessageText.length > 0) {
const lastVisibleActionCreated = ReportActionsUtils.getLastVisibleAction(reportID, optimisticReportActions).created;
const lastVisibleActionCreated = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions).created;
optimisticReport = {
lastMessageText,
lastVisibleActionCreated,
Expand All @@ -870,7 +871,7 @@ function deleteReportComment(reportID, reportAction) {
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
message: reportAction.message,
Expand All @@ -884,7 +885,7 @@ function deleteReportComment(reportID, reportAction) {
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
pendingAction: null,
Expand All @@ -897,19 +898,19 @@ function deleteReportComment(reportID, reportAction) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: optimisticReportActions,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT}${originalReportID}`,
value: optimisticReport,
},
];

const parameters = {
reportID,
reportActionID: reportAction.reportActionID,
reportID: originalReportID,
reportActionID,
};
API.write('DeleteComment', parameters, {optimisticData, successData, failureData});
}
Expand Down Expand Up @@ -962,6 +963,7 @@ const handleUserDeletedLinksInHtml = (newCommentText, originalHtml) => {
*/
function editReportComment(reportID, originalReportAction, textForNewComment) {
const parser = new ExpensiMark();
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);

// Do not autolink if someone explicitly tries to remove a link from message.
// https://github.com/Expensify/App/issues/9090
Expand All @@ -980,7 +982,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {

// Delete the comment if it's empty
if (_.isEmpty(htmlForNewComment)) {
deleteReportComment(reportID, originalReportAction);
deleteReportComment(originalReportID, originalReportAction);
return;
}

Expand Down Expand Up @@ -1009,28 +1011,28 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: optimisticReportActions,
},
];

const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID, optimisticReportActions);
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions);
if (reportActionID === lastVisibleAction.reportActionID) {
const lastMessageText = ReportUtils.formatReportLastMessageText(reportComment);
const optimisticReport = {
lastMessageText,
};
optimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT}${originalReportID}`,
value: optimisticReport,
});
}

const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
...originalReportAction,
Expand All @@ -1043,7 +1045,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
pendingAction: null,
Expand All @@ -1053,7 +1055,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
];

const parameters = {
reportID,
reportID: originalReportID,
reportComment: htmlForNewComment,
reportActionID,
};
Expand Down Expand Up @@ -1533,6 +1535,7 @@ function hasAccountIDReacted(accountID, users, skinTone) {
* @param {number} [skinTone] Optional.
*/
function addEmojiReaction(reportID, originalReportAction, emoji, skinTone = preferredSkinTone) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);
const message = originalReportAction.message[0];
let reactionObject = message.reactions && _.find(message.reactions, (reaction) => reaction.emoji === emoji.name);
const needToInsertReactionObject = !reactionObject;
Expand Down Expand Up @@ -1564,10 +1567,10 @@ function addEmojiReaction(reportID, originalReportAction, emoji, skinTone = pref
};

// Optimistically update the reportAction with the reaction
const optimisticData = getOptimisticDataForReportActionUpdate(originalReportAction, updatedMessage, reportID);
const optimisticData = getOptimisticDataForReportActionUpdate(originalReportAction, updatedMessage, originalReportID);

const parameters = {
reportID,
reportID: originalReportID,
skinTone,
emojiCode: emoji.name,
sequenceNumber: originalReportAction.sequenceNumber,
Expand All @@ -1583,6 +1586,7 @@ function addEmojiReaction(reportID, originalReportAction, emoji, skinTone = pref
* @param {{ name: string, code: string, types: string[] }} emoji
*/
function removeEmojiReaction(reportID, originalReportAction, emoji) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);
const message = originalReportAction.message[0];
const reactionObject = message.reactions && _.find(message.reactions, (reaction) => reaction.emoji === emoji.name);
if (!reactionObject) {
Expand Down Expand Up @@ -1615,10 +1619,10 @@ function removeEmojiReaction(reportID, originalReportAction, emoji) {
};

// Optimistically update the reportAction with the reaction
const optimisticData = getOptimisticDataForReportActionUpdate(originalReportAction, updatedMessage, reportID);
const optimisticData = getOptimisticDataForReportActionUpdate(originalReportAction, updatedMessage, originalReportID);

const parameters = {
reportID,
reportID: originalReportID,
sequenceNumber: originalReportAction.sequenceNumber,
reportActionID: originalReportAction.reportActionID,
emojiCode: emoji.name,
Expand Down Expand Up @@ -1744,6 +1748,7 @@ function openLastOpenedPublicRoom(lastOpenedPublicRoomID) {
* @param {String} severity
*/
function flagComment(reportID, reportAction, severity) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, reportAction);
const message = reportAction.message[0];
let updatedDecision;
if (severity === CONST.MODERATION.FLAG_SEVERITY_SPAM || severity === CONST.MODERATION.FLAG_SEVERITY_INCONSIDERATE) {
Expand Down Expand Up @@ -1778,7 +1783,7 @@ function flagComment(reportID, reportAction, severity) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
Expand All @@ -1791,7 +1796,7 @@ function flagComment(reportID, reportAction, severity) {
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
...reportAction,
Expand All @@ -1804,7 +1809,7 @@ function flagComment(reportID, reportAction, severity) {
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
value: {
[reportActionID]: {
pendingAction: null,
Expand Down
10 changes: 3 additions & 7 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,14 @@ function ReportActionItemMessageEdit(props) {

const trimmedNewDraft = draft.trim();

// If the reportActionID and parentReportActionID are the same then the user is editing the first message of a
// thread and we should pass the parentReportID instead of the reportID of the thread
const reportID = props.report.parentReportActionID === props.action.reportActionID ? props.report.parentReportID : props.reportID;

// When user tries to save the empty message, it will delete it. Prompt the user to confirm deleting.
if (!trimmedNewDraft) {
ReportActionContextMenu.showDeleteModal(reportID, props.action, false, deleteDraft, () => InteractionManager.runAfterInteractions(() => textInputRef.current.focus()));
ReportActionContextMenu.showDeleteModal(props.reportID, props.action, false, deleteDraft, () => InteractionManager.runAfterInteractions(() => textInputRef.current.focus()));
return;
}
Report.editReportComment(reportID, props.action, trimmedNewDraft);
Report.editReportComment(props.reportID, props.action, trimmedNewDraft);
deleteDraft();
}, [props.action, debouncedSaveDraft, deleteDraft, draft, props.reportID, props.report]);
}, [props.action, debouncedSaveDraft, deleteDraft, draft, props.reportID]);

/**
* @param {String} emoji
Expand Down

0 comments on commit 4ba4d5d

Please sign in to comment.