Skip to content

Commit bac49da

Browse files
authored
Merge pull request #699 from GetStream/emit-active-channel-on-all-ws-events
Emit active channel on all ws events
2 parents 441df18 + 8ce7b0c commit bac49da

File tree

6 files changed

+43
-35
lines changed

6 files changed

+43
-35
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,12 +1318,7 @@ export class ChannelService {
13181318
),
13191319
);
13201320
this.activeChannelSubscriptions.push(
1321-
channel.on('capabilities.changed', (_) => {
1322-
this.activeChannelSubject.next(this.activeChannelSubject.getValue());
1323-
}),
1324-
);
1325-
this.activeChannelSubscriptions.push(
1326-
channel.on('channel.updated', (_) => {
1321+
channel.on(() => {
13271322
this.activeChannelSubject.next(this.activeChannelSubject.getValue());
13281323
}),
13291324
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { combineLatest, Observable, Subscription } from 'rxjs';
3-
import { map } from 'rxjs/operators';
3+
import { distinctUntilChanged, map } from 'rxjs/operators';
44
import { ChannelService } from '../channel.service';
55
import { ThemeService } from '../theme.service';
66
import { CustomTemplatesService } from '../custom-templates.service';
@@ -49,6 +49,7 @@ export class ChannelComponent {
4949
this.theme$ = this.themeService.theme$;
5050
this.isActiveChannel$ = this.channelService.activeChannel$.pipe(
5151
map((c) => !!c),
52+
distinctUntilChanged(),
5253
);
5354
}
5455
}

projects/stream-chat-angular/src/lib/message-input/message-input.component.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ describe('MessageInputComponent', () => {
516516
});
517517

518518
it('should not reset textarea and attachments if channel id remains the same', () => {
519+
attachmentService.resetAttachmentUploads.calls.reset();
519520
attachmentService.attachmentUploads$.next([
520521
{
521522
file: { name: 'img.png' } as any as File,
@@ -676,15 +677,10 @@ describe('MessageInputComponent', () => {
676677
expect(component.canSendMessages).toBeFalse();
677678

678679
mockActiveChannel$.next({
679-
data: { own_capabilities: [] },
680+
data: { own_capabilities: ['send-message'] },
680681
getConfig: () => ({ commands: [] }),
681682
} as any as Channel);
682683

683-
expect(component.canSendMessages).toBeFalse();
684-
685-
component.message = generateMockMessages()[0];
686-
component.ngOnChanges({ message: {} as SimpleChange });
687-
688684
expect(component.canSendMessages).toBeTrue();
689685
});
690686

projects/stream-chat-angular/src/lib/message-input/message-input.component.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ export class MessageInputComponent
149149
private subscriptions: Subscription[] = [];
150150
private hideNotification: (() => void) | undefined;
151151
private isViewInited = false;
152-
private channel: Channel | undefined;
152+
private channelId: string | undefined;
153+
private channelCapabilities: string[] = [];
153154
private sendMessageSubcription: Subscription | undefined;
154155
private readonly defaultTextareaPlaceholder = 'streamChat.Type your message';
155156
private readonly slowModeTextareaPlaceholder = 'streamChat.Slow Mode ON';
@@ -186,21 +187,34 @@ export class MessageInputComponent
186187
);
187188
this.subscriptions.push(
188189
this.channelService.activeChannel$.subscribe((channel) => {
189-
if (channel && this.channel && channel.id !== this.channel.id) {
190+
const newChannelId = channel?.id;
191+
const newCapabilities =
192+
(channel?.data?.own_capabilities as string[]) || [];
193+
194+
const channelIdChanged = this.channelId !== newChannelId;
195+
196+
const capabilitiesChanged =
197+
newCapabilities.join(',') !== this.channelCapabilities.join(',');
198+
199+
if (channelIdChanged) {
200+
this.channelId = newChannelId;
190201
this.textareaValue = '';
191202
this.attachmentService.resetAttachmentUploads();
192203
this.voiceRecorderService.isRecorderVisible$.next(false);
193204
}
194-
const capabilities = channel?.data?.own_capabilities as string[];
195-
if (capabilities) {
205+
206+
if (capabilitiesChanged) {
207+
this.channelCapabilities = newCapabilities;
196208
this.isFileUploadAuthorized =
197-
capabilities.indexOf('upload-file') !== -1;
198-
this.canSendLinks = capabilities.indexOf('send-links') !== -1;
199-
this.channel = channel;
209+
newCapabilities.indexOf('upload-file') !== -1;
210+
this.canSendLinks = newCapabilities.indexOf('send-links') !== -1;
200211
this.setCanSendMessages();
201212
}
202-
if (this.isViewInited) {
203-
this.cdRef.markForCheck();
213+
214+
if (channelIdChanged || capabilitiesChanged) {
215+
if (this.isViewInited) {
216+
this.cdRef.markForCheck();
217+
}
204218
}
205219
}),
206220
);
@@ -591,12 +605,11 @@ export class MessageInputComponent
591605
}
592606

593607
private setCanSendMessages() {
594-
const capabilities = this.channel?.data?.own_capabilities as string[];
595-
if (!capabilities) {
608+
if (!this.channelCapabilities || this.channelCapabilities.length === 0) {
596609
this.canSendMessages = false;
597610
} else {
598611
this.canSendMessages =
599-
capabilities.indexOf(
612+
this.channelCapabilities.indexOf(
600613
this.mode === 'main' ? 'send-message' : 'send-reply',
601614
) !== -1 || this.isUpdate;
602615
}

projects/stream-chat-angular/src/lib/message-list/message-list.component.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ export class MessageListComponent
220220
this.resetScrollState();
221221
this.setMessages$();
222222
this.channelId = channel?.id;
223+
this.newMessageSubscription?.unsubscribe();
224+
if (channel) {
225+
this.newMessageSubscription = channel.on('message.new', (event) => {
226+
if (!event.message) {
227+
return;
228+
} else {
229+
this.newMessageReceived(event.message);
230+
}
231+
});
232+
}
223233
if (this.isViewInited) {
224234
this.cdRef.markForCheck();
225235
}
@@ -289,16 +299,6 @@ export class MessageListComponent
289299
this.cdRef.markForCheck();
290300
}
291301
}
292-
this.newMessageSubscription?.unsubscribe();
293-
if (channel) {
294-
this.newMessageSubscription = channel.on('message.new', (event) => {
295-
if (!event.message) {
296-
return;
297-
} else {
298-
this.newMessageReceived(event.message);
299-
}
300-
});
301-
}
302302
}),
303303
);
304304
this.subscriptions.push(

projects/stream-chat-angular/src/lib/message/message.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ export class MessageComponent
511511
this.readByText = this.message?.readBy
512512
? listUsers(this.message.readBy, !hasMoreThan100Members, others)
513513
: '';
514+
if (this.isViewInited) {
515+
this.cdRef.markForCheck();
516+
}
514517
}
515518

516519
private setIsSentByCurrentUser() {

0 commit comments

Comments
 (0)