Skip to content

Commit b67855a

Browse files
committed
Merge branch 'master' into theming-v2
2 parents aa29bec + bbd0464 commit b67855a

17 files changed

+173
-54
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,20 @@ describe('AttachmentListComponent', () => {
327327
} as any as Attachment<DefaultStreamChatGenerics>;
328328
component.messageId = 'message-id';
329329
component.attachments = [attachment];
330+
component.parentMessageId = 'parent-id';
330331
component.ngOnChanges();
331332
fixture.detectChanges();
332333

333334
const actions = queryActions();
334335
actions[1].click();
335336

336-
expect(sendAction).toHaveBeenCalledWith('message-id', {
337-
image_action: 'shuffle',
338-
});
337+
expect(sendAction).toHaveBeenCalledWith(
338+
'message-id',
339+
{
340+
image_action: 'shuffle',
341+
},
342+
'parent-id'
343+
);
339344
});
340345

341346
describe('should display image attachment', () => {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class AttachmentListComponent implements OnChanges {
2828
* The id of the message the attachments belong to
2929
*/
3030
@Input() messageId: string | undefined;
31+
/**
32+
* The parent id of the message the attachments belong to
33+
*/
34+
@Input() parentMessageId: string | undefined;
3135
/**
3236
* The attachments to display
3337
*/
@@ -132,9 +136,13 @@ export class AttachmentListComponent implements OnChanges {
132136
}
133137

134138
sendAction(action: Action) {
135-
void this.channelService.sendAction(this.messageId!, {
136-
[action.name!]: action.value!,
137-
});
139+
void this.channelService.sendAction(
140+
this.messageId!,
141+
{
142+
[action.name!]: action.value!,
143+
},
144+
this.parentMessageId
145+
);
138146
}
139147

140148
trackByActionValue(_: number, item: Action) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ describe('AttachmentService', () => {
297297
it('should create attachmentUploads from attachments', () => {
298298
const imageFile = { name: 'flower.png' };
299299
const dataFile = { name: 'note.txt', size: 3272969 };
300+
const videoFile = { name: 'cute.mov', size: 45367543 };
300301
const result = [
301302
{
302303
file: imageFile,
@@ -310,6 +311,12 @@ describe('AttachmentService', () => {
310311
url: 'url/to/data',
311312
type: 'file',
312313
},
314+
{
315+
file: videoFile,
316+
state: 'success',
317+
url: 'url/to/video',
318+
type: 'video',
319+
},
313320
];
314321
const attachments = [
315322
{ fallback: 'flower.png', image_url: 'url/to/img', type: 'image' },
@@ -319,6 +326,12 @@ describe('AttachmentService', () => {
319326
asset_url: 'url/to/data',
320327
type: 'file',
321328
},
329+
{
330+
title: 'cute.mov',
331+
file_size: 45367543,
332+
asset_url: 'url/to/video',
333+
type: 'video',
334+
},
322335
];
323336
const spy = jasmine.createSpy();
324337
service.attachmentUploads$.subscribe(spy);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ export class AttachmentService {
177177
name: attachment.fallback,
178178
} as File,
179179
});
180-
} else if (attachment.type === 'file') {
180+
} else if (attachment.type === 'file' || attachment.type === 'video') {
181181
attachmentUploads.push({
182182
url: attachment.asset_url,
183183
state: 'success',
184184
file: {
185185
name: attachment.title,
186186
size: attachment.file_size,
187187
} as File,
188-
type: 'file',
188+
type: attachment.type,
189189
});
190190
}
191191
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,8 @@ describe('ChannelService', () => {
455455
spy.calls.reset();
456456
let activeChannel!: Channel<DefaultStreamChatGenerics>;
457457
service.activeChannel$.subscribe((c) => (activeChannel = c!));
458-
const message = {
459-
...activeChannel.state.messages[activeChannel.state.messages.length - 1],
460-
};
458+
const message =
459+
activeChannel.state.messages[activeChannel.state.messages.length - 1];
461460
message.deleted_at = new Date().toISOString();
462461
(activeChannel as MockChannel).handleEvent('message.deleted', { message });
463462

@@ -1002,7 +1001,10 @@ describe('ChannelService', () => {
10021001
spyOn(channel.state, 'removeMessage');
10031002
await service.sendAction('1', { image_action: 'send' });
10041003

1005-
expect(channel.state.removeMessage).toHaveBeenCalledWith({ id: '1' });
1004+
expect(channel.state.removeMessage).toHaveBeenCalledWith({
1005+
id: '1',
1006+
parent_id: undefined,
1007+
});
10061008
});
10071009

10081010
it('should update message', () => {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,17 @@ describe('ChannelService - threads', () => {
529529
{} as any as SendMessageAPIResponse<DefaultStreamChatGenerics>
530530
);
531531
spyOn(channel.state, 'removeMessage');
532-
await service.sendAction(replies[replies.length - 1].id, {
533-
image_action: 'send',
534-
});
532+
await service.sendAction(
533+
replies[replies.length - 1].id,
534+
{
535+
image_action: 'send',
536+
},
537+
parentMessage.id
538+
);
535539

536540
expect(channel.state.removeMessage).toHaveBeenCalledWith({
537541
id: replies[replies.length - 1].id,
542+
parent_id: parentMessage.id,
538543
});
539544
});
540545

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,13 @@ export class ChannelService<
705705
* [Runs a message action](https://getstream.io/chat/docs/rest/#messages-runmessageaction) in the current channel. Updates the message list based on the action result (if no message is returned, the message will be removed from the message list).
706706
* @param messageId
707707
* @param formData
708+
* @param parentMessageId
708709
*/
709-
async sendAction(messageId: string, formData: Record<string, string>) {
710+
async sendAction(
711+
messageId: string,
712+
formData: Record<string, string>,
713+
parentMessageId?: string
714+
) {
710715
const channel = this.activeChannelSubject.getValue()!;
711716
const response = await channel.sendAction(messageId, formData);
712717
if (response?.message) {
@@ -721,21 +726,16 @@ export class ChannelService<
721726
])
722727
: this.activeChannelMessagesSubject.next([...channel.state.messages]);
723728
} else {
724-
channel.state.removeMessage({ id: messageId });
725-
if (
726-
this.activeChannelMessagesSubject
727-
.getValue()
728-
.find((m) => m.id === messageId)
729-
) {
730-
this.activeChannelMessagesSubject.next([...channel.state.messages]);
731-
} else if (
732-
this.activeThreadMessagesSubject
733-
.getValue()
734-
.find((m) => m.id === messageId)
735-
) {
729+
channel.state.removeMessage({
730+
id: messageId,
731+
parent_id: parentMessageId,
732+
});
733+
if (parentMessageId) {
736734
this.activeThreadMessagesSubject.next(
737735
channel.state.threads[this.activeParentMessageIdSubject.getValue()!]
738736
);
737+
} else {
738+
this.activeChannelMessagesSubject.next([...channel.state.messages]);
739739
}
740740
}
741741
}
@@ -1007,14 +1007,21 @@ export class ChannelService<
10071007
private messageUpdated(event: Event<T>) {
10081008
this.ngZone.run(() => {
10091009
const isThreadReply = event.message && event.message.parent_id;
1010-
const messages = isThreadReply
1011-
? this.activeThreadMessagesSubject.getValue()
1012-
: this.activeChannelMessagesSubject.getValue();
1010+
const channel = this.activeChannelSubject.getValue();
1011+
if (!channel) {
1012+
return;
1013+
}
1014+
// Get messages from state as message order could change, and message could've been deleted
1015+
const messages: FormatMessageResponse<T>[] = isThreadReply
1016+
? channel.state.threads[event?.message?.parent_id || '']
1017+
: channel.state.messages;
1018+
if (!messages) {
1019+
return;
1020+
}
10131021
const messageIndex = messages.findIndex(
1014-
(m) => m.id === event.message?.id
1022+
(m) => m.id === event?.message?.id
10151023
);
1016-
if (messageIndex !== -1 && event.message) {
1017-
messages[messageIndex] = event.message;
1024+
if (messageIndex !== -1) {
10181025
isThreadReply
10191026
? this.activeThreadMessagesSubject.next([...messages])
10201027
: this.activeChannelMessagesSubject.next([...messages]);

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,28 @@ describe('ChatClientService', () => {
2929
await service.init(apiKey, userId, userToken);
3030
});
3131

32-
it('should connect user', () => {
32+
it('should connect user', async () => {
33+
mockChatClient.connectUser.calls.reset();
34+
await service.init(apiKey, userId, userToken);
35+
36+
expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey);
37+
const spy = jasmine.createSpy();
38+
service.appSettings$.subscribe(spy);
39+
const userSpy = jasmine.createSpy();
40+
service.user$.subscribe(userSpy);
41+
42+
expect(spy).toHaveBeenCalledWith(undefined);
43+
expect(userSpy).toHaveBeenCalledWith(mockCurrentUser());
44+
expect(mockChatClient.connectUser).toHaveBeenCalledWith(
45+
{ id: userId },
46+
userToken
47+
);
48+
});
49+
50+
it('should connect user - guest user', async () => {
51+
mockChatClient.connectUser.calls.reset();
52+
await service.init(apiKey, userId, 'guest');
53+
3354
expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey);
3455
const spy = jasmine.createSpy();
3556
service.appSettings$.subscribe(spy);
@@ -38,6 +59,8 @@ describe('ChatClientService', () => {
3859

3960
expect(spy).toHaveBeenCalledWith(undefined);
4061
expect(userSpy).toHaveBeenCalledWith(mockCurrentUser());
62+
expect(mockChatClient.connectUser).not.toHaveBeenCalled();
63+
expect(mockChatClient.setGuestUser).toHaveBeenCalledWith({ id: userId });
4164
});
4265

4366
it(`should notify if connection wasn't successful`, async () => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,31 @@ export class ChatClientService<
8383
* Creates a [`StreamChat`](https://github.com/GetStream/stream-chat-js/blob/668b3e5521339f4e14fc657834531b4c8bf8176b/src/client.ts#L124) instance using the provided `apiKey`, and connects a user with the given meta data and token. More info about [connecting users](https://getstream.io/chat/docs/javascript/init_and_users/?language=javascript) can be found in the platform documentation.
8484
* @param apiKey
8585
* @param userOrId
86-
* @param userTokenOrProvider
86+
* @param userTokenOrProvider You can provide a token, or the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
8787
*/
8888
async init(
8989
apiKey: string,
9090
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
91-
userTokenOrProvider: TokenOrProvider
91+
userTokenOrProvider: TokenOrProvider | 'guest'
9292
): ConnectAPIResponse<T> {
9393
this.chatClient = StreamChat.getInstance<T>(apiKey);
9494
this.chatClient.devToken;
9595
let result;
9696
await this.ngZone.runOutsideAngular(async () => {
9797
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
9898
try {
99-
result = await this.chatClient.connectUser(user, userTokenOrProvider);
99+
result =
100+
userTokenOrProvider === 'guest'
101+
? await this.chatClient.setGuestUser(user)
102+
: await this.chatClient.connectUser(user, userTokenOrProvider);
100103
} catch (error) {
101104
this.notificationService.addPermanentNotification(
102105
'streamChat.Error connecting to chat, refresh the page to try again.',
103106
'error'
104107
);
105108
throw error;
106109
}
110+
this.userSubject.next(this.chatClient.user);
107111
this.chatClient.setUserAgent(
108112
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
109113
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ describe('MessageActionsBoxComponent', () => {
176176
expect(spy).toHaveBeenCalledWith(component.message);
177177
});
178178

179-
it('should disable quote action, if message already has a quoted message', () => {
179+
it(`shouldn't disable quote action, if message already has a quoted message`, () => {
180180
component.message = {
181181
...component.message!,
182182
quoted_message: mockMessage() as any as MessageResponseBase,
@@ -188,7 +188,7 @@ describe('MessageActionsBoxComponent', () => {
188188
});
189189
fixture.detectChanges();
190190

191-
expect(queryQuoteAction()).toBeNull();
191+
expect(queryQuoteAction()).not.toBeNull();
192192
});
193193

194194
// eslint-disable-next-line jasmine/no-disabled-tests

0 commit comments

Comments
 (0)