diff --git a/projects/stream-chat-angular/src/lib/read-by.spec.ts b/projects/stream-chat-angular/src/lib/read-by.spec.ts index ef12eaa9..1d1dd387 100644 --- a/projects/stream-chat-angular/src/lib/read-by.spec.ts +++ b/projects/stream-chat-angular/src/lib/read-by.spec.ts @@ -53,4 +53,51 @@ describe('getReadBy', () => { expect(result.find((i) => i.id === 'id3')).toBeDefined(); expect(result.find((i) => i.id === sender.id)).toBeUndefined(); }); + + it('should handle null last_read values gracefully', () => { + const sender = { id: 'sender', name: 'Tess' }; + + const message = { + created_at: new Date('2021-09-14T13:08:30.004112Z'), + user: sender, + } as FormatMessageResponse; + + const channel = { + state: { + read: { + id1: { + last_read: new Date('2021-09-14T13:08:33.004112Z'), + user: { + id: 'id1', + name: 'John', + }, + unread_messages: 2, + }, + id2: { + last_read: null, + user: { + id: 'id2', + name: 'Alice', + }, + unread_messages: 2, + }, + id3: { + last_read: new Date('2021-09-14T13:08:30.004112Z'), + user: { + id: 'id3', + name: 'Bob', + }, + unread_messages: 2, + }, + }, + }, + } as unknown as Channel; + + const result = getReadBy(message, channel); + + expect(result.length).toBe(2); + expect(result.find((i) => i.id === 'id1')).toBeDefined(); + expect(result.find((i) => i.id === 'id3')).toBeDefined(); + expect(result.find((i) => i.id === 'id2')).toBeUndefined(); + }); }); diff --git a/projects/stream-chat-angular/src/lib/read-by.ts b/projects/stream-chat-angular/src/lib/read-by.ts index 1a6d170c..8646d290 100644 --- a/projects/stream-chat-angular/src/lib/read-by.ts +++ b/projects/stream-chat-angular/src/lib/read-by.ts @@ -3,9 +3,10 @@ import { Channel, FormatMessageResponse, UserResponse } from 'stream-chat'; export const getReadBy = (message: FormatMessageResponse, channel: Channel) => { const readBy: UserResponse[] = []; Object.keys(channel.state.read).forEach((key) => { + const lastRead = channel.state.read[key].last_read; if ( - channel.state.read[key].last_read.getTime() >= - message.created_at.getTime() && + lastRead && + lastRead.getTime() >= message.created_at.getTime() && message.user?.id !== key ) { readBy.push(channel.state.read[key].user);