Skip to content

Commit 00e0f52

Browse files
committed
fix #442
1 parent 0bcba8e commit 00e0f52

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

MangoAPI.Client/src/app/components/chats/chats.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ <h5>Chats</h5>
270270
data-target="#createGroup"
271271
data-toggle="modal"
272272
role="button"
273-
(click)="deleteMessage(message)"
273+
(click)="onDeleteMessageClick(message)"
274274
>
275275
<img
276276
alt="Delete Message Icon"

MangoAPI.Client/src/app/components/chats/chats.component.ts

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UserChatsService } from '../../services/api/user-chats.service';
1313
import { ValidationService } from '../../services/messenger/validation.service';
1414
import * as signalR from '@microsoft/signalr';
1515
import { DeleteMessageNotification } from '../../types/notifications/DeleteMessageNotification';
16-
import { BehaviorSubject, firstValueFrom, distinctUntilKeyChanged } from 'rxjs';
16+
import { BehaviorSubject, firstValueFrom, distinctUntilKeyChanged, last } from 'rxjs';
1717
import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours';
1818
import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand';
1919
import ApiBaseService from 'src/app/services/api/api-base.service';
@@ -348,14 +348,40 @@ export class ChatsComponent implements OnInit {
348348
await this.onSendMessageClick().then((r) => r);
349349
}
350350

351-
async deleteMessage(message: Message) {
351+
async onDeleteMessageClick(message: Message) {
352352
const deleteMessageCommand = new DeleteMessageCommand(message.messageId, message.chatId);
353353

354354
const messageIndex = this.messages.findIndex((x) => x.messageId === message.messageId);
355355

356356
if (messageIndex === -1) return;
357357

358-
this.messages.splice(messageIndex, 1);
358+
const lastMessage = this.messages[this.messages.length - 1];
359+
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)
363+
const chat = this.chats[chatIndex];
364+
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+
371+
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
372+
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
373+
return
374+
}
375+
else if (isDeleteMessageLast && !lastMessageAfterDeleting) {
376+
chat.lastMessageId = "";
377+
chat.lastMessageText = "";
378+
chat.lastMessageAuthor = "";
379+
chat.lastMessageTime = "";
380+
381+
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
382+
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$)
383+
return;
384+
}
359385

360386
const deleteMessageSub$ = this._messagesService.deleteMessage(deleteMessageCommand);
361387
await firstValueFrom<DeleteMessageResponse>(deleteMessageSub$);
@@ -490,25 +516,38 @@ export class ChatsComponent implements OnInit {
490516
}
491517

492518
private onMessageDeleteHandler(notification: DeleteMessageNotification) {
493-
const chatIndex = this.chats.findIndex((x) => x.chatId === notification.chatId);
519+
const message = this.messages.find(x => x.messageId === notification.deletedMessageId);
494520

495-
if (notification.isLastMessage && chatIndex !== -1) {
496-
const updatedChat = this.chats[chatIndex];
521+
if (message === undefined) return;
497522

498-
updatedChat.lastMessageAuthor = notification.newLastMessageDisplayName;
499-
updatedChat.lastMessageText = notification.newLastMessageText;
500-
updatedChat.lastMessageTime = notification.newLastMessageTime;
501-
updatedChat.lastMessageId = notification.newLastMessageId;
502-
}
523+
const isDeletedMessageSelf = message.userId === this.userId;
503524

504-
if (this.activeChatId !== notification.chatId) return;
525+
if (isDeletedMessageSelf) return;
505526

506-
const messageIndex = this.messages.findIndex(
507-
(x) => x.messageId === notification.deletedMessageId
508-
);
527+
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;
534+
const chat = this.chats[chatIndex];
509535

510-
if (messageIndex !== -1) {
511-
this.messages.splice(messageIndex, 1);
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+
550+
return;
512551
}
513552
}
514553

0 commit comments

Comments
 (0)