From 95c3eaaaf60b3432e7977509c40b583eed2371f9 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 27 May 2022 10:53:17 -0600 Subject: [PATCH 1/7] refactor Report_TogglePinned --- src/libs/actions/Report.js | 25 ++++++++++++------------- src/libs/deprecatedAPI.js | 6 +++--- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index c64d9a630cbf..e3ad08769020 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -703,16 +703,6 @@ function updateReportWithNewAction( }); } -/** - * Updates a report in Onyx with a new pinned state. - * - * @param {Number} reportID - * @param {Boolean} isPinned - */ -function updateReportPinnedState(reportID, isPinned) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {isPinned}); -} - /** * Get the private pusher channel name for a Report. * @@ -1296,11 +1286,20 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false */ function togglePinnedState(report) { const pinnedValue = !report.isPinned; - updateReportPinnedState(report.reportID, pinnedValue); - DeprecatedAPI.Report_TogglePinned({ + + // Optimistically pin/unpin the report before we send out the command + const optimisticData = [ + { + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + value: pinnedValue, + }, + ]; + + API.write('TogglePinnedChat', { reportID: report.reportID, pinnedValue, - }); + }, {optimisticData}); } /** diff --git a/src/libs/deprecatedAPI.js b/src/libs/deprecatedAPI.js index d154ee6c0472..da6cd7879a08 100644 --- a/src/libs/deprecatedAPI.js +++ b/src/libs/deprecatedAPI.js @@ -336,8 +336,8 @@ function Report_GetHistory(parameters) { * @param {Boolean} parameters.pinnedValue * @returns {Promise} */ -function Report_TogglePinned(parameters) { - const commandName = 'Report_TogglePinned'; +function TogglePinnedChat(parameters) { + const commandName = 'TogglePinnedChat'; requireParameters(['reportID', 'pinnedValue'], parameters, commandName); return Network.post(commandName, parameters); @@ -963,7 +963,7 @@ export { RejectTransaction, Report_AddComment, Report_GetHistory, - Report_TogglePinned, + TogglePinnedChat, Report_EditComment, Report_UpdateLastRead, Report_UpdateNotificationPreference, From 1b3fece250cf07d4d5e4940468a3605873660330 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 09:00:03 -0600 Subject: [PATCH 2/7] rm pusher subscription --- src/libs/actions/Report.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index e3ad08769020..4c410fcb3315 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -18,6 +18,7 @@ import ROUTES from '../../ROUTES'; import NetworkConnection from '../NetworkConnection'; import Timing from './Timing'; import * as DeprecatedAPI from '../deprecatedAPI'; +import * as API from '../API'; import CONFIG from '../../CONFIG'; import CONST from '../../CONST'; import Log from '../Log'; @@ -782,9 +783,6 @@ function subscribeToUserEvents() { // Live-update a report's actions when an 'edit comment chunk' event is received. subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message), true); - - // Live-update a report's pinned state when a 'report toggle pinned' event is received. - subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_TOGGLE_PINNED, pushJSON => updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned)); } /** @@ -1295,7 +1293,7 @@ function togglePinnedState(report) { value: pinnedValue, }, ]; - + API.write('TogglePinnedChat', { reportID: report.reportID, pinnedValue, From 50673a44c2ce7c323455d22673b0e463a7ab7a4b Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 09:24:57 -0600 Subject: [PATCH 3/7] revert changes to deprecatedAPI, fix lint --- src/libs/actions/Report.js | 2 +- src/libs/deprecatedAPI.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 1509791af733..f10ae0546d75 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -709,7 +709,7 @@ function updateReportWithNewAction( * * @param {Object} report */ - function togglePinnedState(report) { +function togglePinnedState(report) { const pinnedValue = !report.isPinned; // Optimistically pin/unpin the report before we send out the command diff --git a/src/libs/deprecatedAPI.js b/src/libs/deprecatedAPI.js index f3266b921c82..67333741f0a7 100644 --- a/src/libs/deprecatedAPI.js +++ b/src/libs/deprecatedAPI.js @@ -336,8 +336,8 @@ function Report_GetHistory(parameters) { * @param {Boolean} parameters.pinnedValue * @returns {Promise} */ -function TogglePinnedChat(parameters) { - const commandName = 'TogglePinnedChat'; +function Report_TogglePinned(parameters) { + const commandName = 'Report_TogglePinned'; requireParameters(['reportID', 'pinnedValue'], parameters, commandName); return Network.post(commandName, parameters); @@ -953,7 +953,7 @@ export { RejectTransaction, Report_AddComment, Report_GetHistory, - TogglePinnedChat, + Report_TogglePinned, Report_EditComment, Report_UpdateLastRead, Report_UpdateNotificationPreference, From af583dc7547a7d4ed3721a5c80f984db6b596f1d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 10:09:11 -0600 Subject: [PATCH 4/7] rename pinnedValue --- src/libs/actions/Report.js | 53 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index f10ae0546d75..d38af403111f 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -704,29 +704,6 @@ function updateReportWithNewAction( }); } -/** - * Toggles the pinned state of the report. - * - * @param {Object} report - */ -function togglePinnedState(report) { - const pinnedValue = !report.isPinned; - - // Optimistically pin/unpin the report before we send out the command - const optimisticData = [ - { - onyxMethod: 'merge', - key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, - value: pinnedValue, - }, - ]; - - API.write('TogglePinnedChat', { - reportID: report.reportID, - pinnedValue, - }, {optimisticData}); -} - /** * Get the private pusher channel name for a Report. * @@ -778,13 +755,6 @@ function subscribeToUserEvents() { pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message), true, ); - - // Live-update a report's pinned state when a 'report toggle pinned' event is received. - PusherUtils.subscribeToPrivateUserChannelEvent( - Pusher.TYPE.REPORT_TOGGLE_PINNED, - currentUserAccountID, - pushJSON => togglePinnedState(pushJSON.report), - ); } /** @@ -1279,6 +1249,29 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false }); } +/** + * Toggles the pinned state of the report. + * + * @param {Object} report + */ +function togglePinnedState(report) { + const isPinned = !report.isPinned; + + // Optimistically pin/unpin the report before we send out the command + const optimisticData = [ + { + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + value: {isPinned}, + }, + ]; + + API.write('TogglePinnedChat', { + reportID: report.reportID, + isPinned, + }, {optimisticData}); +} + /** * Saves the comment left by the user as they are typing. By saving this data the user can switch between chats, close * tab, refresh etc without worrying about loosing what they typed out. From eca4c8727030e738d85c237b87e6d017515c6801 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 11:06:35 -0600 Subject: [PATCH 5/7] rename var --- src/libs/actions/Report.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d38af403111f..bccf499ee6cb 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1255,20 +1255,20 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false * @param {Object} report */ function togglePinnedState(report) { - const isPinned = !report.isPinned; + const pinnedValue = !report.isPinned; // Optimistically pin/unpin the report before we send out the command const optimisticData = [ { onyxMethod: 'merge', key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, - value: {isPinned}, + value: {isPinned: pinnedValue}, }, ]; API.write('TogglePinnedChat', { reportID: report.reportID, - isPinned, + pinnedValue, }, {optimisticData}); } From 4b32aa046a7f04368b1d0115d21b28211242a707 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 12:10:27 -0600 Subject: [PATCH 6/7] rm test for unused pusher event --- src/libs/Pusher/EventType.js | 1 - tests/actions/ReportTest.js | 18 ------------------ 2 files changed, 19 deletions(-) diff --git a/src/libs/Pusher/EventType.js b/src/libs/Pusher/EventType.js index 0b2954be187e..bff093b8e5d2 100644 --- a/src/libs/Pusher/EventType.js +++ b/src/libs/Pusher/EventType.js @@ -7,7 +7,6 @@ export default { REPORT_COMMENT_CHUNK: 'chunked-reportComment', REPORT_COMMENT_EDIT_CHUNK: 'chunked-reportCommentEdit', REPORT_COMMENT_EDIT: 'reportCommentEdit', - REPORT_TOGGLE_PINNED: 'reportTogglePinned', PREFERRED_LOCALE: 'preferredLocale', EXPENSIFY_CARD_UPDATE: 'expensifyCardUpdate', SCREEN_SHARE_REQUEST: 'screenshareRequest', diff --git a/tests/actions/ReportTest.js b/tests/actions/ReportTest.js index 7d1c569603ed..ecc22cdb8a45 100644 --- a/tests/actions/ReportTest.js +++ b/tests/actions/ReportTest.js @@ -154,24 +154,6 @@ describe('actions/Report', () => { .then(() => { // Before pusher event gets sent back, test that Onyx immediately updated the report pin state. expect(reportIsPinned).toEqual(true); - }) - .then(() => { - // We subscribed to the Pusher channel above and now we need to simulate a reportTogglePinned - // Pusher event so we can verify that pinning was handled correctly and merged into the report. - const channel = Pusher.getChannel(`private-encrypted-user-accountID-1${CONFIG.PUSHER.SUFFIX}`); - channel.emit(Pusher.TYPE.REPORT_TOGGLE_PINNED, { - reportID: REPORT_ID, - isPinned: true, - }); - - // Once an event is emitted to the Pusher channel we should see the report pin get updated - // by the Pusher callback and added to the storage so we must wait for promises to resolve again and - // then verify the data is in Onyx. - return waitForPromisesToResolve(); - }) - .then(() => { - // Make sure the pin state gets from the Pusher callback into Onyx. - expect(reportIsPinned).toEqual(true); }); }); From b68b3664d213e43c2230969f630d3f668730ed92 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 3 Jun 2022 12:19:26 -0600 Subject: [PATCH 7/7] update test --- tests/actions/ReportTest.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/actions/ReportTest.js b/tests/actions/ReportTest.js index ecc22cdb8a45..28526aac047b 100644 --- a/tests/actions/ReportTest.js +++ b/tests/actions/ReportTest.js @@ -126,7 +126,7 @@ describe('actions/Report', () => { }); }); - it('should update pins in Onyx when togglePinned event is handled via Pusher', () => { + it('should update pins in Onyx when togglePinned is called', () => { const TEST_USER_ACCOUNT_ID = 1; const TEST_USER_LOGIN = 'test@test.com'; const REPORT_ID = 1; @@ -143,16 +143,12 @@ describe('actions/Report', () => { // Set up Onyx with some test user data return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN) - .then(() => { - Report.subscribeToUserEvents(); - return waitForPromisesToResolve(); - }) .then(() => { Report.togglePinnedState(REPORT); return waitForPromisesToResolve(); }) .then(() => { - // Before pusher event gets sent back, test that Onyx immediately updated the report pin state. + // Test that Onyx immediately updated the report pin state. expect(reportIsPinned).toEqual(true); }); });