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/src/libs/actions/Report.js b/src/libs/actions/Report.js index 1dd7a21b4685..bccf499ee6cb 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -17,6 +17,7 @@ import Visibility from '../Visibility'; import ROUTES from '../../ROUTES'; 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'; @@ -703,16 +704,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. * @@ -764,11 +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 => updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned)); } /** @@ -1270,11 +1256,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: {isPinned: pinnedValue}, + }, + ]; + + API.write('TogglePinnedChat', { reportID: report.reportID, pinnedValue, - }); + }, {optimisticData}); } /** diff --git a/tests/actions/ReportTest.js b/tests/actions/ReportTest.js index 7d1c569603ed..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,34 +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. - 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. + // Test that Onyx immediately updated the report pin state. expect(reportIsPinned).toEqual(true); }); });