Skip to content

Commit 5c65dae

Browse files
committed
fix: don't display read count in read by tootltip if channel has more than 100 members
1 parent 11cfe6f commit 5c65dae

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

projects/stream-chat-angular/src/assets/i18n/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@ export const en = {
130130
"You can't uplod more than {{max}} attachments",
131131
'You currently have {{count}} attachments, the maximum is {{max}}':
132132
'You currently have {{count}} attachments, the maximum is {{max}}',
133+
'and others': 'and others',
133134
},
134135
};

projects/stream-chat-angular/src/lib/list-users.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,19 @@ describe('listUsers', () => {
4848

4949
expect(listUsers(readBy)).toBe('Bob, Sophie, Jack, Rose, John +1');
5050
});
51+
52+
it(`shouldn't display number if #displayRestCount is set to false`, () => {
53+
const readBy = [
54+
{ id: 'id1', name: 'Bob' },
55+
{ id: 'id2', name: 'Sophie' },
56+
{ id: 'id3', name: 'Jack' },
57+
{ id: 'id4', name: 'Rose' },
58+
{ id: 'id5', name: 'John' },
59+
{ id: 'id6', name: 'Adam' },
60+
];
61+
62+
expect(listUsers(readBy, false, 'and more')).toBe(
63+
'Bob, Sophie, Jack, Rose, John and more'
64+
);
65+
});
5166
});

projects/stream-chat-angular/src/lib/list-users.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { UserResponse } from 'stream-chat';
22

3-
export const listUsers = (users: UserResponse[]) => {
3+
export const listUsers = (
4+
users: UserResponse[],
5+
displayRestCount = true,
6+
othersText = ''
7+
) => {
48
let outStr = '';
59

610
const slicedArr = users.map((item) => item.name || item.id).slice(0, 5);
@@ -9,7 +13,7 @@ export const listUsers = (users: UserResponse[]) => {
913
const commaSeparatedUsers = slicedArr.join(', ');
1014
outStr = commaSeparatedUsers;
1115
if (restLength > 0) {
12-
outStr += ` +${restLength}`;
16+
outStr += displayRestCount ? ` +${restLength}` : ` ${othersText}`;
1317
}
1418

1519
return outStr;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ describe('MessageComponent', () => {
5959
let setAsActiveParentMessageSpy: jasmine.Spy;
6060
let jumpToMessageSpy: jasmine.Spy;
6161
let bouncedMessage$: BehaviorSubject<StreamMessage | undefined>;
62+
const mockChannel = {
63+
cid: 'messaging:general',
64+
data: {
65+
member_count: 5,
66+
},
67+
};
6268

6369
beforeEach(() => {
6470
resendMessageSpy = jasmine.createSpy('resendMessage');
@@ -91,6 +97,13 @@ describe('MessageComponent', () => {
9197
{
9298
provide: ChannelService,
9399
useValue: {
100+
_activeChannel$: of(mockChannel),
101+
get activeChannel$() {
102+
return this._activeChannel$;
103+
},
104+
set activeChannel$(value) {
105+
this._activeChannel$ = value;
106+
},
94107
resendMessage: resendMessageSpy,
95108
setAsActiveParentMessage: setAsActiveParentMessageSpy,
96109
jumpToMessage: jumpToMessageSpy,
@@ -129,6 +142,7 @@ describe('MessageComponent', () => {
129142
queryMessageOptionsButton = () =>
130143
nativeElement.querySelector('[data-testid="message-options-button"]');
131144
message = mockMessage();
145+
message.cid = mockChannel.cid;
132146
component.message = message;
133147
component.ngOnChanges({ message: {} as SimpleChange });
134148
component.ngAfterViewInit();

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
NgxFloatUiContentComponent,
4040
NgxFloatUiLooseDirective,
4141
} from 'ngx-float-ui';
42+
import { TranslateService } from '@ngx-translate/core';
4243

4344
type MessagePart = {
4445
content: string;
@@ -125,6 +126,7 @@ export class MessageComponent
125126
private showMessageMenuTimeout?: ReturnType<typeof setTimeout>;
126127
private shouldPreventMessageMenuClose = false;
127128
private _visibleMessageActionsCount = 0;
129+
private channelMemberCount?: number;
128130

129131
constructor(
130132
private chatClientService: ChatClientService,
@@ -134,7 +136,8 @@ export class MessageComponent
134136
private dateParser: DateParserService,
135137
private messageService: MessageService,
136138
public messageActionsService: MessageActionsService,
137-
private ngZone: NgZone
139+
private ngZone: NgZone,
140+
private translateService: TranslateService
138141
) {
139142
this.displayAs = this.messageService.displayAs;
140143
}
@@ -180,6 +183,26 @@ export class MessageComponent
180183
}
181184
})
182185
);
186+
this.subscriptions.push(
187+
this.channelService.activeChannel$.subscribe((activeChannel) => {
188+
const newChannelMemberCount = activeChannel?.data?.member_count;
189+
if (newChannelMemberCount !== this.channelMemberCount) {
190+
const shouldUpdateText =
191+
this.channelMemberCount !== undefined &&
192+
newChannelMemberCount != undefined &&
193+
((this.channelMemberCount <= 1000 && newChannelMemberCount > 100) ||
194+
(this.channelMemberCount > 100 && newChannelMemberCount <= 100));
195+
this.channelMemberCount = activeChannel?.data?.member_count;
196+
if (
197+
this.message &&
198+
this.message.cid === activeChannel?.cid &&
199+
shouldUpdateText
200+
) {
201+
this.updateReadByText();
202+
}
203+
}
204+
})
205+
);
183206
}
184207

185208
ngOnChanges(changes: SimpleChanges): void {
@@ -194,9 +217,7 @@ export class MessageComponent
194217
: [];
195218
this.setIsSentByCurrentUser();
196219
this.setLastReadUser();
197-
this.readByText = this.message?.readBy
198-
? listUsers(this.message.readBy)
199-
: '';
220+
this.updateReadByText();
200221
this.isOnlyReadByMe = !!(
201222
this.message &&
202223
this.message.readBy &&
@@ -567,6 +588,16 @@ export class MessageComponent
567588
return content;
568589
}
569590

591+
private updateReadByText() {
592+
const others = this.translateService.instant(
593+
'streamChat.and others'
594+
) as string;
595+
const hasMoreThan100Members = (this.channelMemberCount ?? 0) > 100;
596+
this.readByText = this.message?.readBy
597+
? listUsers(this.message.readBy, !hasMoreThan100Members, others)
598+
: '';
599+
}
600+
570601
private setIsSentByCurrentUser() {
571602
this.isSentByCurrentUser = this.message?.user?.id === this.userId;
572603
}

0 commit comments

Comments
 (0)