Skip to content
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
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 @@ -821,6 +821,7 @@ Onyx.connect({
* @param {Object} reportAction
*/
function deleteReportComment(reportID, reportAction) {
const originalReportID = ReportUtils.getOriginalReportID(reportID, reportAction);
const reportActionID = reportAction.reportActionID;
const deletedMessage = [
{
Expand All @@ -846,9 +847,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 @@ -860,7 +861,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 @@ -874,7 +875,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 @@ -887,19 +888,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 @@ -952,6 +953,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 @@ -970,7 +972,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 @@ -999,28 +1001,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 @@ -1033,7 +1035,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 @@ -1043,7 +1045,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
];

const parameters = {
reportID,
reportID: originalReportID,
reportComment: htmlForNewComment,
reportActionID,
};
Expand Down Expand Up @@ -1528,6 +1530,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 @@ -1559,10 +1562,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 @@ -1578,6 +1581,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 @@ -1610,10 +1614,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 @@ -1739,6 +1743,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 @@ -1773,7 +1778,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 @@ -1786,7 +1791,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 @@ -1799,7 +1804,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 @@ -208,18 +208,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