diff --git a/projects/sample-app/src/app/custom-message/custom-message.component.ts b/projects/sample-app/src/app/custom-message/custom-message.component.ts index b972f7a2..07675207 100644 --- a/projects/sample-app/src/app/custom-message/custom-message.component.ts +++ b/projects/sample-app/src/app/custom-message/custom-message.component.ts @@ -1,5 +1,5 @@ import { Component, Input } from '@angular/core'; -import { FormatMessageResponse } from 'stream-chat'; +import { LocalMessage } from 'stream-chat'; import { ChatClientService } from 'stream-chat-angular'; @Component({ @@ -8,7 +8,7 @@ import { ChatClientService } from 'stream-chat-angular'; styleUrls: ['./custom-message.component.scss'], }) export class CustomMessageComponent { - @Input() message: FormatMessageResponse | undefined; + @Input() message: LocalMessage | undefined; userId: string | undefined; constructor(private chatClientService: ChatClientService) { diff --git a/projects/stream-chat-angular/src/lib/channel-preview/channel-preview.component.ts b/projects/stream-chat-angular/src/lib/channel-preview/channel-preview.component.ts index 15ae26ef..e28cb766 100644 --- a/projects/stream-chat-angular/src/lib/channel-preview/channel-preview.component.ts +++ b/projects/stream-chat-angular/src/lib/channel-preview/channel-preview.component.ts @@ -1,7 +1,7 @@ import { Component, Input, NgZone, OnDestroy, OnInit } from '@angular/core'; import { Subscription } from 'rxjs'; import { filter } from 'rxjs/operators'; -import { Channel, Event, FormatMessageResponse } from 'stream-chat'; +import { Channel, Event, LocalMessage } from 'stream-chat'; import { ChannelService } from '../channel.service'; import { getChannelDisplayText } from '../get-channel-display-text'; import { ChatClientService } from '../chat-client.service'; @@ -32,7 +32,7 @@ export class ChannelPreviewComponent implements OnInit, OnDestroy { latestMessageText: string = 'streamChat.Nothing yet...'; latestMessageStatus?: 'delivered' | 'read'; latestMessageTime?: string; - latestMessage?: FormatMessageResponse; + latestMessage?: LocalMessage; displayAs: 'text' | 'html'; userId?: string; private subscriptions: (Subscription | { unsubscribe: () => void })[] = []; @@ -156,7 +156,7 @@ export class ChannelPreviewComponent implements OnInit, OnDestroy { }); } - private setLatestMessage(message?: FormatMessageResponse) { + private setLatestMessage(message?: LocalMessage) { this.latestMessage = message; if (message?.deleted_at) { this.latestMessageText = 'streamChat.Message deleted'; diff --git a/projects/stream-chat-angular/src/lib/channel.service.spec.ts b/projects/stream-chat-angular/src/lib/channel.service.spec.ts index b65f17d8..7e6b0a5f 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.spec.ts @@ -8,7 +8,7 @@ import { ChannelResponse, ChannelSort, Event, - FormatMessageResponse, + LocalMessage, Message, ReactionResponse, SendMessageAPIResponse, @@ -1382,7 +1382,9 @@ describe('ChannelService', () => { void service.updateMessage(message); expect(mockChatClient.updateMessage).not.toHaveBeenCalledWith(); - expect(channel.sendMessage).not.toHaveBeenCalledWith(message as Message); + expect(channel.sendMessage).not.toHaveBeenCalledWith( + message as unknown as Message + ); }); it('should update message - #beforeUpdateMessage is provided', async () => { @@ -2356,7 +2358,7 @@ describe('ChannelService', () => { activeChannel.state.latestMessages = [ { id: 'last-read-message-id', - } as any as FormatMessageResponse, + } as any as LocalMessage, ]; service.setAsActiveChannel(activeChannel); diff --git a/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts b/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts index 4d50886c..45c685f8 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.thread.spec.ts @@ -119,6 +119,7 @@ describe('ChannelService - threads', () => { spyOn(channel, 'getReplies').and.resolveTo({ messages: replies, } as any as GetRepliesAPIResponse); + channel.state.threads[parentMessage.id] = replies; await service.setAsActiveParentMessage(parentMessage); expect(messagesSpy).toHaveBeenCalledWith(replies); @@ -252,6 +253,7 @@ describe('ChannelService - threads', () => { spyOn(channel, 'getReplies').and.resolveTo({ messages: replies, } as any as GetRepliesAPIResponse); + channel.state.threads[message.id] = replies; await service.setAsActiveParentMessage(message); let threadMessages!: StreamMessage[]; service.activeThreadMessages$.subscribe((m) => (threadMessages = m)); @@ -273,6 +275,7 @@ describe('ChannelService - threads', () => { messages: replies, } as any as GetRepliesAPIResponse); replies[0].id = 'firstreply'; + channel.state.threads[parentMessage.id] = replies; await service.setAsActiveParentMessage(parentMessage); let threadMessages!: StreamMessage[]; (channel.getReplies as jasmine.Spy).calls.reset(); @@ -492,6 +495,7 @@ describe('ChannelService - threads', () => { spyOn(activeChannel, 'getReplies').and.resolveTo({ messages: replies, } as any as GetRepliesAPIResponse); + activeChannel.state.threads[parentMessage.id] = replies; await service.setAsActiveParentMessage(parentMessage); const message = replies[1]; (activeChannel as MockChannel).handleEvent('reaction.new', { message }); diff --git a/projects/stream-chat-angular/src/lib/channel.service.ts b/projects/stream-chat-angular/src/lib/channel.service.ts index 785b21d6..8ffa8102 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -18,12 +18,11 @@ import { CustomReactionData, Event, EventTypes, - FormatMessageResponse, + LocalMessage, MemberFilters, Message, MessageResponse, ReactionResponse, - UpdatedMessage, UserResponse, } from 'stream-chat'; import { ChatClientService, ClientEvent } from './chat-client.service'; @@ -323,7 +322,7 @@ export class ChannelService { undefined ); private activeChannelMessagesSubject = new BehaviorSubject< - (StreamMessage | MessageResponse | FormatMessageResponse)[] + (StreamMessage | LocalMessage)[] >([]); private activeChannelPinnedMessagesSubject = new BehaviorSubject< StreamMessage[] @@ -335,7 +334,7 @@ export class ChannelService { string | undefined >(undefined); private activeThreadMessagesSubject = new BehaviorSubject< - (StreamMessage | MessageResponse | FormatMessageResponse)[] + (StreamMessage | LocalMessage)[] >([]); private jumpToMessageSubject = new BehaviorSubject<{ id?: string; @@ -637,10 +636,12 @@ export class ChannelService { this.activeParentMessageIdSubject.next(message.id); const activeChannel = this.activeChannelSubject.getValue(); if (loadMessagesForm === 'request') { - const result = await activeChannel?.getReplies(message.id, { + await activeChannel?.getReplies(message.id, { limit: this.messagePageSize, }); - this.activeThreadMessagesSubject.next(result?.messages || []); + this.activeThreadMessagesSubject.next( + activeChannel?.state.threads[message.id] || [] + ); } else { this.activeThreadMessagesSubject.next( activeChannel?.state.threads[message.id] || [] @@ -915,7 +916,7 @@ export class ChannelService { return this.resendMessage(message); } const response = await this.chatClientService.chatClient.updateMessage( - messageToUpdate as unknown as UpdatedMessage + messageToUpdate ); const channel = this.channelsSubject @@ -1750,7 +1751,7 @@ export class ChannelService { return; } // Get messages from state as message order could change, and message could've been deleted - const messages: FormatMessageResponse[] = isThreadReply + const messages: LocalMessage[] = isThreadReply ? channel.state.threads[event?.message?.parent_id || ''] : channel.state.messages; if (!messages) { @@ -1799,7 +1800,7 @@ export class ChannelService { } private formatMessage(message: MessageResponse) { - const m = message as unknown as FormatMessageResponse; + const m = message as unknown as LocalMessage; m.pinned_at = message.pinned_at ? new Date(message.pinned_at) : null; m.created_at = message.created_at ? new Date(message.created_at) @@ -1813,14 +1814,14 @@ export class ChannelService { } private isStreamMessage( - message: StreamMessage | FormatMessageResponse | MessageResponse + message: StreamMessage | LocalMessage | MessageResponse ): message is StreamMessage { return 'readBy' in message; } - private isFormatMessageResponse( - message: StreamMessage | FormatMessageResponse | MessageResponse - ): message is FormatMessageResponse { + private isLocalMessage( + message: StreamMessage | LocalMessage | MessageResponse + ): message is LocalMessage { return message.created_at instanceof Date; } @@ -2089,14 +2090,11 @@ export class ChannelService { } private transformToStreamMessage( - message: StreamMessage | MessageResponse | FormatMessageResponse, + message: StreamMessage | LocalMessage | MessageResponse, channel?: Channel ) { const isThreadMessage = !!message.parent_id; - if ( - this.isStreamMessage(message) && - this.isFormatMessageResponse(message) - ) { + if (this.isStreamMessage(message) && this.isLocalMessage(message)) { if (message.quoted_message) { message.quoted_message.translation = getMessageTranslation( message.quoted_message, @@ -2119,7 +2117,7 @@ export class ChannelService { this.chatClientService.chatClient.user ); } - if (this.isFormatMessageResponse(message)) { + if (this.isLocalMessage(message)) { (message as StreamMessage).readBy = isThreadMessage ? [] : channel diff --git a/projects/stream-chat-angular/src/lib/get-message-translation.ts b/projects/stream-chat-angular/src/lib/get-message-translation.ts index f09ae2d6..8012c9c0 100644 --- a/projects/stream-chat-angular/src/lib/get-message-translation.ts +++ b/projects/stream-chat-angular/src/lib/get-message-translation.ts @@ -1,14 +1,14 @@ import { Channel, - FormatMessageResponse, - MessageResponse, + LocalMessageBase, + MessageResponseBase, TranslationLanguages, UserResponse, } from 'stream-chat'; import { StreamMessage } from './types'; export const getMessageTranslation = ( - message?: StreamMessage | MessageResponse | FormatMessageResponse, + message?: StreamMessage | LocalMessageBase | MessageResponseBase, channel?: Channel, user?: UserResponse ) => { @@ -17,7 +17,7 @@ export const getMessageTranslation = ( (channel?.data?.auto_translation_language as TranslationLanguages); if (language && message?.i18n && message?.user?.id !== user?.id) { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string - return message.i18n[`${language}_text` as `${TranslationLanguages}_text`]; + return message.i18n[`${language}_text`]; } else { return undefined; } diff --git a/projects/stream-chat-angular/src/lib/message-actions-box/message-actions-box.component.spec.ts b/projects/stream-chat-angular/src/lib/message-actions-box/message-actions-box.component.spec.ts index 601392eb..9eceb9a5 100644 --- a/projects/stream-chat-angular/src/lib/message-actions-box/message-actions-box.component.spec.ts +++ b/projects/stream-chat-angular/src/lib/message-actions-box/message-actions-box.component.spec.ts @@ -7,7 +7,7 @@ import { } from '@angular/core/testing'; import { TranslateModule } from '@ngx-translate/core'; import { BehaviorSubject, Observable, of } from 'rxjs'; -import { Channel, MessageResponseBase, ReactionResponse } from 'stream-chat'; +import { Channel, LocalMessage, ReactionResponse } from 'stream-chat'; import { TextareaComponent } from '../message-input/textarea/textarea.component'; import { ChannelService } from '../channel.service'; import { ChatClientService } from '../chat-client.service'; @@ -189,7 +189,7 @@ describe('MessageActionsBoxComponent', () => { it(`shouldn't disable quote action, if message already has a quoted message`, () => { component.message = { ...component.message!, - quoted_message: mockMessage() as any as MessageResponseBase, + quoted_message: mockMessage() as any as LocalMessage, }; component.enabledActions = ['quote-message']; component.ngOnChanges({ diff --git a/projects/stream-chat-angular/src/lib/message/message.component.spec.ts b/projects/stream-chat-angular/src/lib/message/message.component.spec.ts index c5897225..349aa76f 100644 --- a/projects/stream-chat-angular/src/lib/message/message.component.spec.ts +++ b/projects/stream-chat-angular/src/lib/message/message.component.spec.ts @@ -5,7 +5,7 @@ import { tick, } from '@angular/core/testing'; -import { MessageResponseBase, UserResponse } from 'stream-chat'; +import { LocalMessage, UserResponse } from 'stream-chat'; import { StreamMessage } from '../types'; import { LoadingIndicatorComponent } from '../icon/loading-indicator/loading-indicator.component'; import { MessageComponent } from './message.component'; @@ -869,7 +869,7 @@ describe('MessageComponent', () => { describe('quoted message', () => { const quotedMessageContainerSelector = '[data-testid="quoted-message-container"]'; - let quotedMessage: StreamMessage; + let quotedMessage: LocalMessage; beforeEach(() => { quotedMessage = mockMessage(); @@ -879,11 +879,12 @@ describe('MessageComponent', () => { name: 'Sara', image: 'http://url/to/img', }; + quotedMessage.error = undefined; quotedMessage.attachments = [{ image_url: '1' }, { image_url: '2' }]; quotedMessage.text = 'This message was quoted'; component.message = { ...component.message!, - quoted_message: quotedMessage as any as MessageResponseBase, + quoted_message: quotedMessage, }; component.ngOnChanges({ message: {} as SimpleChange }); fixture.detectChanges(); diff --git a/projects/stream-chat-angular/src/lib/read-by.spec.ts b/projects/stream-chat-angular/src/lib/read-by.spec.ts index ef12eaa9..3b72a796 100644 --- a/projects/stream-chat-angular/src/lib/read-by.spec.ts +++ b/projects/stream-chat-angular/src/lib/read-by.spec.ts @@ -1,4 +1,4 @@ -import { Channel, FormatMessageResponse } from 'stream-chat'; +import { Channel, LocalMessage } from 'stream-chat'; import { getReadBy } from './read-by'; describe('getReadBy', () => { @@ -8,7 +8,7 @@ describe('getReadBy', () => { const message = { created_at: new Date('2021-09-14T13:08:30.004112Z'), user: sender, - } as FormatMessageResponse; + } as LocalMessage; const channel = { state: { read: {} }, } as Channel; diff --git a/projects/stream-chat-angular/src/lib/read-by.ts b/projects/stream-chat-angular/src/lib/read-by.ts index 1a6d170c..0c279e2f 100644 --- a/projects/stream-chat-angular/src/lib/read-by.ts +++ b/projects/stream-chat-angular/src/lib/read-by.ts @@ -1,6 +1,6 @@ -import { Channel, FormatMessageResponse, UserResponse } from 'stream-chat'; +import { Channel, LocalMessage, UserResponse } from 'stream-chat'; -export const getReadBy = (message: FormatMessageResponse, channel: Channel) => { +export const getReadBy = (message: LocalMessage, channel: Channel) => { const readBy: UserResponse[] = []; Object.keys(channel.state.read).forEach((key) => { if ( diff --git a/projects/stream-chat-angular/src/lib/types-custom.ts b/projects/stream-chat-angular/src/lib/types-custom.ts index 81267bd0..11427ee5 100644 --- a/projects/stream-chat-angular/src/lib/types-custom.ts +++ b/projects/stream-chat-angular/src/lib/types-custom.ts @@ -1,5 +1,6 @@ export interface DefaultChannelData { image?: string; + name?: string; } export interface DefaultAttachmentData { diff --git a/projects/stream-chat-angular/src/lib/types.ts b/projects/stream-chat-angular/src/lib/types.ts index f67fcedc..0e443177 100644 --- a/projects/stream-chat-angular/src/lib/types.ts +++ b/projects/stream-chat-angular/src/lib/types.ts @@ -7,8 +7,8 @@ import type { ChannelMemberResponse, CommandResponse, CustomMessageData, - FormatMessageResponse, - MessageResponseBase, + LocalMessage, + LocalMessageBase, ReactionGroupResponse, ReactionResponse, User, @@ -26,11 +26,11 @@ export type CustomTrigger = { }; }; -export type StreamMessage = FormatMessageResponse & { +export type StreamMessage = LocalMessage & { readBy: UserResponse[]; translation?: string; errorStatusCode?: number; - quoted_message?: MessageResponseBase & { translation?: string }; + quoted_message?: LocalMessage & { translation?: string }; }; export type AttachmentUploadErrorReason = @@ -461,7 +461,7 @@ export type CustomAutocomplete = { }; export type MessageTextContext = { - message: StreamMessage | undefined | MessageResponseBase; + message: StreamMessage | undefined | LocalMessageBase; isQuoted: boolean; shouldTranslate: boolean; };