From acfbcb92f634728d3f40018befc37163439ee08a Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Tue, 23 Apr 2024 08:16:49 +0530 Subject: [PATCH] MM-57885: Do not mark channel as read on tab unload (#26811) This was spurious code left from old times. It's not needed now, and it was causing a race condition with the server side where we were setting the status to offline on websocket disconnect. So if this request reached the server after the status was set to offline, the status would reset to online. https://mattermost.atlassian.net/browse/MM-57885 ```release-note Fixed a bug where status would incorrectly get stuck to online after user closes the tab. ``` --- .../src/components/logged_in/index.ts | 3 +-- .../components/logged_in/logged_in.test.tsx | 26 ++++++++++++++++++- .../src/components/logged_in/logged_in.tsx | 2 -- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/webapp/channels/src/components/logged_in/index.ts b/webapp/channels/src/components/logged_in/index.ts index ebb093311f..3cb7f3120d 100644 --- a/webapp/channels/src/components/logged_in/index.ts +++ b/webapp/channels/src/components/logged_in/index.ts @@ -5,7 +5,7 @@ import {connect} from 'react-redux'; import {bindActionCreators} from 'redux'; import type {Dispatch} from 'redux'; -import {markChannelAsViewedOnServer, updateApproximateViewTime} from 'mattermost-redux/actions/channels'; +import {updateApproximateViewTime} from 'mattermost-redux/actions/channels'; import {autoUpdateTimezone} from 'mattermost-redux/actions/timezone'; import {getChannel, getCurrentChannelId, isManuallyUnread} from 'mattermost-redux/selectors/entities/channels'; import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general'; @@ -60,7 +60,6 @@ function mapDispatchToProps(dispatch: Dispatch) { actions: bindActionCreators({ autoUpdateTimezone, getChannelURLAction, - markChannelAsViewedOnServer, updateApproximateViewTime, }, dispatch), }; diff --git a/webapp/channels/src/components/logged_in/logged_in.test.tsx b/webapp/channels/src/components/logged_in/logged_in.test.tsx index 7decebd8a5..13527b7599 100644 --- a/webapp/channels/src/components/logged_in/logged_in.test.tsx +++ b/webapp/channels/src/components/logged_in/logged_in.test.tsx @@ -12,13 +12,24 @@ import BrowserStore from 'stores/browser_store'; import LoggedIn from 'components/logged_in/logged_in'; import type {Props} from 'components/logged_in/logged_in'; +import {fireEvent, renderWithContext, screen} from 'tests/react_testing_utils'; + jest.mock('actions/websocket_actions.jsx', () => ({ initialize: jest.fn(), + close: jest.fn(), })); BrowserStore.signalLogin = jest.fn(); describe('components/logged_in/LoggedIn', () => { + const originalFetch = global.fetch; + beforeAll(() => { + global.fetch = jest.fn(); + }); + afterAll(() => { + global.fetch = originalFetch; + }); + const children = {'Test'}; const baseProps: Props = { currentUser: {} as UserProfile, @@ -26,7 +37,6 @@ describe('components/logged_in/LoggedIn', () => { actions: { autoUpdateTimezone: jest.fn(), getChannelURLAction: jest.fn(), - markChannelAsViewedOnServer: jest.fn(), updateApproximateViewTime: jest.fn(), }, isCurrentChannelManuallyUnread: false, @@ -180,4 +190,18 @@ describe('components/logged_in/LoggedIn', () => { shallow({children}); expect(obj.emitBrowserFocus).toBeCalledTimes(1); }); + + it('should not make viewChannel call on unload', () => { + const props = { + ...baseProps, + mfaRequired: false, + showTermsOfService: false, + }; + + renderWithContext({children}); + expect(screen.getByText('Test')).toBeInTheDocument(); + + fireEvent(window, new Event('beforeunload')); + expect(fetch).not.toHaveBeenCalledWith('/api/v4/channels/members/me/view'); + }); }); diff --git a/webapp/channels/src/components/logged_in/logged_in.tsx b/webapp/channels/src/components/logged_in/logged_in.tsx index 1dc253e602..bbed0befae 100644 --- a/webapp/channels/src/components/logged_in/logged_in.tsx +++ b/webapp/channels/src/components/logged_in/logged_in.tsx @@ -36,7 +36,6 @@ export type Props = { actions: { autoUpdateTimezone: (deviceTimezone: string) => void; getChannelURLAction: (channelId: string, teamId: string, url: string) => void; - markChannelAsViewedOnServer: (channelId: string) => void; updateApproximateViewTime: (channelId: string) => void; }; showTermsOfService: boolean; @@ -192,7 +191,6 @@ export default class LoggedIn extends React.PureComponent { window.removeEventListener('beforeunload', this.handleBeforeUnload); if (document.cookie.indexOf('MMUSERID=') > -1 && this.props.currentChannelId && !this.props.isCurrentChannelManuallyUnread) { this.props.actions.updateApproximateViewTime(this.props.currentChannelId); - this.props.actions.markChannelAsViewedOnServer(this.props.currentChannelId); } WebSocketActions.close(); };