Skip to content

Commit

Permalink
fix: mark as read messages sent by current user (#2211)
Browse files Browse the repository at this point in the history
### 🎯 Goal

Adjusts Channel behavior to mark as read messages sent by current user
as well, this adjustment is made to match the behavior of Angular SDK.

---------

Co-authored-by: MartinCupela <[email protected]>
  • Loading branch information
arnautov-anton and MartinCupela authored Dec 12, 2023
1 parent 409db05 commit 38f2363
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ const ChannelInner = <
mainChannelUpdated = false;
}

if (mainChannelUpdated && event.message?.user?.id !== client.userID) {
if (mainChannelUpdated) {
if (!document.hidden) {
markReadThrottled();
} else if (channelConfig?.read_events && !channel.muteStatus().muted) {
Expand Down Expand Up @@ -536,6 +536,14 @@ const ChannelInner = <
),
type: 'initStateFromChannel',
});

/**
* TODO: maybe pass last_read to the countUnread method to get proper value
* combined with channel.countUnread adjustment (_countMessageAsUnread)
* to allow counting own messages too
*
* const lastRead = channel.state.read[client.userID as string].last_read;
*/
if (channel.countUnread() > 0) markRead();
// The more complex sync logic is done in Chat
document.addEventListener('visibilitychange', onVisibilityChange);
Expand Down
14 changes: 14 additions & 0 deletions src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,20 @@ describe('Channel', () => {
await waitFor(() => expect(markReadSpy).toHaveBeenCalledWith());
});

it('should mark the channel as read if the new message author is the current user and the user is looking at the page', async () => {
const { channel, chatClient } = await initClient();
const markReadSpy = jest.spyOn(channel, 'markRead');

const message = generateMessage({ user: generateUser() });
const dispatchMessageEvent = createChannelEventDispatcher({ message }, chatClient, channel);

renderComponent({ channel, chatClient }, () => {
dispatchMessageEvent();
});

await waitFor(() => expect(markReadSpy).toHaveBeenCalledWith());
});

it('title of the page should include the unread count if the user is not looking at the page when a new message event happens', async () => {
const { channel, chatClient } = await initClient();
const unreadAmount = 1;
Expand Down

0 comments on commit 38f2363

Please sign in to comment.