diff --git a/projects/stream-chat-angular/src/lib/channel.service.spec.ts b/projects/stream-chat-angular/src/lib/channel.service.spec.ts index 38ddb05f..06009c29 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.spec.ts @@ -2365,6 +2365,22 @@ describe('ChannelService', () => { expect(service.activeChannelUnreadCount).toBe(0); }); + it(`should set last read message id to undefined if unread count is 0`, () => { + const activeChannel = generateMockChannels()[0]; + activeChannel.id = 'next-active-channel'; + activeChannel.state.read[user.id] = { + last_read: new Date(), + last_read_message_id: 'last-read-message-id', + unread_messages: 0, + user: user, + }; + + service.setAsActiveChannel(activeChannel); + + expect(service.activeChannelLastReadMessageId).toBe(undefined); + expect(service.activeChannelUnreadCount).toBe(0); + }); + it('should be able to select empty channel as active channel', () => { const channel = generateMockChannels()[0]; channel.id = 'new-empty-channel'; @@ -2569,6 +2585,18 @@ describe('ChannelService', () => { expect(service.activeChannelLastReadMessageId).toBe('last-read-message'); expect(service.activeChannelUnreadCount).toBe(12); + + events$.next({ + eventType: 'notification.mark_unread', + event: { + channel_id: service.activeChannel?.id, + unread_messages: 0, + last_read_message_id: 'last-read-message', + } as Event, + }); + + expect(service.activeChannelLastReadMessageId).toBe(undefined); + expect(service.activeChannelUnreadCount).toBe(0); }); it('should halt marking the channel as read if an unread call was made in that session', async () => { diff --git a/projects/stream-chat-angular/src/lib/channel.service.ts b/projects/stream-chat-angular/src/lib/channel.service.ts index e23ad653..a27cdd02 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -571,13 +571,14 @@ export class ChannelService< const readState = channel.state.read[this.chatClientService.chatClient.user?.id || '']; this.activeChannelLastReadMessageId = readState?.last_read_message_id; + this.activeChannelUnreadCount = readState?.unread_messages || 0; if ( channel.state.latestMessages[channel.state.latestMessages.length - 1] - ?.id === this.activeChannelLastReadMessageId + ?.id === this.activeChannelLastReadMessageId || + this.activeChannelUnreadCount === 0 ) { this.activeChannelLastReadMessageId = undefined; } - this.activeChannelUnreadCount = readState?.unread_messages || 0; this.watchForActiveChannelEvents(channel); this.addChannel(channel); this.activeChannelSubject.next(channel); @@ -1572,6 +1573,9 @@ export class ChannelService< this.ngZone.run(() => { this.activeChannelLastReadMessageId = e.last_read_message_id; this.activeChannelUnreadCount = e.unread_messages; + if (this.activeChannelUnreadCount === 0) { + this.activeChannelLastReadMessageId = undefined; + } this.activeChannelSubject.next(this.activeChannel); }); })