From d13f5e0bdcabc2d2356f694c5ea6f6db965a4bec Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Thu, 2 Nov 2023 15:13:49 +0100 Subject: [PATCH] fix: update user references on user.updated event --- .../src/lib/channel.service.spec.ts | 17 +++++++++ .../src/lib/channel.service.ts | 37 +++++++++++++++++++ .../src/lib/chat-client.service.spec.ts | 21 +++++++++++ .../src/lib/chat-client.service.ts | 8 +++- 4 files changed, 82 insertions(+), 1 deletion(-) 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 b4786b81..e05215bd 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.spec.ts @@ -2152,4 +2152,21 @@ describe('ChannelService', () => { expect(spy).toHaveBeenCalledWith(channel); expect(service.activeChannelLastReadMessageId).toBeUndefined(); }); + + it('should update user references on `user.updated` event', async () => { + await init(); + const spy = jasmine.createSpy(); + service.activeChannel$.subscribe(spy); + let activeChannel!: Channel; + service.activeChannel$.pipe(take(1)).subscribe((c) => (activeChannel = c!)); + activeChannel.state.members['jack'].user!.name = 'John'; + mockChatClient.activeChannels[activeChannel.cid] = activeChannel; + spy.calls.reset(); + events$.next({ eventType: 'user.updated' } as ClientEvent); + + const updatedChannel = spy.calls.mostRecent() + .args[0] as Channel; + + expect(updatedChannel.state.members['jack'].user!.name).toBe('John'); + }); }); diff --git a/projects/stream-chat-angular/src/lib/channel.service.ts b/projects/stream-chat-angular/src/lib/channel.service.ts index b3bea3ff..26b99a4c 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -1174,6 +1174,43 @@ export class ChannelService< this.handleRemovedFromChannelNotification(clientEvent); } }); + break; + } + case 'user.updated': { + this.ngZone.run(() => { + const updatedChannels = this.channelsSubject.getValue()?.map((c) => { + if (this.chatClientService.chatClient.activeChannels[c.cid]) { + return this.chatClientService.chatClient.activeChannels[c.cid]; + } else { + return c; + } + }); + this.channelsSubject.next(updatedChannels); + const activeChannel = this.activeChannelSubject.getValue(); + if (activeChannel) { + this.activeChannelSubject.next( + this.chatClientService.chatClient.activeChannels[ + activeChannel.cid + ] || activeChannel + ); + this.activeChannelMessagesSubject.next( + activeChannel.state.messages.map((m) => { + m.readBy = getReadBy(m, activeChannel); + return { ...m }; + }) + ); + const activeParentMessage = + this.activeParentMessageIdSubject.getValue(); + if (activeParentMessage) { + const messages = activeChannel.state.threads[activeParentMessage]; + this.activeThreadMessagesSubject.next([...messages]); + } + this.activeChannelPinnedMessagesSubject.next([ + ...activeChannel.state.pinnedMessages, + ]); + } + }); + break; } } } diff --git a/projects/stream-chat-angular/src/lib/chat-client.service.spec.ts b/projects/stream-chat-angular/src/lib/chat-client.service.spec.ts index 2a07ec79..4fe111ed 100644 --- a/projects/stream-chat-angular/src/lib/chat-client.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/chat-client.service.spec.ts @@ -407,4 +407,25 @@ describe('ChatClientService', () => { jasmine.objectContaining({ total_unread_count: 2 }) ); }); + + it('should update user object on `user.updated` event', () => { + const spy = jasmine.createSpy(); + service.user$.subscribe(spy); + + const updatedName = mockCurrentUser().name! + ' updated'; + const event = { + id: 'mockevent', + type: 'user.updated', + user: { + id: mockChatClient.user.id, + name: updatedName, + }, + } as any as Event; + mockChatClient.user.name = updatedName; + mockChatClient.handleEvent(event.type, event); + + expect(spy).toHaveBeenCalledWith( + jasmine.objectContaining({ name: updatedName }) + ); + }); }); diff --git a/projects/stream-chat-angular/src/lib/chat-client.service.ts b/projects/stream-chat-angular/src/lib/chat-client.service.ts index b1d61d62..0d6cd0d9 100644 --- a/projects/stream-chat-angular/src/lib/chat-client.service.ts +++ b/projects/stream-chat-angular/src/lib/chat-client.service.ts @@ -120,7 +120,6 @@ export class ChatClientService< ); throw error; } - console.log(this.chatClient.user); this.userSubject.next( this.chatClient.user ? { ...this.chatClient.user } : undefined ); @@ -251,5 +250,12 @@ export class ChatClientService< }); } } + if ( + e.type === 'user.updated' && + this.chatClient.user && + e.user?.id === this.chatClient.user.id + ) { + this.userSubject.next({ ...this.chatClient.user }); + } } }