Skip to content

Commit

Permalink
fix: mark all active channels as read only if notification.mark_read …
Browse files Browse the repository at this point in the history
…event's unread_channels is 0 (#955)
  • Loading branch information
MartinCupela authored Apr 15, 2022
1 parent f8b1faa commit 8d2e3ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
this.mutedUsers = event.me.mutes;
}

if (event.type === 'notification.mark_read') {
if (event.type === 'notification.mark_read' && event.unread_channels === 0) {
const activeChannelKeys = Object.keys(this.activeChannels);
activeChannelKeys.forEach((activeChannelKey) => (this.activeChannels[activeChannelKey].state.unreadCount = 0));
}
Expand Down
32 changes: 26 additions & 6 deletions test/unit/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,39 @@ describe('Client userMuteStatus', function () {
expect(client.userMuteStatus('mute4')).not.to.be.ok;
expect(client.userMuteStatus('missingUser')).not.to.be.ok;
});
});

it('should update all active channel unread count to 0 when notification.mark_read event is called', function () {
describe('Client active channels cache', () => {
const client = new StreamChat('', '');
const user = { id: 'user' };

client.connectUser = async () => {
client.user = user;
client.wsPromise = Promise.resolve();
};
beforeEach(() => {
client.activeChannels = { vish: { state: { unreadCount: 1 } }, vish2: { state: { unreadCount: 2 } } };
});

const countUnreadChannels = (channels) =>
Object.values(channels).reduce((prevSum, currSum) => prevSum + currSum.state.unreadCount, 0);

it('should mark all active channels as read on notification.mark_read event if event.unread_channels is 0', function () {
client.dispatchEvent({
type: 'notification.mark_read',
unread_channels: 0,
});

const unreadCountSum = Object.values(client.activeChannels).reduce(
(prevSum, currSum) => prevSum + currSum.state.unreadCount,
0,
);
expect(countUnreadChannels(client.activeChannels)).to.be.equal(0);
});

it('should not mark any active channel as read on notification.mark_read event if event.unread_channels > 0', function () {
client.dispatchEvent({
type: 'notification.mark_read',
unread_channels: 1,
});

expect(unreadCountSum).to.be.equal(0);
expect(countUnreadChannels(client.activeChannels)).to.be.equal(3);
});
});

Expand Down

0 comments on commit 8d2e3ca

Please sign in to comment.