Skip to content

Commit 0a29d99

Browse files
committed
fix: unread indicator logic
1 parent 4873621 commit 0a29d99

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,22 @@ describe('ChannelService', () => {
23652365
expect(service.activeChannelUnreadCount).toBe(0);
23662366
});
23672367

2368+
it(`should set last read message id to undefined if unread count is 0`, () => {
2369+
const activeChannel = generateMockChannels()[0];
2370+
activeChannel.id = 'next-active-channel';
2371+
activeChannel.state.read[user.id] = {
2372+
last_read: new Date(),
2373+
last_read_message_id: 'last-read-message-id',
2374+
unread_messages: 0,
2375+
user: user,
2376+
};
2377+
2378+
service.setAsActiveChannel(activeChannel);
2379+
2380+
expect(service.activeChannelLastReadMessageId).toBe(undefined);
2381+
expect(service.activeChannelUnreadCount).toBe(0);
2382+
});
2383+
23682384
it('should be able to select empty channel as active channel', () => {
23692385
const channel = generateMockChannels()[0];
23702386
channel.id = 'new-empty-channel';
@@ -2569,6 +2585,18 @@ describe('ChannelService', () => {
25692585

25702586
expect(service.activeChannelLastReadMessageId).toBe('last-read-message');
25712587
expect(service.activeChannelUnreadCount).toBe(12);
2588+
2589+
events$.next({
2590+
eventType: 'notification.mark_unread',
2591+
event: {
2592+
channel_id: service.activeChannel?.id,
2593+
unread_messages: 0,
2594+
last_read_message_id: 'last-read-message',
2595+
} as Event<DefaultStreamChatGenerics>,
2596+
});
2597+
2598+
expect(service.activeChannelLastReadMessageId).toBe(undefined);
2599+
expect(service.activeChannelUnreadCount).toBe(0);
25722600
});
25732601

25742602
it('should halt marking the channel as read if an unread call was made in that session', async () => {

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,14 @@ export class ChannelService<
571571
const readState =
572572
channel.state.read[this.chatClientService.chatClient.user?.id || ''];
573573
this.activeChannelLastReadMessageId = readState?.last_read_message_id;
574+
this.activeChannelUnreadCount = readState?.unread_messages || 0;
574575
if (
575576
channel.state.latestMessages[channel.state.latestMessages.length - 1]
576-
?.id === this.activeChannelLastReadMessageId
577+
?.id === this.activeChannelLastReadMessageId ||
578+
this.activeChannelUnreadCount === 0
577579
) {
578580
this.activeChannelLastReadMessageId = undefined;
579581
}
580-
this.activeChannelUnreadCount = readState?.unread_messages || 0;
581582
this.watchForActiveChannelEvents(channel);
582583
this.addChannel(channel);
583584
this.activeChannelSubject.next(channel);
@@ -1572,6 +1573,9 @@ export class ChannelService<
15721573
this.ngZone.run(() => {
15731574
this.activeChannelLastReadMessageId = e.last_read_message_id;
15741575
this.activeChannelUnreadCount = e.unread_messages;
1576+
if (this.activeChannelUnreadCount === 0) {
1577+
this.activeChannelLastReadMessageId = undefined;
1578+
}
15751579
this.activeChannelSubject.next(this.activeChannel);
15761580
});
15771581
})

0 commit comments

Comments
 (0)