@@ -23,7 +23,6 @@ import { SendMessageResponse } from '../../types/responses/SendMessageResponse';
2323import { ReplyStateService } from 'src/app/services/states/replyState.service' ;
2424import { Reply } from 'src/app/types/models/Reply' ;
2525import { GetChatMessagesResponse } from '../../types/responses/GetChatMessagesResponse' ;
26- import { RealtimeService } from '../../services/api/realtime.service' ;
2726import { BaseResponse } from '../../types/responses/BaseResponse' ;
2827import { GetUserChatsResponse } from '../../types/responses/GetUserChatsResponse' ;
2928import { DeleteMessageResponse } from '../../types/responses/DeleteMessageResponse' ;
@@ -46,7 +45,7 @@ export class ChatsComponent implements OnInit {
4645 private _apiBaseService : ApiBaseService ,
4746 public _modalWindowStateService : ModalWindowStateService ,
4847 public _replyStateService : ReplyStateService ,
49- private _realtimeService : RealtimeService ,
48+ // private _realtimeService: RealtimeService,
5049 private _defaultChatHelper : DefaultChatHelper
5150 ) { }
5251
@@ -153,8 +152,11 @@ export class ChatsComponent implements OnInit {
153152 } ) ;
154153
155154 this . connection . on ( 'PrivateChatDeletedAsync' , ( chatId : string ) => {
156- console . log ( `Private chat deleted: ${ chatId } ` ) ;
157- this . chats = this . chats . filter ( ( x ) => x . chatId !== chatId ) ;
155+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === chatId ) ;
156+
157+ if ( chatIndex === - 1 ) return ;
158+
159+ this . chats . splice ( chatIndex , 1 ) ;
158160
159161 if ( this . activeChatId === chatId ) {
160162 this . activeChat = this . _defaultChatHelper . getEmptyChat ( ) ;
@@ -191,7 +193,6 @@ export class ChatsComponent implements OnInit {
191193
192194 this . connection . invoke ( 'JoinGroup' , this . activeChat . chatId ) . then ( ( ) => {
193195 this . realTimeConnections . push ( this . activeChat . chatId ) ;
194- console . log ( `SignalR JoinGroup: ${ this . activeChat . chatId } ` ) ;
195196 } ) ;
196197 }
197198
@@ -227,10 +228,12 @@ export class ChatsComponent implements OnInit {
227228 return ;
228229 }
229230
231+ const messageId = crypto . randomUUID ( ) ;
230232 const createdAt = new Date ( ) . toISOString ( ) ;
231233 const sendMessageFormData = new FormData ( ) ;
232234
233235 sendMessageFormData . append ( 'text' , newMessageText ) ;
236+ sendMessageFormData . append ( 'messageId' , messageId ) ;
234237 sendMessageFormData . append ( 'chatId' , this . activeChatId ) ;
235238 sendMessageFormData . append ( 'inReplyToUser' , this . _replyStateService . reply ?. displayName ?? '' ) ;
236239 sendMessageFormData . append ( 'inReplyToText' , this . _replyStateService . reply ?. text ?? '' ) ;
@@ -240,6 +243,7 @@ export class ChatsComponent implements OnInit {
240243 }
241244
242245 const newMessage = new Message (
246+ messageId ,
243247 tokens . userId ,
244248 this . activeChatId ,
245249 tokens . userDisplayName ,
@@ -256,24 +260,34 @@ export class ChatsComponent implements OnInit {
256260
257261 this . messages . push ( newMessage ) ;
258262
263+ this . pushChatToTop ( this . activeChatId ) ;
264+
259265 this . _replyStateService . setReplyNull ( ) ;
260266
267+ this . scrollToEnd ( ) ;
268+
261269 const sendMessage$ = this . _messagesService . sendMessage ( sendMessageFormData ) ;
262270
263271 const response = await firstValueFrom < SendMessageResponse > ( sendMessage$ ) ;
264272
265- newMessage . messageId = response . messageModel . messageId ;
266- newMessage . attachmentUrl = response . messageModel . attachmentUrl ;
267- newMessage . createdAt = response . messageModel . createdAt ;
273+ newMessage . attachmentUrl = response . attachmentUrl ;
274+ newMessage . createdAt = response . createdAt ;
268275
269- const sendNotification$ = this . _realtimeService . sendRealtimeNewMessageNotification (
270- response . messageModel
271- ) ;
276+ if ( this . messageAttachment ) {
277+ this . clearAttachmentInput ( ) ;
278+ }
279+ }
272280
273- await firstValueFrom < BaseResponse > ( sendNotification$ ) ;
281+ pushChatToTop ( chatId : string ) {
282+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === chatId ) ;
274283
275- this . clearAttachmentInput ( ) ;
276- this . scrollToEnd ( ) ;
284+ if ( chatIndex === - 1 ) return ;
285+
286+ const chat = this . chats [ chatIndex ] ;
287+
288+ this . chats . splice ( chatIndex , 1 ) ;
289+
290+ this . chats . splice ( 0 , 0 , chat ) ;
277291 }
278292
279293 onReplyClick (
@@ -294,15 +308,16 @@ export class ChatsComponent implements OnInit {
294308 }
295309
296310 async deleteMessage ( message : Message ) {
297- const deleteMessageCommand : DeleteMessageCommand = {
298- chatId : message . chatId ,
299- messageId : message . messageId
300- } ;
311+ const deleteMessageCommand = new DeleteMessageCommand ( message . messageId , message . chatId ) ;
301312
302- const deleteMessageSub$ = this . _messagesService . deleteMessage ( deleteMessageCommand ) ;
303- const deleteMessageResult = await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ ) ;
313+ const messageIndex = this . messages . findIndex ( ( x ) => x . messageId === message . messageId ) ;
314+
315+ if ( messageIndex === - 1 ) return ;
304316
305- console . log ( deleteMessageResult . message ) ;
317+ this . messages . splice ( messageIndex , 1 ) ;
318+
319+ const deleteMessageSub$ = this . _messagesService . deleteMessage ( deleteMessageCommand ) ;
320+ await firstValueFrom < DeleteMessageResponse > ( deleteMessageSub$ ) ;
306321 }
307322
308323 async onJoinChatClick ( ) {
@@ -351,13 +366,9 @@ export class ChatsComponent implements OnInit {
351366 const div = event . currentTarget as HTMLDivElement ;
352367 this . chatFilter = div . innerText ;
353368
354- console . log ( this . chatFilter ) ;
355-
356369 const getChatsSub$ = this . _communitiesService . getUserChats ( ) ;
357370 const getChatsResult = await firstValueFrom < GetUserChatsResponse > ( getChatsSub$ ) ;
358371
359- console . log ( getChatsResult . chats ) ;
360-
361372 switch ( this . chatFilter ) {
362373 case 'All chats' :
363374 this . chats = getChatsResult . chats . filter ( ( x ) => ! x . isArchived ) ;
@@ -380,9 +391,7 @@ export class ChatsComponent implements OnInit {
380391
381392 async onLeaveChatClick ( ) {
382393 const leaveChatSub$ = this . _userChatsService . leaveCommunity ( this . activeChatId ) ;
383- const leaveChatResult = await firstValueFrom < BaseResponse > ( leaveChatSub$ ) ;
384-
385- console . log ( leaveChatResult . message ) ;
394+ await firstValueFrom < BaseResponse > ( leaveChatSub$ ) ;
386395
387396 this . activeChatId = '' ;
388397
@@ -391,9 +400,7 @@ export class ChatsComponent implements OnInit {
391400
392401 async onArchiveChatClick ( ) {
393402 const archiveChatSub$ = this . _userChatsService . archiveCommunity ( this . activeChatId ) ;
394- const archiveResult = await firstValueFrom < BaseResponse > ( archiveChatSub$ ) ;
395-
396- console . log ( archiveResult . message ) ;
403+ await firstValueFrom < BaseResponse > ( archiveChatSub$ ) ;
397404
398405 this . activeChatId = '' ;
399406
@@ -402,21 +409,27 @@ export class ChatsComponent implements OnInit {
402409
403410 private onMessageSendHandler ( message : Message ) {
404411 message . self = message . userId == this . userId ;
405- const chat = this . chats . filter ( ( x ) => x . chatId === message . chatId ) [ 0 ] ;
412+
413+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === message . chatId ) ;
414+
415+ if ( chatIndex === - 1 ) return ;
416+ const chat = this . chats [ chatIndex ] ;
417+
406418 chat . lastMessageAuthor = message . userDisplayName ;
407419 chat . lastMessageText = message . text ;
408420 chat . lastMessageTime = message . createdAt ;
409421 chat . lastMessageId = message . messageId ;
410- this . chats = this . chats . filter ( ( x ) => x . chatId !== message . chatId ) ;
411- this . chats = [ chat , ...this . chats ] ;
412422
413- const includesMessage = this . messages . some ( ( x ) => x . messageId === message . messageId ) ;
423+ this . chats . splice ( chatIndex , 1 ) ;
424+ this . chats . splice ( 0 , 0 , chat ) ;
425+
426+ if ( this . activeChatId !== message . chatId ) return ;
427+
428+ const messageIndex = this . messages . findIndex ( ( x ) => x . messageId === message . messageId ) ;
414429
415- if ( message . chatId === this . activeChatId && ! includesMessage ) {
430+ if ( messageIndex === - 1 ) {
416431 this . messages . push ( message ) ;
417432 }
418-
419- this . scrollToEnd ( ) ;
420433 }
421434
422435 private onMessageEditHandler ( notification : EditMessageNotification ) {
@@ -434,25 +447,25 @@ export class ChatsComponent implements OnInit {
434447 }
435448
436449 private onMessageDeleteHandler ( notification : DeleteMessageNotification ) {
437- console . log ( notification ) ;
450+ const chatIndex = this . chats . findIndex ( ( x ) => x . chatId === notification . chatId ) ;
438451
439- // const message = this.messages.filter((x) => x.messageId === notification.deletedMessageId)[0];
452+ if ( notification . isLastMessage && chatIndex !== - 1 ) {
453+ const updatedChat = this . chats [ chatIndex ] ;
440454
441- console . log ( `deleted message id: ${ notification . deletedMessageId } ` ) ;
442- console . log ( `last message id: ${ this . activeChat . lastMessageId } ` ) ;
443-
444- if ( notification . isLastMessage ) {
445- const updatedChat = this . chats . filter ( ( x ) => x . chatId == notification . chatId ) [ 0 ] ;
446455 updatedChat . lastMessageAuthor = notification . newLastMessageAuthor ;
447456 updatedChat . lastMessageText = notification . newLastMessageText ;
448457 updatedChat . lastMessageTime = notification . newLastMessageTime ;
449458 updatedChat . lastMessageId = notification . newLastMessageId ;
450-
451- console . log ( `updated chat: ${ JSON . stringify ( updatedChat ) } ` ) ;
452459 }
453460
454- if ( this . activeChatId === notification . chatId ) {
455- this . messages = this . messages . filter ( ( x ) => x . messageId !== notification . deletedMessageId ) ;
461+ if ( this . activeChatId !== notification . chatId ) return ;
462+
463+ const messageIndex = this . messages . findIndex (
464+ ( x ) => x . messageId === notification . deletedMessageId
465+ ) ;
466+
467+ if ( messageIndex !== - 1 ) {
468+ this . messages . splice ( messageIndex , 1 ) ;
456469 }
457470 }
458471
0 commit comments