Skip to content

Commit

Permalink
MM-57885: Do not mark channel as read on tab unload (#26811)
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
agnivade committed Apr 23, 2024
1 parent 2082510 commit acfbcb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 1 addition & 2 deletions webapp/channels/src/components/logged_in/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -60,7 +60,6 @@ function mapDispatchToProps(dispatch: Dispatch) {
actions: bindActionCreators({
autoUpdateTimezone,
getChannelURLAction,
markChannelAsViewedOnServer,
updateApproximateViewTime,
}, dispatch),
};
Expand Down
26 changes: 25 additions & 1 deletion webapp/channels/src/components/logged_in/logged_in.test.tsx
Expand Up @@ -12,21 +12,31 @@ 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 = <span>{'Test'}</span>;
const baseProps: Props = {
currentUser: {} as UserProfile,
mfaRequired: false,
actions: {
autoUpdateTimezone: jest.fn(),
getChannelURLAction: jest.fn(),
markChannelAsViewedOnServer: jest.fn(),
updateApproximateViewTime: jest.fn(),
},
isCurrentChannelManuallyUnread: false,
Expand Down Expand Up @@ -180,4 +190,18 @@ describe('components/logged_in/LoggedIn', () => {
shallow(<LoggedIn {...props}>{children}</LoggedIn>);
expect(obj.emitBrowserFocus).toBeCalledTimes(1);
});

it('should not make viewChannel call on unload', () => {
const props = {
...baseProps,
mfaRequired: false,
showTermsOfService: false,
};

renderWithContext(<LoggedIn {...props}>{children}</LoggedIn>);
expect(screen.getByText('Test')).toBeInTheDocument();

fireEvent(window, new Event('beforeunload'));
expect(fetch).not.toHaveBeenCalledWith('/api/v4/channels/members/me/view');
});
});
2 changes: 0 additions & 2 deletions webapp/channels/src/components/logged_in/logged_in.tsx
Expand Up @@ -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;
Expand Down Expand Up @@ -192,7 +191,6 @@ export default class LoggedIn extends React.PureComponent<Props> {
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();
};
Expand Down

0 comments on commit acfbcb9

Please sign in to comment.