From 7c32c74ed2c7e0fbb28d3a43703380ab11e7faca Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Thu, 21 Sep 2023 16:27:54 +0200 Subject: [PATCH] fix: misplaced messages in active thread --- .../src/lib/channel.service.thread.spec.ts | 22 +++++++++++++++++++ .../src/lib/channel.service.ts | 9 +++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts b/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts index 149dce4e..006cd6cb 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts @@ -335,6 +335,28 @@ describe('ChannelService - threads', () => { expect(activeChannel.markRead).toHaveBeenCalledWith(); }); + it('should only add new message to thread, if the parent id is the same as active thread id', async () => { + await init(); + const spy = jasmine.createSpy(); + const parentMessage = mockMessage(); + await service.setAsActiveParentMessage(parentMessage); + service.activeThreadMessages$.subscribe(spy); + spy.calls.reset(); + let activeChannel!: Channel; + service.activeChannel$.subscribe((c) => (activeChannel = c!)); + const newMessage = mockMessage(); + newMessage.parent_id = 'not' + parentMessage.id; + activeChannel.state.threads = { + [newMessage.parent_id]: [newMessage], + [parentMessage.id]: [], + }; + (activeChannel as MockChannel).handleEvent('message.new', { + message: newMessage, + }); + + expect(spy).not.toHaveBeenCalled(); + }); + it('should watch for message update events', async () => { await init(); const parentMessage = mockMessage(); diff --git a/projects/stream-chat-angular/src/lib/channel.service.ts b/projects/stream-chat-angular/src/lib/channel.service.ts index b1050eca..7b877041 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -1120,9 +1120,12 @@ export class ChannelService< channel.on('message.new', (event) => { this.ngZone.run(() => { event.message && event.message.parent_id - ? this.activeThreadMessagesSubject.next([ - ...channel.state.threads[event.message.parent_id], - ]) + ? event.message.parent_id === + this.activeParentMessageIdSubject.getValue() + ? this.activeThreadMessagesSubject.next([ + ...channel.state.threads[event.message.parent_id], + ]) + : null : this.activeChannelMessagesSubject.next([ ...channel.state.messages, ]);