Skip to content

Commit

Permalink
fix: unread indicator logic
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Aug 27, 2024
1 parent 4873621 commit 0a29d99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 28 additions & 0 deletions projects/stream-chat-angular/src/lib/channel.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<DefaultStreamChatGenerics>,
});

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 () => {
Expand Down
8 changes: 6 additions & 2 deletions projects/stream-chat-angular/src/lib/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
})
Expand Down

0 comments on commit 0a29d99

Please sign in to comment.