From bcfda4bd40ec23ff6b16674eb3f50c5aab0e558b Mon Sep 17 00:00:00 2001
From: Katsuki <1313124+K4tsuki@users.noreply.github.com>
Date: Tue, 11 Jan 2022 13:39:55 +0700
Subject: [PATCH 1/4] Cut report last message to specified length

---
 src/CONST.js               |  1 +
 src/libs/actions/Report.js | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/CONST.js b/src/CONST.js
index 33a4f3add359..3b8b1adb39ca 100755
--- a/src/CONST.js
+++ b/src/CONST.js
@@ -244,6 +244,7 @@ const CONST = {
         },
         MAX_PREVIEW_AVATARS: 4,
         MAX_ROOM_NAME_LENGTH: 80,
+        MAX_LAST_MESSAGE_LENGTH: 80,
     },
     MODAL: {
         MODAL_TYPE: {
diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js
index 47d0ba7291a5..811acef04806 100644
--- a/src/libs/actions/Report.js
+++ b/src/libs/actions/Report.js
@@ -115,6 +115,17 @@ function getUnreadActionCount(report) {
     return Math.max(0, unreadActionCount);
 }
 
+/**
+ * Used before merging Report data.
+ * Cut last message to specific length, because we don't need full last message
+ *
+ * @param {String} lastMessage
+ * @returns {String}
+ */
+function cutLastMessage(lastMessage) {
+    return lastMessage.substr(0, CONST.REPORT.MAX_LAST_MESSAGE_LENGTH);
+}
+
 /**
  * @param {Object} report
  * @return {String[]}
@@ -210,7 +221,7 @@ function getSimplifiedReportObject(report) {
             'timestamp',
         ], 0),
         lastMessageTimestamp,
-        lastMessageText: isLastMessageAttachment ? '[Attachment]' : lastMessageText,
+        lastMessageText: isLastMessageAttachment ? '[Attachment]' : cutLastMessage(lastMessageText),
         lastActorEmail,
         hasOutstandingIOU: false,
         notificationPreference,
@@ -551,7 +562,7 @@ function updateReportActionMessage(reportID, sequenceNumber, message) {
     // If this is the most recent message, update the lastMessageText in the report object as well
     if (sequenceNumber === reportMaxSequenceNumbers[reportID]) {
         Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
-            lastMessageText: message.text || `[${Localize.translateLocal('common.deletedCommentMessage')}]`,
+            lastMessageText: cutLastMessage(message.text) || `[${Localize.translateLocal('common.deletedCommentMessage')}]`,
         });
     }
 }
@@ -597,10 +608,12 @@ function updateReportWithNewAction(
     // a chat participant in another application), then the last message text and author needs to be updated as well
     if (newMaxSequenceNumber > initialLastReadSequenceNumber) {
         updatedReportObject.lastMessageTimestamp = reportAction.timestamp;
-        updatedReportObject.lastMessageText = messageText;
         updatedReportObject.lastActorEmail = reportAction.actorEmail;
     }
 
+    // Alwasy cut last message
+    updatedReportObject.lastMessageText = cutLastMessage(messageText);
+
     Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedReportObject);
 
     const reportActionsToMerge = {};
@@ -1100,7 +1113,7 @@ function addAction(reportID, text, file) {
     Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
         maxSequenceNumber: newSequenceNumber,
         lastMessageTimestamp: moment().unix(),
-        lastMessageText: textForNewComment,
+        lastMessageText: cutLastMessage(textForNewComment),
         lastActorEmail: currentUserEmail,
     });
 

From 6459b35a6568caadd79b9d5d522190861024630d Mon Sep 17 00:00:00 2001
From: K4tsuki <1313124+K4tsuki@users.noreply.github.com>
Date: Tue, 11 Jan 2022 15:47:17 +0700
Subject: [PATCH 2/4] Update src/CONST.js

Co-authored-by: Rajat Parashar <parasharrajat@users.noreply.github.com>
---
 src/CONST.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/CONST.js b/src/CONST.js
index 3b8b1adb39ca..403098ca1665 100755
--- a/src/CONST.js
+++ b/src/CONST.js
@@ -244,7 +244,7 @@ const CONST = {
         },
         MAX_PREVIEW_AVATARS: 4,
         MAX_ROOM_NAME_LENGTH: 80,
-        MAX_LAST_MESSAGE_LENGTH: 80,
+        LAST_MESSAGE_TEXT_MAX_LENGTH: 80,
     },
     MODAL: {
         MODAL_TYPE: {

From f775fb25d8b9e94c83f8a619c297d3bd889a4e1b Mon Sep 17 00:00:00 2001
From: Katsuki <1313124+K4tsuki@users.noreply.github.com>
Date: Wed, 12 Jan 2022 07:35:11 +0700
Subject: [PATCH 3/4] Add format last message text in report utils

---
 src/libs/actions/Report.js | 22 +++++-----------------
 src/libs/reportUtils.js    | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js
index 811acef04806..d67862ed347c 100644
--- a/src/libs/actions/Report.js
+++ b/src/libs/actions/Report.js
@@ -115,17 +115,6 @@ function getUnreadActionCount(report) {
     return Math.max(0, unreadActionCount);
 }
 
-/**
- * Used before merging Report data.
- * Cut last message to specific length, because we don't need full last message
- *
- * @param {String} lastMessage
- * @returns {String}
- */
-function cutLastMessage(lastMessage) {
-    return lastMessage.substr(0, CONST.REPORT.MAX_LAST_MESSAGE_LENGTH);
-}
-
 /**
  * @param {Object} report
  * @return {String[]}
@@ -192,6 +181,7 @@ function getSimplifiedReportObject(report) {
         lastMessageText = lastActionMessage
             .replace(/((<br[^>]*>)+)/gi, ' ')
             .replace(/(<([^>]+)>)/gi, '') || `[${Localize.translateLocal('common.deletedCommentMessage')}]`;
+        lastMessageText = ReportUtils.formatReportLastMessageText(lastMessageText);
     }
 
     const reportName = lodashGet(report, ['reportNameValuePairs', 'type']) === 'chat'
@@ -221,7 +211,7 @@ function getSimplifiedReportObject(report) {
             'timestamp',
         ], 0),
         lastMessageTimestamp,
-        lastMessageText: isLastMessageAttachment ? '[Attachment]' : cutLastMessage(lastMessageText),
+        lastMessageText: isLastMessageAttachment ? '[Attachment]' : lastMessageText,
         lastActorEmail,
         hasOutstandingIOU: false,
         notificationPreference,
@@ -562,7 +552,7 @@ function updateReportActionMessage(reportID, sequenceNumber, message) {
     // If this is the most recent message, update the lastMessageText in the report object as well
     if (sequenceNumber === reportMaxSequenceNumbers[reportID]) {
         Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
-            lastMessageText: cutLastMessage(message.text) || `[${Localize.translateLocal('common.deletedCommentMessage')}]`,
+            lastMessageText: ReportUtils.formatReportLastMessageText(message.text) || `[${Localize.translateLocal('common.deletedCommentMessage')}]`,
         });
     }
 }
@@ -608,12 +598,10 @@ function updateReportWithNewAction(
     // a chat participant in another application), then the last message text and author needs to be updated as well
     if (newMaxSequenceNumber > initialLastReadSequenceNumber) {
         updatedReportObject.lastMessageTimestamp = reportAction.timestamp;
+        updatedReportObject.lastMessageText = ReportUtils.formatReportLastMessageText(messageText);
         updatedReportObject.lastActorEmail = reportAction.actorEmail;
     }
 
-    // Alwasy cut last message
-    updatedReportObject.lastMessageText = cutLastMessage(messageText);
-
     Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, updatedReportObject);
 
     const reportActionsToMerge = {};
@@ -1113,7 +1101,7 @@ function addAction(reportID, text, file) {
     Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
         maxSequenceNumber: newSequenceNumber,
         lastMessageTimestamp: moment().unix(),
-        lastMessageText: cutLastMessage(textForNewComment),
+        lastMessageText: ReportUtils.formatReportLastMessageText(textForNewComment),
         lastActorEmail: currentUserEmail,
     });
 
diff --git a/src/libs/reportUtils.js b/src/libs/reportUtils.js
index c484555ee5ce..6807d165e4db 100644
--- a/src/libs/reportUtils.js
+++ b/src/libs/reportUtils.js
@@ -220,6 +220,21 @@ function isDeletedAction(action) {
     return action.message.length === 0 || action.message[0].html === '';
 }
 
+/**
+ * Used before merging Report data.
+ * For now cut last message text to specific length, because we don't need full last message
+ *
+ * @param {String} lastMessageText
+ * @returns {String}
+ */
+function formatReportLastMessageText(lastMessageText) {
+    if (_.isString(lastMessageText) && lastMessageText.length > CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH) {
+        return lastMessageText.substr(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH);
+    }
+
+    return lastMessageText;
+}
+
 export {
     getReportParticipantsTitle,
     isDeletedAction,
@@ -236,4 +251,5 @@ export {
     isConciergeChatReport,
     hasExpensifyEmails,
     canShowReportRecipientLocalTime,
+    formatReportLastMessageText,
 };

From 478c4b073ccd880690c73fc8b329975991916408 Mon Sep 17 00:00:00 2001
From: K4tsuki <1313124+K4tsuki@users.noreply.github.com>
Date: Wed, 12 Jan 2022 16:04:02 +0700
Subject: [PATCH 4/4] Apply suggestions from code review

Co-authored-by: Rajat Parashar <parasharrajat@users.noreply.github.com>
---
 src/libs/reportUtils.js | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/src/libs/reportUtils.js b/src/libs/reportUtils.js
index 6807d165e4db..a6164960ae6d 100644
--- a/src/libs/reportUtils.js
+++ b/src/libs/reportUtils.js
@@ -221,18 +221,12 @@ function isDeletedAction(action) {
 }
 
 /**
- * Used before merging Report data.
- * For now cut last message text to specific length, because we don't need full last message
- *
+ * Trim the last message text to a fixed limit.
  * @param {String} lastMessageText
  * @returns {String}
  */
 function formatReportLastMessageText(lastMessageText) {
-    if (_.isString(lastMessageText) && lastMessageText.length > CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH) {
-        return lastMessageText.substr(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH);
-    }
-
-    return lastMessageText;
+    return String(lastMessageText).substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH);
 }
 
 export {
