From 3e4762eeb2b6036057c5f073b67b972827211273 Mon Sep 17 00:00:00 2001 From: VH Date: Wed, 17 May 2023 00:52:49 +0700 Subject: [PATCH 1/4] Show roomName in notif title if message come from a chat room --- .../LocalNotification/BrowserNotifications.js | 30 ++++++++++++++++--- .../LocalNotification/index.desktop.js | 4 +-- .../LocalNotification/index.website.js | 4 +-- src/libs/actions/Report.js | 1 + 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.js b/src/libs/Notification/LocalNotification/BrowserNotifications.js index 6db3fdb7f50c..696180cf25db 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.js +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.js @@ -3,6 +3,7 @@ import _ from 'underscore'; import focusApp from './focusApp'; import * as AppUpdate from '../../actions/AppUpdate'; import EXPENSIFY_ICON_URL from '../../../../assets/images/expensify-logo-round-clearspace.png'; +import * as ReportUtils from '../../ReportUtils'; const DEFAULT_DELAY = 4000; @@ -83,6 +84,27 @@ function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', }); } +/** + * Get notification based on reportRoom and reportAction + * + * @param {Object} report + * @param {Object} reportAction + * + * @return {String} - Notification title + */ +function getNotificationTitle(report, reportAction) { + const isChatRoom = ReportUtils.isChatRoom(report); + + if (isChatRoom) { + const roomName = _.get(report, 'displayName', ''); + return roomName; + } + + const {person} = reportAction; + const plainTextPerson = _.map(person, (f) => f.text).join(); + return plainTextPerson; +} + /** * BrowserNotification * @namespace @@ -92,19 +114,19 @@ export default { * Create a report comment notification * * @param {Object} params + * @param {Object} params.report * @param {Object} params.reportAction * @param {Function} params.onClick * @param {Boolean} usesIcon true if notification uses right circular icon */ - pushReportCommentNotification({reportAction, onClick}, usesIcon = false) { - const {person, message} = reportAction; - const plainTextPerson = _.map(person, (f) => f.text).join(); + pushReportCommentNotification({report, reportAction, onClick}, usesIcon = false) { + const {message} = reportAction; // Specifically target the comment part of the message const plainTextMessage = (_.find(message, (f) => f.type === 'COMMENT') || {}).text; push({ - title: plainTextPerson, + title: getNotificationTitle(report, reportAction), body: plainTextMessage, delay: 0, onClick, diff --git a/src/libs/Notification/LocalNotification/index.desktop.js b/src/libs/Notification/LocalNotification/index.desktop.js index 9564b0ef7f26..2bef51cea0a6 100644 --- a/src/libs/Notification/LocalNotification/index.desktop.js +++ b/src/libs/Notification/LocalNotification/index.desktop.js @@ -1,7 +1,7 @@ import BrowserNotifications from './BrowserNotifications'; -function showCommentNotification({reportAction, onClick}) { - BrowserNotifications.pushReportCommentNotification({reportAction, onClick}); +function showCommentNotification({report, reportAction, onClick}) { + BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}); } function showUpdateAvailableNotification() { diff --git a/src/libs/Notification/LocalNotification/index.website.js b/src/libs/Notification/LocalNotification/index.website.js index 5c94f3360f4c..3410b3144caf 100644 --- a/src/libs/Notification/LocalNotification/index.website.js +++ b/src/libs/Notification/LocalNotification/index.website.js @@ -1,7 +1,7 @@ import BrowserNotifications from './BrowserNotifications'; -function showCommentNotification({reportAction, onClick}) { - BrowserNotifications.pushReportCommentNotification({reportAction, onClick}, true); +function showCommentNotification({report, reportAction, onClick}) { + BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}, true); } function showUpdateAvailableNotification() { diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 268a052d4a4e..d23cce4f5e0f 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1378,6 +1378,7 @@ function showReportActionNotification(reportID, action) { Log.info('[LocalNotification] Creating notification'); LocalNotification.showCommentNotification({ + report: allReports[reportID], reportAction: action, onClick: () => { // Navigate to this report onClick From 709b63d7abd298d4063144ddec4bd0788e08851f Mon Sep 17 00:00:00 2001 From: VH Date: Wed, 17 May 2023 22:16:57 +0700 Subject: [PATCH 2/4] Build web notification title & body if message is come from chatroom --- .../LocalNotification/BrowserNotifications.js | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.js b/src/libs/Notification/LocalNotification/BrowserNotifications.js index 696180cf25db..b07d022b6e8c 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.js +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.js @@ -84,27 +84,6 @@ function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', }); } -/** - * Get notification based on reportRoom and reportAction - * - * @param {Object} report - * @param {Object} reportAction - * - * @return {String} - Notification title - */ -function getNotificationTitle(report, reportAction) { - const isChatRoom = ReportUtils.isChatRoom(report); - - if (isChatRoom) { - const roomName = _.get(report, 'displayName', ''); - return roomName; - } - - const {person} = reportAction; - const plainTextPerson = _.map(person, (f) => f.text).join(); - return plainTextPerson; -} - /** * BrowserNotification * @namespace @@ -120,14 +99,28 @@ export default { * @param {Boolean} usesIcon true if notification uses right circular icon */ pushReportCommentNotification({report, reportAction, onClick}, usesIcon = false) { - const {message} = reportAction; + const {person, message} = reportAction; + const plainTextPerson = _.map(person, (f) => f.text).join(); + const isChatRoom = ReportUtils.isChatRoom(report); // Specifically target the comment part of the message const plainTextMessage = (_.find(message, (f) => f.type === 'COMMENT') || {}).text; + let title = ''; + let body = ''; + + if (isChatRoom) { + const roomName = _.get(report, 'displayName', ''); + title = roomName; + body = `${plainTextPerson}: ${plainTextMessage}`; + } else { + title = plainTextPerson; + body = plainTextMessage; + } + push({ - title: getNotificationTitle(report, reportAction), - body: plainTextMessage, + title, + body, delay: 0, onClick, icon: usesIcon ? EXPENSIFY_ICON_URL : '', From dc63f9bd229633377014f0762170456920d5f825 Mon Sep 17 00:00:00 2001 From: VH Date: Thu, 18 May 2023 06:18:02 +0700 Subject: [PATCH 3/4] Refactor code --- .../LocalNotification/BrowserNotifications.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.js b/src/libs/Notification/LocalNotification/BrowserNotifications.js index b07d022b6e8c..345324bc613e 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.js +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.js @@ -99,16 +99,15 @@ export default { * @param {Boolean} usesIcon true if notification uses right circular icon */ pushReportCommentNotification({report, reportAction, onClick}, usesIcon = false) { + let title; + let body; + + const isChatRoom = ReportUtils.isChatRoom(report); const {person, message} = reportAction; const plainTextPerson = _.map(person, (f) => f.text).join(); - const isChatRoom = ReportUtils.isChatRoom(report); - // Specifically target the comment part of the message const plainTextMessage = (_.find(message, (f) => f.type === 'COMMENT') || {}).text; - let title = ''; - let body = ''; - if (isChatRoom) { const roomName = _.get(report, 'displayName', ''); title = roomName; From 33e560cebaf6430e8bea14dfc7fa17dd74327d9e Mon Sep 17 00:00:00 2001 From: VH Date: Thu, 18 May 2023 06:27:23 +0700 Subject: [PATCH 4/4] Add empty lines to make code look better --- src/libs/Notification/LocalNotification/BrowserNotifications.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.js b/src/libs/Notification/LocalNotification/BrowserNotifications.js index 345324bc613e..e55c0430fe17 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.js +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.js @@ -103,8 +103,10 @@ export default { let body; const isChatRoom = ReportUtils.isChatRoom(report); + const {person, message} = reportAction; const plainTextPerson = _.map(person, (f) => f.text).join(); + // Specifically target the comment part of the message const plainTextMessage = (_.find(message, (f) => f.type === 'COMMENT') || {}).text;