@@ -298,10 +298,13 @@ export class ChatsComponent implements OnInit {
298298 }
299299
300300 async deleteMessage ( message : Message ) {
301- const deleteMessageCommand : DeleteMessageCommand = {
302- chatId : message . chatId ,
303- messageId : message . messageId
304- } ;
301+ const deleteMessageCommand = new DeleteMessageCommand ( message . messageId , message . chatId ) ;
302+
303+ const messageIndex = this . messages . findIndex ( ( x ) => x . messageId === message . messageId ) ;
304+
305+ if ( messageIndex === - 1 ) return ;
306+
307+ this . messages . splice ( messageIndex , 1 ) ;
305308
306309 const deleteMessageSub$ = this . _messagesService . deleteMessage ( deleteMessageCommand ) ;
307310 const deleteMessageResult = await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ ) ;
@@ -405,16 +408,28 @@ export class ChatsComponent implements OnInit {
405408 }
406409
407410 private onMessageSendHandler ( message : Message ) {
408- console . log ( JSON . stringify ( message ) ) ;
411+ // console.log(JSON.stringify(message));
409412
410413 message . self = message . userId == this . userId ;
411- const chat = this . chats . filter ( ( x ) => x . chatId === message . chatId ) [ 0 ] ;
414+
415+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === message . chatId ) ;
416+
417+ if ( chatIndex === - 1 ) return ;
418+
419+ console . log ( 'chats: ' + JSON . stringify ( this . chats ) ) ;
420+
421+ const chat = this . chats [ chatIndex ] ;
422+
412423 chat . lastMessageAuthor = message . userDisplayName ;
413424 chat . lastMessageText = message . text ;
414425 chat . lastMessageTime = message . createdAt ;
415426 chat . lastMessageId = message . messageId ;
416- this . chats = this . chats . filter ( ( x ) => x . chatId !== message . chatId ) ;
417- this . chats = [ chat , ...this . chats ] ;
427+
428+ this . chats . splice ( chatIndex , 1 ) ;
429+
430+ console . log ( 'chats excluded: ' + JSON . stringify ( this . chats ) ) ;
431+
432+ this . chats . splice ( 0 , 0 , chat ) ;
418433
419434 const includesMessage = this . messages . some ( ( x ) => x . messageId === message . messageId ) ;
420435
@@ -445,8 +460,11 @@ export class ChatsComponent implements OnInit {
445460 console . log ( `deleted message id: ${ notification . deletedMessageId } ` ) ;
446461 console . log ( `last message id: ${ this . activeChat . lastMessageId } ` ) ;
447462
448- if ( notification . isLastMessage ) {
449- const updatedChat = this . chats . filter ( ( x ) => x . chatId == notification . chatId ) [ 0 ] ;
463+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === notification . chatId ) ;
464+
465+ if ( notification . isLastMessage && chatIndex !== - 1 ) {
466+ const updatedChat = this . chats [ chatIndex ] ;
467+
450468 updatedChat . lastMessageAuthor = notification . newLastMessageAuthor ;
451469 updatedChat . lastMessageText = notification . newLastMessageText ;
452470 updatedChat . lastMessageTime = notification . newLastMessageTime ;
@@ -455,8 +473,14 @@ export class ChatsComponent implements OnInit {
455473 console . log ( `updated chat: ${ JSON . stringify ( updatedChat ) } ` ) ;
456474 }
457475
458- if ( this . activeChatId === notification . chatId ) {
459- this . messages = this . messages . filter ( ( x ) => x . messageId !== notification . deletedMessageId ) ;
476+ if ( this . activeChatId !== notification . chatId ) return ;
477+
478+ const messageIndex = this . messages . findIndex (
479+ ( x ) => x . messageId === notification . deletedMessageId
480+ ) ;
481+
482+ if ( messageIndex !== - 1 ) {
483+ this . messages . splice ( messageIndex , 1 ) ;
460484 }
461485 }
462486
0 commit comments