Skip to content

Commit

Permalink
fix: update user references on user.updated event
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Nov 2, 2023
1 parent 221fc63 commit d13f5e0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
17 changes: 17 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 @@ -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<DefaultStreamChatGenerics>;
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<DefaultStreamChatGenerics>;

expect(updatedChannel.state.members['jack'].user!.name).toBe('John');
});
});
37 changes: 37 additions & 0 deletions projects/stream-chat-angular/src/lib/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions projects/stream-chat-angular/src/lib/chat-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
);
});
});
8 changes: 7 additions & 1 deletion projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export class ChatClientService<
);
throw error;
}
console.log(this.chatClient.user);
this.userSubject.next(
this.chatClient.user ? { ...this.chatClient.user } : undefined
);
Expand Down Expand Up @@ -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 });
}
}
}

0 comments on commit d13f5e0

Please sign in to comment.