From 1fbfe1494f839c408eb0e13df978168162e82f2d Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Tue, 27 Aug 2024 16:44:43 +0200 Subject: [PATCH] fix: unread indicator logic --- .../src/lib/channel.service.spec.ts | 28 +++++++++++++++++++ .../src/lib/channel.service.ts | 8 ++++-- 2 files changed, 34 insertions(+), 2 deletions(-) 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 ce6dcb5d..10c20d43 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.spec.ts @@ -2328,6 +2328,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'; @@ -2532,6 +2548,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 1f47d21b..32fede50 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -557,13 +557,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); @@ -1513,6 +1514,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); }); })