@@ -13,7 +13,7 @@ import { UserChatsService } from '../../services/api/user-chats.service';
1313import { ValidationService } from '../../services/messenger/validation.service' ;
1414import * as signalR from '@microsoft/signalr' ;
1515import { DeleteMessageNotification } from '../../types/notifications/DeleteMessageNotification' ;
16- import { BehaviorSubject , firstValueFrom , distinctUntilKeyChanged , last } from 'rxjs' ;
16+ import { BehaviorSubject , firstValueFrom , distinctUntilKeyChanged } from 'rxjs' ;
1717import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours' ;
1818import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand' ;
1919import ApiBaseService from 'src/app/services/api/api-base.service' ;
@@ -356,30 +356,36 @@ export class ChatsComponent implements OnInit {
356356 if ( messageIndex === - 1 ) return ;
357357
358358 const lastMessage = this . messages [ this . messages . length - 1 ] ;
359+
359360 const deletedMessage = this . messages . splice ( messageIndex , 1 ) [ 0 ] ;
360- const lastMessageAfterDeleting = this . messages [ this . messages . length - 1 ] ;
361- const isDeleteMessageLast = lastMessage . messageId === deletedMessage . messageId ;
362- const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === message . chatId )
361+
362+ const newLastMessage = this . messages [ this . messages . length - 1 ] ;
363+
364+ const lastMessageDeleted = lastMessage . messageId === deletedMessage . messageId ;
365+
366+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === message . chatId ) ;
367+
363368 const chat = this . chats [ chatIndex ] ;
364369
365- if ( isDeleteMessageLast && lastMessageAfterDeleting ) {
366- chat . lastMessageId = lastMessageAfterDeleting . messageId ;
367- chat . lastMessageText = lastMessageAfterDeleting . text ;
368- chat . lastMessageAuthor = lastMessageAfterDeleting . displayName ;
369- chat . lastMessageTime = lastMessageAfterDeleting . createdAt ;
370+ if ( lastMessageDeleted && newLastMessage ) {
371+ chat . lastMessageId = newLastMessage . messageId ;
372+ chat . lastMessageText = newLastMessage . text ;
373+ chat . lastMessageAuthor = newLastMessage . displayName ;
374+ chat . lastMessageTime = newLastMessage . createdAt ;
370375
371376 const deleteMessageSub$ = this . _messagesService . deleteMessage ( deleteMessageCommand ) ;
372377 await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ ) ;
373- return
378+ return ;
374379 }
375- else if ( isDeleteMessageLast && ! lastMessageAfterDeleting ) {
376- chat . lastMessageId = "" ;
377- chat . lastMessageText = "" ;
378- chat . lastMessageAuthor = "" ;
379- chat . lastMessageTime = "" ;
380+
381+ if ( lastMessageDeleted && ! newLastMessage ) {
382+ chat . lastMessageId = '' ;
383+ chat . lastMessageText = '' ;
384+ chat . lastMessageAuthor = '' ;
385+ chat . lastMessageTime = '' ;
380386
381387 const deleteMessageSub$ = this . _messagesService . deleteMessage ( deleteMessageCommand ) ;
382- await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ )
388+ await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ ) ;
383389 return ;
384390 }
385391
@@ -516,37 +522,25 @@ export class ChatsComponent implements OnInit {
516522 }
517523
518524 private onMessageDeleteHandler ( notification : DeleteMessageNotification ) {
519- const message = this . messages . find ( x => x . messageId === notification . deletedMessageId ) ;
520-
521- if ( message === undefined ) return ;
525+ const selfMessage = notification . userId === this . userId ;
522526
523- const isDeletedMessageSelf = message . userId === this . userId ;
527+ if ( selfMessage ) return ;
524528
525- if ( isDeletedMessageSelf ) return ;
529+ if ( this . activeChatId === notification . chatId ) {
530+ const messageIndex = this . messages . findIndex (
531+ ( x ) => x . messageId === notification . deletedMessageId
532+ ) ;
533+ this . messages . splice ( messageIndex , 1 ) ;
534+ }
526535
527536 const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === notification . chatId ) ;
528-
529- const lastMessage = this . messages [ this . messages . length - 1 ] ;
530- const messageIndex = this . messages . findIndex ( x => x . messageId === notification . deletedMessageId ) ;
531- const deletedMessage = this . messages . splice ( messageIndex , 1 ) [ 0 ] ;
532- const lastMessageAfterDeleting = this . messages [ this . messages . length - 1 ] ;
533- const isDeleteMessageLast = lastMessage . messageId === deletedMessage . messageId ;
534537 const chat = this . chats [ chatIndex ] ;
535538
536- if ( isDeleteMessageLast && lastMessageAfterDeleting ) {
537- chat . lastMessageId = lastMessageAfterDeleting . messageId ;
538- chat . lastMessageText = lastMessageAfterDeleting . text ;
539- chat . lastMessageAuthor = lastMessageAfterDeleting . displayName ;
540- chat . lastMessageTime = lastMessageAfterDeleting . createdAt ;
541-
542- return
543- }
544- else if ( isDeleteMessageLast && ! lastMessageAfterDeleting ) {
545- chat . lastMessageId = "" ;
546- chat . lastMessageText = "" ;
547- chat . lastMessageAuthor = "" ;
548- chat . lastMessageTime = "" ;
549-
539+ if ( notification . isLastMessage ) {
540+ chat . lastMessageId = notification . newLastMessageId ;
541+ chat . lastMessageText = notification . newLastMessageText ;
542+ chat . lastMessageAuthor = notification . newLastMessageDisplayName ;
543+ chat . lastMessageTime = notification . newLastMessageTime ;
550544 return ;
551545 }
552546 }
0 commit comments