Skip to content

Commit a6dfaac

Browse files
committed
Merge branch 'master' into theming-v2
2 parents 262ff78 + c42182b commit a6dfaac

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

docusaurus/docs/Angular/concepts/translation.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ imports: [TranslateModule.forRoot(), StreamChatModule];
2424
export class AppModule {}
2525
```
2626

27+
:::important
28+
You should import the `TranslateModule.forRoot()` in a non-lazy loaded module (even if the chat application is displayed from a lazy loaded module).
29+
:::
30+
2731
### Set up the translations
2832

2933
Typically you will be doing it in your `AppComponent`:

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,33 @@ describe('ChannelService', () => {
785785
expect(channel.on).toHaveBeenCalledWith(jasmine.any(Function));
786786
}));
787787

788+
it(`shouldn't add channels twice if two notification events were received for the same channel`, fakeAsync(async () => {
789+
await init();
790+
const channel = generateMockChannels()[0];
791+
channel.cid = 'channel';
792+
channel.id = 'channel';
793+
channel.type = 'messaging';
794+
mockChatClient.channel.and.returnValue(channel);
795+
spyOn(channel, 'watch').and.callThrough();
796+
spyOn(channel, 'on').and.callThrough();
797+
const spy = jasmine.createSpy();
798+
service.channels$.subscribe(spy);
799+
events$.next({
800+
eventType: 'notification.added_to_channel',
801+
event: { channel: channel } as any as Event<DefaultStreamChatGenerics>,
802+
});
803+
events$.next({
804+
eventType: 'notification.message_new',
805+
event: { channel: channel } as any as Event<DefaultStreamChatGenerics>,
806+
});
807+
tick();
808+
809+
const channels = spy.calls.mostRecent().args[0] as Channel[];
810+
811+
expect(channels.filter((c) => c.cid === channel.cid).length).toBe(1);
812+
expect(channel.on).toHaveBeenCalledOnceWith(jasmine.any(Function));
813+
}));
814+
788815
it('should remove channel form the list if user is removed from channel', async () => {
789816
await init();
790817
let channel!: Channel<DefaultStreamChatGenerics>;

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,8 @@ export class ChannelService<
274274
const deletedChannels = currentChannels.filter(
275275
(c) => !channels?.find((channel) => channel.cid === c.cid)
276276
);
277-
this.addChannelsFromNotification(newChannels as ChannelResponse<T>[]);
277+
void this.addChannelsFromNotification(newChannels as ChannelResponse<T>[]);
278278
this.removeChannelsFromChannelList(deletedChannels.map((c) => c.cid));
279-
if (!newChannels.length && !deletedChannels.length) {
280-
this.channelsSubject.next(channels as Channel<T>[]);
281-
}
282279
};
283280

284281
private messageListSetter = (messages: StreamMessage<T>[]) => {
@@ -876,31 +873,38 @@ export class ChannelService<
876873

877874
private handleNewMessageNotification(clientEvent: ClientEvent<T>) {
878875
if (clientEvent.event.channel) {
879-
this.addChannelsFromNotification([clientEvent.event.channel]);
876+
void this.addChannelsFromNotification([clientEvent.event.channel]);
880877
}
881878
}
882879

883880
private handleAddedToChannelNotification(clientEvent: ClientEvent<T>) {
884881
if (clientEvent.event.channel) {
885-
this.addChannelsFromNotification([clientEvent.event.channel]);
882+
void this.addChannelsFromNotification([clientEvent.event.channel]);
886883
}
887884
}
888885

889-
private addChannelsFromNotification(channelResponses: ChannelResponse<T>[]) {
890-
const newChannels: Channel<T>[] = [];
886+
private async addChannelsFromNotification(
887+
channelResponses: ChannelResponse<T>[]
888+
) {
889+
let newChannels: Channel<T>[] = [];
890+
const watchRequests: Promise<any>[] = [];
891891
channelResponses.forEach((channelResponse) => {
892892
const channel = this.chatClientService.chatClient.channel(
893893
channelResponse.type,
894894
channelResponse.id
895895
);
896-
void channel.watch();
897-
this.watchForChannelEvents(channel);
896+
watchRequests.push(channel.watch());
898897
newChannels.push(channel);
899898
});
900-
this.channelsSubject.next([
901-
...newChannels,
902-
...(this.channelsSubject.getValue() || []),
903-
]);
899+
await Promise.all(watchRequests);
900+
const currentChannels = this.channelsSubject.getValue() || [];
901+
newChannels = newChannels.filter(
902+
(newChannel) => !currentChannels.find((c) => c.cid === newChannel.cid)
903+
);
904+
if (newChannels.length > 0) {
905+
newChannels.forEach((c) => this.watchForChannelEvents(c));
906+
this.channelsSubject.next([...newChannels, ...currentChannels]);
907+
}
904908
}
905909

906910
private removeChannelsFromChannelList(cids: string[]) {

projects/stream-chat-angular/src/lib/chat-client.service.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,23 @@ describe('ChatClientService', () => {
154154
});
155155

156156
it('should set SDK information', () => {
157-
const userAgent = `stream-chat-angular-${version}-${mockChatClient.getUserAgent()}`;
157+
const userAgent = `stream-chat-angular-${version}-${
158+
mockChatClient.getUserAgent() as string
159+
}`;
158160

159161
expect(mockChatClient.setUserAgent).toHaveBeenCalledWith(userAgent);
160162
});
161163

164+
it('should set SDK information only once', async () => {
165+
mockChatClient.getUserAgent.and.returnValue(
166+
'stream-chat-angular-stream-chat-javascript-client-browser-2.2.2'
167+
);
168+
mockChatClient.setUserAgent.calls.reset();
169+
await service.init(apiKey, userId, userToken);
170+
171+
expect(mockChatClient.setUserAgent).not.toHaveBeenCalled();
172+
});
173+
162174
it('should watch for added to channel events', () => {
163175
const spy = jasmine.createSpy();
164176
service.events$.subscribe(spy);

projects/stream-chat-angular/src/lib/chat-client.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ export class ChatClientService<
108108
throw error;
109109
}
110110
this.userSubject.next(this.chatClient.user);
111-
this.chatClient.setUserAgent(
112-
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
113-
);
114-
this.userSubject.next(this.chatClient.user);
111+
const sdkPrefix = 'stream-chat-angular';
112+
if (!this.chatClient.getUserAgent().includes(sdkPrefix)) {
113+
this.chatClient.setUserAgent(
114+
`${sdkPrefix}-${version}-${this.chatClient.getUserAgent()}`
115+
);
116+
}
115117
});
116118
const channels = await this.chatClient.queryChannels(
117119
{ invite: 'pending' } as any as ChannelFilters<T>, // TODO: find out why we need this typecast

projects/stream-chat-angular/src/lib/mocks/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export type MockStreamChatClient = {
281281
flagMessage: jasmine.Spy;
282282
setUserAgent: jasmine.Spy;
283283
queryUsers: jasmine.Spy;
284-
getUserAgent: () => string;
284+
getUserAgent: jasmine.Spy;
285285
getAppSettings: jasmine.Spy;
286286
disconnectUser: jasmine.Spy;
287287
queryChannels: jasmine.Spy;
@@ -313,6 +313,9 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
313313
},
314314
});
315315
const disconnectUser = jasmine.createSpy();
316+
const getUserAgent = jasmine
317+
.createSpy()
318+
.and.returnValue('stream-chat-javascript-client-browser-2.2.2');
316319
/* eslint-enable jasmine/no-unsafe-spy */
317320
const user = mockCurrentUser();
318321
const on = (name: EventTypes | Function, handler: () => {}) => {
@@ -333,7 +336,6 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
333336
eventHandlers['all']({ ...event, type: name });
334337
}
335338
};
336-
const getUserAgent = () => 'stream-chat-javascript-client-browser-2.2.2';
337339
const appSettings$ = new Subject<AppSettings>();
338340

339341
return {

0 commit comments

Comments
 (0)